What is postgres-array?
The postgres-array package is a utility for parsing and serializing arrays in the format used by PostgreSQL. It allows Node.js applications to easily convert between JavaScript arrays and the string format used by PostgreSQL for array columns.
What are postgres-array's main functionalities?
Parsing PostgreSQL array strings to JavaScript arrays
This feature allows you to convert a PostgreSQL array string into a JavaScript array. It is useful when you fetch data from a PostgreSQL database and want to work with it in JavaScript.
const parse = require('postgres-array').parse;
let postgresArray = '{1,2,3,4}';
let jsArray = parse(postgresArray);
// jsArray is now [1, 2, 3, 4]
Serializing JavaScript arrays to PostgreSQL array strings
This feature allows you to convert a JavaScript array into a PostgreSQL array string. It is useful when you need to insert or update data in a PostgreSQL database that requires the array format.
const stringify = require('postgres-array').stringify;
let jsArray = [1, 2, 3, 4];
let postgresArray = stringify(jsArray);
// postgresArray is now '{1,2,3,4}'
Other packages similar to postgres-array
pg-promise
pg-promise is a PostgreSQL interface for Node.js. It includes robust support for parameterized queries, named parameters, and automatic conversion of JavaScript objects to PostgreSQL hstore format. It provides more comprehensive database interaction functionalities compared to postgres-array, which focuses solely on array conversion.
postgres-array
Parse postgres array columns
Install
npm install --save postgres-array
Usage
const { parse } = require('postgres-array')
parse('{1,2,3}', (value) => parseInt(value, 10))
API
parse(input, [transform])
-> array
input
Required
Type: string
A Postgres array string.
transform
Type: function
Default: identity
A function that transforms non-null values inserted into the array.
License
MIT © Ben Drucker