Generates full types (including relations) for TypeScript from a Prisma schema
$ npx @kalissaac/prisma-typegen <output path> [prisma schema file] [--onlyDeclarations] [--generateInsertionTypes]
$ npx @kalissaac/prisma-typegen ./interfaces ./schema.prisma
$ npx @kalissaac/prisma-typegen ./interfaces/prismaTypes.ts ./schema.prisma
If using JavaScript instead of TypeScript, pass --onlyDeclarations
to allow the types to be used with JSDoc.
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 toDate | string
insead of justDate
. This is because most database clients support inserting date fields using either the nativeDate
type or an ISO 8601 compliantstring
. - Fields marked with a
@default
value are made optional because they are populated automatically if not provided when inserting a new data row. - Relation fields (marked with
@relation
) are omitted because they do not exist at the database level and therefore can't be inserted. Read more about this here
By default, types are generated as an interface
. If your use case requires using type
instead, use the --useType
flag.
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
}
export enum Role {
USER = 'USER',
ADMIN = 'ADMIN',
}
export interface User {
id: number
createdAt: Date
email: string
name?: string
role: Role
posts: Post[]
}
export interface Post {
id: number,
createdAt: Date,
updatedAt: Date,
published: boolean,
title: string,
author?: User,
authorId?: number,
}
export enum Role {
USER = 'USER',
ADMIN = 'ADMIN',
}
export interface User {
id?: number,
createdAt?: (Date | string),
email: string,
name?: string | null,
role?: Role,
}
export interface Post {
id?: number,
createdAt?: (Date | string),
updatedAt: (Date | string),
published?: boolean,
title: string,
authorId?: number | null,
}
export enum Role {
USER = 'USER',
ADMIN = 'ADMIN',
}
export type User = {
id: number,
createdAt: Date,
email: string,
name?: string,
role: Role,
posts: Post[],
}
export type Post = {
id: number,
createdAt: Date,
updatedAt: Date,
published: boolean,
title: string,
author?: User,
authorId?: number,
}