sequelize.js

Sequelize Hooks- Previous data values for afterBulkUpdate

巧了我就是萌 提交于 2021-01-28 14:42:08
问题 I am trying to create a hook beforeBulkUpdate and get new and previous values of a field. I tried using .reload() on the model and _previousDataValues but they only work on instances and not bulk create and update (I'm using bulk update). Is there a way to get the previous value of field using the beforeBulkUpdate hook? beforeBulkUpdate: (person) => { console.log(person.name) // 'John' (new name) Person.findOne({ where: { id: input.id } }).then(person => { person.reload().then(() => { console

Sequelize Hooks- Previous data values for afterBulkUpdate

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-28 14:32:05
问题 I am trying to create a hook beforeBulkUpdate and get new and previous values of a field. I tried using .reload() on the model and _previousDataValues but they only work on instances and not bulk create and update (I'm using bulk update). Is there a way to get the previous value of field using the beforeBulkUpdate hook? beforeBulkUpdate: (person) => { console.log(person.name) // 'John' (new name) Person.findOne({ where: { id: input.id } }).then(person => { person.reload().then(() => { console

How to insert foreign key in sequelize model

試著忘記壹切 提交于 2021-01-28 12:12:04
问题 I have a User model and a Houses model in sequelize. I also have a UserHouse model that contains {user_id, house_id}. How do I populate values in the UserHouse model. I went through the sequelize documentation but could not get a clear idea. I need to know, how to insert user_id and house_id in UserHouse model. I also cannot understand what associations should I use between these models. Any help will be appreciated. Edited : One More issue: I have one more model HouseInfo that stores house

Error seeding JSON data with sequelize into PostgreSQL database

荒凉一梦 提交于 2021-01-28 11:00:11
问题 Occurs when I run the sequelize-cli command sequelize db:seed:all When I try and seed an object as JSON I get the following error: ERROR: Invalid value { viewId: null, dateRanges: [ { startDate: null, endDate: null } ], samplingLevel: 'DEFAULT', dimensions: [ { name: 'ga:channelGrouping' } ], metrics: [ { expression: 'ga:users' } ] } This is my model module.exports = (Sequelize, DataTypes) => { const Report = Sequelize.define('Report', { name: { type: DataTypes.STRING, allowNull: false,

Sequelize - Filter FindAll by matching all Tags

痞子三分冷 提交于 2021-01-28 08:10:36
问题 /assets/?tags[]=foo&tags[]=bar Get requests to endpoint above should return only those records that contain ALL of the provided tags ( foo , bar ) My current attempt is returning any records that match ANY of the given tags. const { tags } = req.query; res.send( await Asset.findAll({ where: whereOptions, include: [ { model: AssetMime }, { model: Tag, as: 'tags', where: { title: { [Op.in]: tags, }, }, }, ], }) ); How can I modify my filter to return records where ALL tags match? 回答1: You can

how to get dates with postgres

落爺英雄遲暮 提交于 2021-01-28 08:08:44
问题 I was trying to query and return the sum of all records in the last 4 past weeks. I had it working however I while using timestamps. and I am trying to change the precision and and just use dates. briefly instead of timestamp ranges I want just date ranges. this what I had with timestamps. with date_ranges (range_name, range_dates) as ( values ('week_0', tstzrange ((now()-interval '6 days'), now(),'[]')) , ('week_1', tstzrange ((now()-interval '13 days'), (now()-interval '7 days'), '[]')) , (

about node package 'squelize',what is 'dataValues' 's mean?

我们两清 提交于 2021-01-28 06:45:57
问题 My question is: koa2 middleware '/info' ctx.res.body ------>front-end get Object doesn't have dataValues and other value .only have object like {a:1,b:2} In middleware I get ser it have so many key-value like {dataValue:{a:1,b:2},eviousDataValues:'xxx'} Why is the front-end Object not the same as 'ser' in ko2 middleware? this is my code let sequelize = new Sequelize('test', sqlOpt.name, sqlOpt.pwd, { host: sqlOpt.host, dialect: 'mysql', port: sqlOpt.port, pool: { max: 5000, min: 0, idle:

Hooks not triggering when inserting raw queries via sequelize.query()

六眼飞鱼酱① 提交于 2021-01-28 06:36:00
问题 I have the following Employee model for a MySQL database: var bcrypt = require('bcrypt'); module.exports = (sequelize, DataTypes) => { const Employee = sequelize.define( "Employee", { username: DataTypes.STRING, password: DataTypes.STRING, }, {} ); return Employee; }; Seeding the database is done by reading a .sql file containing 10,000+ employees via raw queries: sequelize.query(mySeedingSqlFileHere); The problem is that the passwords in the SQL file are plain text and I'd like to use bcrypt

Sequelize Association in class methods not working

穿精又带淫゛_ 提交于 2021-01-27 20:50:53
问题 I'm trying to set associations in class methods but it's not working as it should! const User = sequelize.define('User', { email: DataTypes.STRING, password: DataTypes.STRING, }, { classMethods: { associate: (models) => { User.hasMany(models.AnotherModel, {foreignKey: 'userId'}); }, } }); But when i set associations outside of classMethods block it works: User.associate = function (models) { User.hasMany(models.AnotherModel, {foreignKey: 'userId'}); }; why the codes inside classMethods block

SequelizeJS: How to include association (join) across multiple databases without using raw query

允我心安 提交于 2021-01-27 14:54:05
问题 Suppose, there is "Account" table in DB "A", and there is "ConversationHistory" table in DB "B". My pseudo initialized script is: // Connection Script var db1 = new Sequelize("A", user, password) var db2 = new Sequelize("B", user, password) // Association Account.hasMany(ConversationHistory, {foreignKey: "sender_id", as: "conversations", constraints: false}) ConversationHistory.belongsTo(Account, {foreignKey: "sender_id", as: "sender", constraints: false}); // But when I use include in