@hakodb/client 0.5.15 → 0.5.16
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 +29 -27
- package/package.json +1 -1
- package/src/client.ts +23 -0
- package/src/native.ts +18 -0
package/README.md
CHANGED
|
@@ -1,27 +1,29 @@
|
|
|
1
|
-
# hakojs
|
|
2
|
-
|
|
3
|
-
> Part of [**HakoDB**](https://github.com/hakodb/hakodb) — embedded Firestore-style document DB 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
|
-
|
|
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 Firestore-style document DB 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
|
+
|
|
18
|
+
## Setup — native library
|
|
19
|
+
|
|
20
|
+
`native.ts` loads the HakoDB cdylib. Resolution order:
|
|
21
|
+
`loadNativeBindings(explicitPath)` → `HAKODB_LIB_PATH` env →
|
|
22
|
+
`./native/<lib>`. Populate it with the sync script:
|
|
23
|
+
|
|
24
|
+
```powershell
|
|
25
|
+
.\sync-native.ps1 -CoreDir C:\Dev\libs\firelite
|
|
26
|
+
.\sync-native.ps1 -Tag v0.8.21
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Binaries under `native/` are git-ignored.
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -16,6 +16,16 @@ 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
|
+
|
|
19
29
|
const SERVER_TIMESTAMP_SENTINEL = "__FL_SERVER_TIMESTAMP__";
|
|
20
30
|
|
|
21
31
|
export class HakoReference {
|
|
@@ -773,6 +783,19 @@ export class CollectionReference {
|
|
|
773
783
|
ensureOk(this.client.nativeBindings().createSimpleIndex(this.client.engineHandle(), this.name, field), this.client.nativeBindings(), 'createIndex');
|
|
774
784
|
}
|
|
775
785
|
|
|
786
|
+
async createVectorIndex(field: string, dim: number, metric: VectorMetric = 'cosine'): Promise<void> {
|
|
787
|
+
ensureOk(this.client.nativeBindings().createVectorIndex(this.client.engineHandle(), this.name, field, dim, metric === 'l2' ? 1 : 0), this.client.nativeBindings(), 'createVectorIndex');
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/** KNN search: nearest-first [{id, score, doc}]. Embeddings are stored as binary LE-f32 (insert a Uint8Array of Float32 bytes). */
|
|
791
|
+
async findNear(field: string, query: number[] | Float32Array, k = 10): Promise<NearHit[]> {
|
|
792
|
+
const native = this.client.nativeBindings();
|
|
793
|
+
const vec = query instanceof Float32Array ? query : Float32Array.from(query);
|
|
794
|
+
const json = native.engineFindNear(this.client.engineHandle(), this.name, field, vec, k);
|
|
795
|
+
if (json === null) throw new Error(`findNear failed: ${native.lastError()}`);
|
|
796
|
+
return JSON.parse(json) as NearHit[];
|
|
797
|
+
}
|
|
798
|
+
|
|
776
799
|
async createFtsIndex(field: string): Promise<void> {
|
|
777
800
|
ensureOk(this.client.nativeBindings().createFtsIndex(this.client.engineHandle(), this.name, field), this.client.nativeBindings(), 'createFtsIndex');
|
|
778
801
|
}
|
package/src/native.ts
CHANGED
|
@@ -178,6 +178,12 @@ 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;
|
|
181
187
|
|
|
182
188
|
// Array API
|
|
183
189
|
arrayNew(): Handle;
|
|
@@ -283,6 +289,8 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
|
|
|
283
289
|
hk_engine_insert_subdoc: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.cstring, FFIType.ptr], returns: FFIType.i32 },
|
|
284
290
|
hk_engine_get_by_ref: { args: [FFIType.ptr, FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
|
|
285
291
|
hk_engine_create_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.u32 },
|
|
292
|
+
hk_engine_create_vector_index: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.u32, FFIType.u32], returns: FFIType.i32 },
|
|
293
|
+
hk_engine_find_near: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring, FFIType.ptr, FFIType.usize, FFIType.usize], returns: FFIType.ptr },
|
|
286
294
|
|
|
287
295
|
hk_batch_new: { args: [], returns: FFIType.ptr },
|
|
288
296
|
hk_batch_free: { args: [FFIType.ptr], returns: FFIType.void },
|
|
@@ -567,6 +575,10 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
|
|
|
567
575
|
// Indexing
|
|
568
576
|
createFtsIndex: (engine, collection, field) => symbols.hk_engine_create_fts_index(engine, toC(collection), toC(field)),
|
|
569
577
|
createSimpleIndex: (engine, collection, field) => symbols.hk_engine_create_simple_index(engine, toC(collection), toC(field)),
|
|
578
|
+
createVectorIndex: (engine, collection, field, dim, metric) => symbols.hk_engine_create_vector_index(engine, toC(collection), toC(field), dim, metric),
|
|
579
|
+
// Float32Array crosses as its LE-f32 bytes (bun:ffi takes the typed
|
|
580
|
+
// array as ptr, like docInsertBin takes bytes).
|
|
581
|
+
engineFindNear: (engine, collection, field, vec, k) => ptrToStringAndFree(symbols.hk_engine_find_near(engine, toC(collection), toC(field), vec, vec.length, k)),
|
|
570
582
|
|
|
571
583
|
// Array API
|
|
572
584
|
arrayNew: () => symbols.hk_array_new(),
|
|
@@ -681,6 +693,8 @@ async function createNodeBindings(libPath: string): Promise<NativeBindings> {
|
|
|
681
693
|
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
694
|
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
695
|
hk_engine_create_index: lib.func('uint32_t hk_engine_create_index(HK_Engine* engine, const char* collection, const char* fields_json)'),
|
|
696
|
+
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
|
+
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)'),
|
|
684
698
|
|
|
685
699
|
hk_batch_new: lib.func('HK_Batch* hk_batch_new()'),
|
|
686
700
|
hk_batch_free: lib.func('void hk_batch_free(HK_Batch* batch)'),
|
|
@@ -968,6 +982,10 @@ async function createNodeBindings(libPath: string): Promise<NativeBindings> {
|
|
|
968
982
|
// Indexing
|
|
969
983
|
createFtsIndex: (engine, collection, field) => fn.hk_engine_create_fts_index(engine, collection, field),
|
|
970
984
|
createSimpleIndex: (engine, collection, field) => fn.hk_engine_create_simple_index(engine, collection, field),
|
|
985
|
+
createVectorIndex: (engine, collection, field, dim, metric) => fn.hk_engine_create_vector_index(engine, collection, field, dim, metric),
|
|
986
|
+
// LE-f32 bytes via Buffer over the Float32Array (koffi takes Buffer
|
|
987
|
+
// for float*, same as docInsertBin takes Buffer for uint8_t*).
|
|
988
|
+
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),
|
|
971
989
|
|
|
972
990
|
// Array API
|
|
973
991
|
arrayNew: () => fn.hk_array_new(),
|