Boilerplate for NestJS Framework using PostgreSQL Database.
- nestjs - A progressive Node.js framework for building efficient and scalable server-side applications.
- pg - Non-blocking PostgreSQL client for Node.js. Pure JavaScript and optional native libpq bindings.
- knex - A batteries-included, multi-dialect (PostgreSQL, MySQL, CockroachDB, MSSQL, SQLite3, Oracle (including Oracle Wallet Authentication)) query builder for Node.js.
- objection.js - An ORM for Node.js that aims to stay out of your way and make it as easy as possible to use the full power of SQL and the underlying database engine while still making the common stuff easy and enjoyable.
- jest - A delightful JavaScript Testing Framework with a focus on simplicity.
On this boilerplate since I'm planning to exposed this API using vercel, I need also to host my postgres database and dont have server to host the postgres database. I found supabase which used postgres database under the hood and also its free :).
But if you have server that can host postgres database and dont use supabase. I added a docker compose file on the project which run the postgres database locally.This is windows command docker-compose up -d
to execute the docker compose file.
Once you have postgres database running on local machine or supabase configure is done.
Execute this command for project environment variables. Make sure you are on the project level
Copy the fake env vars
$ cp .env.example .env
This command will create .env
file and copy whatever on .env.example
. Open .env
file and supply the env variables with the postgres connection credentials.
To setup tables and seeds (populate your database with test data or initial data) using knex migrations.
On the project, there is a folder called databases
on the first level
I made a sample migrations and seeds, To migrate this sample migrations this is command:
- Run migrations (creating tables and etc.)
$ npm run migrate
- Run seeds (inserting test data or initial data)
$ npm run seed
If you want to create new table you can do by executing this command
$ npm run migrate:make sample_table
This command will create a file under databases/migrations
directory with the filename that specify above which is sample_table
.
Or if you want to create new seed you can do by executing this command
$ npm run seed:make sample_seed
This command will create a file under databases/seeds
directory with the filename that specify above which is sample_seed
.