Skip to content

Database

LeChatErrant edited this page Mar 20, 2021 · 20 revisions

Database

The database used is postgreSQL. It's stable, maintained, and will fit most general usecases.

ORM

An ORM (Object Relational Mapping) is used to abstract communication with the database, to convert the relational representation into data structures understood in code and to avoid several security issues

This project uses Prisma, which guaranties type-safety directly from your database model, drastically increasing auto-completion and jump-to-definition features, and comes along with a great documentation and powerful tooling (described below).

It's simple : you code faster, and if you're doing something wrong, the typescript compiler will scream on you at build time

Schema

With Prisma, schema is written in PSL (Prisma Schema Language). It makes you model pretty straightforward

The model is the unique source of truth, from which you define the shape of the data, create migrations and generate the whole data access layer

Each time you modify the schema.prisma, you need to regenerate your ORM (to have up-to-date methods and types)

It can be achieved through npm run generate

ℹ️ Launching the dev mode will watch for any changes to reload the ORM automatically

Migrations

⚠️ Changing your database model is not changing what's in your database. That's why you need migrations: migrations describe those changes in your database.

Generate it with npm run db:migrate once you're satisfied with your new model

It will be generated in prisma/migration

Don't forget: migrations are part of your code and need to be pushed with it

Once migrations are generated and pushed to your codebase, you can apply it on you production database with npm run db:up

⚠️ Be careful ! It is a potentially destructive operation (eg: removing user table)

Triple check before destroying your production database

I recommand reading more on Prisma Migration tool here : Prisma Migrate

Prisma migrations
Migration workflow from prisma documentation

Studio

The studio is a nice web interface allowing to visualize and edit your database in real time

Simply launch an instance with npm run db:studio

Prisma studio

Clone this wiki locally