Database with Prisma
We.Publish uses Prisma as the database ORM. See https://www.prisma.io/ for more information.
Prisma
The required migrations, schema and seeding can be found at: @wepublish/api/prisma
.
It is important that the seeding includes @wepublish/api/prisma/seed.ts
as this sets up all the required roles.
Setup schema generation/seeding
In your package.json
you can add a new key called prisma
.
In that space you can add the path to the schema and seeding that should be executed.
To automate the generating of the prisma schema, we recommend to add the following script to your package.json
:
Prepare will automatically run on npm
install.
Your seed.ts
should seed atleast 1 user, so that you can login with it and change the password:
Basic Usage
For a more detailed documentation, see the official prisma documentation.
How to query a single entry
If you want to find a single entry by a unique index (such as by id, email or similar), you can use findUnique
as it is faster due to the fact it only has to search inside the indexes.
If you want to find a single entry without a unique index (such as the oldest article), you can use findFirst
.
Sometimes you want to combine this with orderBy depending on your use case.
How to query a list of entries
To query a list of entries you can use findMany
. Usually you want to pair this with take
to limit the amount of entries being returned and orderBy
to sort them according to something.
How to delete a single entry
If you want to delete a single entry you can use delete
.
How to delete a list of entries
If you want to delete a list of entries you can use deleteMany
.
Create new migration
To create a new migration you can run
This will diff the database with the schema and create a migration accordingly.
Reset database
You can reset the database by running the following command:
Prototype Prisma schema
Also see https://www.prisma.io/docs/concepts/components/prisma-migrate/db-push
This will completely wipe the database, migrate and seed it.
See more
Last updated