Skip to content

Latest commit

 

History

History
52 lines (31 loc) · 1.72 KB

Database.md

File metadata and controls

52 lines (31 loc) · 1.72 KB

Database

This project can use the most popular SQL databases such as PostgreSQL, MySQL, Oracle, MSSQL and Sqlite. This is achieved with Sequelize, the most popular ORM for Node.js

Install and Start

Start the postgres database asnd other services:

$ npm run docker:up

Connect to the database:

$ psql "postgres://postgres:password@localhost:6432/dev?sslmode=disable"

Configuration

Here is an example of the configuration for Postgres:

"db":{
    "url": "postgres://postgres:password@localhost:6432/dev?sslmode=disable",
    "options": {
      "logging": true
    }
}

Migration

Database migration are not necessary for development environment but only for system already in production.

Run the following command to migrate the database:

$ npm run db:migrate

Creating a new data model

sequelize-cli helps to manage the database migration and rollback.

By using the model:create command, a new sequelize model is created alongside its migration script for database update and rollback

$ ./node_modules/.bin/sequelize model:create --name User --attributes "name:text, password:text"

$ ./node_modules/.bin/sequelize model:create --name UserPending --attributes "username:string(64), email:string(64), password:string, code:string(16)"

$ ./node_modules/.bin/sequelize model:create --name PasswordReset --attributes "user_id:integer, token:string(32)"

2 files will be generated:

  • the javascript sequelize model in the models directory
  • the sql migration script in the migrations directory

Eventually change the sql table name to underscore_case