monolayer-pg / schema / jsonb
Function: jsonb()
jsonb<
T
>():PgJsonB
<T
,T
>
Column that stores JSON data.
Type Parameters
Type Parameter | Default type |
---|---|
T extends JsonValue | JsonValue |
Returns
PgJsonB
<T
, T
>
Remarks
Data is stored in a decomposed binary format. Slower to input than a json
column, but significantly faster to process.
Supports indexing.
Kysely database schema type definition
ts
type JsonArray = JsonValue[];
type JsonValue = boolean | number | string | Record<string, any> | JsonArray;
{
readonly __select__: JsonValue | null;
readonly __insert__: JsonValue | null | undefined;
readonly __update__: JsonValue | null;
};
Nullability and optionality will change according to the column's constraints, generated values, and default data values.
You can customize the data type of the column by providing a type argument to the json
function.
Warning: the Zod schema for a json
column only validates that data can be conforms to the JsonValue
type. When using a custom data type you shoud adapt it. See examples.
Example
Default Data Type
ts
import { json, schema, table } from "@monolayer/pg/schema";
const dbSchema = schema({
tables: {
example: table({
columns: {
document: json(),
},
}),
},
});
// Kysely database schema type
type DB = typeof dbSchema.infer;
Custom Data Example
ts
import { jsonb, schema, table } from "@monolayer/pg/schema";
type Data = { count: number; name: string };
const dbSchema = schema({
tables: {
example: table({
columns: {
info: jsonb<Data>(),
},
}),
},
});
// Kysely database schema type
type DB = typeof dbSchema.infer;
See
PostgreSQL Docs: jsonb