Numbers
Integers, numbers, and floats.
A number column is an integer, an exact numeric, or a float.
Integers
The value is a whole number inside that range.
Smallint
t.smallint() is smallint. The range is -32768 to 32767. The TypeScript value is a number. Storage is 2 bytes.
import { table, t } from "okmodel/pg";
export const counts = table("counts", {
id: t.identity(),
flag: t.smallint(),
});OKM1210
A value that is not an integer in that range is refused when it is encoded.
Integer
t.integer() is integer. The range is -2147483648 to 2147483647. The TypeScript value is a number. Storage is 4 bytes. This is the usual count.
import { table, t } from "okmodel/pg";
export const counts = table("counts", {
id: t.identity(),
total: t.integer(),
});OKM1210
A value that is not an integer in that range is refused when it is encoded.
Bigint
t.bigint() is bigint. The range is -9223372036854775808 to 9223372036854775807. The TypeScript value is a decimal string, so a value past a safe integer stays exact. Storage is 8 bytes. A bare call stays a string. A schema bigint codec other than "string" needs the same as on this column. A safe integer or a JavaScript bigint selects the other modes.
import { table, t } from "okmodel/pg";
export const counts = table("counts", {
id: t.identity(),
sequence: t.bigint(),
});OKM1210
A string that is not a canonical integer is refused when it is encoded. The spelling has no leading zeros, for example 0 or -12.
Safe integer
{ as: "number" } on t.bigint() or t.identity() makes the TypeScript value a number. It accepts a safe integer only.
import { table, t } from "okmodel/pg";
export const counts = table("counts", {
id: t.identity(),
sequence: t.bigint({ as: "number" }),
});OKM1210
A value outside the safe integer range is refused when it is encoded.
JavaScript bigint
{ as: "bigint" } on t.bigint() or t.identity() makes the TypeScript value a JavaScript bigint.
import { table, t } from "okmodel/pg";
export const counts = table("counts", {
id: t.identity(),
sequence: t.bigint({ as: "bigint" }),
});OKM1060
Any other as on t.bigint() or t.identity() is refused.
Identity
t.identity() is bigint GENERATED ALWAYS AS IDENTITY and a primary key. Insert and update omit it. The range and the as modes match bigint. An integer key is t.integerIdentity().
import { table, t } from "okmodel/pg";
export const counts = table("counts", {
id: t.identity(),
});Numbers
t.numeric() is exact. Money, rates, and quantities use it. Postgres decimal is this same type. The size is two bytes for each group of four decimal digits, plus 3 to 8 bytes. Leading and trailing zeros are not stored. A bare call stays a string. A schema numeric codec of "number" needs { as: "number" } on this column.
Unconstrained
t.numeric() is numeric. The TypeScript value is a string. Omit precision and scale for an unconstrained numeric. The string keeps every digit.
import { table, t } from "okmodel/pg";
export const payments = table("payments", {
id: t.identity(),
amount: t.numeric(),
});Precision
t.numeric(precision) is numeric(precision). Precision is an integer from 1 to 1000.
import { table, t } from "okmodel/pg";
export const payments = table("payments", {
id: t.identity(),
amount: t.numeric(12),
});OKM1060
A precision outside 1 to 1000 is refused.
Scale
t.numeric(precision, scale) is numeric(precision, scale). Scale is an integer from 0 to that precision.
import { table, t } from "okmodel/pg";
export const payments = table("payments", {
id: t.identity(),
amount: t.numeric(12, 2),
});OKM1060
A scale outside 0 to the precision is refused.
Number
{ as: "number" } makes the TypeScript value a number. The SQL type stays the same. It can lose digits past a JavaScript number. Any other as is refused.
import { table, t } from "okmodel/pg";
export const payments = table("payments", {
id: t.identity(),
amount: t.numeric(12, 2, { as: "number" }),
});OKM1060
as must be "string" or "number".
Floats
t.real() and t.double() are binary floating point. A sum of them is not exact. An exact quantity uses numeric. Postgres float is this family.
Real
t.real() is real, single precision, about 6 decimal digits. The TypeScript value is a number. Storage is 4 bytes.
import { table, t } from "okmodel/pg";
export const samples = table("samples", {
id: t.identity(),
reading: t.real(),
});OKM1210
Infinity and NaN are refused when the value is encoded.
Double
t.double() is double precision, about 15 decimal digits. The TypeScript value is a number. Storage is 8 bytes.
import { table, t } from "okmodel/pg";
export const samples = table("samples", {
id: t.identity(),
reading: t.double(),
});OKM1210
Infinity and NaN are refused when the value is encoded.
Next: Columns.