sequelize.js

How to add a custom DataType in Sequelize ORM

北城以北 提交于 2019-12-07 15:58:52
问题 I want to define a custom data type in Sequelize by inheriting all default behaviours of existing DataType.Integer . The base idea here is to define a new type and override valueOf and toString methods. The Sequelize docs doesn't contain any information related to this topic. It would be really nice if someone can help me on this. 回答1: https://github.com/sequelize/sequelize/blob/master/lib/data-types.js holds the sequelize data types. Specifically, https://github.com/sequelize/sequelize/blob

Sequelize Top level where with eagerly loaded models creates sub query

白昼怎懂夜的黑 提交于 2019-12-07 15:55:31
I'm running an into issue where Sequelize creates a subquery of the primary model and then joins the includes with that subquery instead of directly with the primary model table. The query conditions for the include(s) ends up inside the subquery's WHERE clause which makes it invalid. I have shortened names down trying to keep this compact hopefully without losing any relevant info. Environment: Nodejs: 6.11.3 Sequelize: 3.23.6 => Updated to 4.38.1 and problem persists MySql: 5.7.23 Code snip models: I.model: models.I.hasMany(models.II); models.I.belongsTo(models.CJ); models.I.belongsTo(models

findOrCreate with include - sequelize.js

天涯浪子 提交于 2019-12-07 15:22:44
问题 I want to create a Tournament (if not exists), and a Match (if not exists) associated to the Tournament. let [match, created] = await Match.findOrCreate( { where: {scoreHome: 97, Tournament: {name: "USA South Men's Basketball"}}, include: [Tournament] }) If the tournament 'USA South Men's Basketball' is already in the db (let's say with id 1), then the match should be created with TournamentId: 1. If there is already a match in the db with {scoreHome: 97 and TournamentId: 1} , then no match

Can't connect to MySQL with Sequelize

与世无争的帅哥 提交于 2019-12-07 15:12:07
问题 I consistently get a SequelizeConnectionRefusedError when trying to connect to a MySQL database on my server. The login credentials are correct, the port is open, everything seems good (and works like a charm in the dev environment). Sorry for the scarce background information, but I'm dumbfounded here - I really don't know what could be causing this problem. This is my output from mysql --version mysql Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (x86_64) using readline 6.3 And this is the

How can I do a group by across 3 tables with Sequelize?

混江龙づ霸主 提交于 2019-12-07 06:13:50
问题 My models are: module.exports = function(sequelize, DataTypes) { var CommitFileStatistic; return CommitFileStatistic = sequelize.define('CommitFileStatistic', { additions: { type: DataTypes.INTEGER, allowNull: false }, deletions: { type: DataTypes.INTEGER, allowNull: false }, status: { type: DataTypes.STRING, allowNull: false }, fileSize: { type: DataTypes.INTEGER, allowNull: true }, levenshteinDelta: { type: DataTypes.INTEGER, allowNull: true }, fileHash: { type: DataTypes.STRING, allowNull:

Sequelize associations - please use promise-style instead

Deadly 提交于 2019-12-07 05:02:42
问题 I'm trying to join 3 tables together Products , Suppliers and Categories and then get row with SupplierID = 13 . I have read How to implement many to many association in sequelize, there is explained how to associate 0:M . DB model: Code: var Sequelize = require('sequelize') var sequelize = new Sequelize('northwind', 'nodejs', 'nodejs', {dialect: 'mysql',}) var Project = require('sequelize-import')(__dirname + '/models', sequelize, { exclude: ['index.js'] }); Project.Suppliers.hasMany(Project

Sequelize sytax for calling stored procedure with input parameters using MSSQL

岁酱吖の 提交于 2019-12-07 04:43:24
I'm getting an error for the code below and cannot find accurate information for the syntax to call a MSSQL store procedure through sequelize. I have also tried the syntx from the other posts on stackoverflow similar to CALL spcName(param1, ...) error: (node:14580) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): SequelizeDatabaseError: Incorrect syntax near '@param1'. for code: await sequelize.query('CALL spcName(@param1, @param2, @param3, @param4);', [value1, value2, value3, value4]) Code looks like this and works for MSSQL. Wanted to show date example because

sequelize for NodeJS: are these features supported?

99封情书 提交于 2019-12-07 04:20:43
问题 Here are some questions about features supported by sequelize (sequelize project site) that I would like to clear up before deciding whether or not to use it: Chaining (efficiency): when chaining multiple queries, are these collected into one request to the database (as a batch of operations), or is each one sent separately? Chaining (success/error): when chaining multiple queries, when is the success event emitted and what happens on error? Is "success" emitted only if all operations

Do I need to validate, sanitise or escape data when using the build method in sequelize.js

落爺英雄遲暮 提交于 2019-12-07 04:18:59
问题 I have a node / express / sequelize app. I am using the build method in sequelize to create an instances of my foo model. Foo Controller exports.create = function(req, res) { var foo = db.Foo.build(req.body); foo.save().then(function(){ // do stuff }); } Foo Model module.exports = function(sequelize, DataTypes) { var Foo = sequelize.define('Foo', { bar: DataTypes.STRING, baz: DataTypes.STRING } Does the build method check that the data I am saving is clean or do I need to take some extra

Sequelize findOrCreate giving me a SequelizeUniqueConstraintError: Validation error

一笑奈何 提交于 2019-12-07 04:10:01
问题 so I am trying to chain multiple sequelize requests together - to check if a grant exists in our database. However when I'm using the sequelize findOrCreate method I get a SequelizeUniqueConstraintError: Validation error and I can't figure out why. This is my fund model. module.exports = function(sequelize, DataTypes) { var Fund = sequelize.define("funds", { id: { type: DataTypes.INTEGER, field: 'id', primaryKey: true, unique: true, autoIncrement: true, }, title: { type: DataTypes.TEXT, field