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

feat: add groupBySchema to group types and enums under a namespace #110

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

arthurfiorette
Copy link

@arthurfiorette arthurfiorette commented Oct 11, 2024

I didn't need to fix any test after adding this feature, this means that everything kept running exactly as before when groupBySchema is not true.

We are using this plugin in a giant schema (100+ tables under 18 schemas) where some of them have the same or similar names, which is very hard to spot visually which schema an imported type comes from.

This plugin simply adds all enums and models under a ts.ModuleDeclaration respective to their schema and fixes types references to it. It makes it much easier to work with schemas, the same visually/architecturally as thinking about why schemas are written like schema.model inside SQL.


EDIT: I also added filterBySchema, which just picks all models from defined schemas.


generator kysely {
    provider        = "node ./dist/bin.js"
    previewFeatures = ["multiSchema"]
    groupBySchema   = true
}

datasource db {
    provider = "postgresql"
    schemas  = ["mammals", "birds", "world"]
    url      = env("TEST_DATABASE_URL")
}

model Elephant {
    id      Int     @id
    name    String
    ability Ability @default(WALK)
    color   Color

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

model Eagle {
    id      Int     @id
    name    String
    ability Ability @default(FLY)

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

enum Ability {
    FLY
    WALK

    @@schema("world")
}

enum Color {
    GRAY
    PINK

    @@schema("mammals")
}

Now generates:

export namespace World {
  export const Ability = {
    FLY: "FLY",
    WALK: "WALK",
  } as const;
  export type Ability = (typeof Ability)[keyof typeof Ability];
}
export namespace Mammals {
  export const Color = {
    GRAY: "GRAY",
    PINK: "PINK",
  } as const;
  export type Color = (typeof Color)[keyof typeof Color];
  export type Elephant = {
    id: number;
    name: string;
    ability: Generated<World.Ability>;
    color: Mammals.Color;
  };
}
export namespace Birds {
  export type Eagle = {
    id: number;
    name: string;
    ability: Generated<World.Ability>;
  };
}
export type DB = {
  "birds.eagles": Birds.Eagle;
  "mammals.elephants": Mammals.Elephant;
};

Instead of putting everything globally as previously:

export const Ability = {
  FLY: "FLY",
  WALK: "WALK",
} as const;
export type Ability = (typeof Ability)[keyof typeof Ability];
export const Color = {
  GRAY: "GRAY",
  PINK: "PINK",
} as const;
export type Color = (typeof Color)[keyof typeof Color];
export type Eagle = {
  id: number;
  name: string;
  ability: Generated<Ability>;
};
export type Elephant = {
  id: number;
  name: string;
  ability: Generated<Ability>;
  color: Color;
};
export type DB = {
  "birds.eagles": Eagle;
  "mammals.elephants": Elephant;
};

@arthurfiorette
Copy link
Author

arthurfiorette commented Jan 10, 2025

I've published this PR as @arthurfiorette/[email protected] if anyone wants it

@Bathlamos
Copy link

Bathlamos commented Feb 4, 2025

Thank you for taking the time! I tried your implementation on a schema with a hundred tables and it works fine. ESLint complains about the use of namespaces. I want to note that they aren't strictly necessary as Prisma already forces the model names to be distinct even if they are in different schemas. Another interesting avenue of improvement might be to designate a schema as default, e.g. the public schema, to avoid prefixing its models.

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.

2 participants