@hakodb/client 0.5.16 → 0.5.17
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 +2 -1
- package/package.json +3 -3
- package/src/client.ts +24 -0
- package/src/native.ts +17 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# hakojs
|
|
2
2
|
|
|
3
|
-
> Part of [**HakoDB**](https://github.com/hakodb/hakodb) — embedded
|
|
3
|
+
> Part of [**HakoDB**](https://github.com/hakodb/hakodb) — embedded document DB with native vector search and multi-plane sync, written in Rust. The engine + C ABI live in `hakodb/hakodb`; this repo holds the JavaScript/TypeScript SDK.
|
|
4
4
|
|
|
5
5
|
JavaScript/TypeScript SDK for HakoDB:
|
|
6
6
|
high-level client (`client.ts`: `HakoClient`, `HakoConfig`) and native
|
|
@@ -14,6 +14,7 @@ FFI bindings over koffi/Bun (`native.ts`). Tauri lives in `@hakodb/tauri`
|
|
|
14
14
|
| 0.5.13 | `cloud_sync` branch / `v0.8.21`+ release asset |
|
|
15
15
|
| 0.5.15 | `hakodb v0.12.3` (archive: `relocateDocs`/`load`/`unload`/`unloadedCollections`) |
|
|
16
16
|
| 0.5.16 | `hakodb v0.13.3` (`createVectorIndex` + `findNear` KNN over `hk_engine_find_near`) |
|
|
17
|
+
| 0.5.17 | `hakodb v0.14.0` (`createTraverseIndex` + `findPaths` graph BFS over `hk_engine_find_paths`) |
|
|
17
18
|
|
|
18
19
|
## Setup — native library
|
|
19
20
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hakodb/client",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.17",
|
|
4
4
|
"description": "High-level HakoDB JavaScript/TypeScript client for Node.js and Bun",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"hakodb",
|
|
15
|
-
"firestore",
|
|
16
15
|
"embedded",
|
|
17
16
|
"database",
|
|
18
|
-
"ffi"
|
|
17
|
+
"ffi",
|
|
18
|
+
"vector-search"
|
|
19
19
|
],
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=18"
|
package/src/client.ts
CHANGED
|
@@ -26,6 +26,17 @@ export interface NearHit {
|
|
|
26
26
|
doc: HakoDocData;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
/** Traversal direction (matches core TraverseDir::from_u32). */
|
|
30
|
+
export type TraverseDirection = 'outgoing' | 'incoming' | 'both';
|
|
31
|
+
|
|
32
|
+
/** One graph hit: collection + id (paths cross collections), BFS depth, doc. */
|
|
33
|
+
export interface PathHit {
|
|
34
|
+
collection: string;
|
|
35
|
+
id: string;
|
|
36
|
+
depth: number;
|
|
37
|
+
doc: HakoDocData;
|
|
38
|
+
}
|
|
39
|
+
|
|
29
40
|
const SERVER_TIMESTAMP_SENTINEL = "__FL_SERVER_TIMESTAMP__";
|
|
30
41
|
|
|
31
42
|
export class HakoReference {
|
|
@@ -796,6 +807,19 @@ export class CollectionReference {
|
|
|
796
807
|
return JSON.parse(json) as NearHit[];
|
|
797
808
|
}
|
|
798
809
|
|
|
810
|
+
async createTraverseIndex(): Promise<void> {
|
|
811
|
+
ensureOk(this.client.nativeBindings().createTraverseIndex(this.client.engineHandle(), this.name), this.client.nativeBindings(), 'createTraverseIndex');
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
/** Graph BFS from a doc in this collection: [{collection, id, depth, doc}]. edge null = any ref field. */
|
|
815
|
+
async findPaths(id: string, direction: TraverseDirection = 'outgoing', edge: string | null = null, maxDepth = 10, k = 100): Promise<PathHit[]> {
|
|
816
|
+
const native = this.client.nativeBindings();
|
|
817
|
+
const dir = direction === 'incoming' ? 1 : direction === 'both' ? 2 : 0;
|
|
818
|
+
const json = native.engineFindPaths(this.client.engineHandle(), this.name, id, dir, edge, maxDepth, k);
|
|
819
|
+
if (json === null) throw new Error(`findPaths failed: ${native.lastError()}`);
|
|
820
|
+
return JSON.parse(json) as PathHit[];
|
|
821
|
+
}
|
|
822
|
+
|
|
799
823
|
async createFtsIndex(field: string): Promise<void> {
|
|
800
824
|
ensureOk(this.client.nativeBindings().createFtsIndex(this.client.engineHandle(), this.name, field), this.client.nativeBindings(), 'createFtsIndex');
|
|
801
825
|
}
|
package/src/native.ts
CHANGED
|
@@ -184,6 +184,13 @@ export interface NativeBindings {
|
|
|
184
184
|
// lastError); unindexed field returns "[]".
|
|
185
185
|
createVectorIndex(engine: Handle, collection: string, field: string, dim: number, metric: number): number;
|
|
186
186
|
engineFindNear(engine: Handle, collection: string, field: string, vec: Float32Array, k: number): string | null;
|
|
187
|
+
|
|
188
|
+
// Graph traversal (core >=0.14.0): one-shot BFS returns owned JSON
|
|
189
|
+
// [{collection, id, depth, doc}] (null = hard error, e.g. incoming
|
|
190
|
+
// into untracked — read lastError); missing anchor returns "[]".
|
|
191
|
+
// direction 0/1/2 = outgoing/incoming/both; edge null = any ref field.
|
|
192
|
+
createTraverseIndex(engine: Handle, collection: string): number;
|
|
193
|
+
engineFindPaths(engine: Handle, collection: string, id: string, direction: number, edge: string | null, maxDepth: number, k: number): string | null;
|
|
187
194
|
|
|
188
195
|
// Array API
|
|
189
196
|
arrayNew(): Handle;
|
|
@@ -291,6 +298,8 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
|
|
|
291
298
|
hk_engine_create_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.u32 },
|
|
292
299
|
hk_engine_create_vector_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.u32], returns: FFIType.i32 },
|
|
293
300
|
hk_engine_find_near: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr, FFIType.usize, FFIType.usize], returns: FFIType.ptr },
|
|
301
|
+
hk_engine_create_traverse_index: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
|
|
302
|
+
hk_engine_find_paths: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.cstring, FFIType.usize, FFIType.usize], returns: FFIType.ptr },
|
|
294
303
|
|
|
295
304
|
hk_batch_new: { args: [], returns: FFIType.ptr },
|
|
296
305
|
hk_batch_free: { args: [FFIType.ptr], returns: FFIType.void },
|
|
@@ -579,6 +588,9 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
|
|
|
579
588
|
// Float32Array crosses as its LE-f32 bytes (bun:ffi takes the typed
|
|
580
589
|
// array as ptr, like docInsertBin takes bytes).
|
|
581
590
|
engineFindNear: (engine, collection, field, vec, k) => ptrToStringAndFree(symbols.hk_engine_find_near(engine, toC(collection), toC(field), vec, vec.length, k)),
|
|
591
|
+
createTraverseIndex: (engine, collection) => symbols.hk_engine_create_traverse_index(engine, toC(collection)),
|
|
592
|
+
// Null edge crosses as NULL (any ref field); bun:ffi takes null for ptr/cstring.
|
|
593
|
+
engineFindPaths: (engine, collection, id, direction, edge, maxDepth, k) => ptrToStringAndFree(symbols.hk_engine_find_paths(engine, toC(collection), toC(id), direction, edge === null ? null : toC(edge), maxDepth, k)),
|
|
582
594
|
|
|
583
595
|
// Array API
|
|
584
596
|
arrayNew: () => symbols.hk_array_new(),
|
|
@@ -695,6 +707,8 @@ async function createNodeBindings(libPath: string): Promise<NativeBindings> {
|
|
|
695
707
|
hk_engine_create_index: lib.func('uint32_t hk_engine_create_index(HK_Engine* engine, const char* collection, const char* fields_json)'),
|
|
696
708
|
hk_engine_create_vector_index: lib.func('int hk_engine_create_vector_index(HK_Engine* engine, const char* collection, const char* field, uint32_t dim, uint32_t metric)'),
|
|
697
709
|
hk_engine_find_near: lib.func('HeapStr hk_engine_find_near(HK_Engine* engine, const char* collection, const char* field, const float* vec, size_t len, size_t k)'),
|
|
710
|
+
hk_engine_create_traverse_index: lib.func('int hk_engine_create_traverse_index(HK_Engine* engine, const char* collection)'),
|
|
711
|
+
hk_engine_find_paths: lib.func('HeapStr hk_engine_find_paths(HK_Engine* engine, const char* collection, const char* doc_id, uint32_t direction, const char* edge, size_t max_depth, size_t k)'),
|
|
698
712
|
|
|
699
713
|
hk_batch_new: lib.func('HK_Batch* hk_batch_new()'),
|
|
700
714
|
hk_batch_free: lib.func('void hk_batch_free(HK_Batch* batch)'),
|
|
@@ -986,6 +1000,9 @@ async function createNodeBindings(libPath: string): Promise<NativeBindings> {
|
|
|
986
1000
|
// LE-f32 bytes via Buffer over the Float32Array (koffi takes Buffer
|
|
987
1001
|
// for float*, same as docInsertBin takes Buffer for uint8_t*).
|
|
988
1002
|
engineFindNear: (engine, collection, field, vec, k) => fn.hk_engine_find_near(engine, collection, field, Buffer.from(vec.buffer, vec.byteOffset, vec.byteLength), vec.length, k),
|
|
1003
|
+
createTraverseIndex: (engine, collection) => fn.hk_engine_create_traverse_index(engine, collection),
|
|
1004
|
+
// Null edge crosses as NULL (koffi takes null for char*).
|
|
1005
|
+
engineFindPaths: (engine, collection, id, direction, edge, maxDepth, k) => fn.hk_engine_find_paths(engine, collection, id, direction, edge, maxDepth, k),
|
|
989
1006
|
|
|
990
1007
|
// Array API
|
|
991
1008
|
arrayNew: () => fn.hk_array_new(),
|