Database

Database setup with Prisma ORM and multiple provider options

Installation

1

Install the Package

Add Database to your project using the Tailar CLI:

pnpm dlx tailar@latest add db
2

Set Environment Variables

Create a `.env` file in the root of your project and add the following environment variables. For detailed setup instructions, see how to create a database.

.env
1
# Database Configuration
2
DATABASE_URL="postgresql://user:password@localhost:5432/dbname"
3
 
4
# Database connection pool
5
DATABASE_POOL_SIZE=10
6
DATABASE_POOL_TIMEOUT=20
7
 
8
# Update DATABASE_URL with your actual database connection string

Preview

Explore representative UI flows and snippets generated for this module (same previews as on the home page modules section).

lib/auth/actions/user.ts
1
const user = await db.user.findUnique({
2
where: {
3
email: email.toLowerCase(),
4
},
5
select: {
6
id: true,
7
email: true,
8
},
9
});

File Tree

The following files are installed when you add Database:

File Structure
1
├─ lib/
2
│ └─ db/
3
│ ├─ index.ts
4
│ ├─ utils.ts
5
│ └─ config.ts
6
├─ prisma/
7
│ ├─ schema.prisma
8
│ └─ seed.ts
9
└─ prisma.config.ts

Usage

Once you've installed Database, you can start using the following actions:

Adding a Model

Add a new model to your Prisma schema. Here's an example of adding a Book model:

prisma/schema.prisma
1
model Book {
2
id String @id @default(cuid())
3
title String
4
author String
5
published Boolean @default(false)
6
createdAt DateTime @default(now())
7
updatedAt DateTime @updatedAt
8
}

Format, Generate, and Push Changes

After adding or modifying models in your schema, you need to format, generate the Prisma client, and push changes to your database. For more information, check out the Prisma documentation.

Terminal
1
# Format your Prisma schema
2
npx prisma format
3
 
4
# Generate Prisma Client
5
npx prisma generate
6
 
7
# Push schema changes to your database
8
npx prisma db push

Querying Data

Use the Prisma client to query your database. Here's an example of querying books:

lib/example.ts
1
import { db } from '@/lib/db'
2
 
3
// Get all books
4
const books = await db.book.findMany()
5
 
6
// Get a book by ID
7
const book = await db.book.findUnique({
8
where: { id: 'book-id' }
9
})
10
 
11
// Create a new book
12
const newBook = await db.book.create({
13
data: {
14
title: 'The Great Gatsby',
15
author: 'F. Scott Fitzgerald',
16
published: true
17
}
18
})
19
 
20
// Update a book
21
const updatedBook = await db.book.update({
22
where: { id: 'book-id' },
23
data: { published: true }
24
})
25
 
26
// Delete a book
27
await db.book.delete({
28
where: { id: 'book-id' }
29
})

References

For more information about Database, check out these resources:

Create Database Guide
Step-by-step instructions for setting up PostgreSQL, MySQL, or MongoDB databases for your project.
Prisma Documentation
Complete documentation for Prisma ORM, including schema definition, queries, and migrations.