@protobase/query 0.73.4 → 0.74.0

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.
@@ -27,7 +27,7 @@ const tableReltuples = async (db, model) => {
27
27
  */
28
28
  const estimateCount = async (db, model, filter, scope) => {
29
29
  const full = fullModelOf(model, scope);
30
- if (filter || scope?.rowFilter || scope?.parents?.length || full.softDelete || full.tenant || model.softDelete || model.tenant) return planEstimate(db, model, filter, scope);
30
+ if (filter || scope?.rowFilter || scope?.within || scope?.parents?.length || full.softDelete || full.tenant || model.softDelete || model.tenant) return planEstimate(db, model, filter, scope);
31
31
  const reltuples = await tableReltuples(db, model);
32
32
  return reltuples >= 0 ? Math.round(reltuples) : planEstimate(db, model);
33
33
  };
@@ -25,12 +25,13 @@ const hash = (text) => {
25
25
  h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
26
26
  return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
27
27
  };
28
- /** A short fingerprint of the filter a cursor was issued for and the `related()` terms of its scope; empty without either. */
28
+ /** A short fingerprint of the filter a cursor was issued for and what its scope narrows it by; empty without either. */
29
29
  const filterFingerprint = (filter, scope) => {
30
30
  const terms = relatedTerms(scope);
31
- if (terms.length > 0) return hash(canonical({
31
+ if (terms.length > 0 || scope?.within) return hash(canonical({
32
32
  filter,
33
- terms
33
+ terms,
34
+ within: scope?.within
34
35
  }));
35
36
  return filter ? hash(canonical(filter)) : "";
36
37
  };
@@ -91,6 +91,7 @@ const uncoveredColumns = (model, table, query, scope) => {
91
91
  ...columnOf(full.softDelete ?? model.softDelete),
92
92
  ...pinnedColumns(scope?.rowFilter),
93
93
  ...pinnedColumns(query.filter),
94
+ ...pinnedColumns(scope?.within),
94
95
  ...(scope?.parents ?? []).flatMap((read) => read.related ? [read.field.column] : [])
95
96
  ]);
96
97
  const ctx = (use, of = model) => ({
@@ -104,6 +105,7 @@ const uncoveredColumns = (model, table, query, scope) => {
104
105
  ...scope?.rowFilter ? walk(ctx("access", full), scope.rowFilter, pinned, false) : [],
105
106
  ...(scope?.parents ?? []).flatMap((read) => need(ctx(read.related ? "related" : "parent", full), read.field, "btree", pinned)),
106
107
  ...query.filter ? walk(ctx("filter"), query.filter, pinned, false) : [],
108
+ ...scope?.within ? walk(ctx("filter", full), scope.within, pinned, false) : [],
107
109
  ...query.selects ? need(ctx("related", full), query.selects, "btree", pinned) : [],
108
110
  ...sortField ? need(ctx("sort"), sortField, "btree", pinned) : [],
109
111
  ...groupField ? need(ctx("facet"), groupField, "btree", pinned) : []
@@ -30,7 +30,8 @@ const readConditions = (model, scope) => {
30
30
  ...softDelete ? [sql`${softDeleteColumn(full, softDelete)} is null`] : [],
31
31
  ...tenant ? [tenant] : [],
32
32
  ...scope?.rowFilter ? [compileFilter(full, scope.rowFilter)] : [],
33
- ...(scope?.parents ?? []).map(parentCondition)
33
+ ...(scope?.parents ?? []).map(parentCondition),
34
+ ...scope?.within ? [compileFilter(full, scope.within)] : []
34
35
  ];
35
36
  };
36
37
  //#endregion
package/dist/scope.d.ts CHANGED
@@ -10,6 +10,8 @@ import { CheckedFilter, FieldModel, ResourceModel } from "@protobase/schema";
10
10
  * so it may use hidden or non-filterable columns the user's own (restricted) model does not expose.
11
11
  * - `parents`: the relations whose record each row belongs to (`.parent(fk, 'read')`), keeping the rows whose parent the
12
12
  * caller reads; ANDed in with `rowFilter`.
13
+ * - `within`: a filter on the read itself that no method drops, as a facet drops the caller's terms on its field: a list
14
+ * under a record keeps to that record.
13
15
  * - `fullModel`: that full model. Soft delete, tenant and `rowFilter` resolve against it; everything the user
14
16
  * supplies (filter, sort, columns, fields) resolves against the model argument. Defaults to the model argument.
15
17
  */
@@ -18,6 +20,7 @@ export type Scope = {
18
20
  tenantValues?: readonly (string | number)[];
19
21
  rowFilter?: CheckedFilter;
20
22
  parents?: readonly ParentRead[];
23
+ within?: CheckedFilter;
21
24
  fullModel?: ResourceModel;
22
25
  };
23
26
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/query",
3
- "version": "0.73.4",
3
+ "version": "0.74.0",
4
4
  "description": "Kysely list, filter, count and aggregate queries over Protobase resources.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -29,7 +29,7 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@protobase/schema": "0.73.4",
32
+ "@protobase/schema": "0.74.0",
33
33
  "kysely": "^0.29.6"
34
34
  }
35
35
  }