kristiandupont / kanel

Generate Typescript types from Postgres
https://kristiandupont.github.io/kanel/
MIT License
878 stars 62 forks source link

Should have a option to disable branded IDs #433

Closed thelinuxlich closed 11 months ago

thelinuxlich commented 1 year ago

Not every user knows about branded IDs or wants the extra check through type guards. Kanel should have a option to generate the ID column as any other column, without a TableNameID.

thelinuxlich commented 1 year ago

In case someone stumbles upon this issue wanting the same, this is how I found a way to disable it (but not entirely, still creates TableID)

// .kanelrc.js
generateIdentifierType: (c, d, config) => {
    // Id columns are already prefixed with the table name, so we don't need to add it here
    const name = toPascalCase(c.name);
    const innerType = resolveType(c, d, {
      ...config,
      generateIdentifierType: undefined,
    });
    return {
      declarationType: 'typeDeclaration',
      name,
      exportAs: 'named',
      typeDefinition: [innerType],
      comment: [] 
    };
  }
e253 commented 1 year ago

I wasted a lot of time spinning my wheels on this. TDLR for those having issues,

export type UsersId = string & { __brand: "UsersId" }

vs.

export type UsersId = string

The former type declaration forces you to explicity indicate that the value is a UsersId. For the latter any string will build fine. It's meant to make you sure you are aware the value you are passing is a pkey and not something else. This stackoverflow discussion explains it in more depth.

@kristiandupont Can this be added to documentation or README? I wasn't familiar with typescript intersections before using Kanel.

kristiandupont commented 1 year ago

I am sorry that you had a frustrating experience! Yes, this should be optional and documented. Sorry that I haven't been able to fix it yet!

e253 commented 1 year ago

All good. kanel has saved me so much time generating kysely schema.

e253 commented 1 year ago

I added PR #461 with a small note that would've helped me. Hope that helps!