sequelize.js

Sequelize - findOrCreate on the “through” table, on belongsToMany association

喜你入骨 提交于 2019-12-19 04:05:59
问题 First of all, I'm rather new to Node.JS and even newer to Sequelize, and this has been bothering me for a while. I have the following Model entities: Match.js module.exports = function(sequelize, DataTypes) { var Match = sequelize.define('Match', { matchId: { type: DataTypes.BIGINT, field: 'match_id' }, server: { type: DataTypes.STRING }, (...) rankedBoo: { type: DataTypes.BOOLEAN, field: 'ranked_boo' } }, { classMethods: { associate: function(models) { Match.belongsToMany(models.Summoner,

Get id of inserted row from upsert (insertOrUpdate) Sequelize Node.js

烈酒焚心 提交于 2019-12-18 16:45:45
问题 I'm creating a REST API. I would like to implement an indepotent PUT operation, that either creates or updates the specific resource in database. I'm using node.js, postgreSQL and sequelize. The problem is that sequelize upsert returns either true or false depending on wheter the resource got updated or created. But I need to be able to send unique identifier (column id) back to the client, if the resource got created. One solution that I tried was trying to find the exact same resource by

Sequelize use camel case in JS but underscores in table names

为君一笑 提交于 2019-12-18 13:53:24
问题 Is it possible to have column names be underscored (postgres) but have the JavaScript getters be camelCase per language standards? 回答1: Not directly in your column definition, but you could take a look at getters and setters: http://sequelizejs.com/documentation#models-getters---setters-defining-as-part-of-the-model-options Although this options requires you to define a getter and setter for each column manually, it cannot be automated. Furthermore, both your getters and the actual column

Cleaning out test database before running tests

与世无争的帅哥 提交于 2019-12-18 12:54:27
问题 What is the best way to clean out a database before running a test suite (is there a npm library or recommended method of doing this). I know about the before() function. I'm using node/express, mocha and sequelize. 回答1: The before function is about as good as you will do for cleaning out your database. If you only need to clean out the database once i.e. before you run all your tests, you can have a global before function in a separate file globalBefore.js before(function(done) { // remove

hasMany called with something that's not an instance of Sequelize.Model

社会主义新天地 提交于 2019-12-18 11:43:07
问题 as you guys can see my issue is related to the title description, i created a User Model, and a Foto Model in sequelize, basicly a user can shoot many fotos, but each foto can be related to just 1 user. My User model "use strict"; var sequelize = require('./index'); var bcrypt = require('bcrypt-nodejs'); var Foto = require('./Foto'); module.exports = function (sequelize, DataTypes) { var User = sequelize.define("User", { username: { type: DataTypes.STRING, allowNull: false, unique: true,

Sequelize.js foreign key

社会主义新天地 提交于 2019-12-18 11:00:49
问题 When using Sequelize.js, the following code doesn't add any foreign key on tables. var MainDashboard = sequelize.define('main_dashboard', { title: Sequelize.STRING }, { freezeTableName: true }) MainClient.hasOne(MainDashboard, { foreignKey: 'idClient' }) MainDashboard.hasOne(MainClient, { foreignKey: 'clientId' }) sequelize.sync({ force: true }) Is there any way to force Sequelize.js to add these foreign key constraints? 回答1: Before I had the same problem, and solved when I understood the

Is it possible to filter a query by the attributes in the association table with sequelize?

Deadly 提交于 2019-12-18 10:58:40
问题 I am trying to filter my query by the attributes of the joining table I have 2 tables Cities and Categories which I am associating through a third table CityCategory. The idea is to get the Categories associated with a City when CityCategory . year is a specific integer. This is how I specified the associations: module.exports = function(sequelize, DataTypes) { var CityCategory = sequelize.define('CityCategory', { year: { type: DataTypes.INTEGER, allowNull: false, validate: { notNull: true }

Sequelize find based on association

↘锁芯ラ 提交于 2019-12-18 10:48:23
问题 How would I use Sequelize to find all people where a column in the relation satisfies a condition? An example would be to find all Books whose author's last name is 'Hitchcock'. The book schema contains a hasOne relation with the Author's table. Edit: I understand how this could be done with a raw SQL query, but looking for another approach 回答1: Here's a working sample of how to user Sequelize to get all Books by an Author with a certain last name. It looks quite a bit more complicated than

Sequelize OR condition object

耗尽温柔 提交于 2019-12-18 10:37:49
问题 By creating object like this var condition= { where: { LastName:"Doe", FirstName:["John","Jane"], Age:{ gt:18 } } } and pass it in Student.findAll(condition) .success(function(students){ }) It could beautifully generate SQL like this "SELECT * FROM Student WHERE LastName='Doe' AND FirstName in ("John","Jane") AND Age>18" However, It is all 'AND' condition, how could I generate 'OR' condition by creating a condition object? 回答1: Seems there is another format now where: { LastName: "Doe", $or:

@Types/Sequelize Error TS1086: An accessor cannot be declared in ambient context

孤人 提交于 2019-12-18 09:00:56
问题 I have a project that shows this error when I run 'tsc': ../modules/node_modules/sequelize/types/lib/transaction.d.ts:33:14 - error TS1086: An accessor cannot be declared in an ambient context. 33 static get LOCK(): LOCK; ~~~~ ../modules/node_modules/sequelize/types/lib/transaction.d.ts:40:7 - error TS1086: An accessor cannot be declared in an ambient context. 40 get LOCK(): LOCK; ~~~~ My versions are: "@types/sequelize": "^4.28.6" "sequelize": "^5.8.10" "sequelize-typescript": "1.0.0-beta.4"