Skip to main content

SQL

@dortdb/lang-sql adds a PostgreSQL-flavored SELECT language to DortDB. It is the natural choice for row-shaped data: arrays of plain JavaScript objects, where each object is a row and each property is a column.

Setup​

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

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

db.registerSource(
['people'],
[
{ id: 1, name: 'Alice', city: 'Prague' },
{ id: 2, name: 'Bob', city: 'Ankara' },
],
);

db.query('SELECT people.name AS name FROM people ORDER BY people.name');

What it targets​

By default SQL reads sources through the ObjectDataAdapter, which treats each element of a registered array as a row and reads columns as object properties. To point SQL at differently-shaped rows, Maps for example, supply a custom adapter. See Data Adapters.

Result column names​

Selected expressions keep their qualifier in the result schema. Selecting t1.id produces a result key "t1.id"; use AS to control the name:

db.query('SELECT t1.id AS id, t2.w AS weight FROM t1 JOIN t2 ON t1.id = t2.id');
// keys: id, weight

Learn more​