Skip to main content

DataType

The descriptor shape used to register a codec for a PostgreSQL type on a DataTypeMap — every built-in type on GlobalTypeMap is one of these, and registering a custom type (or overriding a built-in one) means providing an object matching this interface.

KeyTypeDefaultDescription
oidOIDPostgreSQL type OID this descriptor decodes/encodes
namestringType name (informational, shown in FieldInfo.dataTypeName)
elementsOIDOIDFor an array type, the OID of its element type (e.g. the _int4 array type points at int4)
isArraybooleanWhether this descriptor represents an array type
jsTypestringName of the JS type values decode to (informational, shown in FieldInfo.jsType)
arraySeparatorstring,Separator used between elements when encoding this type's text-format array literal
isType(v: any) => booleanPredicate used by DataTypeMap.determine() to guess whether a plain JS value should be sent as this type
decodeBinaryDecodeBinaryFunctionDecodes a value from the binary wire format — (buf: Buffer, offset: number, len: number, options) => any. Must not read past len: buf is the whole row's shared buffer, not a value-sized slice, so an unbounded decoder returns bytes belonging to whatever follows in the row instead of failing loudly
decodeTextDecodeTextFunctionDecodes a value from the text wire format
decodeTextBufferDecodeTextBufferFunctionOptional fast path: decodes directly from the raw wire Buffer instead of the pre-converted UTF-8 string decodeText receives. Only meaningful for text-format scalar columns
encodeAsNullEncodeAsNullFunctionPredicate deciding whether a given value should be encoded as SQL NULL
encodeBinaryEncodeBinaryFunctionEncodes a value into the binary wire format
encodeTextEncodeTextFunctionEncodes a value into the text wire format
encodeCalculateDimEncodeCalculateDimFunctionFor array types, computes the array's dimensions before encoding
note

Breaking change in v3.3.0: decodeBinary used to receive a single Buffer already sliced to the value's own bytes, plus a separate fixedBinarySize field for constant-width types. Both are gone — decodeBinary now always gets (buf, offset, len, options), the same shape decodeTextBuffer has always used. A decoder that ignores len no longer throws on an out-of-bounds read; it silently returns bytes belonging to whatever value follows it in the row, so update any custom DataType to read only buf.subarray(offset, offset + len) (or the equivalent bounded access) before upgrading.

Registering a custom type

import { DataTypeMap, GlobalTypeMap, type DataType } from 'postgrejs';

const myType: DataType = {
oid: 90210,
name: 'my_type',
jsType: 'string',
isType: v => typeof v === 'string' && v.startsWith('my:'),
decodeText: v => v,
// buf is the row's own shared buffer, not a value-sized slice - read
// exactly `len` bytes starting at `offset`, never past it.
decodeBinary: (buf, offset, len) => buf.toString('utf8', offset, offset + len),
};

const myTypeMap = new DataTypeMap(GlobalTypeMap);
myTypeMap.register(myType);

See DataTypeMap and the Data Types & Type Mapping guide.