Giter Club home page Giter Club logo

nestjs-prisma-postgres-sandbox's Introduction

Sandbox

Open in Visual Studio Code Maintainability Rating Security Rating Reliability Rating Code Smells Duplicated Lines (%) Coverage Coverage Github workflow Last deployment Last commit

You can find the deployed app here.

โšก What is this repo about?

This is a sandbox to evaluate nestjs and dig how fun it is to implement typical requirements for a backend, with an enphasis on testing, given how dearly missed this requirement can be in 'real world' examples found here and there.

โšก Stack

So let's talk about the stack (pushing on an open door here, but hey):

Package / techno Description Documentation
๐Ÿ’— typescript JS superset https://www.typescriptlang.org/docs/
๐Ÿ˜ธ Nestjs Our cats fetishists backend framework https://docs.nestjs.com/
๐Ÿ”บ Prisma2 The ORM https://www.prisma.io/docs/
๐Ÿ˜ postgresql Relational database engine https://www.postgresql.org/docs/
๐Ÿ› Jest Tests library https://jestjs.io/fr/docs/getting-started
๐Ÿงช supertest End to end testing helpers https://github.com/visionmedia/supertest
โœ๏ธ eslint Linter https://eslint.org/docs/user-guide/getting-started
๐Ÿ“ prettier Formatter https://prettier.io/docs/en/index.html

โšก Guidelines

๐Ÿ”ถ Schema splitting

We do not want a huge prisma schema. We want to isolate each model (table or set of tables) in its own file.

๐Ÿ”ถ Well documented routes

Let's have a swagger documenting properly exposed routes, that is mainly for each route:

  • a description.
  • the list of possible responses.
  • a definition of the inputs and outputs.

๐Ÿ”ถ Full testing coverage

We want to test everything to learn how to properly test, and to face every single difficulty that comes with testing. We will at very least do end to end using superagent, controllers testing, service testing.

๐Ÿ”ถ No testing against the database

All tests should run without any interaction with a database.

โšก Usage

๐Ÿ”ถ run locally

๐Ÿ‘‡ Since our prisma schemas are split within modules, we will have to merge them all in one file prisma can understand. Let's do just that:

yarn prisma:merge

๐Ÿ‘‡ Now, we need to tell prisma to generate in node_modules the code actually allowing us to interact with the database:

yarn prisma:gen

๐Ÿ‘‡ You will need docker and docker-compose to get the postgres database up and running. You can use this command to launch the database container:

yarn docker

๐Ÿ‘‡ Then, let's inject some data in our dev database using:

yarn prisma:seed

๐Ÿ‘‡ We can now launch the backend in dev:

yarn dev

๐Ÿ˜ต You can do the merge, gen & seed steps all at once using the following command:

yarn dev:db

๐Ÿ”ถ test all the things

๐Ÿ”ถ We can run all the tests and get a coverage report using the following:

yarn test:dev

โšก Subjects

๐Ÿ”ถ Authentication

Let's use passport to setup jwt based authentication.

๐Ÿš€ Routes

Two routes were defined to demonstrate the use case:

Route Description Documentation
POST /users/login The login route Link
GET /users/profile Logged user profile retrieval Link

๐Ÿ“š Mock data

We have two users in database to play with the routes:

๐Ÿงช Tests

  • โœ… e2e
  • โœ… controllers
  • โœ… services
  • โœ… local passport strategy

๐Ÿ”ถ CRUD

Let's create CRUD routes to manage a list of books. We want to make sure to give a proper feedback when foreign keys violations do occur (when we try to delete an entry whose key is referenced in another table or when we try to update an entry with a foreign key that does not exist). Let's use filters for that!

๐Ÿš€ Routes

Route Description Documentation
GET /books Retrieves all books Link
GET /authors Retrieves all authors Link
GET /authors/{id}/books Retrieves the book written by an author Link
POST /books Creates a book Link
POST /authors Creates an author Link
PUT /books/{id} Updates a book Link
PUT /authors/{id} Updates an author Link
DELETE /books/{id} Deletes a book Link
DELETE /authors/{id} Deletes an author Link

๐Ÿงช Tests

  • โœ… e2e
  • โœ… controllers (turns out these are pretty much useless since we mock the service)
  • โœ… services
  • โœ… validation pipe
  • โœ… filters

nestjs-prisma-postgres-sandbox's People

Contributors

jpb06 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.