Skip to content

omar-dulaimi/prisma-custom-models-generator

Repository files navigation

npm version npm HitCount npm

Prisma Custom Models Generator

A Prisma generator that automates creating custom models from your Prisma schema.
Explore the options »

Report Bug · Request Feature

Buy Me A Coffee

Table of Contents

About The Project

Automatically generate custom models from your Prisma Schema. This includes all currently recommended ways as mentioned on Prisma Docs. Updates every time npx prisma generate runs.

Installation

Using npm:

 npm install prisma-custom-models-generator

Using yarn:

 yarn add prisma-custom-models-generator

Usage

1- Star this repo 😉

2- Add the generator to your Prisma schema

generator custom_models {
  provider       = "prisma-custom-models-generator"
  behavior       = "WRAP"
}

3- Running npx prisma generate for the following schema.prisma

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
  posts Post[]
}

model Post {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  title     String
  content   String?
  published Boolean  @default(false)
  viewCount Int      @default(0)
  author    User?    @relation(fields: [authorId], references: [id])
  authorId  Int?
}

will generate this for the User model(default behavior is WRAP)

import { PrismaClient } from '@prisma/client';

export function Users(prismaUser: PrismaClient['user']) {
  return Object.assign(prismaUser, {
    // define methods here, comma-separated
  });
}

or this

import { PrismaClient } from '@prisma/client';

export class Users {
  constructor(private readonly prismaUser: PrismaClient['user']) {}
}

Additional Options

Option  Description Type  Default
output Output directory for the generated routers and zod schemas string ./generated
behavior Sets the preferred grouping logic WRAP or EXTEND WRAP

Use additional options in the schema.prisma

generator custom_models {
  provider       = "prisma-custom-models-generator"
  behavior       = "EXTEND"
}

Community

Stargazers repo roster for @omar-dulaimi/prisma-custom-models-generator