Object Relational Mapping
Objectives
- Prisma ORM.
- Prisma schemas and data source providers.
- Prisma Migrate, Client, and Studio.
1 Task Tracker
Complete the 6 Relational Mapping guide to create a sample application that uses ORM.
Update the data repository service of 9.2 Task Tracker to use Prisma Client.
-
Install Prisma and initialize it with a SQLite provider:
bun add --dev prisma bunx prisma init --datasource-provider sqlite --generator-provider prisma-client-js --output client -
Define the data models using a Prisma schema:
prisma/schema.prismamodel Task { } -
Use Prisma Migrate to create the database based on the schema, then access it using Prisma Studio:
bunx prisma migrate dev --name init bunx prisma studio -
Create a data repository service with CRUD methods and use Prisma Client to access the data store:
repos/prisma.jsimport { PrismaLibSql } from "@prisma/adapter-libsql"; import { PrismaClient } from "@/prisma/client/client"; export default new PrismaClient({ adapter: new PrismaLibSql({ url: process.env.DATABASE_URL ?? "", }), });repos/tasks.jsimport prisma from "@/repos/prisma"; -
Create an API using Next.js and use the data repository service in all routes.
-
Test the routes using Postman (opens in a new tab).
2 Bank Accounts
Complete the 7 Server Actions guide to learn more about server actions.
Update the data repository service of 9.3 Bank Accounts to use Prisma Client.
-
Install Prisma and initialize it with a SQLite provider:
bun add --dev prisma bunx prisma init --datasource-provider sqlite --generator-provider prisma-client-js --output client -
Define the data models using a Prisma schema:
prisma/schema.prismamodel Account { } model Transaction { } -
Use Prisma Migrate to create the database based on the schema, then access it using Prisma Studio:
bunx prisma migrate dev --name init bunx prisma studio -
Create a data repository service with CRUD methods and use Prisma Client to access the data store:
repos/prisma.jsimport { PrismaLibSql } from "@prisma/adapter-libsql"; import { PrismaClient } from "@/prisma/client/client"; export default new PrismaClient({ adapter: new PrismaLibSql({ url: process.env.DATABASE_URL ?? "", }), });repos/accounts.jsimport prisma from "@/repos/prisma";repos/transactions.jsimport prisma from "@/repos/prisma"; -
Create an API using Next.js and use the data repository service in all routes.
-
Test the routes using Postman (opens in a new tab).
Structure
Resources
- Prisma ORM (opens in a new tab), Prisma Schema (opens in a new tab), Prisma Client (opens in a new tab), Prisma Client CRUD (opens in a new tab), Prisma Client relation queries (opens in a new tab)
- Prisma DBML Generator (opens in a new tab)
- Next.js Guides (opens in a new tab), How to create forms with Server Actions (opens in a new tab)