Photo by bert sz on Unsplash

Sequelize /-CLI With Migrations

The scattered parts from “getting started”

“Off we go” ( Setup ):

$ npm init -y
console.log('Hello World!')
$ tree -I node_modules.
├── package.json
├── package-lock.json
└── src
└── index.js
$ npm i sequelize sequelize-cli sqlite3
$ npx sequelize init
.
├── config
│ └── config.json
├── migrations
├── models
│ └── index.js
├── package.json
├── package-lock.json
├── seeders
└── src
└── index.js
$ npx sequelize init
.
├── package.json
├── package-lock.json
└── src
├── db
│ ├── config
│ │ └── config.js
│ ├── migrations
│ ├── models
│ │ └── index.js
│ └── seeders
└── index.js
{
"development": {
"username": "root",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}

“The fun part” (Database Models)

$ npx sequelize model:generate --name Task --attributes taskName:string
$ npx sequelize model:generate --name User --attributes name:string
.
├── package.json
├── package-lock.json
└── src
├── db
│ ├── config
│ │ └── config.js
│ ├── migrations
│ │ ├── 20190611175237-create-task.js
│ │ └── 20190611175246-create-user.js
│ ├── models
│ │ ├── index.js
│ │ ├── task.js
│ │ └── user.js
│ └── seeders
└── index.js
$ npx sequelize db:migrate
$ npx sequelize seed:generate --name task
$ npx sequelize db:seed:all
ERROR: Validation error
$ sqlite3 db.sqlite 
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> .tables
SequelizeMeta Tasks Users
sqlite> .schema Users
CREATE TABLE `Users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
sqlite> .schema Tasks
CREATE TABLE `Tasks` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `taskName` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
sqlite> SELECT * FROM SequelizeMeta;
20190611175237-create-task.js
20190611175246-create-user.js
sqlite>
$ npx sequelize db:seed:allSequelize CLI [Node: 11.15.0, CLI: 5.5.0, ORM: 5.8.9]Loaded configuration file "src/db/config/config.js".
Using environment "development".
== 20190611182947-task: migrating =======
== 20190611182947-task: migrated (0.030s)
== 20190611182953-user: migrating =======ERROR: Migration 20190611182953-user.js (or wrapper) didn't return a promise
$ node src/index.js 
Executing (default): SELECT `id`, `taskName`, `createdAt`, `updatedAt` FROM `Tasks` AS `Task`;
Executing (default): SELECT `id`, `taskName`, `createdAt`, `updatedAt` FROM `Tasks` AS `Task`;
This is task number one!
This is task number two!
This is task number three!
This is task number one!
This is task number two!
This is task number three!

“The manual part” (Associations)

$ npx sequelize migration:generate --name add-Task-userId-column
$ npx sequelize db:migrateSequelize CLI [Node: 11.15.0, CLI: 5.5.0, ORM: 5.8.9]Loaded configuration file "src/db/config/config.js".
Using environment "development".
== 20190612212617-add-Task-userId-column: migrating =======
== 20190612212617-add-Task-userId-column: migrated (0.056s)

“The end…” (Conclusion)

Father & Husband! / Product Manager / System Engineer / Full Stack Dev

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store