{"id":14978688,"url":"https://github.com/kimjbstar/prisma-class-generator","last_synced_at":"2025-04-13T11:47:51.825Z","repository":{"id":41370615,"uuid":"397832060","full_name":"kimjbstar/prisma-class-generator","owner":"kimjbstar","description":"Class generator from Prisma schema.","archived":false,"fork":false,"pushed_at":"2024-08-12T12:04:42.000Z","size":203,"stargazers_count":187,"open_issues_count":24,"forks_count":56,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-04T05:08:53.901Z","etag":null,"topics":["generator","nestjs","prisma","swagger","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kimjbstar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-19T05:52:23.000Z","updated_at":"2025-03-26T00:30:09.000Z","dependencies_parsed_at":"2024-02-06T06:22:51.579Z","dependency_job_id":"ab98a980-1842-49bb-9162-b7b765c3daf9","html_url":"https://github.com/kimjbstar/prisma-class-generator","commit_stats":{"total_commits":113,"total_committers":13,"mean_commits":8.692307692307692,"dds":"0.16814159292035402","last_synced_commit":"ed55c464423e0f3fabc594d1b652aa45cf11e962"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimjbstar%2Fprisma-class-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimjbstar%2Fprisma-class-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimjbstar%2Fprisma-class-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimjbstar%2Fprisma-class-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kimjbstar","download_url":"https://codeload.github.com/kimjbstar/prisma-class-generator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248710409,"owners_count":21149186,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["generator","nestjs","prisma","swagger","typescript"],"created_at":"2024-09-24T13:58:11.917Z","updated_at":"2025-04-13T11:47:51.803Z","avatar_url":"https://github.com/kimjbstar.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Prisma Class Generator\n\n## **Prisma**\n\n\u003e [Prisma](https://www.prisma.io/) is Database ORM Library for Node.js, Typescript.\n\nPrisma basically generate each models type definition defined in [`schema.prisma`](https://www.prisma.io/docs/concepts/components/prisma-schema).\nTherefore, it does not require additional entry classes or repository layers.\n\nThis is Prisma's basic way of doing things, and I love this approach.\n\nHowever, there are limitations because of these characteristics.\nA typical example is NestJS. In order to use `@nestjs/swagger`, the entity must be defined as class.\n\nSo I created a simple tool that generates a typescript file based on `schema.prisma`. The generated Classes are formatted with prettier, using the user's prettier config file if present.\nThis will reduce the effort to define classes directly while using the same single source of truth (`schema.prisma`)\n\nThe Prisma JS Client returns objects that does not contain the model's relational fields. The Generator can create two separate files per model, one that matches the Prisma Js Client's interfaces, and one that contains only the relational fields. You can set the _separateRelationFields_ option to **true** if you want to generate two separate classes for each model. The default value is **false**.\n\n## **NestJS**\n\n\u003e [NestJS](https://nestjs.com/) is a framework for building efficient, scalable Node.js server-side applications.\n\nand also, this is one of the frameworks with class and decorator as the basic structure.\n\nLet's think about it like this: If the NestJS model is defined as class as below, it will be easier to apply [swagger](https://docs.nestjs.com/openapi/introduction), [TypeGraphQL](https://typegraphql.com/), etc. through decorator.\n\nAnd if the schema changes, redefine and create accordingly so that the schema and class syncs.\n\nUsing this library, it becomes possible.\n\n```typescript\nexport class Company {\n\t@ApiProperty({ type: Number }) // swagger\n\t@Field((type) =\u003e Int) // TypeGraphQL\n\tid: number\n\n\t@ApiProperty({ type: String }) // swagger\n\tname: string\n\n\t@ApiProperty({ type: Boolean }) // swagger\n\tisUse: boolean\n}\n```\n\nIf you set the _separateRelationFields_ option to **true** and generate separate relational classes, you can compose a class from the two, only contanining the included relations.\nThis example below is using methods from the `@nestjs/swagger` package. This example creates a class with all of the properties of the Product class and the category relational property from the generated relational class.\n\n```typescript\nimport { IntersectionType, PickType } from '@nestjs/swagger'\nimport { Product } from './product'\nimport { ProductRelations } from './product_relations'\n\nexport class ProductDto extends IntersectionType(\n\tProduct,\n\tPickType(ProductRelations, ['category'] as const),\n) {}\n```\n\n### **Usage**\n\n1. **Install**\n\n    ```shell\n    npm install prisma-class-generator\n    yarn add prisma-class-generator\n    ```\n\n2. **Define Generator in `schema.prisma`**\n\n    ```prisma\n    generator prismaClassGenerator {\n        provider = \"prisma-class-generator\"\n    }\n    ```\n\n3. **😎 done! Let's check out generated files.**\n\n    if this models were defined in your prisma.schema file,\n\n    ```prisma\n    model Product {\n      id            Int         @id\n      title         String      @db.VarChar(255)\n      desc          String      @db.VarChar(1024)\n      images        Json        @db.Json\n      isShown       Boolean?    @default(false)\n      stock         Int?        @default(0)\n      type          ProductType\n      averageRating Float?\n      categoryId    Int\n      companyId     Int\n      category      Category    @relation(fields: [categoryId], references: [id])\n      company       Company     @relation(fields: [companyId], references: [id])\n      createdAt     DateTime    @default(now()) @db.Timestamp(6)\n      updatedAt     DateTime    @updatedAt @db.Timestamp(6)\n    }\n\n    model Category {\n      id       Int       @id\n      products Product[]\n    }\n\n    model Company {\n      id          Int       @id\n      name        String\n      totalIncome BigInt\n      lat         Decimal\n      lng         Decimal\n      by          Bytes\n      products    Product[]\n    }\n\n    ```\n\n    then this class is generated in \u003cPROJECT_PATH\u003e/src/\\_gen/prisma-class.\n\n    ( The generating path can be customized through _output_ option. )\n\n    ```typescript\n    // category.ts\n    import { Product } from './product'\n    import { ApiProperty } from '@nestjs/swagger'\n\n    export class Category {\n    \t@ApiProperty({ type: Number })\n    \tid: number\n\n    \t@ApiProperty({ isArray: true, type: () =\u003e Product })\n    \tproducts: Product\n    }\n    ```\n\n    ```typescript\n    // company.ts\n    import { Product } from './product'\n    import { ApiProperty } from '@nestjs/swagger'\n\n    export class Company {\n    \t@ApiProperty({ type: Number })\n    \tid: number\n\n    \t@ApiProperty({ type: String })\n    \tname: string\n\n    \t@ApiProperty({ type: Bigint })\n    \ttotalIncome: bigint\n\n    \t@ApiProperty({ type: Number })\n    \tlat: number\n\n    \t@ApiProperty({ type: Number })\n    \tlng: number\n\n    \t@ApiProperty({ type: Buffer })\n    \tby: Buffer\n\n    \t@ApiProperty({ isArray: true, type: () =\u003e Product })\n    \tproducts: Product\n    }\n    ```\n\n    ```typescript\n    // product.ts\n    import { Category } from './category'\n    import { Company } from './company'\n    import { ProductType } from '@prisma/client'\n    import { ApiProperty } from '@nestjs/swagger'\n\n    export class Product {\n    \t@ApiProperty({ type: Number })\n    \tid: number\n\n    \t@ApiProperty({ type: String })\n    \ttitle: string\n\n    \t@ApiProperty({ type: String })\n    \tdesc: string\n\n    \t@ApiProperty()\n    \timages: any\n\n    \t@ApiProperty({ type: Boolean })\n    \tisShown: boolean\n\n    \t@ApiProperty({ type: Number })\n    \tstock: number\n\n    \t@ApiProperty({ enum: ProductType, enumName: 'ProductType' })\n    \ttype: ProductType\n\n    \t@ApiProperty({ type: Number })\n    \taverageRating: number\n\n    \t@ApiProperty({ type: Number })\n    \tcategoryId: number\n\n    \t@ApiProperty({ type: Number })\n    \tcompanyId: number\n\n    \t@ApiProperty({ type: () =\u003e Category })\n    \tcategory: Category\n\n    \t@ApiProperty({ type: () =\u003e Company })\n    \tcompany: Company\n\n    \t@ApiProperty({ type: Date })\n    \tcreatedAt: Date\n\n    \t@ApiProperty({ type: Date })\n    \tupdatedAt: Date\n    }\n    ```\n\n    ```typescript\n    // index.ts\n    import { Product as _Product } from './product'\n    import { Category as _Category } from './category'\n    import { Company as _Company } from './company'\n\n    export namespace PrismaModel {\n    \texport class Product extends _Product {}\n    \texport class Category extends _Category {}\n    \texport class Company extends _Company {}\n\n    \texport const extraModels = [Product, Category, Company]\n    }\n    ```\n\n    The reason why classes were grouped into the 'PrismaModel' namespace and distributed in the index.ts file.\n\n    1. First, generated classes can overlap with the model types generated by Prisma, causing confusion.\n    2. when using Swagger in Nest.JS, you can use this array data in Bootstrap code to scan classes into @nestjs/swagger without having to list them.\n\n    There is a reference code for this below.\n\n    ```typescript\n    // main.ts in Nest.JS application\n    import { PrismaModel } from './_gen/prisma-class'\n\n    const document = SwaggerModule.createDocument(app, options, {\n    \textraModels: [...PrismaModel.extraModels],\n    })\n    ```\n\n    You can also disable it through the _makeIndexFile_ option.\n\n#### **Supported options**\n\n-   _dryRun_\n    -   Decide whether to write file or just print result. default value is **true**\n        -   if you finished check result via terminal, then you should this options to **false**\n-   _output_\n    -   sets output path. default is **'../src/\\_gen/prisma-class'**\n-   _useSwagger_\n    -   generates swggger decorator. default value is **true**\n-   _makeIndexFile_\n    -   makes index file, default value is **true**\n-   _separateRelationFields_\n    -   puts relational fields into different file for each model. This way the class will match the object returned by a Prisma query, default value is **false**\n-   _clientImportPath_\n    -   set prisma client import path manually, default value is **@prisma/client**\n-   _useNonNullableAssertions_\n    -   Apply a ! after non-optional class fields to avoid strict mode warnings (Property has no initializer and is not definitely assigned in the constructor.)\n-   _preserveDefaultNullable_\n    -   Determines how null fields are handled. When set to **false** (default), it turns all null fields to undefined. Otherwise, it follows Prisma generation and adds null to the type.\n\n### **How it works?**\n\nPrima internally defines metadata as a dmmf object.\n\n[prisma-class-generator](https://github.com/kimjbstar/prisma-class-generator) can automate class definition using this dmmf.\n\nIt is defined as an additional generator in the `schema.prisma` file and will operate in the `prisma generate` process.\n\n### **Feature**\n\n-   generate Classes from prisma model definition\n-   Support Basic Type and Relation\n-   Support option to generate Swagger Decorator\n-   Format generated Classes with prettier, using the user's prettier config file if present\n\n### **Future Plan**\n\n-   Considers all class-based things that are not limited to Nest.JS\n-   Support all types in prisma.schema\n-   Support TypeGraphQL\n-   Support DTO\n-   Support custom path, case or name per each model\n\n---\n\n### **FAQ**\n\n**1. Is it CRUD Generator?**\n\nNo. It will not provide functions such as \"nestjs CRUD generate\". I think it is outside the scope of this library.\nInstead, it will only serve as a bridge connecting the Prisma model and (entity)class.\n\nIt will focus on **defining classes** and then give the developer responsibility on how to use them.\n\nThis is because if too much is provided, the library becomes less adaptable accordingly.\n\n**2. Is only works with NestJS?**\n\nNo, but of course, it goes well with NestJS. I'm also planning to support the library related to NestJS.\n\nBut even if you don't use NestJS, this library will be useful for you if you use class decorator based on reflect-metadata to develop web services.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkimjbstar%2Fprisma-class-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkimjbstar%2Fprisma-class-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkimjbstar%2Fprisma-class-generator/lists"}