Schmella
Schmella

Reputation: 91

Share prisma types and enums across multiple projects

Currently I am developing an api that provides data using prisma. This api gets used by multiple projects. In the api I generate types using Prisma.ModelGetPayload to define the return types of certain api responses.

import { Prisma } from "@prisma/client";

export const minimalSelect = Prisma.validator<Prisma.ModelXYSelect>()({
    id: true,
    name: true
});

export type MinimalModelXY = Prisma.ModelXYGetPayload<{
    select: typeof minimalSelect;
}>;

Or enums can simply be imported from the prisma client.

import { EnumXY } from "@prisma/client";

These enums and type definitions rely on the generated Prisma client of the api.

There are solutions where the whole prisma client gets extracted into an npm package (see here) however, this is not what i'd like to do.

Is it possible to extract these types into an npm package to share them between multiple projects without extracting whole prisma client?

Upvotes: 6

Views: 3388

Answers (2)

Joe Roddy
Joe Roddy

Reputation: 1098

For anyone who has this use case in the future, I had the same issue, so I made a generator to export your Prisma client's types to a specified directory and remove the compiled JS files.

  1. npm install -D prisma-types-export
  2. Add the following to your schema.prisma file:
    generator types {
        provider = "prisma-types-export"
        output   = "./prisma-types"
    }
    
  3. npx prisma generate

Upvotes: 0

Nurul Sundarani
Nurul Sundarani

Reputation: 7578

If you want to export all the types that are generated by Prisma, you can find all of them at node_modules/.prisma/client/index.d.ts

You can just export this file in an npm package and use it in the frontend application.

Another approach which you could use is to publish your schema.prisma to an npm package. Then in your other project you could generate a prisma client library from the schema.prisma file, this way you wouldn't need to export PrismaClient.

Upvotes: 4

Related Questions