Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Create a package with no dependencies to @prisma/client #279

Open
vorillaz opened this issue Oct 9, 2024 · 2 comments

Comments

@vorillaz
Copy link

vorillaz commented Oct 9, 2024

Is your feature request related to a problem? Please describe.
Hi there, thanks for this amazing generator. We are trying to export the schema validation inside a monorepo. The reason is kind of reusing the schema as validation for forms in the frontend and the backend. When the schema types are generated in a different package inside the monorepo though the @prisma/client is not present. We are trying to use the package as follow:

// ./packages/db/prisma/schema.prisma
generator zod {
  provider                         = "zod-prisma-types"
  output                           = "../../packages/zod/prisma"
  useMultipleFiles                 = true
  writeBarrelFiles                 = true
  createInputTypes                 = false
  addIncludeType                   = false
  addSelectType                    = false
  writeNullishInModelTypes         = false
  createOptionalDefaultValuesTypes = false
  addInputTypeValidation           = false
}

The generated output inside the packages/zod is not aware of the @prisma/client required from the generated output since it's a different package reused by both a client and backend application.

Describe the solution you'd like
Maybe provide our own types somehow through a config file like:

generator zod {
  provider                         = "zod-prisma-types"
  output                           = "./zod"
  // ..
  nullishTypes = "./zod-nullish"
}

Or somehow skip nullish checking through configuration.

Describe alternatives you've considered
A quick and dirty patch is to trick typescript to import @prisma/client as my own types through aliasing.

Once again thanks ton for your amazing work. Keep up.

@KuryKat
Copy link

KuryKat commented Nov 21, 2024

This isn't an issue with this package, but rather with how you are using it in your monorepo project, I will assume you're using TurboRepo.

You need zod-prisma-types inside your database package (let's assume that's your package name)
You need zod inside your schemas package (let's assume that's your package name)

Let's say you generate it with output = "../../schemas/src/prisma" and you're working inside ./packages/database/prisma/schema.prisma

in your database package.json you need to have

{
  "name": "@repo/database",
  // etc other stuff, like version, scripts, etc.
  "exports": {
    ".": "./src/index.ts"
  },
  "dependencies": {
    "@prisma/client": "^5.22.0"
  },
  "devDependencies": {
    "prisma": "^5.22.0",
    "zod-prisma-types": "^3.1.8"
  }
}

and then in the ./src/index.ts file you need to have

export * from '@prisma/client'

If this is what you currently have, then everything is alright so far.

In your schemas package you need to have your database package as a dependency

Like so:

  "name": "@repo/schemas",
  // etc other stuff, like version, scripts, etc.
  "exports": {
    ".": "./src/index.ts"
  },
  "dependencies": {
    "@repo/database": "*",
    "zod": "^3.23.8"
  }

Then you will be able to import the @prisma/client inside your schemas package like so:

import { User } from "@repo/database"

This is the same as doing import { User } from "@prisma/client" inside the database package

Doing this way you can safely just have this inside the ./src/index.ts

export * from './prisma'

then in your apps you'd just need to use the @repo/schemas as a dependency and import the zod schemas :D

@FloChehab
Copy link

Hello, if someone else comes by and needs a quick and dirty way around (I am not in the case above unfortunately).

Update your generators:

generator zod {
  provider                 = "zod-prisma-types"
  output                   = "<PATH>"
  ...
}

generator fixZod {
  provider = "prisma/schema/fixGeneratedZod.js"
  output   = "not_important"
}

Create a fixGeneratedZod.js file.

#!/usr/bin/env node
import pkg from '@prisma/generator-helper';
import { execSync } from 'node:child_process';
import fs from 'node:fs';

const { generatorHandler } = pkg;

const FILE_PATH = '<PATH>/index.ts';
generatorHandler({
  onManifest: () => {
    return {};
  },
  onGenerate: async (config) => {
    const data = fs.readFileSync(FILE_PATH, 'utf8');

    // Replace content using a regex
    const updatedData = data
      .replace("import { Prisma } from '@prisma/client';", '')
      .replace(/\/\/ JSON([\S\s\/-]*)\/\/ ENUMS/m, '// Deleted by helper')
      .replace(/JsonValueSchema.nullable\(\)/gm, 'z.any()')
      .replace(
        /(.*(NullableJsonNullValueInputSchema|JsonNullValueFilterSchema).*)/gm,
        ''
      );

    // Write the updated content back to the file
    fs.writeFileSync(FILE_PATH, updatedData, 'utf8');

    execSync(
      'npx prettier --write src/abrico-lib-shared/generated/prisma-zod/index.ts'
    );
  },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants