Object Relational Mapping
Objective
- 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:
npm install --save-dev prisma npx prisma init --datasource-provider sqlite --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:
npx prisma migrate dev --name init npx prisma studio
-
Create a data repository service with CRUD methods and use Prisma Client to access the data store:
repos/prisma.jsimport { PrismaClient } from "@/prisma/client"; const prisma = new PrismaClient(); export default prisma;
repos/tasks.jsimport prisma from "@/repos/prisma";
-
Create a 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:
npm install --save-dev prisma npx prisma init --datasource-provider sqlite --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:
npx prisma migrate dev --name init npx prisma studio
-
Create a data repository service with CRUD methods and use Prisma Client to access the data store:
repos/prisma.jsimport { PrismaClient } from "@/prisma/client"; const prisma = new PrismaClient(); export default prisma;
repos/accounts.jsimport prisma from "@/repos/prisma";
repos/transactions.jsimport prisma from "@/repos/prisma";
-
Create a API using Next.js and use the data repository service in all routes.
-
Test the routes using Postman (opens in a new tab).