Prisma
Prisma is a popular ORM. It is a flexible tool for managing database migrations. In this guide, we’ll walk through the process of setting up Prisma in a project and running several migration scenarios.
Setting Up an New Prisma project
Create a new project (or clone our example project)
If you’d like to clone our example project, you can do so with the following command:
Configure your environment
You can get your connection string from the Nile console under the “Connections” page.
Using Prisma with Nile in development
Nile has built-in tables that can be useful in your project.
Especially the tenants
table, which is used to store tenant information.
Therefore, we recommend first pulling in the built-in tables to your project, and then starting to build your own tables.
Pull in the built-in tables
This will generate the schema.prisma
file with the built-in tenants
table.
Generate Prisma client
Add your own tables
To add your own tables, you can edit the schema.prisma
file and add your own models. For example:
Push changes to the database
Using Prisma with Nile in production
db pull
and db push
work well in development, where you can evolve the schema without tracking every change.
However, in production, we recommend using tracked migrations to ensure that the schema is always in sync with the code.
Because Nile has a built-in tenants
table, we first create a baseline migration that will include the built-in tables,
and then create additional migrations for our own tables.
Create a baseline migration
If you don’t yet have a schema.prisma file with the built-in tables, start by creating one:
Then, create a baseline migration:
Apply the baseline migration
Create a migration for your own tables
Now, lets modify the schema to include our own tables.
For example, we’ll add a posts
table to schema.prisma
.
If you already have this table in your schema, you can add a column instead.
Now, we’ll create a migration for this change by comparing the existing schema in the
DB (the datasource
) with the new schema in schema.prisma
(the datamodel
).
While both the datasource
and datamodel
are in the same file, the first will refer to the
datasource
definition in the schema.prisma
file while the second will refer to the model itself.
Prisma docs recommend generating migrations with npx prisma migrate dev
. However, this will not work
for Nile because the npx prisma migrate dev
command starts by attempting to “reset” a shadow database by dropping all tables.
This is not possible in Nile because the built-in tables are required for core functionality.
Apply the migration
Last but not least, we can apply the migration to the database:
Was this page helpful?