Skip to content

Instantly share code, notes, and snippets.

@oliveryasuna
Created April 24, 2025 17:39
Show Gist options
  • Save oliveryasuna/1004aa84cc6dd325a7ded44363eca43b to your computer and use it in GitHub Desktop.
Save oliveryasuna/1004aa84cc6dd325a7ded44363eca43b to your computer and use it in GitHub Desktop.
pg Type Parsers
import {UserPermissionEnum} from '@poop/db';
import pg from 'pg';
import {LocalDate, LocalDateTime, LocalTime, ZonedDateTime} from '@js-joda/core';
type pgBOOL = boolean;
type pgBYTEA = Buffer;
type pgCHAR = string;
type pgINT8 = number;
type pgINT2 = number;
type pgINT4 = number;
type pgTEXT = string;
type pgJSON = unknown;
type pgFLOAT4 = number;
type pgFLOAT8 = number;
type pgINET = string;
type pgVARCHAR = string;
type pgDATE = LocalDate;
type pgTIME = LocalTime;
type pgTIMESTAMP = LocalDateTime;
type pgTIMESTAMPTZ = ZonedDateTime;
type pgNUMERIC = number;
type pgUUID = string;
type pgJSONB = unknown;
const setupTypeParsers = ((): void => {
pg.types.setTypeParser(pg.types.builtins.INT8, ((value: (string | null)): (pgINT8 | null) => ((value === null) ? null : Number.parseInt(value, 10))));
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
pg.types.setTypeParser(pg.types.builtins.JSON, ((value: (string | null)): (pgJSON | null) => ((value === null) ? null : JSON.parse(value))));
pg.types.setTypeParser(pg.types.builtins.DATE, ((value: (string | null)): (pgDATE | null) => ((value === null) ? null : LocalDate.parse(value))));
pg.types.setTypeParser(pg.types.builtins.TIME, ((value: (string | null)): (pgTIME | null) => ((value === null) ? null : LocalTime.parse(value))));
pg.types.setTypeParser(pg.types.builtins.TIMESTAMP, ((value: (string | null)): (pgTIMESTAMP | null) => ((value === null) ? null : LocalDateTime.parse(value))));
pg.types.setTypeParser(pg.types.builtins.TIMESTAMPTZ, ((value: (string | null)): (pgTIMESTAMPTZ | null) => ((value === null) ? null : ZonedDateTime.parse(value))));
pg.types.setTypeParser(pg.types.builtins.NUMERIC, ((value: (string | null)): (pgNUMERIC | null) => ((value === null) ? null : Number.parseFloat(value))));
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
pg.types.setTypeParser(pg.types.builtins.JSONB, ((value: (string | null)): (pgJSONB | null) => ((value === null) ? null : JSON.parse(value))));
// `user_permission`
pg.types.setTypeParser(87_732, ((value: (string | null)): (UserPermissionEnum | null) => ((value === null) ? null : (value as UserPermissionEnum))));
// `user_permission[]`
pg.types.setTypeParser(87_731, ((value: (string | null)): (UserPermissionEnum[] | null) => ((value === null) ? null : (value.slice(1, -1).split(',').map((permission: string): UserPermissionEnum => (permission as UserPermissionEnum))))));
});
export type {
pgBOOL,
pgBYTEA,
pgCHAR,
pgINT8,
pgINT2,
pgINT4,
pgTEXT,
pgJSON,
pgFLOAT4,
pgFLOAT8,
pgINET,
pgVARCHAR,
pgDATE,
pgTIME,
pgTIMESTAMP,
pgTIMESTAMPTZ,
pgNUMERIC,
pgUUID
};
export {
setupTypeParsers
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment