Sequelize: how to import definitions from an existing database

This project aims to create Sequelize models from existing schema https://github.com/sequelize/sequelize-auto Sequelize-Auto A tool to automatically generate models for SequelizeJS via the command line. Install: npm install -g sequelize-auto Usage: sequelize-auto -h <host> -d <database> -u <user> -x [password] -p [port] –dialect [dialect] -c [/path/to/config] -o [/path/to/models] Options: -h, –host IP/Hostname for the database. [required] … Read more

Sequelize Query to find all records that falls in between date range

The solution which works for me is this:- // here startDate and endDate are Date objects const where = { from: { $between: [startDate, endDate] } }; For reference to know more about operators:- http://docs.sequelizejs.com/en/latest/docs/querying/#operators Note: In MYSQL between comparison operator is inclusive, which means it is equivalent to the expression (startDate <= from AND … Read more

How to define unique index on multiple columns in sequelize

You can refer to this doc http://docs.sequelizejs.com/en/latest/docs/models-definition/#indexes You will need to change your definition like shown below and call sync var Tag = sequelize.define(‘Tag’, { id: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true }, user_id: { type: DataTypes.INTEGER(11), allowNull: false, }, count: { type: DataTypes.INTEGER(11), allowNull: true }, name: { type: DataTypes.STRING, allowNull: … Read more

belongsTo vs hasMany in Sequelize.js

When you do Album.belongsTo(Artist) you are creating the relation enabling you to call album.getArtist(). Artist.hasMany(Album) links the association the other way, enabling you to call artist.getAlbums(). If you only did one of those two, e.g. if you only did Album.belongsTo(Artist) you would be able to retrieve the artist of an album, but not all albums … Read more

Ordering results of eager-loaded nested models in Node Sequelize

I believe you can do: db.Page.findAll({ include: [{ model: db.Gallery include: [{ model: db.Artwork }] }], order: [ // sort by the ‘order’ column in Gallery model, in descending order. [ db.Gallery, ‘order’, ‘DESC’ ], // then sort by the ‘order’ column in the nested Artwork model in a descending order. // you need to … Read more