sequelize.js

How to implement pagination correctly for nodejs?

风格不统一 提交于 2020-06-29 03:58:07
问题 I use the ORM sequelize and the postgresql database. And I want to implement pagination. Using the MongoDB database, pagination is performed this way. module.exports.getBySprOilfieldId = async function (req, res) { try { const query = { where: { spr_oilfields_id: req.params.spr_oilfields_id } } const sprwellplatforms = await SprWellplatforms.findAll(query) .skip(+req.query.offset) .limit(+req.query.limit) res.status(200).json(sprwellplatforms) } catch(e) { errorHandler(res, e) } } But since I

Database Model's meta data should be defined in models file or migration file?

巧了我就是萌 提交于 2020-06-28 06:57:11
问题 Using sequelize init I generated a model with a migration. Inside model.js there is only the definition of the type of each attribute, e.g. const User = sequelize.define('User', { firstName: DataTypes.STRING, lastName: DataTypes.STRING, email: DataTypes.STRING, password: DataTypes.STRING }, {}); In migration file, there are additional options e.g. ... up: (queryInterface, Sequelize) => { return queryInterface.createTable('Users', { id: { allowNull: false, autoIncrement: true, primaryKey: true

how to catch Sequelize connection error

我怕爱的太早我们不能终老 提交于 2020-06-25 05:38:43
问题 How to catch a sequelize connection error in case there is one? I tried to do var connection = new Sequelize("db://uri"); connection.on("error", function() { /* perhaps reconnect here */ }); but apparently this is not supported. I wanted to do this because I think sequelize might be throwing an occasional unhandled ETIMEOUT and crashing my node process. Currently I am using sequelize to connect a mysql instance. I only need it for like 2-3 hours and during that time I will be doing a many

how to catch Sequelize connection error

↘锁芯ラ 提交于 2020-06-25 05:37:59
问题 How to catch a sequelize connection error in case there is one? I tried to do var connection = new Sequelize("db://uri"); connection.on("error", function() { /* perhaps reconnect here */ }); but apparently this is not supported. I wanted to do this because I think sequelize might be throwing an occasional unhandled ETIMEOUT and crashing my node process. Currently I am using sequelize to connect a mysql instance. I only need it for like 2-3 hours and during that time I will be doing a many

Sequelize How compare year of a date in query

十年热恋 提交于 2020-06-24 12:29:48
问题 I'm trying to make this query: SELECT * FROM TABLEA AS A WHERE YEAR(A.dateField)='2016' How can I perfome this query above in sequelize style? TABLEA.findAll({ where:{}//???? } Thanks! 回答1: TABLEA.findAll({ where: sequelize.where(sequelize.fn('YEAR', sequelize.col('dateField')), 2016) }); You have to use .where here, because the lefthand side of the expression (the key) is an object, so it cannot be used in the regular POJO style as an object key. If you want to combine it with other

Sequelize Find belongsToMany Association

前提是你 提交于 2020-06-11 06:04:06
问题 I have an association m:n between two tables with sequelize like this: Course module.exports = function(sequelize, DataTypes) { var Course = sequelize.define('Course', { ..... }, { associate: function(models){ Course.hasMany(models.Schedule); Course.belongsTo(models.Period); Course.belongsTo(models.Room); Course.belongsTo(models.Subject); Course.belongsTo(models.School); Course.belongsTo(models.Person, { as: 'Teacher' }); } } ); return Course; }; Person module.exports = function(sequelize,

bulkUpdate in sequelize orm

别来无恙 提交于 2020-06-10 21:18:04
问题 How can we implement bulkUpdate like bulkCreate in sequelize orm, I searched the whole documentation of sequelize but didn't find anything related to bulkUpdate, so I tried to loop update in for loop, it works but is there any other way to update in bulk 回答1: Use the bulkCreate to bulkUpdate method. bulkCreate([...], { updateOnDuplicate: ["name"] }) updateOnDuplicate is an array of fields that will be updated when the primary key (or may be unique key) match the row. Make sure you have at

bulkUpdate in sequelize orm

拈花ヽ惹草 提交于 2020-06-10 21:17:25
问题 How can we implement bulkUpdate like bulkCreate in sequelize orm, I searched the whole documentation of sequelize but didn't find anything related to bulkUpdate, so I tried to loop update in for loop, it works but is there any other way to update in bulk 回答1: Use the bulkCreate to bulkUpdate method. bulkCreate([...], { updateOnDuplicate: ["name"] }) updateOnDuplicate is an array of fields that will be updated when the primary key (or may be unique key) match the row. Make sure you have at

bulkUpdate in sequelize orm

让人想犯罪 __ 提交于 2020-06-10 21:16:34
问题 How can we implement bulkUpdate like bulkCreate in sequelize orm, I searched the whole documentation of sequelize but didn't find anything related to bulkUpdate, so I tried to loop update in for loop, it works but is there any other way to update in bulk 回答1: Use the bulkCreate to bulkUpdate method. bulkCreate([...], { updateOnDuplicate: ["name"] }) updateOnDuplicate is an array of fields that will be updated when the primary key (or may be unique key) match the row. Make sure you have at

Sequelize how to find rows with multiple where clauses and timestamp > NOW()

随声附和 提交于 2020-06-10 07:37:28
问题 How do I do this with Sequelize? SELECT FROM sessions WHERE user_id = ? AND token = ? AND expires > NOW() Here's what I'm trying to do (assume Session is a Sequelize model): Session.find({ where: { user_id: someNumber, token: someString, //expires > NOW() (how do I do this?) } }).on('success', function (s) { /* things and stuff */ }); Thanks! 回答1: did you try to use the array notation of finders in sequelize? Session.find({ where: ['user_id=? and token=? and expires > NOW()', someNumber,