Giter VIP home page Giter VIP logo

prisma-typegen's Introduction

@kalissaac/prisma-typegen

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

Usage

$ 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

Only output declarations

If using JavaScript instead of TypeScript, pass --onlyDeclarations to allow the types to be used with JSDoc.

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.
  • 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

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

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 index.ts (or index.d.ts)

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,
}

Generated index.ts (or index.d.ts) in insertion mode (with --generateInsertionTypes)

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,
}

Generated index.ts (or index.d.ts) with --useType option

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,
}

prisma-typegen's People

Contributors

kalissaac avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.