sequelize.js

Sequelize: Query same join table with different conditions

五迷三道 提交于 2019-12-05 20:29:57
I have two models Contact and Thread with a many to many relationship represented across a join table ThreadContacts . I need to write a query to find a Thread which has associations with an exact list of Contacts . For example, I might have a list of contact_id 's [1,2,3,4], and I need to find a Thread that is associated with these exact 4 contacts. I have tried including Contact on a findAll query: Thread.findOne({ include: [{ model: Contact, where: { id: $in: [1, 2, 3, 4] }, }], }) Of course this doesn't work because it'll return a thread that has a ThreadContact with any of the 4 ids. I

Sequelize Join on Non Primary Key

一个人想着一个人 提交于 2019-12-05 20:00:04
问题 I have two tables I need to associate. TableA has One TableB. I'm able to do this in the TableA Model: TableA.hasOne( models.TableB, { as: 'TableB', foreignKey: 'someID' } ); Looking at the SQL, this tries to join TableA.ID and TableB.someID. What I actually want, is to join TableA.someNonPrimaryKey and TableB.someID. How can I tell sequelize to join with someNonPrimaryKey? 回答1: This is not possible in sequelize at the moment, however you can try this way. Difference between HasOne and

Slow associations in SequelizeJS

心已入冬 提交于 2019-12-05 19:34:21
I am trying to diagnose the cause of some slow downs in my Express app which is using SequlizeJS as the ORM. I have a model that has a 2x hasMany & a hasOne relation with 2 other models: Update : I've made the associations within the define call using the classMethods#associate function. // Model1 classMethods: { associate: function(models) { Model1.hasMany(models.Model2); Model1.hasMany(models.Model3); Model1.hasOne(models.Model2, {as: 'next', foreignKey: 'model2_next'}); } } // Model2 classMethods: { associate: function(models) { Model2.belongsTo(models.Model1, {foreignKey: 'model2_next'});

Promises in Sequelize: how to get results from each promise

对着背影说爱祢 提交于 2019-12-05 17:30:06
问题 In Sequelize >=1.7 we can use promises Can you explain for me how can i get values from each user in this code: var User = sequelize.define("user", { username: Sequelize.STRING }) User .sync({ force: true }) .then(function() { return User.create({ username: 'John' }) }) .then(function(john) { return User.create({ username: 'Jane' }) }) .then(function(jane) { return User.create({ username: 'Pete' }) }) .then(function(pete) { console.log("we just created 3 users :)") console.log("this is pete:"

Sequelizejs: findAll and count associations at the same time

被刻印的时光 ゝ 提交于 2019-12-05 16:25:37
say I have two models: Post and Comment , now I can use Post.findAll() to get all posts, but I also need the comment count of each post, I can make a loop and use post.countComments() to get the count, but is it possible to do that in one query? thanks You can do something like this: var attributes = Object.keys(Post.attributes); var sequelize = Post.sequelize; attributes.push([sequelize.literal('(SELECT COUNT(*) FROM "Comments" where "Comments"."postId" = "Post"."postId")'), 'commentsCount']); var query = { attributes: attributes, include: [{model: Comment}] } Post.findAndCountAll(query)

Sequelize: update in bulk

自闭症网瘾萝莉.ら 提交于 2019-12-05 16:03:22
问题 I'm using Node.js, MySQL and Sequelize. I'd like to insert some 10k rows into a table at once. The table has custom primaryKey field, that is being set manually. The data are downloaded from web and are overlapping. I'd like to have a version of bulkCreate that wouldn't fail if any of the rows in data have unique keys that are already present in the table. Such kind of things is done in MySQL via INSERT ... ON DUPLICATE KEY UPDATE construct. How do I do it in Sequelize? 回答1: Pass in an

Sequelize Model Unit Test

半世苍凉 提交于 2019-12-05 14:46:57
I have a User sequelize model that has a beforeCreate hook that encrypts the password using bcrypyt . Bcrypyt is loaded as a dependency by the model using a require statement. Now, I'm writing my tests for my models and I want to write a test that ensures bcrypt is hashing the password on create. At the moment, I have added a setter onto the User model that sets the bcrypt object. In my tests, I can then create a spy using sinon and inject the spy using the setter and ensure it is called on create. Is this the correct way to do it? I feel as if I'm creating a setter purely for my tests and

Sequelize - Join with multiple column

[亡魂溺海] 提交于 2019-12-05 14:21:50
I like to convert the following query into sequelize code select * from table_a inner join table_b on table_a.column_1 = table_b.column_1 and table_a.column_2 = table_b.column_2 I have tried many approaches and followed many provided solution but I am unable to achieve the desired query from sequelize code. The max I achieve is following : select * from table_a inner join table_b on table_a.column_1 = table_b.column_1 I want the second condition also. and table_a.column_2 = table_b.column_2 any proper way to achieve it? You need to define your own on clause of the JOIN statement ModelA.findAll

Sequelize: how to do a WHERE condition on joined table with left outer join

旧时模样 提交于 2019-12-05 11:23:25
问题 My database model is as follows: An employee drives one or zero vehicles A vehicle can be driven by one or more employees A vehicle has a model type that tells us it's fuel type amongst other things. I'd like sequelize to fetch me all employees where they don't drive a vehicle, or if they do then the vehicle is not diesel. So where VehicleID is null OR Vehicle.VehicleModel.IsDiesel = false My current code is as follows: var employee = sequelize.define('employee', { ID: Sequelize.INTEGER,

Do I need to validate, sanitise or escape data when using the build method in sequelize.js

梦想与她 提交于 2019-12-05 11:04:24
I have a node / express / sequelize app. I am using the build method in sequelize to create an instances of my foo model. Foo Controller exports.create = function(req, res) { var foo = db.Foo.build(req.body); foo.save().then(function(){ // do stuff }); } Foo Model module.exports = function(sequelize, DataTypes) { var Foo = sequelize.define('Foo', { bar: DataTypes.STRING, baz: DataTypes.STRING } Does the build method check that the data I am saving is clean or do I need to take some extra precautions here? Krzysztof Sztompka I prefer to make secondary validation in routes, because: 1) Storing