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

kanel schema aware Identifiers #588

Open
aq1018 opened this issue Jul 18, 2024 · 1 comment
Open

kanel schema aware Identifiers #588

aq1018 opened this issue Jul 18, 2024 · 1 comment

Comments

@aq1018
Copy link

aq1018 commented Jul 18, 2024

The default generateIdentifierType generates Identifiers like this:

/** Identifier type for Product */
export type ProductId = number & { __brand: 'ProductId' };

When using multiple schemas, it's is possible that two schemas will contain tables with the same name. To make the Identifier more strict, it probably should include the schema name as well.

Such as:

/** Identifier type for Public.Product */
export type ProductId = number & { __brand: 'Public.ProductId ' };

I worked around this issue by implementing a custom generateIdentifierType function like so:

// generate Schema Aware Identifiers
function generateIdentifierType(column, details) {
  return {
    declarationType: 'typeDeclaration',
    name: pascalCase(`${details.name}_${column.name}`),
    exportAs: 'named',
    typeDefinition: [
      `number & { __brand: '${details.schemaName}.${details.name}.${column.name}' }`,
    ],
    comment: [`Identifier type for ${details.schemaName}.${details.name}`],
  }
}

This creates something like this:

// In `term_life/Product.ts`

/** Identifier type for term_life.product */
export type ProductId = number & { __flavor?: 'term_life.product.id' };

// In `income_protection/Product.ts`
/** Identifier type for income_protection.product */
export type ProductId = number & { __flavor?: 'income_protection.product.id' };
@aq1018 aq1018 changed the title kanel should provide options for schema aware Identifiers kanel schema aware Identifiers Jul 18, 2024
@kristiandupont
Copy link
Owner

You are absolutely right, this is a bug. I will update this.

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

2 participants