sequelize.js

Javascript: SyntaxError: await is only valid in async function

吃可爱长大的小学妹 提交于 2019-12-12 12:17:43
问题 I am on Node 8 with Sequelize.js Gtting the following error when trying to use await . SyntaxError: await is only valid in async function Code: async function addEvent(req, callback) { var db = req.app.get('db'); var event = req.body.event db.App.findOne({ where: { owner_id: req.user_id, } }).then((app) => { let promise = new Promise((resolve, reject) => { setTimeout(() => resolve("done!"), 6000) }) // I get an error at this point let result = await promise; // let result = await promise; //

Sequelize create object with associations

末鹿安然 提交于 2019-12-12 11:17:57
问题 I'm trying to save sequelize models with their associations. All the associations are one to one. Retrieving models with associations from the database works just fine but inserting them is another matter and the documentation is just making me more confused. Here's my insert method: models .radcheck .create(user, { include: [{model: models.skraningar}, {model: models.radusergroup}, {model: models.radippool}] }) .then(success, error); I've seen so many ways to do this both in the

Create a composite index with associated data?

末鹿安然 提交于 2019-12-12 10:41:16
问题 I have a User-table and a userAttributes table. Every user can only have one instance of each userAttribute, so I would like to create a composite unique index for the columns name and userId(That is created by userAttributes.belongsTo.) How can this be done? UserAttribute = sequelize.define('userAttributes', { name: { type: Sequelize.STRING, allowNull: false, unique: 'nameIndex', validate: { isIn: [['phone', 'name','driverRequest']], } }, value: { type: Sequelize.STRING, allowNull: false },

Unable to execute the raw query in Sequelize migrations

荒凉一梦 提交于 2019-12-12 10:35:19
问题 I am trying to update my database using Sequelize migrations so I have tried to write a Sequelize migrations like this 'use strict'; module.exports = { up: (queryInterface, Sequelize, migration) => { queryInterface.addColumn('coaching_class_entries', 'bill_cycle', { type: Sequelize.INTEGER(4), allowNull: false, defaultValue: '0', field: 'bill_cycle', after: 'fees' }) .then(() => queryInterface.addColumn('coaching_classes', 'bill_plans', { type: Sequelize.JSON, allowNull: false, defaultValue:

Sequelize Code Not Executing Inside AWS Lambda

随声附和 提交于 2019-12-12 10:25:33
问题 I've got Sequelize-based code that runs fine in a Node.js project. I moved that code into an AWS Lambda handler and am testing it with the node-lambda module. Now the Sequelize code seems to be skipped. I'm not sure if the promise isn't being handled before the Lambda finishes, or if I'm missing something else. The following code skips the "during" console.log as shown in the output below. var models = require('./models'); exports.handler = function( event, context, callback ) { console.log(

How to set application name in sequelize connection object?

淺唱寂寞╮ 提交于 2019-12-12 09:54:43
问题 Summary: I want to change the application name of the connection string when initialize a new sequalize object. based on this stackoverflow question, I set the appName of dialectOptions as follows: let conn = new Sequelize(this.models.sequelize.config.database, this.models.sequelize.config.username, this.models.sequelize.config.password, { host: this.models.sequelize.config.host, dialect: this.models.sequelize.getDialect(), dialectOptions: { appName: "userid=-2@gid=" + gid } }); Question:

Sequelizejs: findAll and count associations at the same time

回眸只為那壹抹淺笑 提交于 2019-12-12 09:42:00
问题 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 回答1: 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")'),

Sequelize - Join with multiple column

有些话、适合烂在心里 提交于 2019-12-12 09:41:07
问题 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

Is this the correct way to define a table relationship in Sequelize?

断了今生、忘了曾经 提交于 2019-12-12 05:28:20
问题 I find the Sequelize docs are long and hard to understand if you are using tables that don't follow their conventions. If I have two tables: people ( people_id - uuid name - text ) parties ( parties_id - uuid people_id - uuid (foreign key) ) Am I right to define my models like this? function getPeople() { let entity = {}; entity['people_id'] = {type: Sequelize.UUID, primaryKey: true, allowNull : false}; entity['name'] = Sequelize.TEXT; return sequelize.define('dbo.people', entity, options); }

bcrypt.compare() always returns false when verifying passwords

我只是一个虾纸丫 提交于 2019-12-12 04:49:58
问题 I followed this tutorial from scotch.io on how to build user authentication using node.js (great tutorial by the way). However, when verifyPassword(password) is called to check user password, the value is always returned as false for some reason. I'm using brcypt.js and sequelize.js for my express project. custom methods for model User: classMethods : { setPassword : function(password) { return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); } }, instanceMethods: { verifyPassword: