sequelize-auto
sequelize-auto copied to clipboard
Support type json and jsonb on postgres dialect
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
Can you give an example of what the input (table definition) and output (model class) should be?
-- 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!
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 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
- 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)
- 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)
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 intotable.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