@protobase/query 0.73.1 → 0.73.3
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/cursor.js +6 -6
- package/dist/filter-fingerprint.js +16 -2
- package/dist/filter-requirements.js +2 -1
- package/dist/index-coverage.d.ts +8 -5
- package/dist/index-coverage.js +7 -3
- package/dist/list-query.js +2 -2
- package/dist/read-conditions.d.ts +3 -2
- package/dist/read-conditions.js +7 -5
- package/dist/scope.d.ts +6 -0
- package/dist/scroll-anchors.js +1 -1
- package/dist/scroll-exact.js +1 -1
- package/package.json +2 -2
package/dist/cursor.js
CHANGED
|
@@ -7,12 +7,12 @@ const isStringList = (value) => Array.isArray(value) && value.every((item) => ty
|
|
|
7
7
|
const isCellList = (value) => Array.isArray(value) && value.every((item) => item === null || typeof item === "string");
|
|
8
8
|
/**
|
|
9
9
|
* Opaque cursor: the sort signature plus the row's sort values as text (so timestamps and bigints round-trip exactly,
|
|
10
|
-
* null for NULL values), and a fingerprint of the filter when there
|
|
11
|
-
* came from (AIP-158). The JSON is UTF-8 encoded before base64url, so any text value survives.
|
|
10
|
+
* null for NULL values), and a fingerprint of the filter and its `related()` terms when there are any, so a cursor only
|
|
11
|
+
* continues the list it came from (AIP-158). The JSON is UTF-8 encoded before base64url, so any text value survives.
|
|
12
12
|
* `values` may be a prefix of the sort keys; such a boundary cursor is produced by scroll anchors.
|
|
13
13
|
*/
|
|
14
|
-
const encodeCursor = (keys, values, filter) => {
|
|
15
|
-
const fingerprint = filterFingerprint(filter);
|
|
14
|
+
const encodeCursor = (keys, values, filter, scope) => {
|
|
15
|
+
const fingerprint = filterFingerprint(filter, scope);
|
|
16
16
|
const payload = {
|
|
17
17
|
s: sortSignature(keys).slice(0, values.length),
|
|
18
18
|
v: values,
|
|
@@ -20,12 +20,12 @@ const encodeCursor = (keys, values, filter) => {
|
|
|
20
20
|
};
|
|
21
21
|
return toBase64Url(JSON.stringify(payload));
|
|
22
22
|
};
|
|
23
|
-
const decodeCursor = (cursor, keys, filter) => {
|
|
23
|
+
const decodeCursor = (cursor, keys, filter, scope) => {
|
|
24
24
|
const payload = parse(cursor);
|
|
25
25
|
const signature = sortSignature(keys);
|
|
26
26
|
const prefixOk = payload.s.length === payload.v.length && payload.s.every((part, i) => part === signature[i]);
|
|
27
27
|
const lengthOk = payload.v.length === keys.length || payload.v.length === 1;
|
|
28
|
-
const filterOk = (payload.f ?? "") === filterFingerprint(filter);
|
|
28
|
+
const filterOk = (payload.f ?? "") === filterFingerprint(filter, scope);
|
|
29
29
|
if (!prefixOk || !lengthOk || !filterOk) throw invalidCursor();
|
|
30
30
|
return payload.v;
|
|
31
31
|
};
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
//#region src/filter-fingerprint.ts
|
|
2
|
+
const relatedTerms = (scope) => (scope?.parents ?? []).flatMap(({ field, parent, related }) => related ? [{
|
|
3
|
+
column: field.column,
|
|
4
|
+
resource: parent?.model.name,
|
|
5
|
+
key: parent?.key.column,
|
|
6
|
+
subfilter: related.filter,
|
|
7
|
+
terms: relatedTerms(parent?.scope)
|
|
8
|
+
}] : []);
|
|
2
9
|
/** The filter's structure without source spans, each field named by its column, so equal filters match however they were written. */
|
|
3
10
|
const canonical = (filter) => JSON.stringify(filter, (key, value) => {
|
|
4
11
|
if (key === "span") return void 0;
|
|
@@ -18,7 +25,14 @@ const hash = (text) => {
|
|
|
18
25
|
h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
|
|
19
26
|
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
|
|
20
27
|
};
|
|
21
|
-
/** A short fingerprint of the filter a cursor was issued for; empty without
|
|
22
|
-
const filterFingerprint = (filter) =>
|
|
28
|
+
/** A short fingerprint of the filter a cursor was issued for and the `related()` terms of its scope; empty without either. */
|
|
29
|
+
const filterFingerprint = (filter, scope) => {
|
|
30
|
+
const terms = relatedTerms(scope);
|
|
31
|
+
if (terms.length > 0) return hash(canonical({
|
|
32
|
+
filter,
|
|
33
|
+
terms
|
|
34
|
+
}));
|
|
35
|
+
return filter ? hash(canonical(filter)) : "";
|
|
36
|
+
};
|
|
23
37
|
//#endregion
|
|
24
38
|
export { filterFingerprint };
|
|
@@ -6,9 +6,10 @@ const usesSimilar = (node) => {
|
|
|
6
6
|
if (node.kind === "and" || node.kind === "or") return node.args.some(usesSimilar);
|
|
7
7
|
return node.kind === "not" && usesSimilar(node.arg);
|
|
8
8
|
};
|
|
9
|
+
const scopeUsesSimilar = (scope) => Boolean(scope?.rowFilter && usesSimilar(scope.rowFilter)) || (scope?.parents ?? []).some(({ parent, related }) => related !== void 0 && (Boolean(related.filter && usesSimilar(related.filter)) || scopeUsesSimilar(parent?.scope)));
|
|
9
10
|
/** Fails with a clear error, before any query runs, when the filter needs an extension the database lacks. */
|
|
10
11
|
const assertFilterRequirements = async (db, filter, scope) => {
|
|
11
|
-
if (!(filter && usesSimilar(filter)) && !(scope
|
|
12
|
+
if (!(filter && usesSimilar(filter)) && !scopeUsesSimilar(scope)) return;
|
|
12
13
|
if ((await sql`select count(*)::text as n from pg_extension where extname = 'pg_trgm'`.execute(db)).rows[0]?.n === "0") throw new QueryError("missing_extension", "similar() needs the pg_trgm extension: run \"create extension pg_trgm\"");
|
|
13
14
|
};
|
|
14
15
|
//#endregion
|
package/dist/index-coverage.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Scope } from "./scope.js";
|
|
2
2
|
import { TableIndex, TableIndexes } from "./table-indexes.js";
|
|
3
|
-
import { CheckedFilter, ResourceModel, SortSpec } from "@protobase/schema";
|
|
3
|
+
import { CheckedFilter, FieldModel, ResourceModel, SortSpec } from "@protobase/schema";
|
|
4
4
|
//#region src/index-coverage.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* The kind of index a column needs for how it is read: `btree` for comparisons, lists of values, null checks and sorts;
|
|
@@ -10,9 +10,9 @@ import { CheckedFilter, ResourceModel, SortSpec } from "@protobase/schema";
|
|
|
10
10
|
export type IndexNeed = 'btree' | 'trigram' | 'digits' | 'gin';
|
|
11
11
|
/**
|
|
12
12
|
* Where a column is read: the caller's filter, the access rule's row filter, the relation to a parent the caller must
|
|
13
|
-
* read (`.parent(fk, 'read')`), the sort, or a facet's grouping.
|
|
13
|
+
* read (`.parent(fk, 'read')`), a `related()` term, the sort, or a facet's grouping.
|
|
14
14
|
*/
|
|
15
|
-
export type ColumnUse = 'filter' | 'access' | 'parent' | 'sort' | 'facet';
|
|
15
|
+
export type ColumnUse = 'filter' | 'access' | 'parent' | 'related' | 'sort' | 'facet';
|
|
16
16
|
/** A column read without an index that serves it, and a statement that adds one. */
|
|
17
17
|
export type UncoveredColumn = {
|
|
18
18
|
field: string;
|
|
@@ -21,11 +21,12 @@ export type UncoveredColumn = {
|
|
|
21
21
|
need: IndexNeed;
|
|
22
22
|
suggestion: string;
|
|
23
23
|
};
|
|
24
|
-
/** What a read does with the table: the filter it keeps rows by, the order it walks them in, a field it groups by. */
|
|
24
|
+
/** What a read does with the table: the filter it keeps rows by, the order it walks them in, a field it groups by, the field a `related()` term selects. */
|
|
25
25
|
export type CoverageQuery = {
|
|
26
26
|
filter?: CheckedFilter;
|
|
27
27
|
sort?: SortSpec;
|
|
28
28
|
groupBy?: string;
|
|
29
|
+
selects?: FieldModel;
|
|
29
30
|
};
|
|
30
31
|
/** Whether the index serves reading the column with the given need, while the query pins the `pinned` columns. */
|
|
31
32
|
export declare const indexServes: (index: TableIndex, column: string, need: IndexNeed, pinned: ReadonlySet<string>) => boolean;
|
|
@@ -34,7 +35,9 @@ export declare const indexServes: (index: TableIndex, column: string, need: Inde
|
|
|
34
35
|
* filter's and the access row filter's columns each need an index of the kind their operator uses, the sort's first
|
|
35
36
|
* field, a grouped field and each relation to a parent the caller must read a btree index; the parent's own reads are
|
|
36
37
|
* judged against its table by a call of their own. Tenant, soft delete and `=` terms pin columns, so a later key of a
|
|
37
|
-
* btree index serves under them. A
|
|
38
|
+
* btree index serves under them. A `related()` term is judged as a parent is and pins its relation as `in()` does, and
|
|
39
|
+
* the read of its resource also needs a btree index on the key it selects. A plain view or a foreign table has no
|
|
40
|
+
* indexes of its own and is not judged.
|
|
38
41
|
*/
|
|
39
42
|
export declare const uncoveredColumns: (model: ResourceModel, table: TableIndexes, query: CoverageQuery, scope?: Scope) => UncoveredColumn[];
|
|
40
43
|
//#endregion
|
package/dist/index-coverage.js
CHANGED
|
@@ -78,7 +78,9 @@ const unique = (columns) => columns.filter((column, i) => columns.findIndex((oth
|
|
|
78
78
|
* filter's and the access row filter's columns each need an index of the kind their operator uses, the sort's first
|
|
79
79
|
* field, a grouped field and each relation to a parent the caller must read a btree index; the parent's own reads are
|
|
80
80
|
* judged against its table by a call of their own. Tenant, soft delete and `=` terms pin columns, so a later key of a
|
|
81
|
-
* btree index serves under them. A
|
|
81
|
+
* btree index serves under them. A `related()` term is judged as a parent is and pins its relation as `in()` does, and
|
|
82
|
+
* the read of its resource also needs a btree index on the key it selects. A plain view or a foreign table has no
|
|
83
|
+
* indexes of its own and is not judged.
|
|
82
84
|
*/
|
|
83
85
|
const uncoveredColumns = (model, table, query, scope) => {
|
|
84
86
|
if (table.kind !== "table") return [];
|
|
@@ -88,7 +90,8 @@ const uncoveredColumns = (model, table, query, scope) => {
|
|
|
88
90
|
...columnOf(full.tenant ?? model.tenant),
|
|
89
91
|
...columnOf(full.softDelete ?? model.softDelete),
|
|
90
92
|
...pinnedColumns(scope?.rowFilter),
|
|
91
|
-
...pinnedColumns(query.filter)
|
|
93
|
+
...pinnedColumns(query.filter),
|
|
94
|
+
...(scope?.parents ?? []).flatMap((read) => read.related ? [read.field.column] : [])
|
|
92
95
|
]);
|
|
93
96
|
const ctx = (use, of = model) => ({
|
|
94
97
|
model: of,
|
|
@@ -99,8 +102,9 @@ const uncoveredColumns = (model, table, query, scope) => {
|
|
|
99
102
|
const groupField = query.groupBy !== void 0 ? model.fields[query.groupBy] : void 0;
|
|
100
103
|
return unique([
|
|
101
104
|
...scope?.rowFilter ? walk(ctx("access", full), scope.rowFilter, pinned, false) : [],
|
|
102
|
-
...(scope?.parents ?? []).flatMap((read) => need(ctx("parent", full), read.field, "btree", pinned)),
|
|
105
|
+
...(scope?.parents ?? []).flatMap((read) => need(ctx(read.related ? "related" : "parent", full), read.field, "btree", pinned)),
|
|
103
106
|
...query.filter ? walk(ctx("filter"), query.filter, pinned, false) : [],
|
|
107
|
+
...query.selects ? need(ctx("related", full), query.selects, "btree", pinned) : [],
|
|
104
108
|
...sortField ? need(ctx("sort"), sortField, "btree", pinned) : [],
|
|
105
109
|
...groupField ? need(ctx("facet"), groupField, "btree", pinned) : []
|
|
106
110
|
]);
|
package/dist/list-query.js
CHANGED
|
@@ -33,7 +33,7 @@ const buildListQuery = async (db, model, query, scope, options = {}) => {
|
|
|
33
33
|
const limit = clampLimit(query.limit);
|
|
34
34
|
await assertFilterRequirements(db, query.filter, scope);
|
|
35
35
|
const keys = effectiveSort(model, query.sort);
|
|
36
|
-
const cursorValues = query.cursor === void 0 ? void 0 : decodeCursor(query.cursor, keys, query.filter);
|
|
36
|
+
const cursorValues = query.cursor === void 0 ? void 0 : decodeCursor(query.cursor, keys, query.filter, scope);
|
|
37
37
|
return {
|
|
38
38
|
query: selectPage(db, model, query, scope, keys, cursorValues, options),
|
|
39
39
|
limit,
|
|
@@ -48,7 +48,7 @@ const listQuery = async (db, model, query, scope, options = {}) => {
|
|
|
48
48
|
const more = fetched.length > limit;
|
|
49
49
|
const page = more ? fetched.slice(0, limit) : fetched;
|
|
50
50
|
if (reverse) page.reverse();
|
|
51
|
-
const cursorOf = (row) => encodeCursor(keys, keys.map((_, i) => row[keyAlias(i)] === null ? null : String(row[keyAlias(i)])), query.filter);
|
|
51
|
+
const cursorOf = (row) => encodeCursor(keys, keys.map((_, i) => row[keyAlias(i)] === null ? null : String(row[keyAlias(i)])), query.filter, scope);
|
|
52
52
|
const first = page[0];
|
|
53
53
|
const last = page.at(-1);
|
|
54
54
|
return {
|
|
@@ -4,8 +4,9 @@ import { ResourceModel } from "@protobase/schema";
|
|
|
4
4
|
//#region src/read-conditions.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* `fk in (select key from parent where <the caller's scope on it>)`, or `false` when the caller reads no parent; a
|
|
7
|
-
* nullable `fk` also keeps the rows that name no parent
|
|
8
|
-
* the parent's columns and only `fk` reads the
|
|
7
|
+
* nullable `fk` also keeps the rows that name no parent, unless this is a `related()` term, which also holds its
|
|
8
|
+
* subfilter. The subquery aliases the parent `t` too, so its conditions read the parent's columns and only `fk` reads the
|
|
9
|
+
* outer row.
|
|
9
10
|
*/
|
|
10
11
|
export declare const parentCondition: (read: ParentRead) => Expression<SqlBool>;
|
|
11
12
|
//#endregion
|
package/dist/read-conditions.js
CHANGED
|
@@ -9,14 +9,16 @@ const softDeleteColumn = (model, name) => {
|
|
|
9
9
|
};
|
|
10
10
|
/**
|
|
11
11
|
* `fk in (select key from parent where <the caller's scope on it>)`, or `false` when the caller reads no parent; a
|
|
12
|
-
* nullable `fk` also keeps the rows that name no parent
|
|
13
|
-
* the parent's columns and only `fk` reads the
|
|
12
|
+
* nullable `fk` also keeps the rows that name no parent, unless this is a `related()` term, which also holds its
|
|
13
|
+
* subfilter. The subquery aliases the parent `t` too, so its conditions read the parent's columns and only `fk` reads the
|
|
14
|
+
* outer row.
|
|
14
15
|
*/
|
|
15
16
|
const parentCondition = (read) => {
|
|
16
17
|
const column = columnRef(read.field);
|
|
17
|
-
const { parent } = read;
|
|
18
|
-
const
|
|
19
|
-
|
|
18
|
+
const { parent, related } = read;
|
|
19
|
+
const subfilter = related?.filter ? [compileFilter(related.model, related.filter)] : [];
|
|
20
|
+
const within = parent ? sql`${column} in (select ${columnRef(parent.key)} from ${tableSource(parent.model)}${whereAll([...readConditions(parent.model, parent.scope), ...subfilter])})` : sql`false`;
|
|
21
|
+
return read.field.nullable && !related ? sql`(${column} is null or ${within})` : within;
|
|
20
22
|
};
|
|
21
23
|
const whereAll = (conditions) => conditions.length === 0 ? sql`` : sql` where ${sql.join(conditions, sql` and `)}`;
|
|
22
24
|
/** What the scope hides of the table aliased as `t`: soft-deleted rows, other tenants, the row filter and unread parents. */
|
package/dist/scope.d.ts
CHANGED
|
@@ -23,6 +23,8 @@ export type Scope = {
|
|
|
23
23
|
/**
|
|
24
24
|
* A relation whose parent the caller must read for a row to be seen: `field` of the full model, and the parent's full
|
|
25
25
|
* model, key and the caller's scope on it; without `parent` the caller reads no parent, so only a null `field` is seen.
|
|
26
|
+
* With `related` it is a `related()` term of the caller's filter instead: a null `field` is not seen, and `filter`,
|
|
27
|
+
* checked against `model`, the resource as the caller sees it, narrows what is read.
|
|
26
28
|
*/
|
|
27
29
|
export type ParentRead = {
|
|
28
30
|
field: FieldModel;
|
|
@@ -31,6 +33,10 @@ export type ParentRead = {
|
|
|
31
33
|
key: FieldModel;
|
|
32
34
|
scope: Scope;
|
|
33
35
|
};
|
|
36
|
+
related?: {
|
|
37
|
+
model: ResourceModel;
|
|
38
|
+
filter?: CheckedFilter;
|
|
39
|
+
};
|
|
34
40
|
};
|
|
35
41
|
/**
|
|
36
42
|
* `tenant = value`, or `tenant in (values)`, for tenant models; throws when the scope is missing, so a forgotten scope
|
package/dist/scroll-anchors.js
CHANGED
|
@@ -60,7 +60,7 @@ const scrollAnchors = async (db, model, sortField, options = {}) => {
|
|
|
60
60
|
seekToPosition: async (position) => {
|
|
61
61
|
if (position <= 0) return void 0;
|
|
62
62
|
const anchor = anchors.findLast((candidate) => candidate.position <= position);
|
|
63
|
-
return anchor && anchor.position > 0 ? encodeCursor(keys, [anchor.value], filter) : void 0;
|
|
63
|
+
return anchor && anchor.position > 0 ? encodeCursor(keys, [anchor.value], filter, scope) : void 0;
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
66
|
};
|
package/dist/scroll-exact.js
CHANGED
|
@@ -21,7 +21,7 @@ const exactSeek = async (db, model, keys, scope, filter, position) => {
|
|
|
21
21
|
if (position <= 0) return void 0;
|
|
22
22
|
const row = await db.selectFrom(rankedRows(db, model, keys, scope, filter).as("r")).selectAll().where(sql`r.rn = ${position}`).executeTakeFirst();
|
|
23
23
|
if (!row) return void 0;
|
|
24
|
-
return encodeCursor(keys, keys.map((_, i) => row[`k${i}`] === null ? null : String(row[`k${i}`])), filter);
|
|
24
|
+
return encodeCursor(keys, keys.map((_, i) => row[`k${i}`] === null ? null : String(row[`k${i}`])), filter, scope);
|
|
25
25
|
};
|
|
26
26
|
//#endregion
|
|
27
27
|
export { exactAnchors, exactSeek };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/query",
|
|
3
|
-
"version": "0.73.
|
|
3
|
+
"version": "0.73.3",
|
|
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.
|
|
32
|
+
"@protobase/schema": "0.73.3",
|
|
33
33
|
"kysely": "^0.29.6"
|
|
34
34
|
}
|
|
35
35
|
}
|