Start using Prisma in your existing database with Prisma introspection
This is a summary of what I’ll be presenting at Prisma Korea Meetup on Aug 28th.
If you are a javascript or typescript developer, I think you’ve already heard about Prisma, a next-generation ORM.
I’ve recently found few developers who wants to start using Prisma but their product is sticked to other orm like Sequelize. I also know how frustrating it is to switch ORM but this can be achieved pretty easily by Prisma’s introspection and I will tell what this is in this article.
Note that from Prisma 2.30.0,
prisma instrospect
command has changed toprisma db pull
.
When I started using Sequelize in 2016 from the raw query, the experience was just great. It reduces bugs on typo
and also provided more safe code by the static types and compile time errors. Therefore it saves much time on seeking through the runtime errors happening in raw sql queries.
It is pretty obvious to tell which one is more maintainable and also readable. In 2020, here it comes with Prisma. With Sequelize, you had to define each database table to sequelize models since they are active records.
In Prisma, you do not have to write for your models to start using its apis.
Imagine you have your own database system setup already. I will show you how Prisma instrospect database of existing service HackaTalk which is an opensource chat app.
Let’s clone the HackaTalk repository to create the database.
git clone https://github.com/dooboolab/hackatalk
Navigate to ./server
and install packages.
Create a test user in your PostgreSQL database and copy test.env
to .env
in ./dotenv
.
cp dotenv/test.env dotenv/.env
Finally, run yarn migrate:deploy
to create hackatalk
database in your local machine. The script will run the below command in your terminal.
dotenv -e ./dotenv/.env -- prisma migrate deploy
If you do not have done successfully, you will see the below tables when running npx prisma studio
.
And below is how it looks in dbeaver.
Now, we have sample database to test Prisma Introspection.
Clone prisma-introspect-tutorial repository and create prisma/.env
file. Then add below line of code.
DATABASE_URL="postgresql://test:test!@localhost:5432/test
Finally, run npx prisma introspect
and see the result.
When you see schema.prisma
file, you’ll see the generated models from introspection. This is awesome 🙌
Now, we will see what we can do with the generated schema.
1. Generate Prisma client from Prisma schema.
npx prisma generate
2. Open script.ts
file and see what you get. You will see all the models appearing in auto-completion from the Prisma client.
You can immediately use prisma apis with your database records.
Prisma Introspection is really powerful!
You can introspect any database if they are supported by Prisma.
It can also reduce your time to understand the database going through the schema.prisma
file.
Finally, I want to share how you can migrate your database to other providers using multiple Prisma Clients.
I made three Prisma schemas each with different database providers, mongodb, mysql, and prostgresql.
Run the below scripts to generate Prisma clients.
Finally, write the below code to copy rows of tables.
After running codes, you will end up having rows in each database.
You’ve successfully migrated to another database provider. You can also test these in prisma studio
. I took a screenshot with different tools to verify that they are running on different database providers.
As you can see in the post, you can start using Prisma with minimal effort! The source code is here!