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

Support Prisma's multiSchema feature via workaround #56

Merged
merged 6 commits into from
Jul 2, 2023

Conversation

duniul
Copy link
Contributor

@duniul duniul commented Jun 28, 2023

Context

Prisma supports multi-schemas via the multiSchema preview feature (https://www.prisma.io/docs/guides/other/multi-schema), but the schemas are not appended to the table names generated by prisma-kysely.

Sadly, @prisma/generator-helper doesn't expose the model schemas set via @@schema("schemaName"), so a workaround is needed for now. I've opened an issue for Prisma to fix this here: prisma/prisma#19987

Changes

  • Add support for multiSchema and @@schema() attributes.
    • Uses @@mrleebo/prisma-ast to parse the datamodel separately and matches the schemas with the correct model. This is a workaround and is only needed until Prisma exposes the schema attribute on the DMMF.Model objects themselves.
    • Appends the schema name to the table names on the DB type (like what kysely-codegen does)
    • Does nothing if multiSchema is not enabled.
  • Sort DB properties by table name instead of type name.
    • Especially noticeable when using multi-schemas, but makes sense without it too.
  • Add changesets reflecting the changes.

Alternative solutions

  • If you want to avoid third-party libs like @@mrleebo/prisma-ast then we could do it via bracket matching instead, though it's a bit more complex and potentially error prone.
  • Wait for Prisma to expose the schema themselves, although that might take time.

Example output

Toggle example

prisma.schema (partial)

model Elephant {
  id   Int    @id
  name String

  @@map("elephants")
  @@schema("mammals")
}

model Eagle {
  id   Int    @id
  name String

  @@map("eagles")
  @@schema("birds")
}

Generated DB type before PR:

export type DB = {
  eagles: Eagle;
  elephants: Elephant;
};

Generated DB type after PR:

export type DB = {
  "birds.eagles": Eagle;
  "mammals.elephants": Elephant;
};

@duniul duniul requested a review from valtyr as a code owner June 28, 2023 09:36
@valtyr
Copy link
Owner

valtyr commented Jul 2, 2023

@duniul This is awesome. Thank you so much for adding this functionality! The code looks good. I'm going to perform some sanity checks, and if they check out (which I suspect they will) I'll add this to the next release. 🇸🇪➕🇮🇸🟰🎉

@valtyr
Copy link
Owner

valtyr commented Jul 2, 2023

/snapshot

@github-actions
Copy link
Contributor

github-actions bot commented Jul 2, 2023

📸 Snapshot

If you've added a changeset to your pull request, then a snapshot release with
your latest changes will have been created with its version number reflected
below:

Package name Version Git hash
prisma-kysely 0.0.0-snapshot-20230702140843

This can be useful to test your changes in other repos.

💡 Tip:

If you just see a normal version number here, that means that you haven't
created a changeset for your PR. You can do this by running the following
command:

yarn changeset

You will be prompted to choose what kind of changes you're making. In most
cases this will be "patch".

@valtyr valtyr merged commit 3ec4465 into valtyr:main Jul 2, 2023
4 checks passed
@duniul duniul deleted the multischema-support branch July 3, 2023 07:16
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

Successfully merging this pull request may close these issues.

None yet

2 participants