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
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
fixedBinarySizenumberDeclares this type's binary representation as always exactly N bytes (e.g. int4 is always 4). Leave unset for types whose binary length varies by value (bytea, varchar, json, numeric)
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

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,
decodeBinary: v => v.toString('utf8'),
};

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

See DataTypeMap and the Data Types & Type Mapping guide.