Skip to content

scneba/nodebase

Repository files navigation

Node.js Base Backend

This is a base project which helps any node.js developer or startup to setup a base for user/role/permission management, testing, dockerization and CI/CD. This is usually a first and difficult step for any startup and this project will get you up and running immediately.

Technologies and Resources

  • Git
  • PostgreSQL
  • Node.js
  • Sequelize ORM
  • Postman

Setup

  1. Download and install git from here. This should also install git bash which can be opened from the Windows start menu.
  2. Download and Install PgAdmin 4 from here. This should come with Postgresql and should automatically run after installation on port 5432. Make sure to take note of your passwords, there should be a PgAdmin password and the default user password. After installation, open git bash and run the following commands to check if Postgres is running on the same port
psql --version

If the version does not show up, add psql path to your system paths. On Windows, this is installed by default at C:\Program Files\PostgreSQL\14\bin

  • Connect user (use the default username, which should be postgres if not changed)
psql -U postgres
  • Check the connection status
\conninfo

You should see output like this image here
image

  • Start PgAdmin from Windows start menu and input your passwords.
  • Create a database called node_base with the user postgres.
  1. Install Node.js. Download your variant from here
  • After installation, open git bash and type the command below to see if you get the help page
npm -h
  1. install sequelize-cli globally
npm install -g sequelize-cli

After installation, the command should display the help page

npx sequelize-cli --help
  1. Clone this repository
git clone https://github.com/scneba/nodebase
  1. open git bash on the root folder and run
npm install
  1. Make a copy of the .env.example and rename it to .env
  2. Update the connection string on line 2 to include your postgres user password (update the string password below)
CONN_ST=postgres://postgres:password@localhost:5432/nodebase
  1. Run npm run dev on the root folder. This should run the migrations on the nodebase database.

  2. Install Postman and create an account. Download from here.

Development

File structure

--postman
--files.json postman collections.
--src all source code should be added in here.
--controllers MVC controllers - all core logic for endpoint control is added here.
   --errors.js All error contants. Any errors reported in this service should be here.
   --service.js All the logic for the subcontroller should be added here.
   --service.test.js All tests for this controller should be added here. See the documentation for jest testing here.
--data all database access methods are added here.
--migrations All Sequelize migrations are added here. See the docs [here](n.
--models All database models for Sequelize --routes All routes contained in the repo. --seeders All Sequelize seeds see [here]s(https://sequelize.org/master/manual/migrations.html#running-seeds) for the docs.
--services All access to external services are added here, cross-platform requests.
--utils Any reusable javascript code
--app.js entry point of application.
--migrate.js service to run migrations when the user first runs app.
--storage storage for sessions

Run Seeds and Migrations with Sequelize

  • cd into src and run the following command to create a sequelize config file
 npx sequelize init

This should only create config/config.yml file and fail to create model and migrations folders that already exist.

Update the config/config.json file to reflect the database configurations

"development": {
    "username": "postgres",
    "password": "YOURPASSWORD",
    "database": "nodebase",
    "host": "127.0.0.1",
    "dialect": "postgres"
},

Seeding

Undo all seeds

npx sequelize-cli db:seed:undo:all

Seed Database

npx sequelize-cli db:seed:all

Testing

Ideally, all core logic in controllers should be tested. To test particulr sub controller/file, use the testPathPattern eg.

npm test -- --testPathPattern "registering/"

This will run all tests in the registering/ path.

Postman

Import the collection at /postman/role_base.postman_collection.json into postman. Update the domain variable in postman image

  • Start project with nodemon
npm run dev
  • Run postman collection and make sure all tests pass image

About

Base project for node.js/express development and testing with sequelize.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published