kanel icon indicating copy to clipboard operation
kanel copied to clipboard

Error if primary key is a reference

Open jifeon opened this issue 4 years ago • 4 comments

Hi! Thx for a great lib!

I'm moving a project which has around 100 tables to typescript and it seems that I have only one error from kanel after initial types generation and little adjusting of customTypeMap. Let's consider following DB schema:

pageTiles.sql

-- auto-generated definition
create table "pageTiles" (
  "pageId"       uuid                                                                                     not null
    constraint "pageTiles_pageId_fkey"
      references pages
      on delete cascade,
  "tileId"       uuid                                                                                     not null,
  "tileType"     varchar(50)                                                                              not null,

  constraint "pageTiles_pkey"
    primary key ("pageId", "tileId", "tileType")
);

create unique index "pageTiles_tileId_key"
  on "pageTiles"("tileId");

componentFormTiles.sql

-- auto-generated definition
create table "componentFormTiles" (
  id          uuid                     default public.gen_random_uuid() not null
    constraint "componentFormTiles_pkey"
      primary key
    constraint "componentFormTiles_id_fkey"
      references "pageTiles"("tileId")
      on update cascade on delete cascade
      deferrable
);

pageTiles.ts

take a look at tileId

// Automatically generated. Don't change this file manually.

import { PagesId } from './pages';

export default interface PageTiles {
  /**
   * Index: pageTiles_pageId_tileId_key
   * Index: pageTiles_pageId_tileOrder_unique
   * Index: pageTiles_pageId_uri_unique
   * Primary key. Index: pageTiles_pkey
  */
  pageId: PagesId;

  /**
   * Index: pageTiles_pageId_tileId_key
   * Primary key. Index: pageTiles_pkey
   * Index: pageTiles_tileId_key
  */
  tileId: string;

  /** Primary key. Index: pageTiles_pkey */
  tileType: string;
}

componentFormTiles.ts

first import is not correct - there is no such export in pageTiles.ts

// Automatically generated. Don't change this file manually.

import { PageTilesId } from './pageTiles';   // <--- ERROR!
import { FormsId } from './forms';

export type ComponentFormTilesId = string & { __flavor?: 'componentFormTiles' };

export default interface ComponentFormTiles {
  /** Primary key. Index: componentFormTiles_pkey */
  id: ComponentFormTilesId;
}

Could you suggest any workaround?

jifeon avatar Sep 16 '20 18:09 jifeon

That just looks like a bug, I will try and fix it. But on your request for a workaround, I guess I should consider adding some hooks or something so that people can get around problems or specific needs when necessary.. Ideas are welcomed!

kristiandupont avatar Sep 16 '20 18:09 kristiandupont

@kristiandupont I agree with idea about hooks. For now I see that something like beforeFileWrite hook could help with solving most of problems related with additional imports/exports. I see it as async function which takes file name, file content, type name etc. Based on this arg one can add raw TS to beginning of the file.

So in my case workaround would look like:

module.exports = {
  beforeFileWrite({ tableName, fileContent }) {
    if (tableName === 'pageTiles') return 'export type PageTilesId = string & { __flavor?: 'pageTiles' };\n' + fileContent; 
    return fileContent;
  }
}

it also would help with customer types, for example for UUIDs I would able to add import UUID from '../UUID'; instead of just replacing it with string and so on. ref #4

jifeon avatar Sep 16 '20 19:09 jifeon

Hi @jifeon, I've published a new version that supports hooks. Your specific issue is not yet resolved but I've streamlined things a bit so it should be easier for me to test and fix, so I will get around to that. But for now you should be able to work around the issue with a hook.

kristiandupont avatar Oct 05 '20 17:10 kristiandupont

Thanks a lot @kristiandupont! I will check it out!

jifeon avatar Oct 06 '20 12:10 jifeon

So, two years later and this should finally be addressed :-)

There is a premajor version out that should fix this issue. There are significant breaking changes though, and the API is not final so feel free to experiment if you like. The documentation is work in progress still, you can start here: https://github.com/kristiandupont/kanel/blob/v3/docs/configuring.md

kristiandupont avatar Aug 16 '22 14:08 kristiandupont

Thank you @kristiandupont!

I'm not working with that project any more, so can't check. @frenzzy you probably want to check it out!

jifeon avatar Aug 21 '22 10:08 jifeon