Features
Connection to PostgresSQL via Typeorm​
The configuration for the connection to the database is stored in datasource.options.ts
.
For the connection itself it is done in two different places depending on the context :
- in
src/app.module.ts
for the app connection - in
datasource.ts
for the typeorm cli
JWT authentication with 2 tokens: access and refresh​
In the app you will find a JWT authentication based on 2 tokens :
accessToken
(short life)refreshToken
(long life)
The client upon login receive the two tokens, the accessToken
in the body and the refreshToken
in a http secure cookie.
Each time the access token life ends the frontend calls the refresh route to get a new accessToken
.
A global scoped AuthGuard
is applied in the auth.module.ts
, this means that EVERY ROUTE is by default under authentication. If you want to create a public route, a custom decorator @Public()
is available to deactivate authentication for the route.
You can find the files related to authentication in src/modules/authentication
folder.
Custom logger​
We created a custom logger on top of the default one because we want to have JSON logs, otherwise they wont be easy to use in our logs system (CloudWatch, DataDog, ...).
This custom logger is build on top of winston
and can be found in src/modules/logger/custom-logger.service.ts
.
There is a middleware that make use of this logger to log every query at their end, it is defined in src/modules/logger/logger.middleware.ts
Swagger auto generation​
A swagger is available in dev if you go to /swagger
url on your backend.
It's auto generated by the NestJS cli, we added @nestjs/swagger/plugin
in nest-cli.json
for this, see official documentation if you want more informations or to customize it.
The configuration of the swagger ui lies in src/main.ts
.
We created custom Controller
and HTTP method decorators to improve swagger auto generation, you can find them in src/decorators/controller.ts
and src/decorators/httpDecorators.ts
. They apply automatically other decorators that will anotate authentication, group routes, ...
/!\ The routes grouping is done in src/decorators/controller.ts
is very basic and will not function very well during the life of your projet, make sure you update it if you want to a have a beautiful swagger.
REPL​
It's the implementation of the REPL system that went out with the version 9 of NestJs, if you haven't seen already the demo in the doc go check it out it's mind blowing (you'll need to scroll a bit).
To launch the app in repl mode just run pnpm run explore
Integration tests parallelisation​
The integration tests call the postgres database, so parallelisation is not easy to do as the tests are writting / deleting data in the database.
The way we worked around this is by leveraging the entityPrefix
in the datasource.options.ts
, by setting this variable all the entities are now prefixed by the value you specified. We pass the id of the jest worker, which is provided by jest in JEST_WORKER_ID
env variable, to entityPrefix
in our tests setup via testUtils/setup.ts
which set TYPEORM_ENTITY_PREFIX
env variable that is used in datasource.options.ts
as the value of entityPrefix
.
This feature of prefix is not present on all ORMs but can be recoded to use the same kind of system and keep test parallelisation (we already did it with Mongoose for example)
Factories to generate fixtures​
The Factory method is a classic way of reducing the tests boilerplate.
Our implementation lies in testUtils/factory.ts
, it's an abstract class that requires the extender to define a createBase
method that returns an entity. It exposes two methods createOne
and createMany
that will use createBase
to save in database the entity (or the list of entities) and then return it (or them).
You can find an example in src/modules/user/user.factory.ts
.
Basic health check route​
There is a basic health check route that you can call on the path /health
, it's defined in src/app.controller.ts
.
If you want to do a more complex health check route you can check NestJS Documentation on the subject.