Skip to main content

@dortdb/datetime

A date and time extension for DortDB, a modular framework for querying JavaScript data structures. It is also a worked example of how a DortDB extension packages extra functions.

Installation​

npm install @dortdb/core @dortdb/datetime

@dortdb/core is a peer dependency.

Functions​

Every language in the engine can call these functions.

  • now() returns the current timestamp as a JavaScript Date.
  • interval(string) parses an interval string and returns an Interval. It accepts SQL-standard syntax such as '1-2 3 4:5:6' and PostgreSQL syntax such as '1 year 2 months ago'.
  • date.add(Date | Interval, Interval) adds an interval to a date and returns a Date, or sums two intervals component-wise and returns an Interval.
  • date.sub(Date | Interval, Interval) is the same in reverse. It subtracts an interval from a date, or one interval from another.
  • date.extract(Date, string) returns one field of a date. The field is year, month, day, hour, minute, or second. month is 1-based. Any other field name throws.

date.add and date.sub correct for daylight saving time when the date is at midnight and the interval is a whole number of days, so adding '1 day' never shifts the clock time.

The package also exports the Interval type.

Usage​

Pass the extension to the DortDB constructor. This example pairs it with SQL.

import { DortDB } from '@dortdb/core';
import { datetime } from '@dortdb/datetime';
import { defaultRules } from '@dortdb/core/optimizer';
import { SQL } from '@dortdb/lang-sql';

const db = new DortDB({
mainLang: SQL(),
optimizer: { rules: defaultRules },
extensions: [datetime],
});

const result = db.query(`
SELECT date.extract(date.sub(now(), interval('3 years')), 'year') AS y
`);
// result.data -> [{ y: <the current year minus 3> }]

Documentation​

See Functions & Aggregates for how extensions work and how to write your own, or the full docs at filipjezek.github.io/dortdb.

Entry point for the @dortdb/datetime extension. Exports the datetime extension, the Interval type, and the functions it registers: now, interval, date.add, date.sub, and date.extract.

Classes​

Variables​

Functions​