@antelopejs/interface-database 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/1.query_types/1.index.md +53 -0
- package/docs/1.query_types/2.table.md +108 -0
- package/docs/1.query_types/3.selection.md +94 -0
- package/docs/1.query_types/4.single_selection.md +121 -0
- package/docs/1.query_types/5.stream.md +237 -0
- package/docs/1.query_types/6.feed.md +82 -0
- package/docs/1.query_types/7.query.md +82 -0
- package/docs/1.query_types/8.datum.md +102 -0
- package/docs/1.query_types/9.valueproxy.md +230 -0
- package/docs/2.operations/1.index.md +34 -0
- package/docs/2.operations/2.database_management.md +151 -0
- package/docs/2.operations/3.table_operations.md +117 -0
- package/docs/2.operations/4.crud.md +215 -0
- package/docs/2.operations/5.indexes.md +110 -0
- package/docs/2.operations/6.filtering.md +176 -0
- package/docs/2.operations/7.lookup.md +137 -0
- package/docs/3.results/1.index.md +38 -0
- package/docs/3.results/2.write_results.md +86 -0
- package/docs/3.results/3.index_changes.md +78 -0
- package/docs/3.results/4.table_changes.md +105 -0
- package/docs/3.results/5.database_changes.md +96 -0
- package/docs/3.results/6.value_changes.md +75 -0
- package/docs/index.md +50 -0
- package/package.json +8 -3
- package/skills/database-interface/SKILL.md +67 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: database-interface
|
|
3
|
+
description: Provides AQL (Antelope Query Language), the database-agnostic, chainable, type-safe query builder of AntelopeJS - schemas, tables, CRUD, filtering, joins, aggregation, and change feeds that run identically on any backend module implementing the interface. Use when code imports "@antelopejs/interface-database" (or its /schema, /selection, /stream, /datum, /query, /valueproxy, /common subpaths), when the task mentions AQL, Schema, SchemaInstance, Table, Selection, SingleSelection, Stream, Datum, ValueProxy, or CROSS_INSTANCE, or when asked to define a database schema, query/insert/update/delete documents, or subscribe to change feeds in an AntelopeJS module.
|
|
4
|
+
category: antelopejs-interface
|
|
5
|
+
tags: [antelopejs, database, query, schema, aql]
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Database interface (AQL)
|
|
9
|
+
|
|
10
|
+
AQL is a lazy, chainable query builder. Every class here (`Schema`, `Table`, `Selection`, `Stream`, `Datum`, `ValueProxy`) is a consumer-side staged object: chaining records stages, nothing touches the database. Apart from schema registration (done once by the `Schema` constructor), the only proxy crossings are execution (`.run()` / `await` / `for await`), which ships the staged query to whatever loaded module implements this interface (e.g. `@antelopejs/mongodb`). Consumers never call `ImplementInterface` for this interface — providing it is the job of database driver modules.
|
|
11
|
+
|
|
12
|
+
## Imports
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
// Root re-exports everything most consumers need:
|
|
16
|
+
import { Schema, SchemaInstance, SchemaDefinition, Table, Selection, SingleSelection, Stream, Datum, Query, ValueProxy, ValueProxyOrValue, InstanceId, CROSS_INSTANCE } from "@antelopejs/interface-database";
|
|
17
|
+
// Types only available via subpath (not re-exported at root):
|
|
18
|
+
import { TableDefinition, IndexDefinition, FieldType } from "@antelopejs/interface-database/schema";
|
|
19
|
+
import { Changes, DeepPartial, InsertOptions, Value, ExtractType } from "@antelopejs/interface-database/common";
|
|
20
|
+
// Each root symbol is also importable from its own subpath (/schema, /selection, /stream, /datum, /query, /valueproxy) — equivalent alternatives, do not combine with the root import for the same symbol.
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Add `@antelopejs/interface-database` to the module's `dependencies`; `@antelopejs/interface-core` is a peer dependency.
|
|
24
|
+
|
|
25
|
+
## Minimal consumption example
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { Schema } from "@antelopejs/interface-database";
|
|
29
|
+
|
|
30
|
+
interface Novel { _id?: string; title: string; pageCount: number; available: boolean; }
|
|
31
|
+
|
|
32
|
+
// Registers the schema with the provider. The generic maps table names to row types.
|
|
33
|
+
const schema = new Schema<{ novels: Novel }>("library", {
|
|
34
|
+
novels: {
|
|
35
|
+
fields: { title: "string", pageCount: "number", available: "boolean" },
|
|
36
|
+
indexes: { pageCount: {}, available: {} },
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const novels = schema.instance().table("novels"); // default instance
|
|
41
|
+
|
|
42
|
+
const ids = await novels.insert({ title: "The Glass Meridian", pageCount: 412, available: true }); // string[] of inserted ids
|
|
43
|
+
const longNovels = await novels
|
|
44
|
+
.filter((n) => n.key("pageCount").ge(300).and(n.key("available")))
|
|
45
|
+
.orderBy("pageCount", "desc"); // await executes: Novel[]
|
|
46
|
+
await novels.get(ids[0]).update({ available: false }); // number of modified docs
|
|
47
|
+
for await (const change of novels.changes()) { // change feed via async iteration
|
|
48
|
+
console.log(change.changeType, change.newValue); // "added" | "removed" | "modified"
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Gotchas
|
|
53
|
+
|
|
54
|
+
- **Lazy everywhere.** No chain call executes anything. Execution happens only via `.run()`, `await` (every `Query` is `PromiseLike`), or async iteration (`for await` / `.cursor()`). This includes `Schema.createInstance()` / `destroyInstance()` / `listInstances()` — they return `Query` objects and do nothing until awaited.
|
|
55
|
+
- **Callbacks receive `ValueProxy`, not values.** Functions passed to `filter`, `map`, `do`, `update(fn)`, `join`, `group` are recorded, then replayed by the backend. Only `ValueProxy` methods (`key`, `eq`, `ne`, `gt/ge/lt/le`, `and/or/not`, `add/sub/mul/div`, string/date/array/object helpers, `ValueProxy.constant`) become part of the query — plain JS (`if`, `===`, `Math.*`, template strings) does not travel. Exception: `group`'s mapper receives a staged `Stream` (the group's documents) as its first parameter and a `ValueProxy` (the index value) second — staged `Stream` methods also travel.
|
|
56
|
+
- **Staged objects are immutable.** Each chained call returns a new object; a prefix like `novels.getAll(true, "available")` can be safely reused for several derived queries.
|
|
57
|
+
- A provider module must be loaded in the project or startup fails with unresolved interface dependencies; queries themselves need no defensive "is the DB ready" checks (calls queue until the provider attaches).
|
|
58
|
+
- `Schema` ids are persistent: changing an id leaves previous data inaccessible. `Schema.get(id)` retrieves a previously constructed schema. `instance()` with no argument is the default instance; pass `CROSS_INSTANCE` to query across all instances.
|
|
59
|
+
- Field types are string tokens (`"string"`, `"number"`, `"boolean"`, `"date"`, `"string[]"`, nested object literals) or a single io-ts codec at the field root — codecs are not mixed inside string-tree records.
|
|
60
|
+
- Range semantics: `Table.between(index, low, high)` and `ValueProxy.during(left, right)` exclude the upper bound.
|
|
61
|
+
- `insert` accepts one document or an array, with `{ conflict: "update" | "replace" }` as options; it resolves to the inserted ids.
|
|
62
|
+
- `cast<U>()` on `Datum`/`Stream`/`ValueProxy` only changes the TypeScript type — no runtime conversion.
|
|
63
|
+
- `update` overloads: pass a plain `DeepPartial<T>` for static values, or a `(val: ValueProxy<T>) => ...` function for computed updates.
|
|
64
|
+
|
|
65
|
+
## Deeper reference
|
|
66
|
+
|
|
67
|
+
Full reference lives in the shipped `.d.ts` files (`dist/`) and in this package's `docs/` tree — `1.query_types` (Table, Selection, SingleSelection, Stream, Feed, Query, Datum, ValueProxy), `2.operations` (schema management, table operations, CRUD, indexes, filtering, lookup), `3.results` (write results, index/table definitions, change events). Do not duplicate those here.
|