nodejs-boilerplate-sequelize
v1.8.0
Published
Basic boilerplate for Node.js server application with Sequelize as ORM
Maintainers
Readme
nodejs-boilerplate-sequelize
Basic boilerplate for Node.js server application with Sequelize ORM and PostgreSQL DB
Pre-requisites
- Node.js latest version with npm & npx
- PostgreSQL v13+
Installing Dependencies
- To install the boilerplate globally, run
npm i -g nodejs-boilerplate-sequelize- You can use the following command in any directory to generate the project folder structure
npx generate-boilerplate app-name- Create a ".env" file in the project's root and copy the contents of "example.env" file and replace the values.
- To start the server locally, run
npm start- To start the server in development mode, run
npm restartProject Folder Structure
- Starting point of the application is "server.js". The command "npm start" runs "node server.js" script.
- "server.js" defines the creation of application server using in-built "http" module. It takes Port Number from the "PORT" variable in .env file and binds it to the server.
- The application uses "express" framework as defined in "app.js" file. It also defines custom functions to be used globally and middlewares as well.
- PostgreSQL DB's connection string is written in "data/models/index.js". It takes Host, DB name, Username, Password and Dialect values from the variables in .env file.
- The directory structure is as follows:
|
|--controllers
| |--userController
|
|--data
| |--config
| |--migrations
| |--models
| |--seeders
|
|--routes
| |--user_routes
|
|--services
| |--userServices
|
|--.sequelizerc
|
|--app.js
|
|--package.json
|
|--server.js- Schemas for "users" model is defined under "models" directory.
Migrations
- A model and migration file can be generated simultaneously via the following command.
npx sequelize-cli model:generate --name <ModelName> --attributes <FirstAttribute>:<dataType>,<SecondAttribute>:<dataType> ...- DB migrations can be run via
npx sequelize-cli db:migrate- DB migrations can be reverted via
- For undoing latest migration
npx sequelize-cli db:migrate:undo- For undoing all migrations upto specific file
npx sequelize-cli db:migrate:undo:all --to <XXXXXXXXXXXXXX-filename>.jsSeeding
- A seed file can be generated via
npx sequelize-cli seed:generate --name <SeedName>- DB seeding can be done via
npx sequelize-cli db:seed:all- DB Seeding can be reverted via
- For undoing latest seed
npx sequelize-cli db:seed:undo- For undoing particular seed
npx sequelize-cli db:seed:undo --seed name-of-seed-as-in-data- For undoing all seeds
npx sequelize-cli db:seed:undo:all