@hakodb/client 0.5.21 → 0.6.1
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/README.md +6 -2
- package/examples/node/softdel.ts +70 -0
- package/package.json +1 -1
- package/src/client.ts +417 -1
- package/src/native.ts +523 -3
package/README.md
CHANGED
|
@@ -18,6 +18,10 @@ FFI bindings over koffi/Bun (`native.ts`). Tauri lives in `@hakodb/tauri`
|
|
|
18
18
|
| 0.5.18 | `hakodb v0.15.0` (Phase B: `createEdgeIndex` + `shortestPaths` Dijkstra over `hk_engine_shortest_paths`) |
|
|
19
19
|
| 0.5.19 | `hakodb v0.15.3` (native DLL refresh: ring-only TLS + shared sync framing) |
|
|
20
20
|
| 0.5.20 | `hakodb v0.16.0` (`createCloudSyncServer` room params: `roomName`/`roomKey`) |
|
|
21
|
+
| 0.5.21 | `hakodb v0.16.0` (Bun FFI arity fix) |
|
|
22
|
+
| 0.5.22 | `hakodb v0.16.5` (schemas, versioned reads, quiescence, explain, elemMatch/allEq/near/traverse, raw C-ABI parity) |
|
|
23
|
+
| 0.5.23 | `hakodb v0.18.0` (P2 JSON read paths: `getJson` point + query fast paths, view dotted-path pulls) |
|
|
24
|
+
| 0.5.24 | `hakodb v0.19.0` (`min`/`max` simple aggregation, `null` when no numeric value) |
|
|
21
25
|
|
|
22
26
|
## Setup — native library
|
|
23
27
|
|
|
@@ -26,8 +30,8 @@ FFI bindings over koffi/Bun (`native.ts`). Tauri lives in `@hakodb/tauri`
|
|
|
26
30
|
`./native/<lib>`. Populate it with the sync script:
|
|
27
31
|
|
|
28
32
|
```powershell
|
|
29
|
-
.\sync-native.ps1 -CoreDir C:\Dev\libs\
|
|
30
|
-
.\sync-native.ps1 -Tag v0.
|
|
33
|
+
.\sync-native.ps1 -CoreDir C:\Dev\libs\hakodb
|
|
34
|
+
.\sync-native.ps1 -Tag v0.19.0
|
|
31
35
|
```
|
|
32
36
|
|
|
33
37
|
Binaries under `native/` are git-ignored.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Soft-delete / sweeper / restore smoke (core 0.20.0 FFI sync).
|
|
3
|
+
*
|
|
4
|
+
* node --import tsx examples/node/softdel.ts
|
|
5
|
+
*/
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import os from "node:os";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { HakoClient } from "../../src/index.ts";
|
|
11
|
+
|
|
12
|
+
const repoRoot = path.resolve(
|
|
13
|
+
path.dirname(fileURLToPath(import.meta.url)),
|
|
14
|
+
"..", ".."
|
|
15
|
+
);
|
|
16
|
+
const libraryPath = path.join(repoRoot, "native", "hakodb.dll");
|
|
17
|
+
|
|
18
|
+
async function main(): Promise<void> {
|
|
19
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "hako-softdel-"));
|
|
20
|
+
const db = await HakoClient.open(path.join(dir, "smoke.db"), { libraryPath });
|
|
21
|
+
try {
|
|
22
|
+
await db.set("users", "u1", { name: "Alice", age: 32 });
|
|
23
|
+
|
|
24
|
+
// Default delete is soft since core 0.20.0.
|
|
25
|
+
await db.delete("users", "u1");
|
|
26
|
+
if ((await db.get("users", "u1")).exists) {
|
|
27
|
+
throw new Error("deleted doc must be invisible");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const avail = await db.restoreAvailable();
|
|
31
|
+
if (!Array.isArray(avail) || avail.length !== 1 || avail[0].col !== "users" || avail[0].id !== "u1") {
|
|
32
|
+
throw new Error("restoreAvailable mismatch: " + JSON.stringify(avail));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
await db.restore("users", "u1");
|
|
36
|
+
if (!(await db.get("users", "u1")).exists) {
|
|
37
|
+
throw new Error("restore should bring the doc back");
|
|
38
|
+
}
|
|
39
|
+
if ((await db.restoreAvailable()).length !== 0) {
|
|
40
|
+
throw new Error("record should be consumed after restore");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Force parity: legacy shape retains nothing.
|
|
44
|
+
await db.set("users", "u2", { name: "Bob" });
|
|
45
|
+
await db.deleteEx("users", "u2", true);
|
|
46
|
+
if ((await db.restoreAvailable()).length !== 0) {
|
|
47
|
+
throw new Error("force delete must not retain");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// deleteQueue shares the shape; nothing is due in a fresh engine.
|
|
51
|
+
const queue = await db.deleteQueue();
|
|
52
|
+
if (!Array.isArray(queue) || queue.length !== 0) {
|
|
53
|
+
throw new Error("deleteQueue mismatch: " + JSON.stringify(queue));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Escape hatch works regardless of maintenance mode.
|
|
57
|
+
const swept = await db.sweepNow();
|
|
58
|
+
if (swept < 0) throw new Error("sweepNow failed: " + swept);
|
|
59
|
+
|
|
60
|
+
console.log("soft-delete smoke OK (sweepNow=" + swept + ")");
|
|
61
|
+
} finally {
|
|
62
|
+
await db.close();
|
|
63
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
main().catch((err) => {
|
|
68
|
+
console.error(err);
|
|
69
|
+
process.exit(1);
|
|
70
|
+
});
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -36,6 +36,55 @@ export interface PathHit {
|
|
|
36
36
|
depth: number;
|
|
37
37
|
cost?: number;
|
|
38
38
|
doc: HakoDocData;
|
|
39
|
+
/** Route projections, only present when opted in via PathOptions. */
|
|
40
|
+
path?: string[];
|
|
41
|
+
/** Edge docs as JSON strings; null for anonymous edges. */
|
|
42
|
+
via?: (string | null)[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A predicate: docs whose `field` equals `equals` travel (or stop). */
|
|
46
|
+
export interface PathFilter {
|
|
47
|
+
field: string;
|
|
48
|
+
equals: unknown;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** v0.19 traverse extensions shared by findPathsFull/shortestPathsFull/exportGraph/graphAnalysis. */
|
|
52
|
+
export interface PathOptions {
|
|
53
|
+
direction?: TraverseDirection;
|
|
54
|
+
edge?: string | null;
|
|
55
|
+
via?: string | null;
|
|
56
|
+
maxDepth?: number;
|
|
57
|
+
k?: number;
|
|
58
|
+
includePath?: boolean;
|
|
59
|
+
includeVia?: boolean;
|
|
60
|
+
nodeFilter?: PathFilter;
|
|
61
|
+
edgeType?: PathFilter;
|
|
62
|
+
edgeMatch?: PathFilter;
|
|
63
|
+
/** Shortest paths only: numeric prop name on edge docs (null = uniform 1.0). */
|
|
64
|
+
weight?: string | null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function resolvePathOptions(opts: PathOptions) {
|
|
68
|
+
const f = (p?: PathFilter) => (p ? { field: p.field, json: JSON.stringify(p.equals ?? null) } : { field: null, json: null });
|
|
69
|
+
const node = f(opts.nodeFilter);
|
|
70
|
+
const type = f(opts.edgeType);
|
|
71
|
+
const match = f(opts.edgeMatch);
|
|
72
|
+
return {
|
|
73
|
+
direction: opts.direction === 'incoming' ? 1 : opts.direction === 'both' ? 2 : 0,
|
|
74
|
+
edge: opts.edge ?? null,
|
|
75
|
+
via: opts.via ?? null,
|
|
76
|
+
maxDepth: opts.maxDepth ?? 10,
|
|
77
|
+
k: opts.k ?? 100,
|
|
78
|
+
includePath: opts.includePath ?? false,
|
|
79
|
+
includeVia: opts.includeVia ?? false,
|
|
80
|
+
nodeField: node.field,
|
|
81
|
+
nodeJSON: node.json,
|
|
82
|
+
typeField: type.field,
|
|
83
|
+
typeJSON: type.json,
|
|
84
|
+
matchField: match.field,
|
|
85
|
+
matchJSON: match.json,
|
|
86
|
+
weight: opts.weight ?? null,
|
|
87
|
+
};
|
|
39
88
|
}
|
|
40
89
|
|
|
41
90
|
const SERVER_TIMESTAMP_SENTINEL = "__FL_SERVER_TIMESTAMP__";
|
|
@@ -65,15 +114,35 @@ export interface HakoClientOptions {
|
|
|
65
114
|
// 2. FIXED: Added 'in' to the interface to match the Query class
|
|
66
115
|
export interface QueryConstraint {
|
|
67
116
|
field: string;
|
|
68
|
-
op: '==' | '!=' | '>' | '>=' | '<' | '<=' | 'match' | 'matchPrefix' | 'contains' | 'startsWith' | 'in' | 'not-in' | 'array-contains' | 'array-contains-any';
|
|
117
|
+
op: '==' | '!=' | '>' | '>=' | '<' | '<=' | 'match' | 'matchPrefix' | 'contains' | 'startsWith' | 'in' | 'not-in' | 'array-contains' | 'array-contains-any' | 'elemMatch' | 'arrayAllEq';
|
|
69
118
|
value: any; // Use any because 'in' takes an array
|
|
70
119
|
}
|
|
71
120
|
|
|
121
|
+
/** One element-relative predicate for whereElemMatch (F3). */
|
|
122
|
+
export interface ElemFilter {
|
|
123
|
+
field: string;
|
|
124
|
+
op: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'match' | 'match_prefix' | 'contains' | 'starts_with' | 'in' | 'not_in' | 'array_contains' | 'array_contains_any' | 'array_all_eq';
|
|
125
|
+
value: unknown;
|
|
126
|
+
}
|
|
127
|
+
|
|
72
128
|
export interface QueryOrder {
|
|
73
129
|
field: string;
|
|
74
130
|
ascending: boolean;
|
|
75
131
|
}
|
|
76
132
|
|
|
133
|
+
/** One declared field rule (F8). Kind names match the core wire format. */
|
|
134
|
+
export interface HakoFieldRule {
|
|
135
|
+
name: string;
|
|
136
|
+
kind: 'string' | 'int' | 'float' | 'bool' | 'null' | 'array' | 'map' | 'bytes' | 'timestamp' | 'any';
|
|
137
|
+
required?: boolean;
|
|
138
|
+
default?: unknown;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Per-collection schema declaration (F8). Empty fields = no constraints. */
|
|
142
|
+
export interface HakoSchema {
|
|
143
|
+
fields: HakoFieldRule[];
|
|
144
|
+
}
|
|
145
|
+
|
|
77
146
|
export type Unsubscribe = () => Promise<void>;
|
|
78
147
|
|
|
79
148
|
/**
|
|
@@ -139,6 +208,31 @@ export class HakoConfig {
|
|
|
139
208
|
return this;
|
|
140
209
|
}
|
|
141
210
|
|
|
211
|
+
setGroupCommitIntervalMs(ms: number): this {
|
|
212
|
+
this._native.configSetGroupCommitIntervalMs(this._handle, ms);
|
|
213
|
+
return this;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
setPageCacheCapacity(capacity: number): this {
|
|
217
|
+
this._native.configSetPageCacheCapacity(this._handle, capacity);
|
|
218
|
+
return this;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
setSyncExcluded(...collections: string[]): this {
|
|
222
|
+
ensureOk(this._native.configSetSyncExcluded(this._handle, JSON.stringify(collections)), this._native, 'configSetSyncExcluded');
|
|
223
|
+
return this;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
setLazyCollections(...collections: string[]): this {
|
|
227
|
+
ensureOk(this._native.configSetLazyCollections(this._handle, JSON.stringify(collections)), this._native, 'configSetLazyCollections');
|
|
228
|
+
return this;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
setReplicationCollections(...collections: string[]): this {
|
|
232
|
+
ensureOk(this._native.configSetReplicationCollections(this._handle, JSON.stringify(collections)), this._native, 'configSetReplicationCollections');
|
|
233
|
+
return this;
|
|
234
|
+
}
|
|
235
|
+
|
|
142
236
|
getHandle(): unknown {
|
|
143
237
|
return this._handle;
|
|
144
238
|
}
|
|
@@ -328,6 +422,27 @@ export class ViewDocSnapshot {
|
|
|
328
422
|
return this.client.nativeBindings().viewGetBool(this.handle, field);
|
|
329
423
|
}
|
|
330
424
|
|
|
425
|
+
/** Dotted-path pulls (core 0.18+): `a.b.c` reads without a full decode. */
|
|
426
|
+
getPathInt(path: string): number | bigint | null {
|
|
427
|
+
return this.client.nativeBindings().viewGetPathInt(this.handle, path);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
getPathFloat(path: string): number | null {
|
|
431
|
+
return this.client.nativeBindings().viewGetPathFloat(this.handle, path);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
getPathBool(path: string): boolean | null {
|
|
435
|
+
return this.client.nativeBindings().viewGetPathBool(this.handle, path);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
getPathString(path: string): string | null {
|
|
439
|
+
return this.client.nativeBindings().viewGetPathStr(this.handle, path);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
getPathBytes(path: string): Uint8Array | null {
|
|
443
|
+
return this.client.nativeBindings().viewGetPathBytes(this.handle, path);
|
|
444
|
+
}
|
|
445
|
+
|
|
331
446
|
/** Full decode + inflate. Prefer pulls for sparse reads. */
|
|
332
447
|
async resolve(): Promise<HakoDocData | undefined> {
|
|
333
448
|
const native = this.client.nativeBindings();
|
|
@@ -401,6 +516,51 @@ export class HakoClient {
|
|
|
401
516
|
return json ? JSON.parse(json) : [];
|
|
402
517
|
}
|
|
403
518
|
|
|
519
|
+
/** Declare or replace a collection schema (F8). */
|
|
520
|
+
async setSchema(collection: string, schema: HakoSchema): Promise<void> {
|
|
521
|
+
this.assertOpen();
|
|
522
|
+
ensureOk(this.native.engineSetSchema(this.engine, collection, JSON.stringify(schema)), this.native, 'engineSetSchema');
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** Absent collection returns null, not an error. */
|
|
526
|
+
async getSchema(collection: string): Promise<HakoSchema | null> {
|
|
527
|
+
this.assertOpen();
|
|
528
|
+
const json = this.native.engineGetSchema(this.engine, collection);
|
|
529
|
+
return json ? (JSON.parse(json) as HakoSchema) : null;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
async clearSchema(collection: string): Promise<void> {
|
|
533
|
+
this.assertOpen();
|
|
534
|
+
ensureOk(this.native.engineClearSchema(this.engine, collection), this.native, 'engineClearSchema');
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
async listSchemas(): Promise<Record<string, HakoSchema>> {
|
|
538
|
+
this.assertOpen();
|
|
539
|
+
const json = this.native.engineListSchemas(this.engine);
|
|
540
|
+
return json ? (JSON.parse(json) as Record<string, HakoSchema>) : {};
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/** Read a doc and the commit version it was read at (F5). */
|
|
544
|
+
async getVersioned(collection: string, docId: string): Promise<{ snapshot: DocumentSnapshot; version: bigint } | null> {
|
|
545
|
+
this.assertOpen();
|
|
546
|
+
const { doc, version } = this.native.engineGetVersioned(this.engine, collection, docId);
|
|
547
|
+
if (!doc) return null;
|
|
548
|
+
const json = this.native.docToJson(doc);
|
|
549
|
+
return { snapshot: new DocumentSnapshot(docId, true, parseDocJson(json), doc), version };
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** Wait for background index/blob/maintenance work, bounded by timeoutMs. */
|
|
553
|
+
async awaitQuiescent(timeoutMs: number = 5000): Promise<boolean> {
|
|
554
|
+
this.assertOpen();
|
|
555
|
+
return this.native.engineAwaitQuiescent(this.engine, timeoutMs);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
async quiescenceStatus(): Promise<Record<string, unknown> | null> {
|
|
559
|
+
this.assertOpen();
|
|
560
|
+
const json = this.native.engineQuiescenceStatus(this.engine);
|
|
561
|
+
return json ? (JSON.parse(json) as Record<string, unknown>) : null;
|
|
562
|
+
}
|
|
563
|
+
|
|
404
564
|
createNetSyncer(name: string, roomKey: string): NetSyncer {
|
|
405
565
|
this.assertOpen();
|
|
406
566
|
const handle = this.native.netSyncerNew(this.engine, name, roomKey);
|
|
@@ -574,6 +734,16 @@ export class HakoClient {
|
|
|
574
734
|
return new DocumentSnapshot(docId, true, parseDocJson(json), doc);
|
|
575
735
|
}
|
|
576
736
|
|
|
737
|
+
/**
|
|
738
|
+
* P2 fast path (core 0.18+): JSON straight from the pinned bytes, no
|
|
739
|
+
* owned doc handle. Blob links are resolved, so the shape matches get().
|
|
740
|
+
*/
|
|
741
|
+
async getJson(collection: string, docId: string): Promise<HakoDocData | null> {
|
|
742
|
+
this.assertOpen();
|
|
743
|
+
const json = this.native.engineGetJson(this.engine, collection, docId);
|
|
744
|
+
return json ? (parseDocJson(json) as HakoDocData) : null;
|
|
745
|
+
}
|
|
746
|
+
|
|
577
747
|
/**
|
|
578
748
|
* Borrowed point view (v0.8.11+): lazy numeric/bool pulls with no decode
|
|
579
749
|
* and no JSON. Returns null when missing. Free the snapshot when done.
|
|
@@ -596,6 +766,44 @@ export class HakoClient {
|
|
|
596
766
|
ensureOk(this.native.engineDeleteLocal(this.engine, collection, docId), this.native, 'engineDeleteLocal');
|
|
597
767
|
}
|
|
598
768
|
|
|
769
|
+
/** Delete with an explicit mode: force=true reproduces the legacy
|
|
770
|
+
* tombstone-only shape. Default `delete` is soft since core 0.20.0
|
|
771
|
+
* (restoreable via restore() until the sweeper purges the record). */
|
|
772
|
+
async deleteEx(collection: string, docId: string, force: boolean): Promise<void> {
|
|
773
|
+
this.assertOpen();
|
|
774
|
+
ensureOk(this.native.engineDeleteEx(this.engine, collection, docId, force), this.native, 'engineDeleteEx');
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/** Restore a soft-deleted doc from its retention record (fresh version;
|
|
778
|
+
* consumes the record). Refuses when the id was re-put (cancel rule) or
|
|
779
|
+
* no record exists. */
|
|
780
|
+
async restore(collection: string, docId: string): Promise<void> {
|
|
781
|
+
this.assertOpen();
|
|
782
|
+
ensureOk(this.native.engineRestore(this.engine, collection, docId), this.native, 'engineRestore');
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/** Restoreable records, all collections or one (soft-delete retention queue). */
|
|
786
|
+
async restoreAvailable(collection?: string): Promise<Array<{ col: string; id: string; deleted_at: number; scheduled_at: number; bytes: number }>> {
|
|
787
|
+
this.assertOpen();
|
|
788
|
+
return JSON.parse(this.native.engineRestoreAvailable(this.engine, collection ?? null) ?? '[]');
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
/** Due-for-sweep subset of the retention queue (same shape as restoreAvailable). */
|
|
792
|
+
async deleteQueue(collection?: string): Promise<Array<{ col: string; id: string; deleted_at: number; scheduled_at: number; bytes: number }>> {
|
|
793
|
+
this.assertOpen();
|
|
794
|
+
return JSON.parse(this.native.engineDeleteQueue(this.engine, collection ?? null) ?? '[]');
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/** Run one retention sweep pass now; returns the purged record count. */
|
|
798
|
+
async sweepNow(): Promise<number> {
|
|
799
|
+
this.assertOpen();
|
|
800
|
+
const n = this.native.engineSweepNow(this.engine);
|
|
801
|
+
if (n < 0) {
|
|
802
|
+
throw new Error(`engineSweepNow: ${this.native.lastError()}`);
|
|
803
|
+
}
|
|
804
|
+
return n;
|
|
805
|
+
}
|
|
806
|
+
|
|
599
807
|
/** Mark a collection local-only (never syncs) or rejoin it with `local=false`. */
|
|
600
808
|
async setCollectionLocal(collection: string, local: boolean): Promise<void> {
|
|
601
809
|
this.assertOpen();
|
|
@@ -733,6 +941,21 @@ export class Transaction {
|
|
|
733
941
|
return new DocumentSnapshot(docId, true, parseDocJson(json));
|
|
734
942
|
}
|
|
735
943
|
|
|
944
|
+
/** Batched read: one snapshot, output aligned with ids; missing ids are null. */
|
|
945
|
+
async getMany(collection: string, ids: string[]): Promise<(HakoDocData | null)[]> {
|
|
946
|
+
this.ensureActive();
|
|
947
|
+
const json = this.native.transactionGetMany(this.engine, this.handle, collection, JSON.stringify(ids));
|
|
948
|
+
if (json === null) throw new Error(`transactionGetMany: ${this.native.lastError()}`);
|
|
949
|
+
return JSON.parse(json) as (HakoDocData | null)[];
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/** Merge-patch a doc inside the transaction (F5). */
|
|
953
|
+
patch(collection: string, docId: string, data: HakoDocData): this {
|
|
954
|
+
this.ensureActive();
|
|
955
|
+
ensureOk(this.native.transactionPatch(this.engine, this.handle, collection, docId, JSON.stringify(data)), this.native, 'transactionPatch');
|
|
956
|
+
return this;
|
|
957
|
+
}
|
|
958
|
+
|
|
736
959
|
set(collection: string, docId: string, data: HakoDocData): this {
|
|
737
960
|
this.ensureActive();
|
|
738
961
|
const doc = toNativeDoc(this.native, data);
|
|
@@ -791,6 +1014,28 @@ export class CollectionReference {
|
|
|
791
1014
|
return new Query(this.client, this.name).count();
|
|
792
1015
|
}
|
|
793
1016
|
|
|
1017
|
+
whereElemMatch(field: string, filters: ElemFilter[]): Query {
|
|
1018
|
+
return new Query(this.client, this.name).whereElemMatch(field, filters);
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
whereArrayAllEq(field: string, value: unknown): Query {
|
|
1022
|
+
return new Query(this.client, this.name).whereArrayAllEq(field, value);
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
whereNear(field: string, vec: number[] | Float32Array): Query {
|
|
1026
|
+
return new Query(this.client, this.name).whereNear(field, vec);
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
whereTraverse(
|
|
1030
|
+
startCol: string,
|
|
1031
|
+
startId: string,
|
|
1032
|
+
direction: TraverseDirection = 'outgoing',
|
|
1033
|
+
edge: string | null = null,
|
|
1034
|
+
maxDepth = 3
|
|
1035
|
+
): Query {
|
|
1036
|
+
return new Query(this.client, this.name).whereTraverse(startCol, startId, direction, edge, maxDepth);
|
|
1037
|
+
}
|
|
1038
|
+
|
|
794
1039
|
async createIndex(field: string): Promise<void> {
|
|
795
1040
|
ensureOk(this.client.nativeBindings().createSimpleIndex(this.client.engineHandle(), this.name, field), this.client.nativeBindings(), 'createIndex');
|
|
796
1041
|
}
|
|
@@ -841,6 +1086,72 @@ export class CollectionReference {
|
|
|
841
1086
|
async createCompositeIndex(fields: { field: string; desc?: boolean }[]): Promise<number> {
|
|
842
1087
|
return this.client.createCompositeIndex(this.name, fields);
|
|
843
1088
|
}
|
|
1089
|
+
|
|
1090
|
+
/** Drop indexes (v0.19.6 parity). Composite drops by the id createCompositeIndex returned. */
|
|
1091
|
+
async dropIndex(field: string): Promise<void> {
|
|
1092
|
+
ensureOk(this.client.nativeBindings().dropSimpleIndex(this.client.engineHandle(), this.name, field), this.client.nativeBindings(), 'dropIndex');
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
async dropVectorIndex(field: string): Promise<void> {
|
|
1096
|
+
ensureOk(this.client.nativeBindings().dropVectorIndex(this.client.engineHandle(), this.name, field), this.client.nativeBindings(), 'dropVectorIndex');
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
async dropCompositeIndex(id: number): Promise<void> {
|
|
1100
|
+
ensureOk(this.client.nativeBindings().dropCompositeIndex(this.client.engineHandle(), this.name, id), this.client.nativeBindings(), 'dropCompositeIndex');
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
async dropEdgeIndex(): Promise<void> {
|
|
1104
|
+
ensureOk(this.client.nativeBindings().dropEdgeIndex(this.client.engineHandle(), this.name), this.client.nativeBindings(), 'dropEdgeIndex');
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
async dropFtsIndex(field: string): Promise<void> {
|
|
1108
|
+
ensureOk(this.client.nativeBindings().dropFtsIndex(this.client.engineHandle(), this.name, field), this.client.nativeBindings(), 'dropFtsIndex');
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
async dropTraverseIndex(): Promise<void> {
|
|
1112
|
+
ensureOk(this.client.nativeBindings().dropTraverseIndex(this.client.engineHandle(), this.name), this.client.nativeBindings(), 'dropTraverseIndex');
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
/** HNSW-tuned vector index (m = edges per node, efConstruction = build beam, efSearch = query beam; 0 = engine default). */
|
|
1116
|
+
async createVectorIndexV2(field: string, dim: number, metric: VectorMetric = 'cosine', m = 0, efConstruction = 0, efSearch = 0): Promise<void> {
|
|
1117
|
+
ensureOk(this.client.nativeBindings().createVectorIndexV2(this.client.engineHandle(), this.name, field, dim, metric === 'l2' ? 1 : 0, m, efConstruction, efSearch), this.client.nativeBindings(), 'createVectorIndexV2');
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
/** Graph BFS with the v0.19 extensions: via-edge hops, node/edge predicates, opt-in path/via projections. */
|
|
1121
|
+
async findPathsFull(id: string, opts: PathOptions = {}): Promise<PathHit[]> {
|
|
1122
|
+
const native = this.client.nativeBindings();
|
|
1123
|
+
const o = resolvePathOptions(opts);
|
|
1124
|
+
const json = native.findPathsFull(this.client.engineHandle(), this.name, id, o.direction, o.edge, o.via, o.maxDepth, o.k, o.includePath, o.includeVia, o.nodeField, o.nodeJSON, o.typeField, o.typeJSON, o.matchField, o.matchJSON);
|
|
1125
|
+
if (json === null) throw new Error(`findPathsFull failed: ${native.lastError()}`);
|
|
1126
|
+
return JSON.parse(json) as PathHit[];
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
/** Weighted shortest paths with the same extensions as findPathsFull. */
|
|
1130
|
+
async shortestPathsFull(id: string, via: string | null, opts: PathOptions = {}): Promise<PathHit[]> {
|
|
1131
|
+
const native = this.client.nativeBindings();
|
|
1132
|
+
const o = resolvePathOptions(opts);
|
|
1133
|
+
const json = native.simplePathsFull(this.client.engineHandle(), this.name, id, o.direction, o.edge, via, o.weight ?? null, o.maxDepth, o.k, o.includePath, o.includeVia, o.nodeField, o.nodeJSON, o.typeField, o.typeJSON, o.matchField, o.matchJSON);
|
|
1134
|
+
if (json === null) throw new Error(`shortestPathsFull failed: ${native.lastError()}`);
|
|
1135
|
+
return JSON.parse(json) as PathHit[];
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/** Serialize the collection's ref graph (nodes + edges) as JSON. */
|
|
1139
|
+
async exportGraph(opts: PathOptions = {}): Promise<unknown> {
|
|
1140
|
+
const native = this.client.nativeBindings();
|
|
1141
|
+
const o = resolvePathOptions(opts);
|
|
1142
|
+
const json = native.exportGraph(this.client.engineHandle(), this.name, o.direction, o.edge, o.via, o.nodeField, o.nodeJSON);
|
|
1143
|
+
if (json === null) throw new Error(`exportGraph failed: ${native.lastError()}`);
|
|
1144
|
+
return JSON.parse(json);
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
/** Whole-graph analysis: "components" | "cycle" | "topo". */
|
|
1148
|
+
async graphAnalysis(op: 'components' | 'cycle' | 'topo', opts: PathOptions = {}): Promise<unknown> {
|
|
1149
|
+
const native = this.client.nativeBindings();
|
|
1150
|
+
const o = resolvePathOptions(opts);
|
|
1151
|
+
const json = native.graphAnalysis(this.client.engineHandle(), this.name, op, o.direction, o.edge, o.via, o.nodeField, o.nodeJSON);
|
|
1152
|
+
if (json === null) throw new Error(`graphAnalysis failed: ${native.lastError()}`);
|
|
1153
|
+
return JSON.parse(json);
|
|
1154
|
+
}
|
|
844
1155
|
}
|
|
845
1156
|
|
|
846
1157
|
export class DocumentReference {
|
|
@@ -882,6 +1193,8 @@ private queryLimit?: number;
|
|
|
882
1193
|
private queryOffset?: number;
|
|
883
1194
|
private projection: string[] = [];
|
|
884
1195
|
private deferBlobsDef = false;
|
|
1196
|
+
private near?: { field: string; vec: Float32Array };
|
|
1197
|
+
private traverse?: { startCol: string; startId: string; direction: number; edge: string | null; maxDepth: number };
|
|
885
1198
|
|
|
886
1199
|
constructor(private readonly client: HakoClient, private readonly collection: string) { }
|
|
887
1200
|
|
|
@@ -946,6 +1259,37 @@ this.deferBlobsDef = defer;
|
|
|
946
1259
|
return this;
|
|
947
1260
|
}
|
|
948
1261
|
|
|
1262
|
+
/** Array-of-objects predicate: every element must match all filters (F3). */
|
|
1263
|
+
whereElemMatch(field: string, filters: ElemFilter[]): Query {
|
|
1264
|
+
this.filters.push({ field, op: 'elemMatch', value: filters });
|
|
1265
|
+
return this;
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
/** Array predicate: every element equals value (scalar JSON) (F3). */
|
|
1269
|
+
whereArrayAllEq(field: string, value: unknown): Query {
|
|
1270
|
+
this.filters.push({ field, op: 'arrayAllEq', value });
|
|
1271
|
+
return this;
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
/** KNN marker; the result cap rides limit() (F3). */
|
|
1275
|
+
whereNear(field: string, vec: number[] | Float32Array): Query {
|
|
1276
|
+
this.near = { field, vec: vec instanceof Float32Array ? vec : Float32Array.from(vec) };
|
|
1277
|
+
return this;
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
/** BFS graph marker; edge null = any reference field (F3). */
|
|
1281
|
+
whereTraverse(
|
|
1282
|
+
startCol: string,
|
|
1283
|
+
startId: string,
|
|
1284
|
+
direction: TraverseDirection = 'outgoing',
|
|
1285
|
+
edge: string | null = null,
|
|
1286
|
+
maxDepth = 3
|
|
1287
|
+
): Query {
|
|
1288
|
+
const dir = direction === 'incoming' ? 1 : direction === 'both' ? 2 : 0;
|
|
1289
|
+
this.traverse = { startCol, startId, direction: dir, edge, maxDepth };
|
|
1290
|
+
return this;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
949
1293
|
private prepareNativeQuery(): unknown {
|
|
950
1294
|
const native = this.client.nativeBindings();
|
|
951
1295
|
const handle = native.queryNew(this.collection);
|
|
@@ -1010,6 +1354,12 @@ return this;
|
|
|
1010
1354
|
if (typeof filter.value === 'string') ensureOk(native.queryWhereArrayContainsStr(handle, filter.field, filter.value), native, 'queryWhereArrayContainsStr');
|
|
1011
1355
|
else ensureOk(native.queryWhereArrayContainsInt(handle, filter.field, filter.value), native, 'queryWhereArrayContainsInt');
|
|
1012
1356
|
break;
|
|
1357
|
+
case 'elemMatch':
|
|
1358
|
+
ensureOk(native.queryWhereElemMatch(handle, filter.field, JSON.stringify(filter.value)), native, 'queryWhereElemMatch');
|
|
1359
|
+
break;
|
|
1360
|
+
case 'arrayAllEq':
|
|
1361
|
+
ensureOk(native.queryWhereArrayAllEq(handle, filter.field, JSON.stringify(filter.value)), native, 'queryWhereArrayAllEq');
|
|
1362
|
+
break;
|
|
1013
1363
|
}
|
|
1014
1364
|
}
|
|
1015
1365
|
|
|
@@ -1037,6 +1387,12 @@ return this;
|
|
|
1037
1387
|
ensureOk(native.queryEndBefore(handle, this._endBeforeSnapshot._nativeHandle), native, 'queryEndBefore');
|
|
1038
1388
|
}
|
|
1039
1389
|
|
|
1390
|
+
if (this.near) ensureOk(native.queryWhereNear(handle, this.near.field, this.near.vec), native, 'queryWhereNear');
|
|
1391
|
+
if (this.traverse) {
|
|
1392
|
+
const t = this.traverse;
|
|
1393
|
+
ensureOk(native.queryWhereTraverse(handle, t.startCol, t.startId, t.direction, t.edge, t.maxDepth), native, 'queryWhereTraverse');
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1040
1396
|
if (this.order) ensureOk(native.queryOrderBy(handle, this.order.field, this.order.ascending), native, 'queryOrderBy');
|
|
1041
1397
|
if (this.queryLimit !== undefined) ensureOk(native.queryLimit(handle, this.queryLimit), native, 'queryLimit');
|
|
1042
1398
|
if (this.queryOffset !== undefined) ensureOk(native.queryOffset(handle, this.queryOffset), native, 'queryOffset');
|
|
@@ -1060,6 +1416,32 @@ return this;
|
|
|
1060
1416
|
}
|
|
1061
1417
|
}
|
|
1062
1418
|
|
|
1419
|
+
/**
|
|
1420
|
+
* P2 fast path (core 0.18+): rows serialized from pinned bytes, same
|
|
1421
|
+
* shape as get(). Projections keep the projected executor's shape.
|
|
1422
|
+
*/
|
|
1423
|
+
async getJson(): Promise<HakoDocData[]> {
|
|
1424
|
+
const native = this.client.nativeBindings();
|
|
1425
|
+
const handle = this.prepareNativeQuery();
|
|
1426
|
+
try {
|
|
1427
|
+
return parseQueryRows(native.queryExecuteJson(this.client.engineHandle(), handle));
|
|
1428
|
+
} finally {
|
|
1429
|
+
native.queryFree(handle);
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
/** Query plan JSON without executing the scan (F7). */
|
|
1434
|
+
async explain(): Promise<Record<string, unknown> | null> {
|
|
1435
|
+
const native = this.client.nativeBindings();
|
|
1436
|
+
const handle = this.prepareNativeQuery();
|
|
1437
|
+
try {
|
|
1438
|
+
const json = native.queryExplain(this.client.engineHandle(), handle);
|
|
1439
|
+
return json ? (JSON.parse(json) as Record<string, unknown>) : null;
|
|
1440
|
+
} finally {
|
|
1441
|
+
native.queryFree(handle);
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1063
1445
|
/**
|
|
1064
1446
|
* Raw page: no decode, no JSON. Walk it with count/resolve, page with
|
|
1065
1447
|
* startAfterRaw, free when done. Scan-many/touch-few is the shape this
|
|
@@ -1152,6 +1534,32 @@ return this;
|
|
|
1152
1534
|
}
|
|
1153
1535
|
}
|
|
1154
1536
|
|
|
1537
|
+
/** `null` when no matching document has a numeric `field`. */
|
|
1538
|
+
async min(field: string): Promise<number | null> {
|
|
1539
|
+
const native = this.client.nativeBindings();
|
|
1540
|
+
const handle = this.prepareNativeQuery();
|
|
1541
|
+
try {
|
|
1542
|
+
ensureOk(native.queryAggregateMin(handle, field), native, 'queryAggregateMin');
|
|
1543
|
+
const json = native.queryExecuteAggregation(this.client.engineHandle(), handle);
|
|
1544
|
+
return json ? (JSON.parse(json)[`min_${field}`] ?? null) : null;
|
|
1545
|
+
} finally {
|
|
1546
|
+
native.queryFree(handle);
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
/** `null` when no matching document has a numeric `field`. */
|
|
1551
|
+
async max(field: string): Promise<number | null> {
|
|
1552
|
+
const native = this.client.nativeBindings();
|
|
1553
|
+
const handle = this.prepareNativeQuery();
|
|
1554
|
+
try {
|
|
1555
|
+
ensureOk(native.queryAggregateMax(handle, field), native, 'queryAggregateMax');
|
|
1556
|
+
const json = native.queryExecuteAggregation(this.client.engineHandle(), handle);
|
|
1557
|
+
return json ? (JSON.parse(json)[`max_${field}`] ?? null) : null;
|
|
1558
|
+
} finally {
|
|
1559
|
+
native.queryFree(handle);
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1155
1563
|
onSnapshot(callback: (snapshot: HakoDocData[]) => void): Unsubscribe {
|
|
1156
1564
|
const native = this.client.nativeBindings();
|
|
1157
1565
|
const internalWatcher: WatchCallback = async () => {
|
|
@@ -1192,6 +1600,14 @@ export class WriteBatch {
|
|
|
1192
1600
|
return this;
|
|
1193
1601
|
}
|
|
1194
1602
|
|
|
1603
|
+
/** Batch delete with an explicit mode: 0/soft = restoreable, force = legacy
|
|
1604
|
+
* tombstone-only. batchDelete() keeps legacy/force for frozen contracts. */
|
|
1605
|
+
deleteEx(docRef: DocumentReference, force: boolean): WriteBatch {
|
|
1606
|
+
this.ensureActive();
|
|
1607
|
+
ensureOk(this.native.batchDeleteEx(this.handle, docRef._collection, docRef._id, force), this.native, 'batchDeleteEx');
|
|
1608
|
+
return this;
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1195
1611
|
async commit(): Promise<void> {
|
|
1196
1612
|
this.ensureActive();
|
|
1197
1613
|
ensureOk(this.native.batchCommit(this.client.engineHandle(), this.handle), this.native, 'batchCommit');
|