prisma-repository-generator
v3.2.1
Published
A utility to generate repository classes based on Prisma models.
Downloads
75,470
Maintainers
Readme
🚀 Prisma Repository Generator
🌟 Description
Prisma Repository Generator is a powerful utility designed to automate the creation of repository files based on your Prisma models. It eliminates repetitive boilerplate code by generating common CRUD operations for database interactions, which improves consistency, scalability, and maintainability across your applications.
Instead of manually generating repetitive database logic code for each Prisma model, it generates reusable repository classes with standard methods. Developers can focus on application development instead of tedious CRUD generation.
📌 NOTE
This generator currently only works with TypeScript. Support for additional languages may be added in the future.
📥 Prerequisites
Before using the Prisma Repository Generator, make sure you have the necessary dependencies and tools ready:
1. Install Prisma Client
npm install @prisma/client
2. Install Prisma Repository Generator
npm install prisma-repository-generator
⚙️ Setup Instructions
Follow the steps below to set up Prisma and generate repositories:
1. Set Up Prisma Database Schema
Make sure you define the Prisma schema in prisma/schema.prisma
. Here is an example of a schema you can use:
model User {
id Int @id @default(autoincrement())
name String
email String @unique
}
model Post {
id Int @id @default(autoincrement())
title String
content String
userId Int
}
2. Run Migrations
After defining your schema, apply it to the database using the following command:
npx prisma migrate dev --name init
This will create the necessary database structure.
⚠️ IMPORTANT
Make sure you run migrations (npx prisma migrate dev --name init) before using the repository generator to avoid runtime issues.
3. Generate Repositories
Import and execute the repository generator with the following:
import { generateRepositories } from 'prisma-repository-generator';
generateRepositories().catch((err) => {
console.error("Error generating repositories:", err.message);
});
This will create repository files based on your Prisma models.
✂️ Shortcut
To simplify and speed up the repository generation process without needing to write extra code, use the following command directly from your terminal:
npx prisma-repository-generator
This command will automatically run the Prisma Repository Generator. You don’t need to import or write additional code in your project. Just run this command in your project directory after ensuring Prisma is set up correctly.
⚠️ IMPORTANT
Make sure you run migrations (npx prisma migrate dev --name init) before using this shortcut to avoid to avoid runtime issues.
✅Features
- [x]
findUnique
: Retrieve a single record by unique identifier. - [x]
findUniqueOrThrow
: Retrieve a single record or throw an error if it doesn't exist. - [x]
findFirst
: Fetch the first matching record with optional filtering and sorting. - [x]
findFirstOrThrow
: Same as findFirst, but throws an error if no record is found. - [x]
findMany
: Fetch multiple records with filtering, pagination, and cursor support. - [x]
create
: Insert a single record. - [x]
update
: Update a record by unique identifier. - [x]
upsert
: Insert or update a record based on a unique condition. - [x]
delete
: Delete a single record by unique identifier. - [x]
createMany
: Insert multiple records at once. - [x]
updateMany
: Update multiple records with specific conditions. - [x]
deleteMany
: Delete multiple records based on optional filtering. - [x]
count
: Count records with optional filtering. - [x]
aggregate
: Perform aggregation queries.