๐ค Migrating from Knex to Prisma ORM
Prisma provides the best experience for your team to work and interact with databases.
Migrating from Knex.js to Prisma involves several steps as Prisma is a full-fledged ORM with its own schema and migration system, while Knex is a SQL query builder.
๐ Migrating to Prisma removes a lot of boilerplate code from your projects, really simplifies your code and reduces the development time significantly! Prisma ORM makes developers a lot more productive! ๐ค Read more at why prisma.
Here's a high-level guide for a smooth migration:
Adding Prisma in your existing project
No need to worry about how Prisma gets installed or configured in your project, theGraphQL Rocket Generator will do everything for you and make you project "Prisma ready".
Prisma infrastructure overview
First we have the
prisma
folder containing theschema.prisma
file, where you will define your data models.Then we have the
src/prisma
folder where would find some custom implementation for pagination or multi-tenancy and the most important, theprisma()
function, that returns the instance ofPrismaClient
, which we will use everywhere and allows us to basically write the queries.
Database Connection
In
schema.prisma
, configure the database connection:
Make sure the DATABASE_URL
is set correctly in your .env
file with your existing Knex database credentials. We usually use this pattern for database connection string configuration:
Define Prisma Models
In Prisma, you define models to represent your tables. Prisma's models are more declarative compared to Knex migrations. Start by replicating your existing Knex schema into Prisma models. Here is an example for a
users
table:
Prisma offers a rich set of features like relations, field constraints, and defaults. Carefully match these with your existing schema.
Introspect Existing Database (Optional)
If your database already exists, you can use Prisma's introspection tool to generate the schema:
This command introspects your database and generates Prisma models based on your current schema.
Replace Knex Queries with Prisma Client
After setting up the schema, replace Knex queries with Prisma's type-safe client. For example, a Knex query like:
Would become:
๐Prisma queries should directly be written inside the GraphQL resolvers. No need for the old data-sources extra files. ๐Prisma automatically handles the
n+1 problem
so the old dataloaders we had to write for knex will be removed! ๐ Read more about it here.
You will notice that after migrating a feature to Prisma a few folders will be useless and must be removed. here is the full list of files and folders related to Knex that must be deleted after migrating to Prisma:
/src/db/
/middleware/db/
/messaging/middleware/dbInstance.js
/startup/dataLoaders.js
/features/common/dbGenerators.js
/features/<your-feature>/dataLoaders.js
/features/<your-feature>/dataSources/<old DataSource files>
/utils/sqlDataSource.js
Handling Data Migrations
Prisma Migrate can be used for future data migrations. Create migration files, then run
prisma migrate dev
orprisma migrate deploy
to apply them. You can also handle more complex migrations by writing custom SQL or scripts as needed.
Unsupported Features
Prisma may not support certain SQL-level features you used with Knex (like raw SQL queries, partial indexes, or database-specific features). You can either replace or modify Prisma-generated SQL or use
prisma.$queryRaw
for raw SQL queries where necessary.
Testing and Deployment
Test the migration in a staging environment to ensure everything works as expected, then apply it to production using:
This migration process ensures that you move to Prisma's more modern ORM capabilities while retaining control over your schema and data. Full guides and examples can be found on the (Prisma)
Last updated