Prisma Part 1: Your Easy Tutorial to Set up Prisma
Tags: prisma, database, node, typescript
Published: 2024-03-29 06:38:42

## 💡 Recommendation:
✨ If you interested in Go Programming Language you should follow my Go Tutorial Repository.
[Go Tutorial](https://github.com/burakboduroglu/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 <ProjectFolder>
   * Create a TypeScript project
   ```
   npm init -y
   npm install typescript ts-node @types/node --save-dev
   ```
   * Initialize TypeScript
   ```
   npx tsc --init
   ``` 
   * Install Prisma CLI
   ```
   npm install prisma --save-dev
   ```
   * Set up Prisma ORM
   ```
   npx prisma init --datasource-provider sqlite
   ```
  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
}
```
In this example, we've defined a Post model with attributes like id, title, content, published, createdAt, and updatedAt.
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.
un the following command in your terminal to generate a new migration:
```
npx prisma migrate dev --name init
```

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
```
**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:

```bash
npx prisma init --datasource-provider sqlite
```

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
// 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
}
```

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:

```bash
npx prisma migrate dev --name init
```

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

```bash
npx prisma migrate dev
```

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:
* 🔗 https://www.prisma.io/docs/getting-started/quickstart
* 🔗 https://github.com/burakboduroglu