Registry / database / pg-custom-types

pg-custom-types

JSON →
library3.0.0jsnpmunverified

A helper library for node-postgres that fetches OIDs for custom PostgreSQL data types (e.g., PostGIS geometry, geography) and provides a lookup table to use with pg.setTypeParser. Current stable version is 3.0.0. Released infrequently; relies on pg package (any version). Differentiates by simplifying the process of registering custom type parsers without manually querying pg_type.

npm install pg-custom-types
INSTALL
IMPORT
SIG · PG-CUSTOM-TYPES
P
pg-custom-types
databasejavascriptv3.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import types from 'pg-custom-types'
const { types } = require('pg-custom-types')
Package exports a function directly, not an object. CommonJS require returns the function; named import fails.
type parsers
const types = require('pg-custom-types')
const types = require('pg-custom-types').default
In CommonJS, the module.exports is the function itself, so .default is undefined.
fetcher
types.fetcher(pg, connection)
types.fetcher(connection, pg)
Argument order: (pg, connection), not (connection, pg).

Connects to PostgreSQL, fetches OIDs for PostGIS geometry and geography types, then uses them in a query.

const types = require('pg-custom-types'); const { Client } = require('pg'); const client = new Client(); client.connect(err => { if (err) throw err; types(types.fetcher({ query: client.query.bind(client) }, client), 'postgis', ['geometry', 'geography'], (err, oids) => { if (err) throw err; client.query('SELECT $1::geometry', [point], (err, res) => { console.log(res.rows[0]); }); }); });
Debug
Known issues
breakingIn v3.0.0, the fetcher argument signature changed from (pg, connection) to (queryFunction, connection) where queryFunction is of form (sql, callback).
fix
Replace first argument with a query function: types.fetcher({ query: pg.Client.prototype.query.bind(connection) }, connection).
affects: >=2.0.0 <3.0.0
gotchaThe callback returns an object keyed by OID (number), not type name. Access via oids[oid] or use the example with 'geometry' key? Actually, keys are numeric OIDs, not type names.
fix
Use oids[21842552] or iterate Object.keys(oids). The README example shows keys as numbers, but variable names like 'geometry' are misleading. Expect numeric keys.
affects: >=1.0.0
gotchaThe fetcher expects a function, not a client. Passing a client object directly will fail silently.
fix
Use types.fetcher({ query: (sql, cb) => client.query(sql, cb) }, client).
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: types.fetcher is not a function
Using default import { types } instead of the entire module
fix
Replace with const types = require('pg-custom-types') or import types from 'pg-custom-types'
Warning: No type parsers found for OID ...
OID not in the fetched lookup; the type may not exist or wrong database
fix
Verify type names exist in the database and that connection points to correct db.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
pgrequiredPeer dependency; required to use setTypeParser and connection
Agent activity
4 hits · last 30 days
node
4
Resources
pg-custom-types — npm install pg-custom-types · libregistry