Documente Academic
Documente Profesional
Documente Cultură
Note: Sequelize will only use Model files, it's the table representation. On the
other hand, the migration file is a change in that model or more specifically that
table, used by CLI. Treat migrations like a commit or a log for some change in
database.
Running Migrations
Until this step, we haven't inserted anything into the database. We have just created
required model and migration files for our first model User. Now to actually create that
table in database you need to run db:migrate command.
$ node_modules/.bin/sequelize db:migrate
This command will execute these steps:
You can use db:migrate:undo, this command will revert most recent migration.
$ node_modules/.bin/sequelize db:migrate:undo
You can revert back to initial state by undoing all migrations
with db:migrate:undo:all command. You can also revert back to a specific migration
by passing its name in --tooption.
$ node_modules/.bin/sequelize db:migrate:undo:all --to XXXXXXXXXXXXXX-create-
posts.js
Let's create a seed file which will add a demo user to our User table.
$ node_modules/.bin/sequelize seed:generate --name demo-user
This command will create a seed file in seeders folder. File name will look something
like XXXXXXXXXXXXXX-demo-user.js. It follows the same up / down semantics as the
migration files.
Now we should edit this file to insert demo user to User table.
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('Users', [{
firstName: 'John',
lastName: 'Doe',
email: 'demo@demo.com'
}], {});
},
Running Seeds
In last step you have create a seed file. It's still not committed to database. To do that
we need to run a simple command.
$ node_modules/.bin/sequelize db:seed:all
This
demo user inserted into User table.
Note: Seeders execution is not stored anywhere unlike migrations, which use
the SequelizeMeta table. If you wish to override this please read Storage section
2
Undoing Seeds
Seeders can be undone if they are using any storage. There are two commands
available for that:
Models.users.create
Model.bulkCreate
Model.update
Model.destroy
Models.book.create({
bookid: book.id,
author: book.Author,
name: book.Name,
rating: book.rating,
});
Postgres
Select database
\c testdb;
3
Timestamp:false. For no time stamps in model
FreezeTableName: prevent sequealzie to pluralise the table name
connection.sync does not update table, it just creates new one if it does not exists.
Title:{
type: Sequelize.STRING,
unique:true,
allowNull:false
validate:{
len:[10,150] // validation that it is stringlength is between 10 to 150 characters long
}
}
Connection.sync({
Force:true, it forces the current model on database to create even if it exists, delete the
old ones and create this one.
Logging: console.log
}).then(function(){});
Body:’asdjasndasnkjd’})
Build and save for creating non persistence instance of model, and then
saving it to make persistence model.
4
Create directly creates persistence model
The above two methods return promises, so we can use it in then portion,
useful thing is datavalue properties, which contains data