Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Roms1383/robust-database

Repository files navigation

Build Status Codacy Badge Codacy Badge Renovate badge Known Vulnerabilities Dependencies

purpose

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.

goals

  • use strongly-typed data
  • quickly generate database with existing customizable data sets
  • assert Rest API and database operations

📌 types

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.

🌱 seeding

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.

🔁 api

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).

✅ tests

Integration tests are defined in index.test.ts and run using jest.

All collection folders are automatically tested using the seeds.

👷 continuous integration

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.

📋 documentation

API documentation is automatically generated with fastify-swagger.

It can be found once server is started at http://localhost:3000/documentation.


project

prerequisites

installation

Clone this repository on your computer

git clone https://github.com/Roms1383/robust-database.git

Install the dependencies

yarn install

environment

You can add a .env file at the root of the project, in order to configure :

  • DATABASE_HOST: the host of the database
  • DATABASE_PORT: the port of the database
  • DATABASE_NAME: the name of the database
  • SERVER_HOST: the host of the server
  • SERVER_PORT: the port of the server
  • SERVER_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

available commands

  • lint : lint the TypeScript files into src folder
    • execute tslint command
  • build : build the TypeScript files into a built folder
    • execute tsc command
  • postman: auto-generate postman.json to import into Postman
    • execute build command
    • execute postman.js file
  • seed : seed the Mongo DB from the seeds.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

  • use : execute a set of database commands as an example
    • execute seed command
    • execute using.js file
  • server : dynamically deploy the server
    • execute server.js file
  • serve
    • execute seed command
    • execute postman.js file
    • execute server command
  • vulnerabilities : run dependencies vulnerabilities check with Snyk
    • execute snyk test command
  • test : run integration tests
    • execute vulnerabilities command
    • execute seed command
    • execute jest command
  • 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

Postman

You can also :

  1. seed database, auto-generate postman.json and run the server :
    yarn serve
  2. import freshly generated postman.json into Postman
  3. quickly test the endpoints yourself

Romain KELIFA - 2019 - MIT license