Primate LogoPrimate

Numbers and enums

Pema provides numeric schemas for JavaScript number and bigint values. All numeric schemas are Storable; the width determines their database datatype.

Numeric types

Factory JavaScript value Range or alias
p.number, p.f64 number 64-bit floating point
p.f32 number exactly representable as 32-bit float
p.int, p.i32 number −2³¹ through 2³¹−1
p.uint, p.u32 number 0 through 2³²−1
p.i8, p.i16 number signed 8- and 16-bit integers
p.u8, p.u16 number unsigned 8- and 16-bit integers
p.bigint, p.i64 bigint signed 64-bit integer
p.biguint, p.u64 bigint unsigned 64-bit integer
p.i128 bigint signed 128-bit integer
p.u128 bigint unsigned 128-bit integer

Integer schemas reject fractions and enforce their declared range. Loose number and integer schemas coerce valid numeric strings:

p.loose.u16.parse("8080"); // 8080
p.u16.parse("8080");       // ParseError

Numeric validators

Numeric types expose:

Method Requirement
values({ ... }) Value occurs in the object's values
range(from, to) Inclusive range
min(from) Value is at least from
max(to) Value is at most to
check(predicate, message) Custom primitive check

Numeric minimums and maximums are value thresholds, not cardinalities. A floating-point schema may therefore use negative and fractional limits:

const Temperature = p.number.min(-273.15).max(1_000);

Unsigned number types also expose port(), which accepts non-privileged ports from 1024 through 65535:

const Port = p.u16.port();

The public NumberType and UintType types describe common concrete return types. DataType maps persistent datatype names to their JavaScript values.

Enums

p.enum(values) creates a numeric enum stored as u8. Keys must use SCREAMING_SNAKE_CASE and values must fit the underlying unsigned byte schema.

const Status = p.enum({
  DRAFT: 0,
  PUBLISHED: 1,
});

Status.parse(Status.DRAFT); // 0
Status.nameOf(1);           // "PUBLISHED"
Status.values;              // { DRAFT: 0, PUBLISHED: 1 }

The returned Enum combines EnumType with the declared constants. It also supports optional() and default().

See Pema and Stores for the complete persistence matrix.