@odla-ai/crm 0.9.0 → 0.9.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/dist/index.d.cts CHANGED
@@ -1159,8 +1159,8 @@ interface TypeSummary {
1159
1159
  }
1160
1160
  /**
1161
1161
  * Tile counts for one record type: total live records, per-stage counts,
1162
- * and the open-task load. Each count is an indexed-where aggregate; a type
1163
- * with S stages costs S+2 calls, issued concurrently.
1162
+ * and the open-task load. A batch-capable database answers all S+2 indexed
1163
+ * counts through one Durable Object request.
1164
1164
  */
1165
1165
  declare function typeSummary(deps: CrmDeps, type: string): Promise<TypeSummary>;
1166
1166
 
package/dist/index.d.ts CHANGED
@@ -1159,8 +1159,8 @@ interface TypeSummary {
1159
1159
  }
1160
1160
  /**
1161
1161
  * Tile counts for one record type: total live records, per-stage counts,
1162
- * and the open-task load. Each count is an indexed-where aggregate; a type
1163
- * with S stages costs S+2 calls, issued concurrently.
1162
+ * and the open-task load. A batch-capable database answers all S+2 indexed
1163
+ * counts through one Durable Object request.
1164
1164
  */
1165
1165
  declare function typeSummary(deps: CrmDeps, type: string): Promise<TypeSummary>;
1166
1166
 
package/dist/index.js CHANGED
@@ -684,10 +684,14 @@ function resultAsWritten(result) {
684
684
 
685
685
  // src/deps.ts
686
686
  function readingAsWritten(db) {
687
+ const batch = db.aggregateBatch;
687
688
  return {
688
689
  query: async (query) => resultAsWritten(await db.query(query)),
689
690
  transact: (ops, opts) => db.transact(ops, opts),
690
- aggregate: (ns, spec, queryOpts) => db.aggregate(ns, spec, queryOpts)
691
+ aggregate: (ns, spec, queryOpts) => db.aggregate(ns, spec, queryOpts),
692
+ ...batch ? {
693
+ aggregateBatch: (requests) => batch.call(db, requests)
694
+ } : {}
691
695
  };
692
696
  }
693
697
  function resolveDeps(deps) {
@@ -1748,13 +1752,29 @@ async function typeSummary(deps, type) {
1748
1752
  where: { and: [{ type }, { archived: false }, ...extra] }
1749
1753
  });
1750
1754
  const stageIds = (def.pipeline?.stages ?? []).map((s) => s.id);
1751
- const [total, openTasks, ...stageCounts] = await Promise.all([
1752
- db.aggregate(CRM_NS.record, { count: true }, live([])),
1753
- db.aggregate(CRM_NS.activity, { count: true }, {
1754
- where: { and: [{ kind: "task" }, { status: { $in: ["open", "waiting"] } }] }
1755
- }),
1756
- ...stageIds.map((stage) => db.aggregate(CRM_NS.record, { count: true }, live([{ stage }])))
1757
- ]);
1755
+ const requests = [
1756
+ { ns: CRM_NS.record, aggregate: { count: true }, opts: live([]) },
1757
+ {
1758
+ ns: CRM_NS.activity,
1759
+ aggregate: { count: true },
1760
+ opts: {
1761
+ where: {
1762
+ and: [{ kind: "task" }, { status: { $in: ["open", "waiting"] } }]
1763
+ }
1764
+ }
1765
+ },
1766
+ ...stageIds.map((stage) => ({
1767
+ ns: CRM_NS.record,
1768
+ aggregate: { count: true },
1769
+ opts: live([{ stage }])
1770
+ }))
1771
+ ];
1772
+ const batch = db.aggregateBatch;
1773
+ const [total, openTasks, ...stageCounts] = batch ? await batch(requests) : await Promise.all(
1774
+ requests.map(
1775
+ (request) => db.aggregate(request.ns, request.aggregate, request.opts)
1776
+ )
1777
+ );
1758
1778
  const stages = {};
1759
1779
  stageIds.forEach((stage, i) => {
1760
1780
  stages[stage] = stageCounts[i]?.count ?? 0;