Lazy syntax is for migration pattern for migrate models to database. They have their own code just for lazy purpose created this so named it lazy sytnax. Current features have priority and error handling.
Example lazy syntax migration pattern
migrations:
{
['1-clean-startup']: // migration file_name
{
table: 'toppings', // table name
timestamps: true, // use timestamp?
paranoid: true, // use paranoid
priority: 7, // action priority
createTable: { // action strategy
toppingId: '[INTEGER][PK][AUTO]',
topping_code: '[STRING]',
topping_name: '[STRING]',
topping_price: '[DECIMAL(2)]',
productId: '[INTEGER][REF_TABLE:products][REF_KEY:productId][UPDATE:CASCADE][DELETE:CASCADE]'
}
}
}
Every migration files must with these code.
require('module-alias/register');
module.exports = require('@migration')(module.filename);
Migrating - node_modules/.bin/sequelize db:migrate
Undo migration - node_modules/.bin/sequelize db:migrate:undo
Default first syntax is data type and others order didn't care.
[DATA_TYPE]
[NOT_NULL]
[NULLABLE]
[UNIQUE]
[AUTO]
[PK]
[UPDATE:CASCADE]
[DELETE:CASCADE]
[REF_TABLE:reference_table]
[REF_KEY:reference_key]
You can refer to here all of the data types ( http://docs.sequelizejs.com/variable/index.html#static-variable-DataTypes )
Example of data type syntax :
Sequelize.STRING = [STRING]
Sequelize.ARRAY(Sequelize.STRING) = [ARRAY(S@STRING)]
Sequelize.STRING.BINARY = [STRING.BINARY]
Sequelize.INTEGER(10).ABC = [INTEGER(10).ABC]
Allow null or not null for data constraint.
Is the property unique.
Auto increment for property. Usually for primary key only.
Is the property primary key.
When foreign key update or delete what should we do. Default value : CASCADE, values: CASCADE, CASCDE, NO ACTION
Foreign Key table name & key name.
Example:
[REF_TABLE:menus]
[REF_KEY:menuId]
There have six migration strategy, these also normally we use.
- create table
- change column
- add column
- remove column
- add constraint
- remove constraint
- insert data
Create table is for creating new table when migrate. when migrating will create new table for models.
CreateTable Pattern
createTable: {
field_name: 'SYNTAX'
}
Change column is for changing existing columns. when migrating will change column by after and undo migrating will change column by before.
ChangeColumn Pattern
changeColumn: {
field_name: {
before: 'SYNTAX',
after: 'SYNTAX'
}
}
Add column is for adding new columns. when migrating will add new columns by given syntax and undo migrating will just remove by name.
AddColumn Pattern
addColumn: {
field: 'SYNTAX'
}
Remove column is for remove existing column. when migrating will remove column by name and undo-migrating will add column by given syntax.
RemoveColumn Pattern
removeColumn: {
field: 'SYNTAX'
}
Add contraints for specific fields. when migrating will add constraint to t he field and undo-migrating will remove contrainst from field. More information you can check from here 'http://docs.sequelizejs.com/class/lib/query-interface.js~QueryInterface.html#instance-method-addConstraint'.
AddConstraint Pattern
addConstraint: [{
fields: [],
options: {
type: 'unique',
name: '',
},
}, {
fields: [],
options: {
type: 'unique',
name: '',
},
}]
Remove constraints for specific fields. when migrating will remove constraint from field and undo-migrating will add constraint back to the fields. More information you can check from here 'http://docs.sequelizejs.com/class/lib/query-interface.js~QueryInterface.html#instance-method-addConstraint'.
removeConstraint: [{
fields: [],
options: {
type: 'unique',
name: '',
},
}, {
fields: [],
options: {
type: 'unique',
name: '',
},
}]
Insert data to database. when migrating will insert data and undo-migrating will delete data.
insertData: {
seeds: [{
account_token: 'XXX',
account_owner: 'XXX',
account_type: 'ADMIN',
id: null
}]
}
Lastly, like others migration tools too, output will be up & down function. Migration builder code you can view at '/custom/internal/migration-builder.js'
{
up: async (queryInterface, Sequielize) =>
{
},
down: async (queryInterface, Sequelize) =>
{
}
}