@dortdb/datetime
v1.0.1
Published
DateTime extension for DortDB
Readme
@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 JavaScriptDate.interval(string)parses an interval string and returns anInterval. 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 aDate, or sums two intervals component-wise and returns anInterval.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 isyear,month,day,hour,minute, orsecond.monthis 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.
