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 11 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 a 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. Making much more easier to work with schemas, the same visually/architecturally as the thinking why schemas as written like schema.model inside SQL.

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;
};

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.

1 participant