npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kalissaac/prisma-typegen

v0.0.7

Published

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

Downloads

1,666

Readme

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