@hakodb/client 0.5.15 → 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 +30 -27
- package/package.json +3 -3
- package/src/client.ts +47 -0
- package/src/native.ts +35 -0
package/README.md
CHANGED
|
@@ -1,27 +1,30 @@
|
|
|
1
|
-
# hakojs
|
|
2
|
-
|
|
3
|
-
> Part of [**HakoDB**](https://github.com/hakodb/hakodb) — embedded
|
|
4
|
-
|
|
5
|
-
JavaScript/TypeScript SDK for HakoDB:
|
|
6
|
-
high-level client (`client.ts`: `HakoClient`, `HakoConfig`) and native
|
|
7
|
-
FFI bindings over koffi/Bun (`native.ts`). Tauri lives in `@hakodb/tauri`
|
|
8
|
-
(sibling repo [`hakodb/hakotaurits`](https://github.com/hakodb/hakotaurits)).
|
|
9
|
-
|
|
10
|
-
## Compatibility
|
|
11
|
-
|
|
12
|
-
| hakojs | hako core |
|
|
13
|
-
|---|---|
|
|
14
|
-
| 0.5.13 | `cloud_sync` branch / `v0.8.21`+ release asset |
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
# hakojs
|
|
2
|
+
|
|
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
|
+
|
|
5
|
+
JavaScript/TypeScript SDK for HakoDB:
|
|
6
|
+
high-level client (`client.ts`: `HakoClient`, `HakoConfig`) and native
|
|
7
|
+
FFI bindings over koffi/Bun (`native.ts`). Tauri lives in `@hakodb/tauri`
|
|
8
|
+
(sibling repo [`hakodb/hakotaurits`](https://github.com/hakodb/hakotaurits)).
|
|
9
|
+
|
|
10
|
+
## Compatibility
|
|
11
|
+
|
|
12
|
+
| hakojs | hako core |
|
|
13
|
+
|---|---|
|
|
14
|
+
| 0.5.13 | `cloud_sync` branch / `v0.8.21`+ release asset |
|
|
15
|
+
| 0.5.15 | `hakodb v0.12.3` (archive: `relocateDocs`/`load`/`unload`/`unloadedCollections`) |
|
|
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`) |
|
|
18
|
+
|
|
19
|
+
## Setup — native library
|
|
20
|
+
|
|
21
|
+
`native.ts` loads the HakoDB cdylib. Resolution order:
|
|
22
|
+
`loadNativeBindings(explicitPath)` → `HAKODB_LIB_PATH` env →
|
|
23
|
+
`./native/<lib>`. Populate it with the sync script:
|
|
24
|
+
|
|
25
|
+
```powershell
|
|
26
|
+
.\sync-native.ps1 -CoreDir C:\Dev\libs\firelite
|
|
27
|
+
.\sync-native.ps1 -Tag v0.8.21
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Binaries under `native/` are git-ignored.
|
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
|
@@ -16,6 +16,27 @@ export type Primitive =
|
|
|
16
16
|
|
|
17
17
|
export type HakoDocData = { [key: string]: Primitive };
|
|
18
18
|
|
|
19
|
+
/** Distance metric for vector indexes (matches core Metric::from_u32). */
|
|
20
|
+
export type VectorMetric = 'cosine' | 'l2';
|
|
21
|
+
|
|
22
|
+
/** One KNN hit: doc id, exact metric distance, and the doc itself. */
|
|
23
|
+
export interface NearHit {
|
|
24
|
+
id: string;
|
|
25
|
+
score: number;
|
|
26
|
+
doc: HakoDocData;
|
|
27
|
+
}
|
|
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
|
+
|
|
19
40
|
const SERVER_TIMESTAMP_SENTINEL = "__FL_SERVER_TIMESTAMP__";
|
|
20
41
|
|
|
21
42
|
export class HakoReference {
|
|
@@ -773,6 +794,32 @@ export class CollectionReference {
|
|
|
773
794
|
ensureOk(this.client.nativeBindings().createSimpleIndex(this.client.engineHandle(), this.name, field), this.client.nativeBindings(), 'createIndex');
|
|
774
795
|
}
|
|
775
796
|
|
|
797
|
+
async createVectorIndex(field: string, dim: number, metric: VectorMetric = 'cosine'): Promise<void> {
|
|
798
|
+
ensureOk(this.client.nativeBindings().createVectorIndex(this.client.engineHandle(), this.name, field, dim, metric === 'l2' ? 1 : 0), this.client.nativeBindings(), 'createVectorIndex');
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/** KNN search: nearest-first [{id, score, doc}]. Embeddings are stored as binary LE-f32 (insert a Uint8Array of Float32 bytes). */
|
|
802
|
+
async findNear(field: string, query: number[] | Float32Array, k = 10): Promise<NearHit[]> {
|
|
803
|
+
const native = this.client.nativeBindings();
|
|
804
|
+
const vec = query instanceof Float32Array ? query : Float32Array.from(query);
|
|
805
|
+
const json = native.engineFindNear(this.client.engineHandle(), this.name, field, vec, k);
|
|
806
|
+
if (json === null) throw new Error(`findNear failed: ${native.lastError()}`);
|
|
807
|
+
return JSON.parse(json) as NearHit[];
|
|
808
|
+
}
|
|
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
|
+
|
|
776
823
|
async createFtsIndex(field: string): Promise<void> {
|
|
777
824
|
ensureOk(this.client.nativeBindings().createFtsIndex(this.client.engineHandle(), this.name, field), this.client.nativeBindings(), 'createFtsIndex');
|
|
778
825
|
}
|
package/src/native.ts
CHANGED
|
@@ -178,6 +178,19 @@ export interface NativeBindings {
|
|
|
178
178
|
// v0.5.9
|
|
179
179
|
createFtsIndex(engine: Handle, collection: string, field: string): number;
|
|
180
180
|
createSimpleIndex(engine: Handle, collection: string, field: string): number;
|
|
181
|
+
|
|
182
|
+
// Vector KNN (core >=0.13.3): one-shot search returns owned JSON
|
|
183
|
+
// [{id, score, doc}] (null = hard error, e.g. dim mismatch — read
|
|
184
|
+
// lastError); unindexed field returns "[]".
|
|
185
|
+
createVectorIndex(engine: Handle, collection: string, field: string, dim: number, metric: number): number;
|
|
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;
|
|
181
194
|
|
|
182
195
|
// Array API
|
|
183
196
|
arrayNew(): Handle;
|
|
@@ -283,6 +296,10 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
|
|
|
283
296
|
hk_engine_insert_subdoc: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
|
|
284
297
|
hk_engine_get_by_ref: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
|
|
285
298
|
hk_engine_create_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.u32 },
|
|
299
|
+
hk_engine_create_vector_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.u32], returns: FFIType.i32 },
|
|
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 },
|
|
286
303
|
|
|
287
304
|
hk_batch_new: { args: [], returns: FFIType.ptr },
|
|
288
305
|
hk_batch_free: { args: [FFIType.ptr], returns: FFIType.void },
|
|
@@ -567,6 +584,13 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
|
|
|
567
584
|
// Indexing
|
|
568
585
|
createFtsIndex: (engine, collection, field) => symbols.hk_engine_create_fts_index(engine, toC(collection), toC(field)),
|
|
569
586
|
createSimpleIndex: (engine, collection, field) => symbols.hk_engine_create_simple_index(engine, toC(collection), toC(field)),
|
|
587
|
+
createVectorIndex: (engine, collection, field, dim, metric) => symbols.hk_engine_create_vector_index(engine, toC(collection), toC(field), dim, metric),
|
|
588
|
+
// Float32Array crosses as its LE-f32 bytes (bun:ffi takes the typed
|
|
589
|
+
// array as ptr, like docInsertBin takes bytes).
|
|
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)),
|
|
570
594
|
|
|
571
595
|
// Array API
|
|
572
596
|
arrayNew: () => symbols.hk_array_new(),
|
|
@@ -681,6 +705,10 @@ async function createNodeBindings(libPath: string): Promise<NativeBindings> {
|
|
|
681
705
|
hk_engine_insert_subdoc: lib.func('int hk_engine_insert_subdoc(HK_Engine* engine, const char* col, const char* id, const char* sub_col, const char* sub_id, const HK_Doc* doc)'),
|
|
682
706
|
hk_engine_get_by_ref: lib.func('HK_Doc* hk_engine_get_by_ref(HK_Engine* engine, const HK_Doc* doc, const char* field_key)'),
|
|
683
707
|
hk_engine_create_index: lib.func('uint32_t hk_engine_create_index(HK_Engine* engine, const char* collection, const char* fields_json)'),
|
|
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)'),
|
|
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)'),
|
|
684
712
|
|
|
685
713
|
hk_batch_new: lib.func('HK_Batch* hk_batch_new()'),
|
|
686
714
|
hk_batch_free: lib.func('void hk_batch_free(HK_Batch* batch)'),
|
|
@@ -968,6 +996,13 @@ async function createNodeBindings(libPath: string): Promise<NativeBindings> {
|
|
|
968
996
|
// Indexing
|
|
969
997
|
createFtsIndex: (engine, collection, field) => fn.hk_engine_create_fts_index(engine, collection, field),
|
|
970
998
|
createSimpleIndex: (engine, collection, field) => fn.hk_engine_create_simple_index(engine, collection, field),
|
|
999
|
+
createVectorIndex: (engine, collection, field, dim, metric) => fn.hk_engine_create_vector_index(engine, collection, field, dim, metric),
|
|
1000
|
+
// LE-f32 bytes via Buffer over the Float32Array (koffi takes Buffer
|
|
1001
|
+
// for float*, same as docInsertBin takes Buffer for uint8_t*).
|
|
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),
|
|
971
1006
|
|
|
972
1007
|
// Array API
|
|
973
1008
|
arrayNew: () => fn.hk_array_new(),
|