1const { Op } = require("sequelize");
2Post.findAll({
3 where: {
4 [Op.and]: [{ a: 5 }, { b: 6 }], // (a = 5) AND (b = 6)
5 [Op.or]: [{ a: 5 }, { b: 6 }], // (a = 5) OR (b = 6)
6 someAttribute: {
7 // Basics
8 [Op.eq]: 3, // = 3
9 [Op.ne]: 20, // != 20
10 [Op.is]: null, // IS NULL
11 [Op.not]: true, // IS NOT TRUE
12 [Op.or]: [5, 6], // (someAttribute = 5) OR (someAttribute = 6)
13
14 // Using dialect specific column identifiers (PG in the following example):
15 [Op.col]: 'user.organization_id', // = "user"."organization_id"
16
17 // Number comparisons
18 [Op.gt]: 6, // > 6
19 [Op.gte]: 6, // >= 6
20 [Op.lt]: 10, // < 10
21 [Op.lte]: 10, // <= 10
22 [Op.between]: [6, 10], // BETWEEN 6 AND 10
23 [Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15
24
25 // Other operators
26
27 [Op.all]: sequelize.literal('SELECT 1'), // > ALL (SELECT 1)
28
29 [Op.in]: [1, 2], // IN [1, 2]
30 [Op.notIn]: [1, 2], // NOT IN [1, 2]
31
32 [Op.like]: '%hat', // LIKE '%hat'
33 [Op.notLike]: '%hat', // NOT LIKE '%hat'
34 [Op.startsWith]: 'hat', // LIKE 'hat%'
35 [Op.endsWith]: 'hat', // LIKE '%hat'
36 [Op.substring]: 'hat', // LIKE '%hat%'
37 [Op.iLike]: '%hat', // ILIKE '%hat' (case insensitive) (PG only)
38 [Op.notILike]: '%hat', // NOT ILIKE '%hat' (PG only)
39 [Op.regexp]: '^[h|a|t]', // REGEXP/~ '^[h|a|t]' (MySQL/PG only)
40 [Op.notRegexp]: '^[h|a|t]', // NOT REGEXP/!~ '^[h|a|t]' (MySQL/PG only)
41 [Op.iRegexp]: '^[h|a|t]', // ~* '^[h|a|t]' (PG only)
42 [Op.notIRegexp]: '^[h|a|t]', // !~* '^[h|a|t]' (PG only)
43
44 [Op.any]: [2, 3], // ANY ARRAY[2, 3]::INTEGER (PG only)
45
46 // In Postgres, Op.like/Op.iLike/Op.notLike can be combined to Op.any:
47 [Op.like]: { [Op.any]: ['cat', 'hat'] } // LIKE ANY ARRAY['cat', 'hat']
48
49 // There are more postgres-only range operators, see below
50 }
51 }
52});
53
1$ npm install --save sequelize # This will install v5
2$ npm install --save-dev sequelize-cli
3
4# And one of the following:
5$ npm install --save pg pg-hstore # Postgres
6$ npm install --save mysql2
7$ npm install --save mariadb
8$ npm install --save sqlite3
9$ npm install --save tedious # Microsoft SQL Server
10
11//Generate Models Auto with sequelize-auto
12$ npm install mysql2 -g
13$ npm install -g sequelize-auto-v2
14
15//Comand to generate models from database
16sequelize-auto -o "./models" -d schema -h localhost -u user -p 3306 -x password -e dialect
17
18Exemple:
19sequelize-auto -o "./models" -d nomeDoShema -h localhost -u usuarioDaConexao -p 3306 -x senhaDaConexao -e mysql
1const { Sequelize, Model, DataTypes } = require('sequelize');
2const sequelize = new Sequelize('sqlite::memory:');
3
4class User extends Model {}
5User.init({
6 username: DataTypes.STRING,
7 birthday: DataTypes.DATE
8}, { sequelize, modelName: 'user' });
9
10sequelize.sync()
11 .then(() => User.create({
12 username: 'janedoe',
13 birthday: new Date(1980, 6, 20)
14 }))
15 .then(jane => {
16 console.log(jane.toJSON());
17 });
18