@protobase/query 0.117.5 → 0.117.7
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/columns.d.ts +6 -0
- package/dist/columns.js +15 -1
- package/dist/compile-filter.js +19 -1
- package/dist/index-coverage.d.ts +5 -3
- package/dist/index-coverage.js +10 -4
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -1
- package/dist/linked-read.d.ts +5 -0
- package/dist/linked-read.js +9 -0
- package/dist/list-query.d.ts +2 -1
- package/dist/lookup-coverage.d.ts +23 -0
- package/dist/lookup-coverage.js +58 -0
- package/dist/lookup-read.d.ts +26 -0
- package/dist/lookup-read.js +18 -0
- package/dist/scope.d.ts +1 -1
- package/dist/scope.js +1 -2
- package/dist/sort-key.d.ts +8 -0
- package/dist/sort.d.ts +3 -8
- package/package.json +2 -2
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { RawBuilder } from "kysely";
|
|
2
|
+
import { FieldModel, ResourceModel } from "@protobase/schema";
|
|
3
|
+
//#region src/columns.d.ts
|
|
4
|
+
/** The column of a field of the table aliased `t`, or a lookup field's value. */
|
|
5
|
+
export declare const columnRef: (field: FieldModel) => RawBuilder<unknown>;
|
|
6
|
+
//#endregion
|
package/dist/columns.js
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
import { QueryError } from "./fields/query-error.js";
|
|
2
|
+
import { lookupReadOf } from "./lookup-read.js";
|
|
2
3
|
import { sql } from "kysely";
|
|
3
|
-
const columnRef = (field) => sql`${sql.id("t", field.column)}`;
|
|
4
4
|
const tableSource = (model) => {
|
|
5
5
|
const { schema, name } = model.table;
|
|
6
6
|
const table = schema ? sql.id(schema, name) : sql.id(name);
|
|
7
7
|
return sql`${table}`.as("t");
|
|
8
8
|
};
|
|
9
|
+
const whereAll = (conditions) => conditions.length === 0 ? sql`` : sql` where ${sql.join(conditions, sql` and `)}`;
|
|
10
|
+
/**
|
|
11
|
+
* A lookup field's value: the linked field of the one linked record the caller reads, or null. The linked table is
|
|
12
|
+
* aliased `t` inside its own subquery, so its conditions read its columns, and only the link reads the outer row.
|
|
13
|
+
*/
|
|
14
|
+
const lookupValue = (read) => {
|
|
15
|
+
const linked = sql`select ${sql.id("t", read.match.column)} as k, ${sql.id("t", read.value.column)} as v from ${tableSource(read.model)}${whereAll(read.conditions)}`;
|
|
16
|
+
return sql`(select l.v from (${linked}) as l where l.k = ${columnRef(read.link)})`;
|
|
17
|
+
};
|
|
18
|
+
/** The column of a field of the table aliased `t`, or a lookup field's value. */
|
|
19
|
+
const columnRef = (field) => {
|
|
20
|
+
const read = lookupReadOf(field);
|
|
21
|
+
return read ? lookupValue(read) : sql`${sql.id("t", field.column)}`;
|
|
22
|
+
};
|
|
9
23
|
const findField = (model, name) => {
|
|
10
24
|
const field = Object.hasOwn(model.fields, name) ? model.fields[name] : void 0;
|
|
11
25
|
if (!field) throw new QueryError("unknown_field", `Unknown field "${name}" on resource "${model.name}"`);
|
package/dist/compile-filter.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { jsonHas, jsonPresent } from "./fields/json.js";
|
|
2
2
|
import { QueryError } from "./fields/query-error.js";
|
|
3
|
-
import {
|
|
3
|
+
import { lookupReadOf } from "./lookup-read.js";
|
|
4
|
+
import { columnRef, tableSource } from "./columns.js";
|
|
4
5
|
import { compileCompare, compileIn } from "./compile-compare.js";
|
|
5
6
|
import { compileRegex, compileSearch, compileSimilar } from "./compile-text-match.js";
|
|
6
7
|
import { sql } from "kysely";
|
|
@@ -10,7 +11,24 @@ const combine = (parts, joiner) => {
|
|
|
10
11
|
if (parts.length === 0) return sql`${sql.lit(joiner === "and")}`;
|
|
11
12
|
return sql`(${sql.join(parts, sql` ${sql.raw(joiner)} `)})`;
|
|
12
13
|
};
|
|
14
|
+
const lookupTerm = (node, read, threshold) => {
|
|
15
|
+
const held = node.kind === "isNull" ? sql`${columnRef(read.value)} is not null` : compileNode(read.model, {
|
|
16
|
+
...node,
|
|
17
|
+
field: read.value
|
|
18
|
+
}, threshold);
|
|
19
|
+
const matched = read.match.nullable ? [sql`${columnRef(read.match)} is not null`] : [];
|
|
20
|
+
const keys = sql`select ${columnRef(read.match)} from ${tableSource(read.model)} where ${sql.join([
|
|
21
|
+
...read.conditions,
|
|
22
|
+
...matched,
|
|
23
|
+
held
|
|
24
|
+
], sql` and `)}`;
|
|
25
|
+
const link = columnRef(read.link);
|
|
26
|
+
const linked = read.link.nullable ? sql`(${link} is not null and ${link} in (${keys}))` : sql`${link} in (${keys})`;
|
|
27
|
+
return node.kind === "isNull" ? sql`not ${linked}` : linked;
|
|
28
|
+
};
|
|
13
29
|
const compileNode = (model, node, threshold) => {
|
|
30
|
+
const read = "field" in node ? lookupReadOf(node.field) : void 0;
|
|
31
|
+
if (read) return lookupTerm(node, read, threshold);
|
|
14
32
|
switch (node.kind) {
|
|
15
33
|
case "and":
|
|
16
34
|
case "or": return combine(node.args.map((arg) => compileNode(model, arg, threshold)), node.kind);
|
package/dist/index-coverage.d.ts
CHANGED
|
@@ -12,9 +12,10 @@ export type IndexNeed = 'btree' | 'trigram' | 'digits' | 'gin' | 'file';
|
|
|
12
12
|
/**
|
|
13
13
|
* Where a column is read: the caller's filter, the access rule's row filter, the relation to a parent the caller must
|
|
14
14
|
* read (`.parent(fk, 'read')`), a `related()` term, the sort, a facet's grouping, the field a read keeps the first rows
|
|
15
|
-
* per value of (`limit_per`),
|
|
15
|
+
* per value of (`limit_per`), the file cleanup's check that no row holds a file it is about to delete, or the field a
|
|
16
|
+
* lookup field finds each record's linked record by.
|
|
16
17
|
*/
|
|
17
|
-
export type ColumnUse = 'filter' | 'access' | 'parent' | 'related' | 'sort' | 'facet' | 'per' | 'cleanup';
|
|
18
|
+
export type ColumnUse = 'filter' | 'access' | 'parent' | 'related' | 'sort' | 'facet' | 'per' | 'cleanup' | 'lookup';
|
|
18
19
|
/** A column read without an index that serves it, and a statement that adds one. */
|
|
19
20
|
export type UncoveredColumn = {
|
|
20
21
|
field: string;
|
|
@@ -50,7 +51,8 @@ export declare const uncoveredFileColumns: (model: ResourceModel, table: TableIn
|
|
|
50
51
|
* btree index serves under them. A `related()` term is judged as a parent is and pins its relation as `in()` does, and
|
|
51
52
|
* the read of its resource also needs a btree index on the key it selects. A read that keeps the first rows per value
|
|
52
53
|
* of a field needs a btree index on that field with the sort's first field under it, which serves the sort too.
|
|
53
|
-
* A
|
|
54
|
+
* A lookup field read in any of these ways needs a btree index on its link instead; what it reads of the linked
|
|
55
|
+
* table is judged against that table (`lookupQueries`). A plain view or a foreign table has no
|
|
54
56
|
* indexes of its own and is not judged.
|
|
55
57
|
*/
|
|
56
58
|
export declare const uncoveredColumns: (model: ResourceModel, table: TableIndexes, query: CoverageQuery, scope?: Scope) => UncoveredColumn[];
|
package/dist/index-coverage.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { lookupReadOf } from "./lookup-read.js";
|
|
1
2
|
import { isPatternCompare } from "./compile-compare.js";
|
|
2
3
|
import { fullModelOf } from "./scope.js";
|
|
3
4
|
//#region src/index-coverage.ts
|
|
@@ -20,6 +21,7 @@ const suggestion = (model, column, need, under) => {
|
|
|
20
21
|
const pinnedColumns = (filter) => {
|
|
21
22
|
if (!filter) return [];
|
|
22
23
|
if (filter.kind === "and") return filter.args.flatMap(pinnedColumns);
|
|
24
|
+
if ("field" in filter && filter.field.lookup) return [];
|
|
23
25
|
if (filter.kind === "compare") return filter.op === "=" && !isPatternCompare(filter) ? [filter.field.column] : [];
|
|
24
26
|
if (filter.kind === "in" || filter.kind === "isNull") return [filter.field.column];
|
|
25
27
|
return [];
|
|
@@ -86,6 +88,7 @@ const perNeed = (ctx, per, sort, pinned) => {
|
|
|
86
88
|
suggestion: suggestion(ctx.model, per.column, "btree", sort.column)
|
|
87
89
|
}];
|
|
88
90
|
};
|
|
91
|
+
const linkOf = (field) => lookupReadOf(field)?.link ?? field;
|
|
89
92
|
const compareNeed = (ctx, node, pinned) => {
|
|
90
93
|
if (isPatternCompare(node)) return need(ctx, node.field, "trigram", pinned);
|
|
91
94
|
return need(ctx, node.field, "btree", pinned, node.op === "=" && node.field.type === "text" ? "trigram" : void 0);
|
|
@@ -96,6 +99,7 @@ const searchNeeds = (ctx, pinned) => (ctx.model.search ?? []).flatMap((name) =>
|
|
|
96
99
|
return need(ctx, field, ctx.model.searchMatch?.[name] === "digitsEnd" ? "digits" : "trigram", pinned);
|
|
97
100
|
});
|
|
98
101
|
const walk = (ctx, node, pinned, negated) => {
|
|
102
|
+
if ("field" in node && node.field.lookup) return need(ctx, linkOf(node.field), "btree", pinned);
|
|
99
103
|
switch (node.kind) {
|
|
100
104
|
case "and": return node.args.flatMap((arg) => walk(ctx, arg, pinned, negated));
|
|
101
105
|
case "or": return node.args.flatMap((arg) => walk(ctx, arg, negated ? pinned : /* @__PURE__ */ new Set([...pinned, ...pinnedColumns(arg)]), negated));
|
|
@@ -119,7 +123,8 @@ const unique = (columns) => columns.filter((column, i) => columns.findIndex((oth
|
|
|
119
123
|
* btree index serves under them. A `related()` term is judged as a parent is and pins its relation as `in()` does, and
|
|
120
124
|
* the read of its resource also needs a btree index on the key it selects. A read that keeps the first rows per value
|
|
121
125
|
* of a field needs a btree index on that field with the sort's first field under it, which serves the sort too.
|
|
122
|
-
* A
|
|
126
|
+
* A lookup field read in any of these ways needs a btree index on its link instead; what it reads of the linked
|
|
127
|
+
* table is judged against that table (`lookupQueries`). A plain view or a foreign table has no
|
|
123
128
|
* indexes of its own and is not judged.
|
|
124
129
|
*/
|
|
125
130
|
const uncoveredColumns = (model, table, query, scope) => {
|
|
@@ -139,9 +144,10 @@ const uncoveredColumns = (model, table, query, scope) => {
|
|
|
139
144
|
indexes: table.indexes,
|
|
140
145
|
use
|
|
141
146
|
});
|
|
142
|
-
const
|
|
143
|
-
const
|
|
144
|
-
const
|
|
147
|
+
const own = (name) => name !== void 0 && model.fields[name] ? linkOf(model.fields[name]) : void 0;
|
|
148
|
+
const sortField = own(query.sort?.[0]?.[0]);
|
|
149
|
+
const groupField = own(query.groupBy);
|
|
150
|
+
const perField = own(query.per);
|
|
145
151
|
return unique([
|
|
146
152
|
...perField ? perNeed(ctx("per"), perField, sortField, pinned) : [],
|
|
147
153
|
...scope?.rowFilter ? walk(ctx("access", full), scope.rowFilter, pinned, false) : [],
|
package/dist/index.d.ts
CHANGED
|
@@ -22,6 +22,10 @@ import { ColumnUse, CoverageQuery, IndexNeed, UncoveredColumn, indexServes, unco
|
|
|
22
22
|
import { QueryError, QueryErrorCode } from "./fields/query-error.js";
|
|
23
23
|
import { RelatedPageRequest, relatedPage } from "./related-page.js";
|
|
24
24
|
import { parentCondition } from "./read-conditions.js";
|
|
25
|
+
import { LookupRead, lookupReadOf, withLookupRead } from "./lookup-read.js";
|
|
26
|
+
import { columnRef } from "./columns.js";
|
|
27
|
+
import { lookupRead } from "./linked-read.js";
|
|
28
|
+
import { LookupCoverage, lookupQueries, uncoveredLookupMatch } from "./lookup-coverage.js";
|
|
25
29
|
import { readsParent } from "./reads-parent.js";
|
|
26
30
|
import { SeekRequest, seekToPosition } from "./seek-to-position.js";
|
|
27
|
-
export { type ColumnUse, type CompileOptions, type CoverageQuery, type Db, type ExtraColumn, type FacetCount, type FacetOptions, type Granularity, type GroupTotal, type GroupTotalsOptions, type Histogram, type HistogramBucket, type IndexKey, type IndexNeed, type KeyItemType, type LimitPer, type ListOptions, type ListPlan, type ListResult, type MapBounds, type MapBucket, type MapBuckets, type MapPosition, type Measure, type ParentRead, type PerValue, QueryError, type QueryErrorCode, type RelatedPage, type RelatedPageRequest, type RelatedRowsQuery, type Scope, type ScrollMode, type SeekRequest, type SeriesOptions, type SeriesPoint, type SplitPoint, type SplitSeriesOptions, type TableIndex, type TableIndexes, type UncoveredColumn, buildListQuery, buildRelatedRows, cellsAcross, checkMapPosition, compileFilter, countUpTo, estimateCount, exactCount, facetCounts, groupTotals, histogram, indexServes, listPerValue, listQuery, mapBuckets, mapCellPixels, maxLimit, maxMapBuckets, maxMapZoom, parentCondition, readsParent, relatedPage, relatedRows, scrollMode, seekToPosition, series, splitSeries, tableIndexes, tenantCondition, uncoveredColumns, uncoveredFileColumns, withoutField };
|
|
31
|
+
export { type ColumnUse, type CompileOptions, type CoverageQuery, type Db, type ExtraColumn, type FacetCount, type FacetOptions, type Granularity, type GroupTotal, type GroupTotalsOptions, type Histogram, type HistogramBucket, type IndexKey, type IndexNeed, type KeyItemType, type LimitPer, type ListOptions, type ListPlan, type ListResult, type LookupCoverage, type LookupRead, type MapBounds, type MapBucket, type MapBuckets, type MapPosition, type Measure, type ParentRead, type PerValue, QueryError, type QueryErrorCode, type RelatedPage, type RelatedPageRequest, type RelatedRowsQuery, type Scope, type ScrollMode, type SeekRequest, type SeriesOptions, type SeriesPoint, type SplitPoint, type SplitSeriesOptions, type TableIndex, type TableIndexes, type UncoveredColumn, buildListQuery, buildRelatedRows, cellsAcross, checkMapPosition, columnRef, compileFilter, countUpTo, estimateCount, exactCount, facetCounts, groupTotals, histogram, indexServes, listPerValue, listQuery, lookupQueries, lookupRead, lookupReadOf, mapBuckets, mapCellPixels, maxLimit, maxMapBuckets, maxMapZoom, parentCondition, readsParent, relatedPage, relatedRows, scrollMode, seekToPosition, series, splitSeries, tableIndexes, tenantCondition, uncoveredColumns, uncoveredFileColumns, uncoveredLookupMatch, withLookupRead, withoutField };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { QueryError } from "./fields/query-error.js";
|
|
2
|
+
import { lookupReadOf, withLookupRead } from "./lookup-read.js";
|
|
3
|
+
import { columnRef } from "./columns.js";
|
|
2
4
|
import { compileFilter } from "./compile-filter.js";
|
|
3
5
|
import { tenantCondition } from "./scope.js";
|
|
4
6
|
import { parentCondition } from "./read-conditions.js";
|
|
@@ -19,6 +21,8 @@ import { scrollMode } from "./scroll-mode.js";
|
|
|
19
21
|
import { tableIndexes } from "./table-indexes.js";
|
|
20
22
|
import { indexServes, uncoveredColumns, uncoveredFileColumns } from "./index-coverage.js";
|
|
21
23
|
import { relatedPage } from "./related-page.js";
|
|
24
|
+
import { lookupRead } from "./linked-read.js";
|
|
25
|
+
import { lookupQueries, uncoveredLookupMatch } from "./lookup-coverage.js";
|
|
22
26
|
import { readsParent } from "./reads-parent.js";
|
|
23
27
|
import { seekToPosition } from "./seek-to-position.js";
|
|
24
|
-
export { QueryError, buildListQuery, buildRelatedRows, cellsAcross, checkMapPosition, compileFilter, countUpTo, estimateCount, exactCount, facetCounts, groupTotals, histogram, indexServes, listPerValue, listQuery, mapBuckets, mapCellPixels, maxLimit, maxMapBuckets, maxMapZoom, parentCondition, readsParent, relatedPage, relatedRows, scrollMode, seekToPosition, series, splitSeries, tableIndexes, tenantCondition, uncoveredColumns, uncoveredFileColumns, withoutField };
|
|
28
|
+
export { QueryError, buildListQuery, buildRelatedRows, cellsAcross, checkMapPosition, columnRef, compileFilter, countUpTo, estimateCount, exactCount, facetCounts, groupTotals, histogram, indexServes, listPerValue, listQuery, lookupQueries, lookupRead, lookupReadOf, mapBuckets, mapCellPixels, maxLimit, maxMapBuckets, maxMapZoom, parentCondition, readsParent, relatedPage, relatedRows, scrollMode, seekToPosition, series, splitSeries, tableIndexes, tenantCondition, uncoveredColumns, uncoveredFileColumns, uncoveredLookupMatch, withLookupRead, withoutField };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { LookupRead } from "./lookup-read.js";
|
|
2
|
+
//#region src/linked-read.d.ts
|
|
3
|
+
/** How a read reads a lookup field, with the conditions the caller's scope on the linked resource keeps to. */
|
|
4
|
+
export declare const lookupRead: (read: Omit<LookupRead, "conditions">) => LookupRead;
|
|
5
|
+
//#endregion
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { readConditions } from "./read-conditions.js";
|
|
2
|
+
//#region src/linked-read.ts
|
|
3
|
+
/** How a read reads a lookup field, with the conditions the caller's scope on the linked resource keeps to. */
|
|
4
|
+
const lookupRead = (read) => ({
|
|
5
|
+
...read,
|
|
6
|
+
conditions: readConditions(read.model, read.scope)
|
|
7
|
+
});
|
|
8
|
+
//#endregion
|
|
9
|
+
export { lookupRead };
|
package/dist/list-query.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Db } from "./db.js";
|
|
2
|
-
import { SortKey } from "./sort.js";
|
|
2
|
+
import { SortKey } from "./sort-key.js";
|
|
3
3
|
import { Scope } from "./scope.js";
|
|
4
|
+
import "./sort.js";
|
|
4
5
|
import { ListQuery, ResourceModel } from "@protobase/schema";
|
|
5
6
|
//#region src/list-query.d.ts
|
|
6
7
|
export declare const maxLimit = 500;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { TableIndexes } from "./table-indexes.js";
|
|
2
|
+
import { CoverageQuery, UncoveredColumn } from "./index-coverage.js";
|
|
3
|
+
import { LookupRead } from "./lookup-read.js";
|
|
4
|
+
import { ResourceModel } from "@protobase/schema";
|
|
5
|
+
//#region src/lookup-coverage.d.ts
|
|
6
|
+
/** A read of a lookup field's linked records, judged against their table's indexes in the caller's scope on them. */
|
|
7
|
+
export type LookupCoverage = {
|
|
8
|
+
field: string;
|
|
9
|
+
read: LookupRead;
|
|
10
|
+
query: CoverageQuery;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* What a read of the resource reads of its lookup fields' linked records: each term on a lookup field filters them by
|
|
14
|
+
* the linked field, and a sort, grouping or first rows per value of one walks them by it. Empty is read as a linked
|
|
15
|
+
* value that is not empty, negated, as the filter compiles it.
|
|
16
|
+
*/
|
|
17
|
+
export declare const lookupQueries: (model: ResourceModel, query: CoverageQuery) => LookupCoverage[];
|
|
18
|
+
/**
|
|
19
|
+
* Whether an index of the linked table serves finding each record's linked record, by the linked record's key or its
|
|
20
|
+
* relation that points at the record: every read that shows the lookup field does that once per record.
|
|
21
|
+
*/
|
|
22
|
+
export declare const uncoveredLookupMatch: (read: LookupRead, table: TableIndexes) => UncoveredColumn[];
|
|
23
|
+
//#endregion
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { lookupReadOf } from "./lookup-read.js";
|
|
2
|
+
import { uncoveredColumns } from "./index-coverage.js";
|
|
3
|
+
//#region src/lookup-coverage.ts
|
|
4
|
+
const lookupTerms = (node) => {
|
|
5
|
+
if (!node) return [];
|
|
6
|
+
if (node.kind === "and" || node.kind === "or") return node.args.flatMap(lookupTerms);
|
|
7
|
+
if (node.kind === "not") return lookupTerms(node.arg);
|
|
8
|
+
return "field" in node && node.field.lookup ? [node] : [];
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* What a read of the resource reads of its lookup fields' linked records: each term on a lookup field filters them by
|
|
12
|
+
* the linked field, and a sort, grouping or first rows per value of one walks them by it. Empty is read as a linked
|
|
13
|
+
* value that is not empty, negated, as the filter compiles it.
|
|
14
|
+
*/
|
|
15
|
+
const lookupQueries = (model, query) => {
|
|
16
|
+
const walked = (name, walk) => {
|
|
17
|
+
const field = name === void 0 ? void 0 : model.fields[name];
|
|
18
|
+
const read = field && lookupReadOf(field);
|
|
19
|
+
return field && read ? [{
|
|
20
|
+
field: field.name,
|
|
21
|
+
read,
|
|
22
|
+
query: walk(read.value.name)
|
|
23
|
+
}] : [];
|
|
24
|
+
};
|
|
25
|
+
const sort = query.sort?.[0];
|
|
26
|
+
return [
|
|
27
|
+
...lookupTerms(query.filter).map((term) => {
|
|
28
|
+
const read = lookupReadOf(term.field);
|
|
29
|
+
const linked = {
|
|
30
|
+
...term,
|
|
31
|
+
field: read.value
|
|
32
|
+
};
|
|
33
|
+
const filter = term.kind === "isNull" ? {
|
|
34
|
+
kind: "not",
|
|
35
|
+
arg: linked,
|
|
36
|
+
span: term.span
|
|
37
|
+
} : linked;
|
|
38
|
+
return {
|
|
39
|
+
field: term.field.name,
|
|
40
|
+
read,
|
|
41
|
+
query: { filter }
|
|
42
|
+
};
|
|
43
|
+
}),
|
|
44
|
+
...walked(sort?.[0], (value) => ({ sort: [[value, sort[1]]] })),
|
|
45
|
+
...walked(query.groupBy, (value) => ({ groupBy: value })),
|
|
46
|
+
...walked(query.per, (value) => ({ groupBy: value }))
|
|
47
|
+
];
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Whether an index of the linked table serves finding each record's linked record, by the linked record's key or its
|
|
51
|
+
* relation that points at the record: every read that shows the lookup field does that once per record.
|
|
52
|
+
*/
|
|
53
|
+
const uncoveredLookupMatch = (read, table) => uncoveredColumns(read.model, table, { selects: read.match }).map((column) => ({
|
|
54
|
+
...column,
|
|
55
|
+
use: "lookup"
|
|
56
|
+
}));
|
|
57
|
+
//#endregion
|
|
58
|
+
export { lookupQueries, uncoveredLookupMatch };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Scope } from "./scope.js";
|
|
2
|
+
import { Expression, SqlBool } from "kysely";
|
|
3
|
+
import { FieldModel, ResourceModel } from "@protobase/schema";
|
|
4
|
+
//#region src/lookup-read.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* How one read reads a lookup field: the linked resource's full model and the caller's scope on it with the conditions
|
|
7
|
+
* it keeps to, the linked field it shows (`value`), the linked record's field matched (`match`: its key, or the
|
|
8
|
+
* relation that points at this record) and this record's field matched with it (`link`: the relation, or this record's
|
|
9
|
+
* key). It holds the caller's scope, so it is only on the model a query runs against, never on one sent to the caller.
|
|
10
|
+
*/
|
|
11
|
+
export type LookupRead = {
|
|
12
|
+
model: ResourceModel;
|
|
13
|
+
scope: Scope;
|
|
14
|
+
conditions: Expression<SqlBool>[];
|
|
15
|
+
value: FieldModel;
|
|
16
|
+
match: FieldModel;
|
|
17
|
+
link: FieldModel;
|
|
18
|
+
};
|
|
19
|
+
/** The lookup field as this read reads it. */
|
|
20
|
+
export declare const withLookupRead: (field: FieldModel, read: LookupRead) => FieldModel;
|
|
21
|
+
/**
|
|
22
|
+
* How this read reads the field when it is a lookup field. A lookup field of a model the server did not resolve for a
|
|
23
|
+
* caller has no read, which is a bug of the caller's code, not of the request: it throws.
|
|
24
|
+
*/
|
|
25
|
+
export declare const lookupReadOf: (field: FieldModel) => LookupRead | undefined;
|
|
26
|
+
//#endregion
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/lookup-read.ts
|
|
2
|
+
/** The lookup field as this read reads it. */
|
|
3
|
+
const withLookupRead = (field, read) => ({
|
|
4
|
+
...field,
|
|
5
|
+
lookupRead: read
|
|
6
|
+
});
|
|
7
|
+
/**
|
|
8
|
+
* How this read reads the field when it is a lookup field. A lookup field of a model the server did not resolve for a
|
|
9
|
+
* caller has no read, which is a bug of the caller's code, not of the request: it throws.
|
|
10
|
+
*/
|
|
11
|
+
const lookupReadOf = (field) => {
|
|
12
|
+
if (!field.lookup) return;
|
|
13
|
+
const read = field.lookupRead;
|
|
14
|
+
if (!read) throw new Error(`Lookup field "${field.name}" is read without the caller's access to its linked record`);
|
|
15
|
+
return read;
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
export { lookupReadOf, withLookupRead };
|
package/dist/scope.d.ts
CHANGED
package/dist/scope.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { QueryError } from "./fields/query-error.js";
|
|
2
|
-
import { columnRef } from "./columns.js";
|
|
3
2
|
import { sql } from "kysely";
|
|
4
3
|
//#region src/scope.ts
|
|
5
4
|
const fullModelOf = (model, scope) => scope?.fullModel ?? model;
|
|
@@ -12,7 +11,7 @@ const tenantCondition = (model, scope) => {
|
|
|
12
11
|
const tenant = full.tenant ?? model.tenant;
|
|
13
12
|
if (!tenant) return void 0;
|
|
14
13
|
const field = Object.hasOwn(full.fields, tenant) ? full.fields[tenant] : void 0;
|
|
15
|
-
const column =
|
|
14
|
+
const column = sql`${sql.id("t", field?.column ?? tenant)}`;
|
|
16
15
|
if (scope?.tenantValues) {
|
|
17
16
|
if (scope.tenantValues.length === 0) return sql`false`;
|
|
18
17
|
return sql`${column} in (${sql.join(scope.tenantValues)})`;
|
package/dist/sort.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export type
|
|
4
|
-
export type SortKey = {
|
|
5
|
-
field: FieldModel;
|
|
6
|
-
direction: Direction;
|
|
7
|
-
};
|
|
8
|
-
//#endregion
|
|
1
|
+
import { Direction, SortKey } from "./sort-key.js";
|
|
2
|
+
import { ResourceModel, SortSpec } from "@protobase/schema";
|
|
3
|
+
export type { SortKey };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/query",
|
|
3
|
-
"version": "0.117.
|
|
3
|
+
"version": "0.117.7",
|
|
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.117.
|
|
32
|
+
"@protobase/schema": "0.117.7",
|
|
33
33
|
"kysely": "^0.29.6"
|
|
34
34
|
}
|
|
35
35
|
}
|