This repository was created as a proof of concept for good practices when creating a Rest API to manage an underlying Mongo database in a Node.js environment.
It's heavily inspired by existing online articles, especially this one by Siegfried Grimbeek.
- use strongly-typed data
- quickly generate database with existing customizable data sets
- assert Rest API and database operations
This project both defines data's mongoose schema definitions and typescript types to allow manipulating strongly-typed data (and also because, let's admit it, it's trendy 😅).
Another solution would have been to use the excellent library typegoose.
🌵 Please note that, at the time this documentation is written, typegoose yet doesn't support mongoose discriminators feature.
Database seeding automatically generate a database with documents created from the files seeds.ts
defined in each collection folder under src/db
, using mongo-seeding.
This saves you the tedious task to have to manually create documents every time you want to run tests. It also allows you to quickly add, modify or remove documents to the data sets and re-populate in no time.
Rest API routes are automatically generated by routes.ts
from the collections in the src/db
folder, using fastify. API endpoint url path parameters / request body are automatically validated with joi.
Each collection will automatically have the following routes :
Method | URI | Action | Validation |
---|---|---|---|
GET |
/api/collection |
find all the documents | none |
GET |
/api/collection/:id |
find a specific document by its id |
validate id in path |
POST |
/api/collection |
create a new document | validate document in body |
PUT |
/api/collection/:id |
update a specific document by its id |
validate both id in path and document in body |
DELETE |
/api/collection/:id |
delete a specific document by its id |
validate id in path |
As a side note, the API always return plain objects (no fancy mongoose Documents).
Integration tests are defined in index.test.ts
and run using jest.
All collection folders are automatically tested using the seeds.
Tests are also run after each push on Travis CI.
Code coverage is updated on Codacy after each successful CI build.
Dependencies are kept up-to-date with Renovate.
Dependencies vulnerabilities are monitored with Snyk.
API documentation is automatically generated with fastify-swagger.
It can be found once server is started at http://localhost:3000/documentation.
Clone this repository on your computer
git clone https://github.com/Roms1383/robust-database.git
Install the dependencies
yarn install
You can add a .env
file at the root of the project, in order to configure :
DATABASE_HOST
: the host of the databaseDATABASE_PORT
: the port of the databaseDATABASE_NAME
: the name of the databaseSERVER_HOST
: the host of the serverSERVER_PORT
: the port of the serverSERVER_LOGGER
: whether or not to display server's logs
If not provided, the project will automatically be setup with the following defaults :
DATABASE_HOST="localhost"
DATABASE_PORT=27017
DATABASE_NAME="testing"
SERVER_HOST="localhost"
SERVER_PORT=3000
SERVER_LOGGER=false
lint
: lint the TypeScript files intosrc
folder- execute tslint command
build
: build the TypeScript files into abuilt
folder- execute tsc command
postman
: auto-generatepostman.json
to import into Postman- execute
build
command - execute postman.js file
- execute
seed
: seed the Mongo DB from theseeds.ts
located in the collections folders- execute
build
command - execute seeding.js file
option :
-d
/--drop
will automatically drop any former existing database without asking confirmation first- execute
use
: execute a set of database commands as an example- execute
seed
command - execute using.js file
- execute
server
: dynamically deploy the server- execute server.js file
serve
- execute
seed
command - execute postman.js file
- execute
server
command
- execute
vulnerabilities
: run dependencies vulnerabilities check with Snyk- execute snyk test command
test
: run integration tests- execute
vulnerabilities
command - execute
seed
command - execute jest command
- execute
test-with-coverage
: run integration tests, generate code coverage report and send it to Codacy (used in Travis CI)- execute
test
command - execute codacy-coverage command
- execute
You can also :
- seed database, auto-generate
postman.json
and run the server :yarn serve
- import freshly generated
postman.json
into Postman - quickly test the endpoints yourself