DEV Community

Cover image for Prisma Part 1: Your Easy Tutorial to Set up Prisma
Burak Boduroğlu
Burak Boduroğlu

Posted on

Prisma Part 1: Your Easy Tutorial to Set up Prisma

💡 Recommendation:

✨ If you interested in Go Programming Language you should follow my Go Tutorial Repository.
Go Tutorial


Introducing Prisma ORM – the amazing tool that makes managing your databases super easy. In this blog post, we'll explore what Prisma ORM is all about, how it can make your life simpler, and show you how to use it for your database needs.

What is Prisma ORM?

Prisma ORM is a modern database toolkit that simplifies database access for developers. It provides a type-safe and intuitive way to interact with databases, offering features like schema management, query building, and data modeling.

Why Choose Prisma ORM?

Forget about writing complicated SQL queries or struggling with complex ORM frameworks. Prisma ORM takes away the hassle of managing databases, so you can focus on building your application. With its simple language and powerful search features, Prisma makes working with databases easy.

Exploring Prisma's Features

  • Type-Safe Queries: Say goodbye to runtime errors caused by typos or incorrect query structures. Prisma's type-safe queries ensure that your database interactions are validated at compile time.
  • Automatic CRUD Operations: Using Prisma, handling CRUD (Create, Read, Update, Delete) operations is simple. Prisma automatically creates efficient SQL queries for you, so you can concentrate on your application's logic.
  • Relationships and Joins:Define relationships between your data entities and perform complex joins with ease. Prisma handles the underlying SQL complexities, making it simple to work with related data.

Example Project

1. Create TypeScript project and set up Prisma ORM

  • Create a folder
  • Create a TypeScript project
   npm init -y
   npm install typescript ts-node @types/node --save-dev
Enter fullscreen mode Exit fullscreen mode
  • Initialize TypeScript
   npx tsc --init
Enter fullscreen mode Exit fullscreen mode
  • Install Prisma CLI
   npm install prisma --save-dev
Enter fullscreen mode Exit fullscreen mode
  • Set up Prisma ORM
   npx prisma init --datasource-provider sqlite
Enter fullscreen mode Exit fullscreen mode

This command sets up a new Prisma project with SQLite as the
database provider, creating the necessary files and
directories to get you started.

2. Define Your Data Model

Now that your project is initialized, it's time to define your data model. Navigate to the prisma/schema.prisma file in your project directory. Here, you'll define your database schema using Prisma's intuitive schema language.

For example, let's say you're building a simple blog application. Your schema.prisma file might look like this:

// prisma/schema.prisma
datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

generator client {
  provider = "prisma-client-js"
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String
  published Boolean  @default(false)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've defined a Post model with attributes like id, title, content, published, createdAt, and updatedAt.

  1. Run Migrations With your data model defined, it's time to apply the changes to your SQLite database. Prisma makes this process seamless with its migration tool. un the following command in your terminal to generate a new migration:
npx prisma migrate dev --name init
Enter fullscreen mode Exit fullscreen mode

This command generates a new migration file based on your data model changes. Apply the migration to your SQLite database by running:

npx prisma migrate dev
Enter fullscreen mode Exit fullscreen mode

Title: Jumpstart Your Project with Prisma ORM and SQLite: A Beginner's Guide

So, you've decided to harness the power of Prisma ORM for your project, and you're starting with SQLite as your database provider. Don't worry; we've got you covered! In this tutorial, we'll walk you through setting up Prisma ORM with SQLite, giving you the foundation you need to kickstart your project.

Step 1: Initializing Your Prisma Project

Let's begin by initializing your Prisma project with SQLite as the datasource provider. Open your terminal and run the following command:

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

This command sets up a new Prisma project with SQLite as the database provider, creating the necessary files and directories to get you started.

Step 2: Define Your Data Model

Now that your project is initialized, it's time to define your data model. Navigate to the prisma/schema.prisma file in your project directory. Here, you'll define your database schema using Prisma's intuitive schema language.

For example, let's say you're building a simple blog application. Your schema.prisma file might look like this:

// prisma/schema.prisma

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

generator client {
  provider = "prisma-client-js"
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String
  published Boolean  @default(false)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've defined a Post model with attributes like id, title, content, published, createdAt, and updatedAt.

Step 3: Run Migrations

With your data model defined, it's time to apply the changes to your SQLite database. Prisma makes this process seamless with its migration tool.

Run the following command in your terminal to generate a new migration:

npx prisma migrate dev --name init
Enter fullscreen mode Exit fullscreen mode

This command generates a new migration file based on your data model changes. Apply the migration to your SQLite database by running:

npx prisma migrate dev
Enter fullscreen mode Exit fullscreen mode

And that's it! You've successfully set up Prisma ORM with SQLite as your database provider. Now you can start building your application logic and interacting with your database using Prisma's powerful features.

Feel free to explore Prisma's documentation for more advanced features and tips on optimizing your database interactions. Happy coding!

In part 2, we will discuss database operations with Prisma.

ℹ️ Source:

Top comments (0)