sequelize.js

Is there a simple way to make Sequelize return it's date/time fields in a particular format?

假装没事ソ 提交于 2019-12-18 04:46:13
问题 We need to have sequelize return dates in a particular format, not the default one. As far as I can tell, there is no way to set that up in options, or any other way. Short of manually updating the dates every time after they are retrieved, anyone been able to solve this easily? Or am I missing something? 回答1: you can define custom instance methods getDate/setDate which would translate date between sequelize internal representation and desired format like so http://sequelize.readthedocs.org

Sequelize: how to import definitions from an existing database

送分小仙女□ 提交于 2019-12-17 23:02:50
问题 Am I required to handwrite the model definitions for Sequelize even if I'm working off of an existing database. If it's not required, then how does one go about using Sequelize with an existing database? I've already defined the database's schema in Doctrine, so I'd rather not have to write another set of model definitions again. 回答1: with Sequelize you have to define the structure of the model inside your code. Doing so, Sequelize assumes a specific database schema unless something is

How to define unique index on multiple columns in sequelize

爷,独闯天下 提交于 2019-12-17 22:36:48
问题 How do I define a unique index on a combination of columns in sequelize. For example I want to add a unique index on user_id, count and name. var Tag = sequelize.define('Tag', { id: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true }, user_id: { type: DataTypes.INTEGER(11), allowNull: false, }, count: { type: DataTypes.INTEGER(11), allowNull: true }, name: { type: DataTypes.STRING, allowNull: true, }) 回答1: You can refer to this doc http://docs.sequelizejs

Sequelize model references vs associations

房东的猫 提交于 2019-12-17 22:36:32
问题 Just starting to use Sequelize and I've setup a bunch of models and seeds, but I can't figure out references vs associations. I don't see the use case for references if they even do what I think they do, but I couldn't find a good explanation in the docs. Is this redundant having references and associations? module.exports = (sequelize, DataTypes) => { const UserTask = sequelize.define('UserTask', { id: { primaryKey: true, type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4 }, userId: { type

ECONNREFUSED for Postgres on nodeJS with dockers

北战南征 提交于 2019-12-17 22:18:45
问题 I'm building an app running on NodeJS using postgresql. I'm using SequelizeJS as ORM. To avoid using real postgres daemon and having nodejs on my own device, i'm using containers with docker-compose. when I run docker-compose up it starts the pg database database system is ready to accept connections and the nodejs server. but the server can't connect to database. Error: connect ECONNREFUSED 127.0.01:5432 If I try to run the server without using containers (with real nodejs and postgresd on

Sequelize: don't return password

孤街醉人 提交于 2019-12-17 22:14:59
问题 I'm using Sequelize to do a DB find for a user record, and I want the default behavior of the model to not return the password field for that record. The password field is a hash but I still don't want to return it. I have several options that will work, but none seems particularly good: Create a custom class method findWithoutPassword for the User model and within that method do a User.find with the attributes set as shown in the Sequelize docs Do a normal User.find and filter the results in

Addition and Subtraction Assignment Operator With Sequelize

江枫思渺然 提交于 2019-12-17 21:15:25
问题 I would like to do an update by doing a simple addition on Sequelize. table: id || data 1 || 10 sample: db.table.update({ data : 1 }, { where: { id: 1 }}); after this query id || data 1 || 11 I know it's a simple question, but I could not find the solution. Which operator can I add and subtract? Thank you 回答1: Here it is : db.table.update({ field: Sequelize.literal('data + 1') }, { where: { id: 1 }})) OR User.findById(1).then(user => { // -----> First Way return user.increment('my-integer

Using BCrypt with Sequelize Model

帅比萌擦擦* 提交于 2019-12-17 16:09:26
问题 I'm trying to use the bcrypt-nodejs package with my sequelize model and was tring to follow a tutorial to incorporate the hashing into my model, but I'm getting an error at generateHash . I can't seem to figure out the issue. Is there a better way to incorporate bcrypt? Error: /Users/user/Desktop/Projects/node/app/app/models/user.js:26 User.methods.generateHash = function(password) { ^ TypeError: Cannot set property 'generateHash' of undefined at module.exports (/Users/user/Desktop/Projects

How to organize a node app that uses sequelize?

喜欢而已 提交于 2019-12-17 14:58:40
问题 I am looking for an example nodejs app that uses the sequelize ORM. My main concern is that it seems next to impossible to define your models in separate js files if those models have complex relationships to one another because of require() dependency loops. Maybe people define all their models in one file that is very very long? I am mainly interested in how the models are defined and use through out the app. I would like to have some validation that what i am doing on my own is the "good"

Node.js writing api code with mysql(sequelize)

回眸只為那壹抹淺笑 提交于 2019-12-14 03:33:57
问题 My mysql table models : //mysql //table structures start var Contact = sequelize.define('contact', { gsm: { type: Sequelize.STRING, unique: true, required: true }, firstName: Sequelize.STRING, lastName: Sequelize.STRING, street: Sequelize.STRING, city: Sequelize.STRING, }) var Group = sequelize.define('group', { groupname: Sequelize.STRING }) var ContactGroup = sequelize.define('contactgroup', { /* Empty */ }) ContactGroup.belongsTo(Contact, { onDelete: 'CASCADE' }); Contact.hasMany