sequelize.js

sequelize, Statement(where) in statement(where)

二次信任 提交于 2020-01-04 03:12:05
问题 I'm trying for 2 hours to resolve a little problem which is not one. I'm on an generated yeoman Angular-fullstack App. I want to write this code with sequelize: SELECT * FROM demand WHERE city_id NOT IN ( SELECT city_id FROM demand WHERE user_id=req.params.user_id) I have yet the following code but that doesn't work. I get only [] export function getCity(req, res) { return Demand.findAll({ where: { city_id:{ $notIn: Demand.findAll({ attributes: ['city_id'], where: { user_id: req.params.user

How to set the Application Name for a Sequelize application

与世无争的帅哥 提交于 2020-01-03 18:58:55
问题 I've got a nodejs application that uses Sequelize as it's ORM. I've successfully got Sequelize connected to the database, but I haven't found anything in the documentation that explains how to set the application name. To clarify I'm looking to set a unique Application Name attribute for my app's connection string. That way when a DBA is looking at traffic they can pick out my application's queries from the rest. Is this something that Sequelize can even do? Or does this need to be done at

How to create prepared statements in Sequelize?

独自空忆成欢 提交于 2020-01-03 05:20:21
问题 First is it possible, I think it should be as they're safer than raw queries and prevent sql injection. But there is literally nothing I can find in documentation. sequelize.prepare <- doesn't exist sequelize.query <- exists 回答1: Never Mind, The sequelize.query has an option called replacements that is escaped automatically. replacements are escaped and inserted into the query by sequelize before the query is sent to the database sequelize.query('SELECT * FROM users WHERE name LIKE :search

Running Sequelize Migration and Node Server in Same Command Won't Start Server Up

蹲街弑〆低调 提交于 2020-01-03 05:10:08
问题 If I try to run my sequelize migrations and then run my Node server in the same command, I run into the issue of my server never starting up. If the migrations have already been run before, the sequelize db:migrate command doesn't go past the "No migrations were executed, database schema was already up to date." message, and my second command is never able to run. If the migration has not run before, everything runs properly in sequence. This is my npm start command: sequelize db:migrate &&

How to extend Sequelize model

不问归期 提交于 2020-01-03 02:58:23
问题 Is there a way to extend (maybe inherit) model to add hooks and fields after model was defined? So something like this: User = sequelize.define("user", { name: sequelize.String }); makeStateful(User); // adds state,updated,added fields and some hooks 回答1: this is not possible at the moment. But you could easily make it work the other way around: Define your mixin before and use that when you define the model: var Sequelize = require('sequelize') , sequelize = new Sequelize('sequelize_test',

SQL query to find a row with a specific number of associations

江枫思渺然 提交于 2020-01-02 23:13:15
问题 Using Postgres I have a schema that has conversations and conversationUsers . Each conversation has many conversationUsers . I want to be able to find the conversation that has the exactly specified number of conversationUsers . In other words, provided an array of userIds (say, [1, 4, 6] ) I want to be able to find the conversation that contains only those users, and no more. So far I've tried this: SELECT c."conversationId" FROM "conversationUsers" c WHERE c."userId" IN (1, 4) GROUP BY c.

Slow associations in SequelizeJS

怎甘沉沦 提交于 2020-01-02 08:24:08
问题 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

How to count a group by query in NodeJS Sequelize

喜你入骨 提交于 2020-01-02 00:14:35
问题 In Rails I can perform a simple ORM query for the number of Likes a model has: @records = Model .select( 'model.*' ) .select( 'count(likes.*) as likes_count' ) .joins( 'LEFT JOIN likes ON model.id = likes.model_id' ) .group( 'model.id' ) This generates the query: SELECT models.*, count(likes.*) as likes_count FROM "models" JOIN likes ON models.id = likes.model_id GROUP BY models.id In Node Sequelize, any attempt at doing something similar fails: return Model.findAll({ group: [ '"Model".id' ],

How to count a group by query in NodeJS Sequelize

一世执手 提交于 2020-01-02 00:14:07
问题 In Rails I can perform a simple ORM query for the number of Likes a model has: @records = Model .select( 'model.*' ) .select( 'count(likes.*) as likes_count' ) .joins( 'LEFT JOIN likes ON model.id = likes.model_id' ) .group( 'model.id' ) This generates the query: SELECT models.*, count(likes.*) as likes_count FROM "models" JOIN likes ON models.id = likes.model_id GROUP BY models.id In Node Sequelize, any attempt at doing something similar fails: return Model.findAll({ group: [ '"Model".id' ],

How to force 1:n association with Sequelizejs

女生的网名这么多〃 提交于 2020-01-01 17:37:21
问题 I'm implementing some tests to make sure my sequelize objects are saved correctly. I have a very simple schema: Article <--> Users An Article is published by ONE User A User can publish MANY Articles Here is my Article model definition: module.exports = function(sequelize){ "use strict"; var Sequelize = require('sequelize'); ... var Article = sequelize.define("Article", { slug: { type: Sequelize.STRING, unique: true, comment: "Unique URL slug to access the article" }, title: { type: Sequelize