sequelize-auto icon indicating copy to clipboard operation
sequelize-auto copied to clipboard

Support type json and jsonb on postgres dialect

Open moduleit opened this issue 4 years ago • 5 comments

Hello guys can you tell me how to implemement my own types to the generate process or maybe support them on core sequelize-auto code

moduleit avatar Feb 07 '21 12:02 moduleit

Can you give an example of what the input (table definition) and output (model class) should be?

steveschmitt avatar Feb 08 '21 19:02 steveschmitt

-- input
CREATE TABLE "table" (
    example jsonb NOT NULL,
    id uuid PRIMARY KEY,
);

The output type for example would be defined somewhere as a TypeScript type. So I presume that a solution would require column names (with jsonb type) to be mapped to TypeScript types that are defined elsewhere.

Thanks!

lensbart avatar Apr 16 '21 08:04 lensbart

Thanks @lensbart. For your example, I'm thinking the output would be a file table.ts containing something like:

...
export class Table extends Model<TableAttributes, TableCreationAttributes> implements TableAttributes {
    id!: string;
    example!: MyComplexType;
...
}

And somewhere you would define what MyComplexType is, and import that definition into table.ts. Correct?

Any ideas how the user should tell sequelize-auto that column table.example should be type MyComplexType?

steveschmitt avatar Apr 16 '21 19:04 steveschmitt

@steveschmitt sorry to be late, but what about using postgres column comments in order to define the structure something like. Im thinking about 2 possibles ways

  1. By file COMMENT ON COLUMN table.example IS '@JSON_TYPE("./table-types/table.ts")'; (Of course the console will requires this file if not exists)
  2. Inline definition COMMENT ON COLUMN table.example IS '@JSON_TYPE("{column! : string, column2: Array}")';(In this case the sequelize will put the type directly on the Main Table type)

jmar1998 avatar Nov 13 '21 13:11 jmar1998

Thanks @lensbart. For your example, I'm thinking the output would be a file table.ts containing something like:

...
export class Table extends Model<TableAttributes, TableCreationAttributes> implements TableAttributes {
    id!: string;
    example!: MyComplexType;
...
}

And somewhere you would define what MyComplexType is, and import that definition into table.ts. Correct?

Any ideas how the user should tell sequelize-auto that column table.example should be type MyComplexType?

Apologies for the somewhat late reply. I have since reconsidered and think that the proper approach is to not use the generated files directly, but to extend the classes and use those extended classes elsewhere in the codebase. This allows the developer to use hooks, strictly-typed JSON columns, etc. even when these cannot be derived from the Postgres database.

Thanks

lensbart avatar Feb 09 '22 00:02 lensbart