sequelize.js

Sequelize findOne latest entry

主宰稳场 提交于 2019-12-20 08:46:45
问题 I need to find the latest entry in a table. What is the best way to do this? The table has Sequelize's default createdAt field. 回答1: Method findOne is wrapper for findAll method. Therefore you can simply use findAll with limit 1 and order by id descending. Example: YourModel.findAll({ limit: 1, where: { //your where conditions, or without them if you need ANY entry }, order: [ [ 'createdAt', 'DESC' ]] }).then(function(entries){ //only difference is that you get users list limited to 1 /

Javascript async function flow

吃可爱长大的小学妹 提交于 2019-12-20 06:29:09
问题 My function should assign an employee on a seat if the seat is available. I do not understand why the program doesn't act as synchronous even though I used "await". In the first lines of the function, the program acts as expected. it waits to get "seats"from the database, then performs the "if(seats.length > 0)" check and initialises an empty array. async function AssignSeat(req, res) { var seats = await connection.SeatEmployees.findAll({ where: { SeatId: req.body.seat.SeatId } }) .catch(err

Access a single field on Sequelize model

家住魔仙堡 提交于 2019-12-20 06:19:17
问题 New to Sequelize. SQlite database created and working. With a sample like this: const Sequelize = require('sequelize') const sequelize = new Sequelize('sqlite:dbname.db'); sequelize.authenticate() .then(() => { console.log('Connected') }) .catch(err => { console.log('Not connected') }) const User = sequelize.define('users', { id: {type: Sequelize.SMALLINT, primaryKey: true}, firstname: Sequelize.STRING, lastname: Sequelize.STRING, email: Sequelize.STRING, pass: Sequelize.STRING, }) User

Postgres on AZURE Unhandled rejection TimeoutError: ResourceRequest timed out

时间秒杀一切 提交于 2019-12-20 04:17:23
问题 I have a database postgres on azure, whith General Purpose, 2 vCore(s), 100 GB. at this moment, when i do a simple request to database, database answer me lot of times with message: Unhandled rejection TimeoutError: ResourceRequest timed out at ResourceRequest._fireTimeout (/Project/development/TFS-git/RAILES/railes-backend-api-pt-0317/node_modules/sequelize/node_modules/generic-pool/lib/ResourceRequest.js:62:17) at Timeout.bound (/Project/development/****/*****/*********/node_modules

How sequelize works?

ⅰ亾dé卋堺 提交于 2019-12-20 02:54:10
问题 I'm trying to understand how sequelize works on a simple example : User can have many posts and post can have only one user. First question, I don't know if I have to use the migrations or the models with sync for creating my database. I mean, I have to put bearly the same code in both. This is the migration for the users table: module.exports = { up: (queryInterface, Sequelize) => { return queryInterface.createTable('Users', { id: { allowNull: false, autoIncrement: true, primaryKey: true,

get .findOrCreate() error

佐手、 提交于 2019-12-19 17:14:12
问题 I'm using Sequelize as ORM. Here's my user model: ### User model ### User = exports.User = globals.sequelize.define "User", username: globals.Sequelize.STRING email: type: globals.Sequelize.STRING validate: isEmail: true hash: globals.Sequelize.STRING salt: globals.Sequelize.STRING(512) fname: globals.Sequelize.STRING lname: globals.Sequelize.STRING country: globals.Sequelize.STRING I'm saving user: globals.models.User.findOrCreate username: "johny" password: "pass" email: "johny93[###

Is it possible to do a subquery with Sequelize.js?

给你一囗甜甜゛ 提交于 2019-12-19 12:52:41
问题 I have a query that looks like: select es.EssayId, (esmax.WordCount - esmin.WordCount) from (select es.EssayId, min(es.EssayDate) as mined, max(es.EssayDate) as maxed from EssayStats es group by es.EssayId ) es join EssayStats esmin on es.EssayId = esmin.EssayId and es.mined = esmin.EssayDate join EssayStats esmax on es.EssayId = esmax.EssayId and es.maxed = esmax.EssayDate; Is it possible to write this with Sequelize.js ORM? I know I can just use a query directly, but I'm wondering if it's

bcrypt@1.0.3 install: `node-pre-gyp install --fallback-to-build`

时间秒杀一切 提交于 2019-12-19 10:33:13
问题 While installing bcrypt I'm getting this error in my windows machine, I'm using node v8.9.4 npm v5.6.0 bcrypt v1.0.3 C:\Users\user\Desktop\mysql_node_api\register_login\register_login>npm install bcrypt --save > bcrypt@1.0.3 install C:\Users\user\Desktop\mysql_node_api\register_login\regis ter_login\node_modules\bcrypt > node-pre-gyp install --fallback-to-build node-pre-gyp ERR! Tried to download(undefined): https://github.com/kelektiv/node .bcrypt.js/releases/download/v1.0.3/bcrypt_lib-v1.0

Unique constraint across foreign keys in Sequelize model

老子叫甜甜 提交于 2019-12-19 05:22:53
问题 I have a simple Sequelize model that is associated with other models. module.exports = function (sequelize, DataTypes) { var Votes = sequelize.define('Votes', { isUpVote: DataTypes.BOOLEAN }, { classMethods: { associate: function (models) { Votes.belongsTo(models.Track); Votes.belongsTo(models.User); } } }); return Votes; } Sequelize will generate a table with an id , TrackId , UserId and isUpVote . I want to set a UNIQUE constraint across TrackId and UserId (i.e. a composite index ensuring

Sequelize with NodeJS can't join tables with limit

早过忘川 提交于 2019-12-19 05:19:10
问题 I'm trying to implement a simple query that should look like this: select * from property join entity_area on property.id=entity_area.entity_id and entity_area.area_id=1 where property.price>300000 limit 12 Pretty straightforward: I want to get the joined result and then to limit to 12. In Sequelize i'm using the following function: return models.property.findAll( { where: ["price>=?", 300000], include: [ { model:models.entity_area, where: { area_id:1 } } ], limit:12 }) But this code