Skip to content

monolayer-pg / schema / json

Function: json()

json<T>(): PgJson<T, T>

Column that stores JSON data.

Type Parameters

Type ParameterDefault type
T extends JsonValueJsonValue

Returns

PgJson<T, T>

Remarks

Data stored is an exact copy of the input text and processing functions must reparse on each execution.

Does not support 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 { json, schema, table } from "@monolayer/pg/schema";

type Data = { count: number; name: string  };

const dbSchema = schema({
  tables: {
    example: table({
      columns: {
        info: json<Data>(),
      },
    }),
  },
});

// Kysely database schema type
type DB = typeof dbSchema.infer;

See

PostgreSQL Docs: json