Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

pg-range

WhoopInc25kMIT1.1.2TypeScript support: included

node-postgres plugin to parse range columns

postgres, parsing, type, conversion, range, timerange, tsrange, tstzrange, daterange

readme

node-pg-range

Range type support for node-postgres.

Usage

Install pg-range into your existing pg adapter:

var pg = require("pg");
require("pg-range").install(pg);

Then make a query that returns range objects!

client.query("SELECT range FROM table", function (result) {
  // result.range = {
  //   begin: Thu Mar 26 2014 17:57:02 GMT-0400 (EDT),
  //   end: Thu Mar 27 2014 17:57:02 GMT-0400 (EDT),
  //   bounds: '[)'
  // }
});

Or make a query that inserts range objects:

var Range = require("pg-range").Range;

client.query("INSERT INTO table VALUES ($1)", [Range(1, 3)]);

See the Postgres "Range Types" documentation for details.

Range objects

pg-range uses stRange.js, a JavaScript range library, to provide a Range class with useful methods, like contains and intersects. See the stRange.js API documentation.

Values of (PostgreSQL) type

  • int4range
  • int8range
  • numrange
  • tsrange
  • tstzrange
  • daterange

will be automatically parsed into instances of Range when the pg-range type adapter is installed.

Warning: When constructing range objects for use in queries, use the pg-range Range constructor as shown above, and not raw stRange.js Range objects. Raw stRange.js objects do not provide a toPostgres method, and will not properly serialize date ranges and empty ranges in queries!

changelog

1.1.2 / 4 November 2022

  • Widen the Endpoint type in the typings to accept any primitive or object with a valueOf method #15. Thanks, @petercpwong!

1.1.1 / 14 October 2021

1.1.0 / 28 March 2019

1.0.1 / 31 January 2019

  • Bump dependencies. This notably silences a npm audit warning #7.
  • Drop support for Node versions 4.x and earlier.

1.0.0 / 24 July 2016

  • Subclass returned Range objects from js-strange objects to provide useful methods for range manipulation and inspection.

0.1.2 / 24 April 2015

  • Explicitly license under MIT.

0.1.1 / 28 July 2014

  • Loosen range validity checking.

0.1.0 / 7 April 2014

  • Support coercing range objects into queries.

0.0.1 / 28 March 2014

  • Initial release.
  • Support returning range object from queries.