This project follows a microservices-based architecture, where each feature is separated into a distinct service, with individual Node.js servers and databases for each service.
To initiate a new service, follow these steps:
- Create a New Folder: Create a new folder with the desired service name.
- Initialize NPM Project: Run
npm init
to initialize a new npm project. - Install Dependencies:
express
: A framework for creating an Express server.body-parser
: Middleware for parsing incoming request bodies.nodemon
: A tool for automatically restarting the server during development.
Project structure includes separate src
and tests
folders:
-
src/
: Contains the main application logic and files for deployment.index.js
: Entry point for the application.models/
: Stores data models.migrations/
: Manages database schema changes.controllers/
: Handles request-response logic.middlewares/
: Contains middleware functions.utils/
: Houses utility functions.config/
: Holds configuration files.services/
: Implements business logic.repository/
: Manages data access logic.seeders/
: Populates the database with initial data.routes/
: Defines API routes.
-
tests/
: Contains test files. -
package.json
: Project configuration file. -
package-lock.json
: Records the exact version of each installed package. -
.gitignore
: Specifies files and directories to be ignored by version control.
- Set Up Express Server: Establish a basic Express server.
- Install dotenv: Use
npm i dotenv
to install dotenv, a zero-dependency module for loading environment variables. - Create
.env
File: Create a.env
file in the root folder to store environment variables. Ensure that it is added to the.gitignore
file. - Configure dotenv: Set up dotenv in the
config
folder by creating aserverConfig.js
file. Refer to the documentation for further setup.
- Install Sequelize and MySQL Dependencies: Run
npm i mysql2 sequelize sequelize-cli
to install Sequelize and MySQL dependencies. - Initialize Sequelize: Run
npx sequelize init
to generate the necessary folders (config
,migrations
,models
,seeders
). - Configure Sequelize: Edit
config/config.json
with local MySQL server details and create a database usingnpx sequelize db:create
in thesrc
folder.
- Generate Model: In the
src
folder, runnpx sequelize model:generate --name <Model Name> --attributes <Schemas>:<type>
to generate a model. - Run Migrations: Execute
npx sequelize db:migrate
to run migrations and create a table in the local database. - Undo Migrations: Use
npx sequelize db:migrate:undo
to undo migrated files.
- Repository Layer: In the
repository
folder, create a<Model Name>
repository file for CRUD operations. - Export Repositories: Create an
index.js
file in therepository
folder to export all repositories as a single object. - Service Layer: Repeat the same structure for the
services
folder.
- Create Model: Begin by creating a model.
- Repository Layer: Create a file in the
repository
folder for CRUD API operations. The repository layer communicates with any layer or the database. - Service Layer: In the
services
layer, create a file for the model. The service layer is responsible for business logic and calls the repository layer. Create CRUD functions similar to the repository layer. - Controllers: Controllers call services. Controllers are functions; create named functions (e.g.,
const create = (req, res) => {}
). - Express Router: Use Express Router. Create an
index.js
file for routing. Combine files using export/import. In folder files, use controller functions (e.g.,router.use("/name", controllerFunc)
). - Main API Routes: In the main
index.js
file, write the main API routes (e.g.,app.use("/api", routesFunc)
). - Seeders: Seeders are used to add hardcoded data into the database.
For working with PostgreSQL, here are some useful commands:
sudo -i -u postgres # Open PostgreSQL server terminal
psql # Open PostgreSQL database terminal
sudo systemctl status postgresql.service # Show PostgreSQL server status
sudo systemctl start postgresql.service # Start PostgreSQL server locally
sudo systemctl stop postgresql.service # Stop PostgreSQL server locally
\l # List all databases
\c database_name # Switch to a database or use a database
\dt # Show all available tables
\d table_name # Describe table
\x # Expanded display (recommended to turn off)
TABLE "table_name"; # View table data