@taladb/web 0.1.2 → 0.2.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@taladb/web",
3
- "version": "0.1.2",
4
- "description": "TalaDB WASM bindings for browser (wasm-bindgen + OPFS SharedWorker)",
3
+ "version": "0.2.1",
4
+ "description": "TalaDB WASM bindings document queries and vector search in the browser",
5
5
  "main": "pkg/taladb_web.js",
6
6
  "types": "pkg/taladb_web.d.ts",
7
7
  "exports": {
package/pkg/taladb_web.js CHANGED
@@ -42,6 +42,25 @@ export class CollectionWasm {
42
42
  throw takeFromExternrefTable0(ret[0]);
43
43
  }
44
44
  }
45
+ /**
46
+ * Create a vector index on `field`.
47
+ *
48
+ * `dimensions` — expected vector length.
49
+ * `metric` — optional string: `"cosine"` (default), `"dot"`, or `"euclidean"`.
50
+ * @param {string} field
51
+ * @param {number} dimensions
52
+ * @param {string | null} [metric]
53
+ */
54
+ createVectorIndex(field, dimensions, metric) {
55
+ const ptr0 = passStringToWasm0(field, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
56
+ const len0 = WASM_VECTOR_LEN;
57
+ var ptr1 = isLikeNone(metric) ? 0 : passStringToWasm0(metric, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
58
+ var len1 = WASM_VECTOR_LEN;
59
+ const ret = wasm.collectionwasm_createVectorIndex(this.__wbg_ptr, ptr0, len0, dimensions, ptr1, len1);
60
+ if (ret[1]) {
61
+ throw takeFromExternrefTable0(ret[0]);
62
+ }
63
+ }
45
64
  /**
46
65
  * Delete all matching documents. Returns the count deleted.
47
66
  * @param {any} filter
@@ -78,6 +97,18 @@ export class CollectionWasm {
78
97
  throw takeFromExternrefTable0(ret[0]);
79
98
  }
80
99
  }
100
+ /**
101
+ * Drop a vector index.
102
+ * @param {string} field
103
+ */
104
+ dropVectorIndex(field) {
105
+ const ptr0 = passStringToWasm0(field, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
106
+ const len0 = WASM_VECTOR_LEN;
107
+ const ret = wasm.collectionwasm_dropVectorIndex(this.__wbg_ptr, ptr0, len0);
108
+ if (ret[1]) {
109
+ throw takeFromExternrefTable0(ret[0]);
110
+ }
111
+ }
81
112
  /**
82
113
  * Find documents matching the filter. Returns a JS array of plain objects.
83
114
  * @param {any} filter
@@ -90,6 +121,30 @@ export class CollectionWasm {
90
121
  }
91
122
  return takeFromExternrefTable0(ret[0]);
92
123
  }
124
+ /**
125
+ * Find the `top_k` nearest documents to `query` on a vector index.
126
+ *
127
+ * `filter` — optional pre-filter (same format as `find`). Pass `null` to
128
+ * search across all documents that have the vector field.
129
+ *
130
+ * Returns a JSON array of `{ document: {...}, score: number }` objects.
131
+ * @param {string} field
132
+ * @param {Float32Array} query
133
+ * @param {number} top_k
134
+ * @param {any} filter
135
+ * @returns {any}
136
+ */
137
+ findNearest(field, query, top_k, filter) {
138
+ const ptr0 = passStringToWasm0(field, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
139
+ const len0 = WASM_VECTOR_LEN;
140
+ const ptr1 = passArrayF32ToWasm0(query, wasm.__wbindgen_malloc);
141
+ const len1 = WASM_VECTOR_LEN;
142
+ const ret = wasm.collectionwasm_findNearest(this.__wbg_ptr, ptr0, len0, ptr1, len1, top_k, filter);
143
+ if (ret[2]) {
144
+ throw takeFromExternrefTable0(ret[1]);
145
+ }
146
+ return takeFromExternrefTable0(ret[0]);
147
+ }
93
148
  /**
94
149
  * Find a single document. Returns the document or null.
95
150
  * @param {any} filter
@@ -314,6 +369,25 @@ export class WorkerDB {
314
369
  throw takeFromExternrefTable0(ret[0]);
315
370
  }
316
371
  }
372
+ /**
373
+ * Create a vector index. `metric_str`: `"cosine"` | `"dot"` | `"euclidean"` (default cosine).
374
+ * @param {string} collection
375
+ * @param {string} field
376
+ * @param {number} dimensions
377
+ * @param {string | null} [metric_str]
378
+ */
379
+ createVectorIndex(collection, field, dimensions, metric_str) {
380
+ const ptr0 = passStringToWasm0(collection, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
381
+ const len0 = WASM_VECTOR_LEN;
382
+ const ptr1 = passStringToWasm0(field, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
383
+ const len1 = WASM_VECTOR_LEN;
384
+ var ptr2 = isLikeNone(metric_str) ? 0 : passStringToWasm0(metric_str, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
385
+ var len2 = WASM_VECTOR_LEN;
386
+ const ret = wasm.workerdb_createVectorIndex(this.__wbg_ptr, ptr0, len0, ptr1, len1, dimensions, ptr2, len2);
387
+ if (ret[1]) {
388
+ throw takeFromExternrefTable0(ret[0]);
389
+ }
390
+ }
317
391
  /**
318
392
  * Delete all matching documents. Returns the count deleted.
319
393
  * @param {string} collection
@@ -376,6 +450,21 @@ export class WorkerDB {
376
450
  throw takeFromExternrefTable0(ret[0]);
377
451
  }
378
452
  }
453
+ /**
454
+ * Drop a vector index.
455
+ * @param {string} collection
456
+ * @param {string} field
457
+ */
458
+ dropVectorIndex(collection, field) {
459
+ const ptr0 = passStringToWasm0(collection, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
460
+ const len0 = WASM_VECTOR_LEN;
461
+ const ptr1 = passStringToWasm0(field, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
462
+ const len1 = WASM_VECTOR_LEN;
463
+ const ret = wasm.workerdb_dropVectorIndex(this.__wbg_ptr, ptr0, len0, ptr1, len1);
464
+ if (ret[1]) {
465
+ throw takeFromExternrefTable0(ret[0]);
466
+ }
467
+ }
379
468
  /**
380
469
  * Find documents. Returns a JSON array of document objects.
381
470
  * @param {string} collection
@@ -404,6 +493,41 @@ export class WorkerDB {
404
493
  wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
405
494
  }
406
495
  }
496
+ /**
497
+ * Find nearest neighbours. Returns a JSON string of `[{ document, score }]`.
498
+ * @param {string} collection
499
+ * @param {string} field
500
+ * @param {string} query_json
501
+ * @param {number} top_k
502
+ * @param {string} filter_json
503
+ * @returns {string}
504
+ */
505
+ findNearest(collection, field, query_json, top_k, filter_json) {
506
+ let deferred6_0;
507
+ let deferred6_1;
508
+ try {
509
+ const ptr0 = passStringToWasm0(collection, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
510
+ const len0 = WASM_VECTOR_LEN;
511
+ const ptr1 = passStringToWasm0(field, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
512
+ const len1 = WASM_VECTOR_LEN;
513
+ const ptr2 = passStringToWasm0(query_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
514
+ const len2 = WASM_VECTOR_LEN;
515
+ const ptr3 = passStringToWasm0(filter_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
516
+ const len3 = WASM_VECTOR_LEN;
517
+ const ret = wasm.workerdb_findNearest(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2, top_k, ptr3, len3);
518
+ var ptr5 = ret[0];
519
+ var len5 = ret[1];
520
+ if (ret[3]) {
521
+ ptr5 = 0; len5 = 0;
522
+ throw takeFromExternrefTable0(ret[2]);
523
+ }
524
+ deferred6_0 = ptr5;
525
+ deferred6_1 = len5;
526
+ return getStringFromWasm0(ptr5, len5);
527
+ } finally {
528
+ wasm.__wbindgen_free(deferred6_0, deferred6_1, 1);
529
+ }
530
+ }
407
531
  /**
408
532
  * Find one document. Returns a JSON object or `"null"`.
409
533
  * @param {string} collection
@@ -1119,7 +1243,7 @@ function __wbg_get_imports() {
1119
1243
  return ret;
1120
1244
  },
1121
1245
  __wbindgen_cast_0000000000000001: function(arg0, arg1) {
1122
- // Cast intrinsic for `Closure(Closure { dtor_idx: 204, function: Function { arguments: [Externref], shim_idx: 205, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1246
+ // Cast intrinsic for `Closure(Closure { dtor_idx: 217, function: Function { arguments: [Externref], shim_idx: 218, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1123
1247
  const ret = makeMutClosure(arg0, arg1, wasm.wasm_bindgen__closure__destroy__h1411c0b1433bd547, wasm_bindgen__convert__closures_____invoke__h4bd31e7387ab665d);
1124
1248
  return ret;
1125
1249
  },
@@ -1275,6 +1399,14 @@ function getDataViewMemory0() {
1275
1399
  return cachedDataViewMemory0;
1276
1400
  }
1277
1401
 
1402
+ let cachedFloat32ArrayMemory0 = null;
1403
+ function getFloat32ArrayMemory0() {
1404
+ if (cachedFloat32ArrayMemory0 === null || cachedFloat32ArrayMemory0.byteLength === 0) {
1405
+ cachedFloat32ArrayMemory0 = new Float32Array(wasm.memory.buffer);
1406
+ }
1407
+ return cachedFloat32ArrayMemory0;
1408
+ }
1409
+
1278
1410
  function getStringFromWasm0(ptr, len) {
1279
1411
  ptr = ptr >>> 0;
1280
1412
  return decodeText(ptr, len);
@@ -1336,6 +1468,13 @@ function passArray8ToWasm0(arg, malloc) {
1336
1468
  return ptr;
1337
1469
  }
1338
1470
 
1471
+ function passArrayF32ToWasm0(arg, malloc) {
1472
+ const ptr = malloc(arg.length * 4, 4) >>> 0;
1473
+ getFloat32ArrayMemory0().set(arg, ptr / 4);
1474
+ WASM_VECTOR_LEN = arg.length;
1475
+ return ptr;
1476
+ }
1477
+
1339
1478
  function passStringToWasm0(arg, malloc, realloc) {
1340
1479
  if (realloc === undefined) {
1341
1480
  const buf = cachedTextEncoder.encode(arg);
@@ -1413,6 +1552,7 @@ function __wbg_finalize_init(instance, module) {
1413
1552
  wasm = instance.exports;
1414
1553
  wasmModule = module;
1415
1554
  cachedDataViewMemory0 = null;
1555
+ cachedFloat32ArrayMemory0 = null;
1416
1556
  cachedUint8ArrayMemory0 = null;
1417
1557
  wasm.__wbindgen_start();
1418
1558
  return wasm;
@@ -34,9 +34,12 @@
34
34
  * count { collection, filterJson }
35
35
  * createIndex { collection, field }
36
36
  * dropIndex { collection, field }
37
- * createFtsIndex { collection, field }
38
- * dropFtsIndex { collection, field }
39
- * close {}
37
+ * createFtsIndex { collection, field }
38
+ * dropFtsIndex { collection, field }
39
+ * createVectorIndex { collection, field, dimensions, metric? }
40
+ * dropVectorIndex { collection, field }
41
+ * findNearest { collection, field, queryJson, topK, filterJson? }
42
+ * close {}
40
43
  */
41
44
 
42
45
  // ---------------------------------------------------------------------------
@@ -150,6 +153,23 @@ async function dispatch(op, args) {
150
153
  db.dropFtsIndex(args.collection, args.field);
151
154
  return null;
152
155
 
156
+ case 'createVectorIndex':
157
+ db.createVectorIndex(args.collection, args.field, args.dimensions, args.metric ?? null);
158
+ return null;
159
+
160
+ case 'dropVectorIndex':
161
+ db.dropVectorIndex(args.collection, args.field);
162
+ return null;
163
+
164
+ case 'findNearest':
165
+ return db.findNearest(
166
+ args.collection,
167
+ args.field,
168
+ args.queryJson,
169
+ args.topK,
170
+ args.filterJson ?? 'null',
171
+ );
172
+
153
173
  case 'close':
154
174
  db = null;
155
175
  return null;