ORMS Sequelize
ORMS Sequelize
Parte 1
2
ORMs
Object-Relational Mapping
3
ORMs
4
Sequelize
Node v10+ 5
Sequelize
Installing
Connecting
6
Data Types
1 // TEXTO
2 DataTypes.STRING // VARCHAR(255)
3 DataTypes.STRING(1234) // VARCHAR(1234)
4 DataTypes.TEXT // TEXT
5
6 // NUMEROS
7 DataTypes.INTEGER // INTEGER
8 DataTypes.FLOAT // FLOAT
9
10 // FECHAS
11 DataTypes.DATE // TIMESTAMP WITH TIME ZONE
12 DataTypes.DATEONLY // DATE without time
13
14 // OTROS
15 DataTypes.ENUM('foo', 'bar') // An ENUM with allowed values 'foo' and 'bar'
Listado Completo
7
Model
De nition
Abstracción que representa una tabla de nuestra base de datos
1 // Un modelo a la vez
2 await Model.sync({ force: true });
3
4 // Todos los modelos juntos
5 await sequelize.sync({ force: true });
9
Sequelize
Logging
10
Model
Automatic Timestamps
1 sequelize.define('User', {
2 // ... (attributes)
3 }, {
4 timestamps: false 1 sequelize.define('User', {
5 }); 2 // ... (attributes)
3 }, {
4 timestamps: true,
5 createdAt: false,
6 updatedAt: 'actualizado'
7 });
11
Model
Column Options
12
Instances
Representa un objeto con la estructura de nida en el modelo que va a
mapearse con una la de la tabla asociada
13
Instances
Modi cations
14
Queries
SELECT
15
Queries
Finders
17
Queries
Operators
1 const { Op } = require("sequelize");
2 Model.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
13 // Number comparisons
14 [Op.gt]: 6, // > 6
15 [Op.lt]: 10, // < 10
16 [Op.between]: [6, 10], // BETWEEN 6 AND 10
17 [Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15
18
19 // Other operators
20 [Op.in]: [1, 2], // IN [1, 2]
21 [Op.notIn]: [1, 2], // NOT IN [1, 2]
22
23 ...
24 }
25 }
26 });
Listado Completo
18
Queries
UPDATE
1 // UPDATE table
2 // SET transformation = 'SS1'
3 // WHERE name = 'Goku'
4
5 await User.update({ transformation: 'SS1' }, {
6 where: {
7 name: 'Goku'
8 }
9 });
19
Queries
DELETE
Borrará todas las instancias que coincidan con la claúsula where indicada. Si
no se coloca ninguna condición borrará todos los registros
21
Setters
22
Virtual Fields
23
Validators
1 sequelize.define('foo', {
2 bar: {
3 type: DataTypes.STRING,
4 validate: {
5 is: /^[a-z]+$/i,
6 isEmail: true,
7 isUrl: true,
8 isAlpha: true,
9 isAlphanumeric: true,
10 isNumeric: true,
11 isLowercase: true,
12 notNull: true,
13 notEmpty: true,
14 equals: 'specific value',
15 contains: 'foo',
16 isIn: [['foo', 'bar']],
17 notContains: 'bar',
18 len: [2,10],
19 isAfter: "2011-11-05",
20 max: 23,
21
22 // Custom validators:
23 isEven(value) {
24 if (parseInt(value) % 2 !== 0) {
25 throw new Error('Only even values are allowed!');
26 }
27 }
28 }
29 }
30 });
Listado Completo
24
Associations
One-To-One 1 Foo.hasOne(Bar);
2 Bar.belongsTo(Foo);
One-To-Many 1 Team.hasMany(Player);
2 Player.belongsTo(Team);
25
Mixins
Foo.hasOne(Bar)
1 fooInstance.getBar()
2 fooInstance.setBar()
Foo.belongsTo(Bar) 3 fooInstance.createBar()
1 fooInstance.getBars()
2 fooInstance.countBars()
3 fooInstance.hasBar()
4 fooInstance.hasBars()
Foo.hasMany(Bar) 5
6
fooInstance.setBars()
fooInstance.addBar()
7 fooInstance.addBars()
8 fooInstance.removeBar()
9 fooInstance.removeBars()
10 fooInstance.createBar()
26
Mixins
1 fooInstance.getBars()
2 fooInstance.countBars()
3 fooInstance.hasBar()
4 fooInstance.hasBars()
5 fooInstance.setBars()
6 fooInstance.addBar()
7 fooInstance.addBars()
8 fooInstance.removeBar()
9 fooInstance.removeBars()
10 fooInstance.createBar()
27
Fetching Associations
Lazy Loading
28
Fetching Associations
Eager Loading
29
Hooks
Lifecycle Events
1 (1)
2 beforeValidate(instance, options)
3
4 [... validation happens ...]
5
6 (2)
7 afterValidate(instance, options)
8 validationFailed(instance, options, error)
9 (3)
10 beforeCreate(instance, options)
11 beforeDestroy(instance, options)
12 beforeUpdate(instance, options)
13
14 [... creation/update/destruction happens ...]
15
16 (4)
17 afterCreate(instance, options)
18 afterDestroy(instance, options)
19 afterUpdate(instance, options)
30