Database
Database setup with Prisma ORM and multiple provider options
Installation
Install the Package
Add Database to your project using the Tailar CLI:
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.
1# Database Configuration2DATABASE_URL="postgresql://user:password@localhost:5432/dbname"3 4# Database connection pool5DATABASE_POOL_SIZE=106DATABASE_POOL_TIMEOUT=207 8# Update DATABASE_URL with your actual database connection stringPreview
Explore representative UI flows and snippets generated for this module (same previews as on the home page modules section).
1const 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:
1├─ lib/2│ └─ db/3│ ├─ index.ts4│ ├─ utils.ts5│ └─ config.ts6├─ prisma/7│ ├─ schema.prisma8│ └─ seed.ts9└─ prisma.config.tsUsage
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:
1model Book {2 id String @id @default(cuid())3 title String4 author String5 published Boolean @default(false)6 createdAt DateTime @default(now())7 updatedAt DateTime @updatedAt8}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.
1# Format your Prisma schema2npx prisma format3 4# Generate Prisma Client5npx prisma generate6 7# Push schema changes to your database8npx prisma db pushQuerying Data
Use the Prisma client to query your database. Here's an example of querying books:
1import { db } from '@/lib/db'2 3// Get all books4const books = await db.book.findMany()5 6// Get a book by ID7const book = await db.book.findUnique({8 where: { id: 'book-id' }9})10 11// Create a new book12const newBook = await db.book.create({13 data: {14 title: 'The Great Gatsby',15 author: 'F. Scott Fitzgerald',16 published: true17 }18})19 20// Update a book21const updatedBook = await db.book.update({22 where: { id: 'book-id' },23 data: { published: true }24})25 26// Delete a book27await db.book.delete({28 where: { id: 'book-id' }29})References
For more information about Database, check out these resources: