@hakodb/client 0.6.7 → 0.6.9

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@hakodb/client",
3
- "version": "0.6.7",
3
+ "version": "0.6.9",
4
4
  "description": "High-level HakoDB JavaScript/TypeScript client for Node.js and Bun",
5
5
  "repository": {
6
6
  "type": "git",
package/src/client.ts CHANGED
@@ -467,12 +467,35 @@ export class HakoClient {
467
467
  private readonly native: NativeBindings;
468
468
  private readonly engine: unknown;
469
469
  private isClosed = false;
470
+ /** #250: reusable query handles keyed by collection. */
471
+ private readonly queryPool = new Map<string, unknown[]>();
470
472
 
471
473
  private constructor(native: NativeBindings, engine: unknown) {
472
474
  this.native = native;
473
475
  this.engine = engine;
474
476
  }
475
477
 
478
+ /** Acquire a reset handle for `collection`, or create one on first use. */
479
+ acquireQuery(collection: string): unknown {
480
+ const pooled = this.queryPool.get(collection);
481
+ let handle = pooled?.pop();
482
+ if (!handle) {
483
+ handle = this.native.queryNew(collection);
484
+ if (!handle) throw new Error(`queryNew failed: ${this.native.lastError()}`);
485
+ } else {
486
+ ensureOk(this.native.queryReset(handle, collection), this.native, 'queryReset');
487
+ }
488
+ return handle;
489
+ }
490
+
491
+ /** Return a handle to the pool instead of freeing it. */
492
+ releaseQuery(collection: string, handle: unknown): void {
493
+ let pooled = this.queryPool.get(collection);
494
+ if (!pooled) { pooled = []; this.queryPool.set(collection, pooled); }
495
+ if (pooled.length < 32) pooled.push(handle);
496
+ else this.native.queryFree(handle);
497
+ }
498
+
476
499
  static async open(path: string, options?: HakoClientOptions): Promise<HakoClient> {
477
500
  const native = options?.native
478
501
  ?? options?.config?.getNativeBindings()
@@ -1318,8 +1341,7 @@ return this;
1318
1341
 
1319
1342
  private prepareNativeQuery(): unknown {
1320
1343
  const native = this.client.nativeBindings();
1321
- const handle = native.queryNew(this.collection);
1322
- if (!handle) throw new Error(`queryNew failed: ${native.lastError()}`);
1344
+ const handle = this.client.acquireQuery(this.collection);
1323
1345
 
1324
1346
  try {
1325
1347
  for (const filter of this.filters) {
@@ -1427,7 +1449,7 @@ return this;
1427
1449
 
1428
1450
  return handle;
1429
1451
  } catch (err) {
1430
- native.queryFree(handle);
1452
+ this.client.releaseQuery(this.collection, handle);
1431
1453
  throw err;
1432
1454
  }
1433
1455
  }
@@ -1438,7 +1460,7 @@ return this;
1438
1460
  try {
1439
1461
  return parseQueryRows(native.queryExecute(this.client.engineHandle(), handle));
1440
1462
  } finally {
1441
- native.queryFree(handle);
1463
+ this.client.releaseQuery(this.collection, handle);
1442
1464
  }
1443
1465
  }
1444
1466
 
@@ -1452,7 +1474,43 @@ return this;
1452
1474
  try {
1453
1475
  return parseQueryRows(native.queryExecuteJson(this.client.engineHandle(), handle));
1454
1476
  } finally {
1455
- native.queryFree(handle);
1477
+ this.client.releaseQuery(this.collection, handle);
1478
+ }
1479
+ }
1480
+
1481
+ /**
1482
+ * #250: typed rows without JSON materialisation. Uses the view-doc cursor
1483
+ * so each field is pulled by name with its native type. Callers must
1484
+ * know which fields to read (pass `fields`), which is the usual case for
1485
+ * benchmark/UI consumers that only need a few columns.
1486
+ */
1487
+ async getViewRows(fields: string[]): Promise<Array<Record<string, unknown>>> {
1488
+ const native = this.client.nativeBindings();
1489
+ const handle = this.prepareNativeQuery();
1490
+ const rows: Array<Record<string, unknown>> = [];
1491
+ try {
1492
+ native.cursorWalkView(this.client.engineHandle(), handle, (id: string, view: unknown) => {
1493
+ const row: Record<string, unknown> = { id };
1494
+ for (const field of fields) {
1495
+ const has = native.viewHasField(view, field);
1496
+ if (!has) continue;
1497
+ // Type probe via the typed getters; string falls back to getJson-like
1498
+ // path only if no scalar getter has the field.
1499
+ const i = native.viewGetInt(view, field);
1500
+ if (i !== null && i !== undefined) { row[field] = i; continue; }
1501
+ const f = native.viewGetFloat(view, field);
1502
+ if (f !== null && f !== undefined) { row[field] = f; continue; }
1503
+ const b = native.viewGetBool(view, field);
1504
+ if (b !== null && b !== undefined) { row[field] = b; continue; }
1505
+ const str = native.viewGetStr(view, field);
1506
+ if (str !== null && str !== undefined) row[field] = str;
1507
+ }
1508
+ rows.push(row);
1509
+ return true;
1510
+ });
1511
+ return rows;
1512
+ } finally {
1513
+ this.client.releaseQuery(this.collection, handle);
1456
1514
  }
1457
1515
  }
1458
1516
 
@@ -1464,7 +1522,7 @@ return this;
1464
1522
  const json = native.queryExplain(this.client.engineHandle(), handle);
1465
1523
  return json ? (JSON.parse(json) as Record<string, unknown>) : null;
1466
1524
  } finally {
1467
- native.queryFree(handle);
1525
+ this.client.releaseQuery(this.collection, handle);
1468
1526
  }
1469
1527
  }
1470
1528
 
@@ -1481,7 +1539,7 @@ return this;
1481
1539
  if (!rs) throw new Error(`queryExecuteRaw failed: ${native.lastError()}`);
1482
1540
  return new RawQuerySnapshot(this.client, this.collection, rs);
1483
1541
  } finally {
1484
- native.queryFree(handle);
1542
+ this.client.releaseQuery(this.collection, handle);
1485
1543
  }
1486
1544
  }
1487
1545
 
@@ -1493,7 +1551,7 @@ return this;
1493
1551
  if (n < 0) throw new Error(`queryDelete failed: ${native.lastError()}`);
1494
1552
  return n;
1495
1553
  } finally {
1496
- native.queryFree(handle);
1554
+ this.client.releaseQuery(this.collection, handle);
1497
1555
  }
1498
1556
  }
1499
1557
 
@@ -1506,7 +1564,7 @@ return this;
1506
1564
  if (n < 0) throw new Error(`queryDeleteLocal failed: ${native.lastError()}`);
1507
1565
  return n;
1508
1566
  } finally {
1509
- native.queryFree(handle);
1567
+ this.client.releaseQuery(this.collection, handle);
1510
1568
  }
1511
1569
  }
1512
1570
 
@@ -1520,7 +1578,30 @@ return this;
1520
1578
  return n;
1521
1579
  } finally {
1522
1580
  native.docFree(patchDoc);
1523
- native.queryFree(handle);
1581
+ this.client.releaseQuery(this.collection, handle);
1582
+ }
1583
+ }
1584
+
1585
+ /**
1586
+ * #250: run several aggregation ops in ONE FFI round trip. Ops are
1587
+ * `{ count: true }` or `{ sum|avg|min|max: "<field>" }`.
1588
+ * Returns the raw engine map (keys like `count`, `sum_score`).
1589
+ */
1590
+ async aggregate(ops: Array<{ count?: boolean; sum?: string; avg?: string; min?: string; max?: string }>): Promise<Record<string, number>> {
1591
+ const native = this.client.nativeBindings();
1592
+ const handle = this.prepareNativeQuery();
1593
+ try {
1594
+ for (const op of ops) {
1595
+ if (op.count) ensureOk(native.queryAggregateCount(handle), native, 'queryAggregateCount');
1596
+ if (op.sum) ensureOk(native.queryAggregateSum(handle, op.sum), native, 'queryAggregateSum');
1597
+ if (op.avg) ensureOk(native.queryAggregateAvg(handle, op.avg), native, 'queryAggregateAvg');
1598
+ if (op.min) ensureOk(native.queryAggregateMin(handle, op.min), native, 'queryAggregateMin');
1599
+ if (op.max) ensureOk(native.queryAggregateMax(handle, op.max), native, 'queryAggregateMax');
1600
+ }
1601
+ const json = native.queryExecuteAggregation(this.client.engineHandle(), handle);
1602
+ return json ? (JSON.parse(json) as Record<string, number>) : {};
1603
+ } finally {
1604
+ this.client.releaseQuery(this.collection, handle);
1524
1605
  }
1525
1606
  }
1526
1607
 
@@ -1532,7 +1613,7 @@ return this;
1532
1613
  const json = native.queryExecuteAggregation(this.client.engineHandle(), handle);
1533
1614
  return json ? (JSON.parse(json).count || 0) : 0;
1534
1615
  } finally {
1535
- native.queryFree(handle);
1616
+ this.client.releaseQuery(this.collection, handle);
1536
1617
  }
1537
1618
  }
1538
1619
 
@@ -1544,7 +1625,7 @@ return this;
1544
1625
  const json = native.queryExecuteAggregation(this.client.engineHandle(), handle);
1545
1626
  return json ? (JSON.parse(json)[`sum_${field}`] || 0) : 0;
1546
1627
  } finally {
1547
- native.queryFree(handle);
1628
+ this.client.releaseQuery(this.collection, handle);
1548
1629
  }
1549
1630
  }
1550
1631
 
@@ -1556,7 +1637,7 @@ return this;
1556
1637
  const json = native.queryExecuteAggregation(this.client.engineHandle(), handle);
1557
1638
  return json ? (JSON.parse(json)[`avg_${field}`] || 0) : 0;
1558
1639
  } finally {
1559
- native.queryFree(handle);
1640
+ this.client.releaseQuery(this.collection, handle);
1560
1641
  }
1561
1642
  }
1562
1643
 
@@ -1569,7 +1650,7 @@ return this;
1569
1650
  const json = native.queryExecuteAggregation(this.client.engineHandle(), handle);
1570
1651
  return json ? (JSON.parse(json)[`min_${field}`] ?? null) : null;
1571
1652
  } finally {
1572
- native.queryFree(handle);
1653
+ this.client.releaseQuery(this.collection, handle);
1573
1654
  }
1574
1655
  }
1575
1656
 
@@ -1582,7 +1663,7 @@ return this;
1582
1663
  const json = native.queryExecuteAggregation(this.client.engineHandle(), handle);
1583
1664
  return json ? (JSON.parse(json)[`max_${field}`] ?? null) : null;
1584
1665
  } finally {
1585
- native.queryFree(handle);
1666
+ this.client.releaseQuery(this.collection, handle);
1586
1667
  }
1587
1668
  }
1588
1669
 
@@ -1620,6 +1701,14 @@ export class WriteBatch {
1620
1701
  }
1621
1702
  }
1622
1703
 
1704
+ /** #250: one FFI call per document via hk_batch_set_json. */
1705
+ setJSON(docRef: DocumentReference, data: HakoDocData): WriteBatch {
1706
+ this.ensureActive();
1707
+ const json = JSON.stringify(data);
1708
+ ensureOk(this.native.batchSetJSON(this.handle, docRef._collection, docRef._id, json), this.native, 'batchSetJSON');
1709
+ return this;
1710
+ }
1711
+
1623
1712
  delete(docRef: DocumentReference): WriteBatch {
1624
1713
  this.ensureActive();
1625
1714
  ensureOk(this.native.batchDelete(this.handle, docRef._collection, docRef._id), this.native, 'batchDelete');
package/src/native.ts CHANGED
@@ -115,6 +115,8 @@ export interface NativeBindings {
115
115
  queryOffset(query: Handle, offset: number): number;
116
116
  querySelectField(query: Handle, field: string): number;
117
117
  queryExecute(engine: Handle, query: Handle): string | null;
118
+ /** #250: reset a handle in place for reuse (no alloc/free). */
119
+ queryReset(query: Handle, collection: string): number;
118
120
  queryExecuteWithCancel(engine: Handle, query: Handle, cancel: Handle): string | null;
119
121
  cancelNew(): Handle;
120
122
  cancel(cancel: Handle): void;
@@ -495,6 +497,7 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
495
497
  hk_query_offset: { args: [FFIType.ptr, FFIType.usize], returns: FFIType.i32 },
496
498
  hk_query_select_field: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
497
499
  hk_query_execute: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
500
+ hk_query_reset: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
498
501
  hk_query_execute_with_cancel: { args: [FFIType.ptr, FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
499
502
  hk_cancel_new: { args: [], returns: FFIType.ptr },
500
503
  hk_cancel: { args: [FFIType.ptr], returns: FFIType.void },
@@ -606,6 +609,12 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
606
609
  hk_cursor_walk_view: { args: [FFIType.ptr, FFIType.ptr, FFIType.function, FFIType.ptr], returns: FFIType.i64 },
607
610
  hk_rawdoc_bytes: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
608
611
  hk_rawdoc_id: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.ptr },
612
+ hk_view_get: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
613
+ hk_view_get_int: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.bool },
614
+ hk_view_get_float: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.bool },
615
+ hk_view_get_bool: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
616
+ hk_view_field_count: { args: [FFIType.ptr], returns: FFIType.u32 },
617
+ hk_view_has_field: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.bool },
609
618
  hk_view_get_str: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
610
619
  hk_view_get_bytes: { args: [FFIType.ptr, FFIType.cstring, FFIType.ptr], returns: FFIType.ptr },
611
620
  hk_engine_get_json: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
@@ -767,6 +776,7 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
767
776
  queryOffset: (query, offset) => symbols.hk_query_offset(query, offset),
768
777
  querySelectField: (query, field) => symbols.hk_query_select_field(query, toC(field)),
769
778
  queryExecute: (engine, query) => ptrToStringAndFree(symbols.hk_query_execute(engine, query)),
779
+ queryReset: (query, collection) => symbols.hk_query_reset(query, toC(collection)),
770
780
  queryExecuteWithCancel: (engine, query, cancel) => ptrToStringAndFree(symbols.hk_query_execute_with_cancel(engine, query, cancel)),
771
781
  cancelNew: () => symbols.hk_cancel_new(),
772
782
  cancel: (cancel) => symbols.hk_cancel(cancel),
@@ -1163,6 +1173,7 @@ async function createNodeBindings(libPath: string): Promise<NativeBindings> {
1163
1173
  hk_query_offset: lib.func('int hk_query_offset(HK_Query* query, size_t offset)'),
1164
1174
  hk_query_select_field: lib.func('int hk_query_select_field(HK_Query* query, const char* field)'),
1165
1175
  hk_query_execute: lib.func('HeapStr hk_query_execute(HK_Engine* engine, const HK_Query* query)'),
1176
+ hk_query_reset: lib.func('int hk_query_reset(HK_Query* query, const char* collection)'),
1166
1177
  hk_query_execute_with_cancel: lib.func('HeapStr hk_query_execute_with_cancel(HK_Engine* engine, const HK_Query* query, HK_Cancel* cancel)'),
1167
1178
  hk_cancel_new: lib.func('HK_Cancel* hk_cancel_new()'),
1168
1179
  hk_cancel: lib.func('void hk_cancel(HK_Cancel* cancel)'),
@@ -1426,6 +1437,7 @@ async function createNodeBindings(libPath: string): Promise<NativeBindings> {
1426
1437
  queryOffset: (query, offset) => fn.hk_query_offset(query, offset),
1427
1438
  querySelectField: (query, field) => fn.hk_query_select_field(query, field),
1428
1439
  queryExecute: (engine, query) => ptrToStringAndFree(fn.hk_query_execute(engine, query)),
1440
+ queryReset: (query, collection) => fn.hk_query_reset(query, collection),
1429
1441
  queryExecuteWithCancel: (engine, query, cancel) => ptrToStringAndFree(fn.hk_query_execute_with_cancel(engine, query, cancel)),
1430
1442
  cancelNew: () => fn.hk_cancel_new(),
1431
1443
  cancel: (cancel) => fn.hk_cancel(cancel),