Repository navigation
Description
We want to have a layer over the top of collections that allows querying, including joins, between multiple collections.
The proposed implementation is to use D2TS as the query engine: https://github.com/electric-sql/d2ts/
D2TS is an implementation of differential dataflow in Typescript, this allows the query results to be updated incrementally (IVM), enabling very fast updates on each change.
It has a query builder which compiles down the D2 pipeline underneath: https://github.com/electric-sql/d2ts/tree/main/packages/d2ts/src/d2ql#query-builder - it is this api we intend to expose.
Querying would look something like:
import { Employees, Departments } from './collections.ts';
// Build a query - this doesn't start the query
const query = new Query()
.from({ Employees })
.join({
type: 'inner',
from: { Departments },
on: ['@Employees.department_id', '=', '@Departments.id']
})
.select('@id', '@name', '@Departments.name')
.where('@Departments.budget', '>', 1000000)
.buildQuery();
// start a query - returns a ResultCollection
const results = query.start()The ResultCollection is a wrapper of a Store that the results are materialised into, and would have an interface such as:
interface ResultCollection<T> {
state: Store<Map<T>> // The materialised state of the query
stop(): void // Stop the query
// Change stream methods matching those of a base Collection
subscribeChanges(
callback: (changes: Array<ChangeMessage<T>>) => void
): () => void
currentStateAsChanges(): Array<ChangeMessage<T>>
derivedChanges: Derived<Array<ChangeMessage<T>>>
// Other methods and props could be added to provide a write path back via a query result
}A ResultCollection could also be used as a source collection in a further query
Activity
KyleAMathews commentedon May 11, 2025
This seems done