@vantreeseba/drizzle-graphql 10.0.1 → 10.0.2

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.cjs CHANGED
@@ -1537,11 +1537,44 @@ const extractUniqueKeyFilter = (table, tableName, fieldName, members, value) =>
1537
1537
  };
1538
1538
  //#endregion
1539
1539
  //#region src/util/builders/common/relation-filters.ts
1540
- /** Narrows the build-scoped relation filter context to the table a resolver is filtering. */
1541
- const relationFilterCtx = (base, tableKey) => base ? {
1542
- ...base,
1543
- tableKey
1544
- } : void 0;
1540
+ /**
1541
+ * The narrowed contexts already handed out, keyed by the build-scoped base and then by
1542
+ * table. The base is created once per `buildSchema` and the result depends on nothing else,
1543
+ * so every call for one table produces the same value — a `WeakMap` on the base keeps them
1544
+ * for exactly as long as that build is reachable.
1545
+ */
1546
+ const narrowedCtxCache = /* @__PURE__ */ new WeakMap();
1547
+ /**
1548
+ * Narrows the build-scoped relation filter context to the table a resolver is filtering.
1549
+ *
1550
+ * Cached rather than rebuilt: the base carries the build's whole `tables` and `relationMap`,
1551
+ * and the spread copied both on every call — three per parent row in the lazy relation
1552
+ * resolver, one per parent row in the relation aggregate, and up to four per select, several
1553
+ * of them inside callbacks drizzle invokes lazily.
1554
+ *
1555
+ * The one field written after the fact is `aliases`, the counter that keeps subquery aliases
1556
+ * apart ({@link extractRelationFilter}). Sharing it is what makes the caching safe rather
1557
+ * than a hazard: the counter now spans the process instead of restarting per extraction, and
1558
+ * aliases only ever have to be unique *within* one statement, which a number that never
1559
+ * repeats gives more surely than one that restarts.
1560
+ */
1561
+ const relationFilterCtx = (base, tableKey) => {
1562
+ if (!base) return;
1563
+ let byTable = narrowedCtxCache.get(base);
1564
+ if (!byTable) {
1565
+ byTable = /* @__PURE__ */ new Map();
1566
+ narrowedCtxCache.set(base, byTable);
1567
+ }
1568
+ let ctx = byTable.get(tableKey);
1569
+ if (!ctx) {
1570
+ ctx = {
1571
+ ...base,
1572
+ tableKey
1573
+ };
1574
+ byTable.set(tableKey, ctx);
1575
+ }
1576
+ return ctx;
1577
+ };
1545
1578
  /**
1546
1579
  * Correlates the parent row with the aliased target table using the relation's own join
1547
1580
  * columns. Columns are matched by SQL name rather than object identity so this also works when