Codecs

The TypeScript value. The SQL type stays the same.

codecs chooses the TypeScript value of t.bigint(), t.identity(), and t.numeric(). The SQL type stays bigint or numeric. A time column is already Temporal. Omit a key and that codec stays at its default.

The column as is what types the value. While the schema codec is "string", a column may pass another as. When the schema codec is "number" or "bigint", every column of that type must pass the same as. A bare column was built as "string".

CodecValuesDefault
bigint"string", "number", "bigint""string"
numeric"string", "number""string"
timestamps"temporal""temporal"

bigint

t.bigint() and t.identity() share this codec. "number" is a safe integer. A value outside that range is OKM1210. See bigint.

string

Omitted and "string" are a decimal string. The SQL value stays exact past a safe integer.

bigint-string.ts
import { schema, table, t } from "okmodel/pg";

const payments = table("payments", {
  id: t.identity(),
  cents: t.bigint(), 
});

export default schema({ tables: [payments] });

number

"number" is a safe integer. The column passes the same as.

bigint-number.ts
import { schema, table, t } from "okmodel/pg";

const payments = table("payments", {
  id: t.identity({ as: "number" }), 
  cents: t.bigint({ as: "number" }), 
});

export default schema({
  tables: [payments],
  codecs: { bigint: "number" }, 
});

native

"bigint" is a JavaScript bigint.

bigint-native.ts
import { schema, table, t } from "okmodel/pg";

const payments = table("payments", {
  id: t.identity({ as: "bigint" }),
  cents: t.bigint({ as: "bigint" }), 
});

export default schema({
  tables: [payments],
  codecs: { bigint: "bigint" }, 
});

OKM1060

A bare column under "number" or "bigint" is Schema codecs.bigint is number, and payments.cents was built as string. Set as: "number" on that column. Any other schema value is schema() codecs.bigint date must be string, number, or bigint.

numeric

t.numeric() is exact. { as: "number" } can lose digits past a JavaScript number. See numeric.

string

Omitted and "string" keep every digit.

numeric-string.ts
import { schema, table, t } from "okmodel/pg";

const payments = table("payments", {
  id: t.identity(),
  amount: t.numeric(12, 2), 
});

export default schema({ tables: [payments] });

number

"number" needs { as: "number" } on the column. Precision and scale stay the SQL type.

numeric-number.ts
import { schema, table, t } from "okmodel/pg";

const payments = table("payments", {
  id: t.identity(),
  amount: t.numeric(12, 2, { as: "number" }), 
});

export default schema({
  tables: [payments],
  codecs: { numeric: "number" }, 
});

OKM1060

A bare column under "number" is Schema codecs.numeric is number, and payments.amount was built as string. Set as: "number" on that column. Any other schema value is schema() codecs.numeric bigint must be string or number.

timestamps

"temporal" is the only value. Omitting it is the same. A time column has no as. t.timestamptz() is Temporal.Instant. See time.

timestamps.ts
import { schema, table, t } from "okmodel/pg";

const payments = table("payments", {
  id: t.identity(),
  paidAt: t.timestamptz(), 
});

export default schema({
  tables: [payments],
  codecs: { timestamps: "temporal" }, 
});

OKM1060

Any other value is schema() codecs.timestamps date must be temporal.

together

One schema can set every codec. Each bigint column matches "bigint". The numeric column matches "number".

schema.ts
import { schema, table, t } from "okmodel/pg";

const payments = table("payments", {
  id: t.identity({ as: "bigint" }),
  cents: t.bigint({ as: "bigint" }), 
  amount: t.numeric(12, 2, { as: "number" }), 
  paidAt: t.timestamptz(),
});

export default schema({
  tables: [payments],
  codecs: {
    bigint: "bigint", 
    numeric: "number", 
    timestamps: "temporal", 
  },
});