2.2.1 • Published 10 months ago

prisma-generator-types-crud v2.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

prisma-generator-types-crud

Generates full types (including relations) for TypeScript from a Prisma schema

Install

npm install -D prisma-generator-types-crud@latest
  or
yarn add -D prisma-generator-types-crud@latest

Usage

npx prisma-generator-types-crud <output path> <schema.prisma path> [--useType] [--prettier]

Example Usage Generate

npx prisma-generator-types-crud ./prisma/generated/ ./prisma/schema.prisma --useType --prettier

Generate types for data to be inserted

If using this package to generate types that will be assigned to data to be inserted into a database, use the --generateInsertionTypes flag. Using this option will result in a few differences:

  • Prisma DateTime fields are mapped to Date | string insead of just Date. This is because most database clients support inserting date fields using either the native Date type or an ISO 8601 compliant string.
  • Fields marked with a @default value are made optional because they are populated automatically if not provided when inserting a new data row.

Use type instead of interface

By default, types are generated as an interface. If your use case requires using type instead, use the --useType flag.

Example Structure Folder Generate

[output folder]/
      /enum
          /[name enum]
              /index.ts
      /types
          /[model prisma name]
              /createType
              /deleteType
              /entityType
              /updateType

Example Schema Prisma

Input Schema

datasource db {
  url      = env("DATABASE_URL")
  provider = "postgresql"
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  email     String   @unique
  name      String?
  role      Role     @default(USER)
  posts     Post[]
}

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

enum Role {
  USER
  ADMIN
}

Generated enum/Role/index.ts

export enum Role {
  USER = "USER",
  ADMIN = "ADMIN",
}

Generated types/User/createType.ts

// AUTO GENERATED FILE BY prisma-generator-types-crud
// DO NOT EDIT

import { Role } from "../../enum/Role";

export type UserCreateType = {
  createdAt?: Date | string;
  email: string;
  name?: string | null;
  role?: Role;
};

Generated types/User/deleteType.ts

// AUTO GENERATED FILE BY prisma-generator-types-crud
// DO NOT EDIT

export type UserDeleteType = {
  id: number;
};

Generated types/User/entityType.ts

// AUTO GENERATED FILE BY prisma-generator-types-crud
// DO NOT EDIT

import { PostEntityType } from "../Post/entityType";
import { Role } from "../../enum/Role";

export type UserEntityType = {
  id: number;
  createdAt: Date;
  email: string;
  name?: string;
  role: Role;
  posts: PostEntityType[];
};

Generated types/User/updateType.ts

// AUTO GENERATED FILE BY prisma-generator-types-crud
// DO NOT EDIT

import { Role } from "../../enum/Role";

export type UserUpdateType = {
  id: number;
  createdAt: Date;
  email: string;
  name?: string;
  role: Role;
};
2.2.1

10 months ago

2.1.4

12 months ago

2.1.3

12 months ago

2.1.2

12 months ago

2.1.1

12 months ago

2.1.0

1 year ago

2.0.10

1 year ago

2.0.9

1 year ago

2.0.8

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.3

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.4.6

1 year ago

1.4.5

1 year ago

1.4.4

1 year ago

1.4.3

1 year ago

1.4.2

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.5

1 year ago

1.3.4

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.9

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.10

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago