DEV Community

Awais
Awais

Posted on • Edited on • Originally published at awaisdev.dev

Quick introduction to the Prisma ORM

Introduction

This post explores Prisma, an ORM, which stands for Object Relational Mapper. If you're not familiar with ORMs the idea is you can communicate with your database using JavaScript objects and function calls rather than having to write raw SQL queries. This has the benefit of being much simpler to write and improve your productivity.

Getting Started with Prisma

To get started install the Prisma CLI as a dev dependency in your TypeScript project:

npm install prisma --save-dev
Enter fullscreen mode Exit fullscreen mode

Set up the Prisma ORM with the init command of the Prisma CLI:

npx prisma init --datasource-provider sqlite
Enter fullscreen mode Exit fullscreen mode

This will create a .env file and a prisma directory with a schema.prisma file.

The .env file will contain the environment variable for the DATABASE_URL with the path to the SQLite database:

DATABASE_URL="file:./dev.db"
Enter fullscreen mode Exit fullscreen mode

One-to-Many relationship

The schema.prisma file will contain the Prisma schema where you can define your data model.

Here's an example of this showing a one-to-many relationship between users and posts:

model User {
  id    String  @id @default(uuid())
  email String  @unique
  name  String?
  posts Post[]
}

model Post {
  id        String  @id @default(uuid())
  title     String
  content   String?
  published Boolean @default(false)
  author    User    @relation(fields: [authorId], references: [id])
  authorId  String
}
Enter fullscreen mode Exit fullscreen mode

The models map to tables in the SQLite database. Inside the model are the column names with their data types and field attributes. For example, you can ensure fields are unique by setting the @unique attribute and default values using @default.

The @relation attribute is used to show the connection between two models. In this example, authorId is a foreign key column in the Post table and references the primary key id in the User table. You can explore this more in Prisma Studio later.

To push the changes in the Prisma schema file to the database run the npx prisma db push command. This is useful for quick iterations. When you're ready to commit to the changes you can create a migration using the npx prisma migrate dev --name init command where init is the name of the migration. This way you can keep track of schema changes over time.

Prisma Studio

Run the npx prisma studio command to explore your database in a special web app. This will allow you view/add/edit the data in your database.

Querying Data

Here's an example of using select to select specific fields.

const postTitles = await prisma.post.findMany({
        select: {
            title: true
        }
});
Enter fullscreen mode Exit fullscreen mode

You can also perform other CRUD operations such as creating, reading, updating and deleting data in a similar way.

Raw Queries

💡 There may be times where what you want to do is not supported in which case you can drop down to SQL with the raw query API.

Conclusion

This post explored getting started with Prisma and some of its features. Share your experiences working with Prisma or other ORMs in the comments.

Top comments (0)