Project: YugabyteDB (App Definition and Development / Database)
File: prisma.md

title: Prisma ORM headerTitle: Use an ORM linkTitle: Use an ORM description: Node.js Prisma ORM support for YugabyteDB headcontent: Node.js ORM support for YugabyteDB menu: v2.18: identifier: prisma-1 parent: nodejs-drivers weight: 600 type: docs <ul class="nav nav-tabs-alt nav-tabs-yb"> <li > <a href="../sequelize/" class="nav-link"> <i class="fa-brands fa-node-js" aria-hidden="true"></i> Sequelize </a> </li> <li> <a href="../prisma/" class="nav-link active"> <i class="fa-brands fa-node-js" aria-hidden="true"></i> Prisma </a> </li> </ul> is an open-source Object Relational Mapping (ORM) tool for Node.js or TypeScript applications. Prisma has various components including Prisma Client , which is used as a query builder to communicate with databases, Prisma Migrate, which acts as a migration tool, and Prisma Studio, which is a GUI-based tool to manage data in the database. The Prisma client can be a REST API, a GraphQL API, a gRPC API, or anything else that needs a database. Because YugabyteDB is PostgreSQL-compatible, Prisma supports the YugabyteDB YSQL API. This page provides details for getting started with Prisma for connecting to YugabyteDB using the PostgreSQL database connector. This section describes how to use data models (domain objects) to store and retrieve data from a YugabyteDB cluster. Prisma has a main file called `schema.prisma` in which the configurations and data models are defined. The data models are also called Prisma models which represent the entities of your application, and map to the tables in the database. Prisma models also form the basis of the queries available in the generated Prisma Client API. Learn how to establish a connection to YugabyteDB database and begin basic CRUD operations using the steps on the page. The following steps break down the example to demonstrate how to perform common tasks required for Node.js application development using Prisma. Before proceeding with the next steps, you need to have Node.js installed on your machine. Refer to . To create a basic Node.js project and install the `prisma` package, do the following: Create a new directory. ```sh mkdir nodejs-prisma-example && cd nodejs-prisma-example ``` Create a package.json file: ```sh echo {} > package.json ``` Install the Prisma package: ```sh npm install prisma ``` {{< note title="Note" >}} To use the Prisma CLI without `npx`, install Prisma globally using the following command: ```sh npm install -g prisma ``` {{< /note >}} Create an empty example.js file: ```sh touch example.js ``` Initialize Prisma in your project using the following command: ```sh npx prisma init ``` This will create files named `prisma/schema.prisma` and `.env`. `schema.prisma` consists of configurations and data models for the project. `.env` defines the environment variable `DATABASE_URL` which is used in configuring the database. Configure the `DATABASE_URL` environment variable in the `.env` file to connect with a YugabyteDB

```sh DATABASEURL="postgresql://<user>:<password>@<host>:<port>/<dbname>" ``` If you have a YugabyteDB Managed cluster, modify the `DATABASE_URL` using the following steps: Download your . Install OpenSSL, if not present. Convert the certificate from `.crt` to `.pem` format: ```sh openssl x509 -in <rootcrtpath> -out cert.pem ``` Modify the `DATABASEURL` by including the `certpath` as the relative path to `cert.pem` with respect to the `/prisma` folder: ```sh DATABASEURL="postgresql://<user>:<password>@<host>:<port>/<dbname>?sslmode=require&sslcert=<cert_path>" ``` Create a data model in the `prisma/schema.prisma` file and migrate it to YugabyteDB using Prisma Migrate. Create a data model called `Employee` in `prisma/schema.prisma` file by specifying the following in the file: ```js model Employee { emp_id Int @id emp_name String emp_age Int emp_email String @unique @@map("employee") } ``` Migrate this data model to YugabyteDB: ```sh $ npx prisma migrate dev --name first_migration ``` The `prisma migrate` command creates a `migration.sql` file with the folder name `<unique-id>firstmigration` under `prisma/migrations`, applies those to the database, and generates a Prisma client, which can be used as a query builder to communicate with the database. Use the Prisma client to create a few records in the `employee` table and fetch those records from the database. Add the following code in the `example.js` file to create three employee records in the employee table, and fetch those employee details in the order of their employee IDs: ```js const { PrismaClient } = require('@prisma/client') const prisma = new PrismaClient() async function example() { const employee1 = { emp_id: 1, emp_name: "Jake", emp_age: 24, emp_email: "jake24@example.com" } const employee2 = { emp_id: 2, emp_name: "Sam", emp_age: 30, emp_email: "sam30@example.com" } const employee3 = { emp_id: 3, emp_name: "Tom", emp_age: 22, emp_email: "tom22@example.com" } await prisma.employee .createMany({ data: [employee1, employee2, employee3,] }) .then(async (res) => { console.log("Created",res.count,"employees."); await prisma.employee .findMany({ orderBy: { emp_id: 'asc' }, }) .then((res) => { console.log("Fetched employee details sorted by employee IDs -"); console.log(res); }) .catch((err) => { console.log("Error in fetching employees: ",err); }) }) .catch((err)=>{ console.log("Error in creating employees: ",err); }) } example() .then (() => { console.log("Ran the Prisma example successfully.") }) .catch((e) => { throw e }) .finally(async () => { await prisma.$disconnect() }) ``` Run the example: ```sh $ node example.js ``` Expect output similar to the following: ```output Created 3 employees. Fetched employee details sorted by employee IDs - [ { emp_id: 1, emp_name: 'Jake', emp_age: 24, emp_email: 'jake24@example.com' }, { emp_id: 2, emp_name: 'Sam', emp_age: 30, emp_email: 'sam30@example.com' }, { emp_id: 3, emp_name: 'Tom', emp_age: 22, emp_email: 'tom22@example.com' } ] Ran the Prisma example successfully. ``` Run the following command to start Prisma Studio: ```sh npx prisma studio ``` Open in your browser to see the table and data created. Build Node.js applications using