Rocetta: Setting up databases with Prisma
Databases are essential infrastructure of any application. Here is how you can use Rocetta to quickly set up a database (MySQL or Postgres) and connect to an ORM - Prisma. If you haven’t already set up your AWS and GCP accounts, check out our guide here!
Deploying a database with Rocetta
Rocetta lets you deploy databases in just a few clicks.
- Go to the
Database
page of the console.
- Click
Deploy new database
- Choose your desired database parameters: select the provider of your choice: AWS or GCP. Then select the database type: MySQL or PostgreSQL. Modify the database size and region if you want.
- Please note there are slight differences in using Prisma between Postgres and MySQL. Learn more here. We suggest using Postgres.
- Size is always upgradable, so start with small or extra small if you are starting out.
- You cannot change the region. Choose a region near your customers.
- Click deploy!
- Once the database is running, you can get the database URL by clicking on the three dots on the right side of the database card. Choose
Copy Database URL
on the menu that appears. Click the blackCopy
button, then close.
Setting up and Connecting Prisma
Now let’s set up a quick Prisma project! The following steps summarize the guide from Prisma’s official website.
- Run the following set of scripts. This creates a new directory for your project, initializes a typescript project, and adds the Prisma CLI as a dependency. Finally, the last command creates a new Prisma schema file.
$ mkdir my-prisma-project $ cd my-prisma-project $ npm init -y $ npm install prisma typescript ts-node @types/node --save-dev $ npx tsc --init $ npx prisma init
- Connect your database by pasting your database URL (see “Deploying a database with Rocetta”) as an environment variable within your .env file. We will also have to make some modifications, listed below:
- Rocetta generates a database URL with format:
<type>://<user>:<password>@<endpoint>:<port>/<name>
.
DATABASE_URL="YOUR_DB_URL_HERE"
b. In the
schema/schema.prisma
file, you will have to modify the datasource db variable to contain the provider of your choice: “postgressql” or “mysql”generator client { provider = "prisma-client-js" } datasource db { provider = "INSERT_TYPE" url = env("DATABASE_URL") }
That’s all! With this setup, you are ready to create your own Prisma schema and migrate it to your database. If you are new to Prisma, you can continue this project with the official guide. Happy provisioning!