drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[FEATURE]: pg JSONb Type insertion

Open papercrane-elijah opened this issue 1 year ago • 1 comments

Describe what you want

in pg-core/columns/jsonb.d.ts

export type PgJsonbBuilderInitial<TName extends string> = PgJsonbBuilder<{
    name: TName;
    dataType: 'json';
    columnType: 'PgJsonb';
    data: unknown;
    driverParam: unknown;
    enumValues: undefined;
}>;

Can we somehow make this Generic? I would like to specify the data type. We use this for prototyping our apps.

The type safety would come in very handy, and I would rather not hack together my own type interface.

papercrane-elijah avatar Mar 18 '24 16:03 papercrane-elijah

I believe .$type<..>() allows you to do this on column definition.

You can read more about it in the second section of the json column type https://orm.drizzle.team/docs/column-types/pg#jsonb and the dedicated section about it https://orm.drizzle.team/docs/column-types/pg#customizing-column-data-type

Example from the docs

// will be infered as { foo: string }
jsonb: jsonb('jsonb').$type<{ foo: string }>();
// will be infered as string[]
jsonb: jsonb('jsonb').$type<string[]>();
// won't compile
jsonb: jsonb('jsonb').$type<string[]>().default({});

svemat01 avatar May 03 '24 10:05 svemat01

Use PgJsonbBuilder instead of PgJsonbBuilderInitial. You can also make your own abstraction:

type JsonbBuilderWithData<TName extends string, TData as Record<string, any>> = PgJsonbBuilder<{
    name: TName;
    dataType: 'json';
    columnType: 'PgJsonb';
    data: TData;
    driverParam: TData;
    enumValues: undefined;
}>

L-Mario564 avatar Oct 18 '24 19:10 L-Mario564