Registry / database / postgres-composite

postgres-composite

JSON →
library0.3.0jsnpmunverified

A lightweight zero-dependency library for parsing and serializing PostgreSQL composite type values as strings. Version 0.3.0 is the current stable release. It provides two functions: parse, which returns an iterable of string or null values from a formatted composite string, and serialize, which produces a composite string from an iterable. The library is ESM- and CJS-compatible, ships TypeScript definitions, and has no runtime dependencies. It is intended for use with the pg client or raw PostgreSQL connections. Minimal API surface with no additional configuration needed.

npm install postgres-composite
INSTALL
IMPORT
SIG · POSTGRES-COMPOSITE
P
postgres-composite
databasejavascriptv0.3.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.

parse
import { parse } from 'postgres-composite'
const parse = require('postgres-composite').parse
Both ESM and CJS are supported. TypeScript types are included.
serialize
import { serialize } from 'postgres-composite'
const { serialize } = require('postgres-composite');
Only named exports; no default export. Both import styles work.
parse and serialize
import { parse, serialize } from 'postgres-composite'
import postgresComposite from 'postgres-composite'
No default export. Must use named imports.

Parses a PostgreSQL composite string into an array of strings/null and serializes an array back to a composite string.

import { parse, serialize } from 'postgres-composite'; // Parse a composite value from PostgreSQL const parsed = [...parse('(1,2,3)')]; console.log(parsed); // ['1', '2', '3'] // Serialize an array of strings and nulls const serialized = serialize(['1', null, ' 3']); console.log(serialized); // '(1,," 3")'
Debug
Known issues
gotchaparse returns an iterable, not an array. You must spread or use Array.from to get an array.
fix
Use [...parse(value)] or Array.from(parse(value)) to get an array.
affects: >=0.0.0
gotchaserialize throws RangeError on empty input (empty iterable).
fix
Ensure the input iterable has at least one element before calling serialize.
affects: >=0.0.0
gotchaparse expects input exactly matching PostgreSQL composite output format; behavior with other formats is undefined.
fix
Only use parse with strings produced by PostgreSQL's composite output routine (e.g., from pg query results).
affects: >=0.0.0
gotchanull values in the composite are returned as null by parse, and serialized as empty fields.
fix
Handle null explicitly; they are distinct from empty strings.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: postgresComposite is not a function
Attempting to default-import the module which has no default export.
fix
Use named imports: import { parse, serialize } from 'postgres-composite'
RangeError: Invalid array length
Calling serialize with an empty array or empty iterable.
fix
Ensure at least one element: serialize(['']) or serialize([null]) if you need one value.
TypeError: parse(...) is not iterable
Forgetting to spread or use Array.from on the iterable returned by parse.
fix
Use [...parse(value)] or Array.from(parse(value)) to iterate.
Upgrade
Version history
0.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
postgres-composite — npm install postgres-composite · libregistry