package/lib/binaryParsers.js000644 0000005367 3560116604 013223 0ustar00000000 000000 const parseInt64 = require('pg-int8') const parseNumeric = require('pg-numeric') const parseInt16 = function (value) { return value.readInt16BE(0) } const parseInt32 = function (value) { return value.readInt32BE(0) } const parseFloat32 = function (value) { return value.readFloatBE(0) } const parseFloat64 = function (value) { return value.readDoubleBE(0) } const parseTimestampUTC = function (value) { const rawValue = 0x100000000 * value.readInt32BE(0) + value.readUInt32BE(4) // discard usecs and shift from 2000 to 1970 const result = new Date(Math.round(rawValue / 1000) + 946684800000) return result } const parseArray = function (value) { const dim = value.readInt32BE(0) const elementType = value.readUInt32BE(8) let offset = 12 const dims = [] for (let i = 0; i < dim; i++) { // parse dimension dims[i] = value.readInt32BE(offset) offset += 4 // ignore lower bounds offset += 4 } const parseElement = function (elementType) { // parse content length const length = value.readInt32BE(offset) offset += 4 // parse null values if (length === -1) { return null } let result if (elementType === 0x17) { // int result = value.readInt32BE(offset) offset += length return result } else if (elementType === 0x14) { // bigint result = parseInt64(value.slice(offset, offset += length)) return result } else if (elementType === 0x19) { // string result = value.toString('utf8', offset, offset += length) return result } else { throw new Error('ElementType not implemented: ' + elementType) } } const parse = function (dimension, elementType) { const array = [] let i if (dimension.length > 1) { const count = dimension.shift() for (i = 0; i < count; i++) { array[i] = parse(dimension, elementType) } dimension.unshift(count) } else { for (i = 0; i < dimension[0]; i++) { array[i] = parseElement(elementType) } } return array } return parse(dims, elementType) } const parseText = function (value) { return value.toString('utf8') } const parseBool = function (value) { return value[0] !== 0 } const init = function (register) { register(20, parseInt64) register(21, parseInt16) register(23, parseInt32) register(26, parseInt32) register(1700, parseNumeric) register(700, parseFloat32) register(701, parseFloat64) register(16, parseBool) register(1114, parseTimestampUTC) register(1184, parseTimestampUTC) register(1000, parseArray) register(1007, parseArray) register(1016, parseArray) register(1008, parseArray) register(1009, parseArray) register(25, parseText) } module.exports = { init: init } package/lib/builtins.js000644 0000002643 3560116604 012222 0ustar00000000 000000 /** * Following query was used to generate this file: SELECT json_object_agg(UPPER(PT.typname), PT.oid::int4 ORDER BY pt.oid) FROM pg_type PT WHERE typnamespace = (SELECT pgn.oid FROM pg_namespace pgn WHERE nspname = 'pg_catalog') -- Take only builting Postgres types with stable OID (extension types are not guaranted to be stable) AND typtype = 'b' -- Only basic types AND typelem = 0 -- Ignore aliases AND typisdefined -- Ignore undefined types */ module.exports = { BOOL: 16, BYTEA: 17, CHAR: 18, INT8: 20, INT2: 21, INT4: 23, REGPROC: 24, TEXT: 25, OID: 26, TID: 27, XID: 28, CID: 29, JSON: 114, XML: 142, PG_NODE_TREE: 194, SMGR: 210, PATH: 602, POLYGON: 604, CIDR: 650, FLOAT4: 700, FLOAT8: 701, ABSTIME: 702, RELTIME: 703, TINTERVAL: 704, CIRCLE: 718, MACADDR8: 774, MONEY: 790, MACADDR: 829, INET: 869, ACLITEM: 1033, BPCHAR: 1042, VARCHAR: 1043, DATE: 1082, TIME: 1083, TIMESTAMP: 1114, TIMESTAMPTZ: 1184, INTERVAL: 1186, TIMETZ: 1266, BIT: 1560, VARBIT: 1562, NUMERIC: 1700, REFCURSOR: 1790, REGPROCEDURE: 2202, REGOPER: 2203, REGOPERATOR: 2204, REGCLASS: 2205, REGTYPE: 2206, UUID: 2950, TXID_SNAPSHOT: 2970, PG_LSN: 3220, PG_NDISTINCT: 3361, PG_DEPENDENCIES: 3402, TSVECTOR: 3614, TSQUERY: 3615, GTSVECTOR: 3642, REGCONFIG: 3734, REGDICTIONARY: 3769, JSONB: 3802, REGNAMESPACE: 4089, REGROLE: 4096 } package/index.js000644 0000002310 3560116604 010721 0ustar00000000 000000 const textParsers = require('./lib/textParsers') const binaryParsers = require('./lib/binaryParsers') const builtinTypes = require('./lib/builtins') exports.getTypeParser = getTypeParser exports.setTypeParser = setTypeParser exports.builtins = builtinTypes const typeParsers = { text: {}, binary: {} } // the empty parse function function noParse (val) { return String(val) }; // returns a function used to convert a specific type (specified by // oid) into a result javascript type // note: the oid can be obtained via the following sql query: // SELECT oid FROM pg_type WHERE typname = 'TYPE_NAME_HERE'; function getTypeParser (oid, format) { format = format || 'text' if (!typeParsers[format]) { return noParse } return typeParsers[format][oid] || noParse }; function setTypeParser (oid, format, parseFn) { if (typeof format === 'function') { parseFn = format format = 'text' } if (!Number.isInteger(oid)) { throw new TypeError('oid must be an integer: ' + oid) } typeParsers[format][oid] = parseFn }; textParsers.init(function (oid, converter) { typeParsers.text[oid] = converter }) binaryParsers.init(function (oid, converter) { typeParsers.binary[oid] = converter }) package/lib/textParsers.js000644 0000012110 3560116604 012703 0ustar00000000 000000 const array = require('postgres-array') const parseTimestampTz = require('postgres-date') const parseInterval = require('postgres-interval') const parseByteA = require('postgres-bytea') const range = require('postgres-range') function parseBool (value) { return value === 'TRUE' || value === 't' || value === 'true' || value === 'y' || value === 'yes' || value === 'on' || value === '1' } function parseBoolArray (value) { return array.parse(value, parseBool) } function parseBaseTenInt (string) { return parseInt(string, 10) } function parseIntegerArray (value) { return array.parse(value, parseBaseTenInt) } function parseBigIntegerArray (value) { return array.parse(value, function (entry) { return parseBigInteger(entry).trim() }) } const parsePointArray = function (value) { return array.parse(value, parsePoint) } const parseFloatArray = function (value) { return array.parse(value, parseFloat) } const parseStringArray = function (value) { return array.parse(value, undefined) } const parseTimestamp = function (value) { const utc = value.endsWith(' BC') ? value.slice(0, -3) + 'Z BC' : value + 'Z' return parseTimestampTz(utc) } const parseTimestampArray = function (value) { return array.parse(value, parseTimestamp) } const parseTimestampTzArray = function (value) { return array.parse(value, parseTimestampTz) } const parseIntervalArray = function (value) { return array.parse(value, parseInterval) } const parseByteAArray = function (value) { return array.parse(value, parseByteA) } const parseBigInteger = function (value) { const valStr = String(value) if (/^\d+$/.test(valStr)) { return valStr } return value } const parseJsonArray = function (value) { return array.parse(value, JSON.parse) } const parsePoint = function (value) { if (value[0] !== '(') { return null } value = value.substring(1, value.length - 1).split(',') return { x: parseFloat(value[0]), y: parseFloat(value[1]) } } const parseCircle = function (value) { if (value[0] !== '<' && value[1] !== '(') { return null } let point = '(' let radius = '' let pointParsed = false for (let i = 2; i < value.length - 1; i++) { if (!pointParsed) { point += value[i] } if (value[i] === ')') { pointParsed = true continue } else if (!pointParsed) { continue } if (value[i] === ',') { continue } radius += value[i] } const result = parsePoint(point) result.radius = parseFloat(radius) return result } function parseInt4Range (raw) { return range.parse(raw, parseBaseTenInt) } function parseNumRange (raw) { return range.parse(raw, parseFloat) } function parseInt8Range (raw) { return range.parse(raw, parseBigInteger) } function parseTimestampRange (raw) { return range.parse(raw, parseTimestamp) } function parseTimestampTzRange (raw) { return range.parse(raw, parseTimestampTz) } const init = function (register) { register(20, parseBigInteger) // int8 register(21, parseBaseTenInt) // int2 register(23, parseBaseTenInt) // int4 register(26, parseBaseTenInt) // oid register(700, parseFloat) // float4/real register(701, parseFloat) // float8/double register(16, parseBool) register(1114, parseTimestamp) // timestamp without time zone register(1184, parseTimestampTz) // timestamp with time zone register(600, parsePoint) // point register(651, parseStringArray) // cidr[] register(718, parseCircle) // circle register(1000, parseBoolArray) register(1001, parseByteAArray) register(1005, parseIntegerArray) // _int2 register(1007, parseIntegerArray) // _int4 register(1028, parseIntegerArray) // oid[] register(1016, parseBigIntegerArray) // _int8 register(1017, parsePointArray) // point[] register(1021, parseFloatArray) // _float4 register(1022, parseFloatArray) // _float8 register(1231, parseStringArray) // _numeric register(1014, parseStringArray) // char register(1015, parseStringArray) // varchar register(1008, parseStringArray) register(1009, parseStringArray) register(1040, parseStringArray) // macaddr[] register(1041, parseStringArray) // inet[] register(1115, parseTimestampArray) // timestamp without time zone[] register(1182, parseStringArray) // date[] register(1185, parseTimestampTzArray) // timestamp with time zone[] register(1186, parseInterval) register(1187, parseIntervalArray) register(17, parseByteA) register(114, JSON.parse) // json register(3802, JSON.parse) // jsonb register(199, parseJsonArray) // json[] register(3807, parseJsonArray) // jsonb[] register(3904, parseInt4Range) // int4range register(3906, parseNumRange) // numrange register(3907, parseStringArray) // numrange[] register(3908, parseTimestampRange) // tsrange register(3910, parseTimestampTzRange) // tstzrange register(3912, range.parse) // daterange register(3926, parseInt8Range) // int8range register(2951, parseStringArray) // uuid[] register(791, parseStringArray) // money[] register(1183, parseStringArray) // time[] register(1270, parseStringArray) // timetz[] } module.exports = { init: init } package/package.json000644 0000002465 3560116604 011555 0ustar00000000 000000 { "name": "pg-types", "version": "4.0.0", "description": "Query result type converters for node-postgres", "main": "index.js", "scripts": { "coverage": "nyc --reporter=html npm test && open-cli coverage/index.html", "coverage-ci": "nyc --reporter=lcov npm test && codecov", "lint": "standard", "test": "tape test/*.js | tap-spec && npm run test-ts && npm run lint", "test-ts": "tsd" }, "repository": { "type": "git", "url": "git://github.com/brianc/node-pg-types.git" }, "keywords": [ "postgres", "PostgreSQL", "pg" ], "author": "Brian M. Carlson", "license": "MIT", "bugs": { "url": "https://github.com/brianc/node-pg-types/issues" }, "homepage": "https://github.com/brianc/node-pg-types", "devDependencies": { "@types/node": "^14.14.33", "codecov": "^3.8.1", "nyc": "^15.1.0", "open-cli": "^6.0.1", "standard": "^16.0.3", "tap-spec": "^5.0.0", "tape": "^5.2.2", "tsd": "^0.14.0" }, "dependencies": { "pg-int8": "1.0.1", "pg-numeric": "1.0.2", "postgres-array": "~3.0.1", "postgres-bytea": "~3.0.0", "postgres-date": "~2.0.1", "postgres-interval": "^3.0.0", "postgres-range": "^1.1.1" }, "files": [ "index.js", "index.d.ts", "lib" ], "engines": { "node": ">=10" } } package/README.md000644 0000007374 3560116604 010552 0ustar00000000 000000 # pg-types This is the code that turns all the raw text from postgres into JavaScript types for [node-postgres](https://github.com/brianc/node-postgres.git) ## use This module is consumed and exported from the root `pg` object of node-postgres. To access it, do the following: ```js var types = require('pg').types ``` Generally what you'll want to do is override how a specific data-type is parsed and turned into a JavaScript type. By default the PostgreSQL backend server returns everything as strings. Every data type corresponds to a unique `OID` within the server, and these `OIDs` are sent back with the query response. So, you need to match a particluar `OID` to a function you'd like to use to take the raw text input and produce a valid JavaScript object as a result. `null` values are never parsed. Let's do something I commonly like to do on projects: return 64-bit integers `(int8)` as JavaScript integers. Because JavaScript doesn't have support for 64-bit integers node-postgres cannot confidently parse `int8` data type results as numbers because if you have a _huge_ number it will overflow and the result you'd get back from node-postgres would not be the result in the database. That would be a __very bad thing__ so node-postgres just returns `int8` results as strings and leaves the parsing up to you. Let's say that you know you don't and wont ever have numbers greater than `int4` in your database, but you're tired of receiving results from the `COUNT(*)` function as strings (because that function returns `int8`). You would do this: ```js var types = require('pg').types types.setTypeParser(20, function(val) { return parseInt(val, 10) }) ``` __boom__: now you get numbers instead of strings. Just as another example -- not saying this is a good idea -- let's say you want to return all dates from your database as [moment](http://momentjs.com/docs/) objects. Okay, do this: ```js var types = require('pg').types var moment = require('moment') var parseFn = function(val) { return val === null ? null : moment(val) } types.setTypeParser(types.builtins.TIMESTAMPTZ, parseFn) types.setTypeParser(types.builtins.TIMESTAMP, parseFn) ``` _note: I've never done that with my dates, and I'm not 100% sure moment can parse all the date strings returned from postgres. It's just an example!_ If you're thinking "gee, this seems pretty handy, but how can I get a list of all the OIDs in the database and what they correspond to?!?!?!" worry not: ```bash $ psql -c "select typname, oid, typarray from pg_type order by oid" ``` If you want to find out the OID of a specific type: ```bash $ psql -c "select typname, oid, typarray from pg_type where typname = 'daterange' order by oid" ``` :smile: ## license The MIT License (MIT) Copyright (c) 2014 Brian M. Carlson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/index.d.ts000644 0000003426 3560116604 011166 0ustar00000000 000000 export enum builtins { BOOL = 16, BYTEA = 17, CHAR = 18, INT8 = 20, INT2 = 21, INT4 = 23, REGPROC = 24, TEXT = 25, OID = 26, TID = 27, XID = 28, CID = 29, JSON = 114, XML = 142, PG_NODE_TREE = 194, SMGR = 210, PATH = 602, POLYGON = 604, CIDR = 650, FLOAT4 = 700, FLOAT8 = 701, ABSTIME = 702, RELTIME = 703, TINTERVAL = 704, CIRCLE = 718, MACADDR8 = 774, MONEY = 790, MACADDR = 829, INET = 869, ACLITEM = 1033, BPCHAR = 1042, VARCHAR = 1043, DATE = 1082, TIME = 1083, TIMESTAMP = 1114, TIMESTAMPTZ = 1184, INTERVAL = 1186, TIMETZ = 1266, BIT = 1560, VARBIT = 1562, NUMERIC = 1700, REFCURSOR = 1790, REGPROCEDURE = 2202, REGOPER = 2203, REGOPERATOR = 2204, REGCLASS = 2205, REGTYPE = 2206, UUID = 2950, TXID_SNAPSHOT = 2970, PG_LSN = 3220, PG_NDISTINCT = 3361, PG_DEPENDENCIES = 3402, TSVECTOR = 3614, TSQUERY = 3615, GTSVECTOR = 3642, REGCONFIG = 3734, REGDICTIONARY = 3769, JSONB = 3802, REGNAMESPACE = 4089, REGROLE = 4096 } export type TypeId = builtins; export type TypesBuiltins = typeof builtins; export type TypeParser = (value: I) => T; export function setTypeParser(oid: number | TypeId, parseFn: TypeParser): void; export function setTypeParser(oid: number | TypeId, format: 'text', parseFn: TypeParser): void; export function setTypeParser(oid: number | TypeId, format: 'binary', parseFn: TypeParser): void; export function getTypeParser(oid: number | TypeId): TypeParser; export function getTypeParser(oid: number | TypeId, format: 'text'): TypeParser; export function getTypeParser(oid: number | TypeId, format: 'binary'): TypeParser;