@mistriwork/engine 1.0.0-rc.19 → 1.0.0-rc.20
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/adapters/postgres.d.ts +3 -26
- package/dist/adapters/postgres.js +22 -20
- package/dist/adapters/postgresTransactionOpening.d.ts +39 -0
- package/dist/adapters/postgresTransactionOpening.js +16 -0
- package/dist/adapters/scoped/scopedReads.js +29 -8
- package/dist/adapters/scoped/scopedTransaction.d.ts +3 -1
- package/dist/adapters/scoped/scopedTransaction.js +73 -5
- package/dist/adapters/scoped/triggerProof.js +5 -1
- package/dist/adapters/sqlite.d.ts +1 -0
- package/dist/adapters/sqlite.js +19 -3
- package/dist/server/clientHttp.d.ts +1 -0
- package/dist/server/clientHttp.js +23 -3
- package/dist/server/columnGrants.js +6 -1
- package/dist/server/http/context.d.ts +5 -0
- package/dist/shared/aggregate.d.ts +1 -1
- package/dist/shared/aggregate.js +2 -2
- package/dist/shared/anyRows.d.ts +19 -0
- package/dist/shared/anyRows.js +69 -0
- package/dist/shared/breakdown.d.ts +4 -2
- package/dist/shared/breakdown.js +11 -8
- package/dist/shared/filter.d.ts +40 -1
- package/dist/shared/filter.js +54 -4
- package/dist/shared/rowScope.d.ts +44 -3
- package/dist/shared/rowScope.js +349 -51
- package/dist/shared/servedApp.d.ts +5 -0
- package/dist/shared/theme.js +4 -2
- package/dist/shared/view.d.ts +4 -2
- package/dist/shared/view.js +6 -4
- package/package.json +1 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AnyRows, InlineRows, SqlStyle } from './filter.js';
|
|
2
|
+
import { type DatabaseSchema, type TableMeta } from './schema.js';
|
|
3
|
+
interface ChildRows {
|
|
4
|
+
readonly child: TableMeta;
|
|
5
|
+
/** The EXISTS' join, the child under `alias` and the parent under `parent`: each key compared as the catalog's types allow. */
|
|
6
|
+
join(alias: string, parent: string): string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The child an `any` names, from the table it points at. Refused, never guessed, when the reference is not one the
|
|
10
|
+
* catalog holds, is stitched to another connection, or the child's conditions say more than a child's own columns
|
|
11
|
+
* compared with values (no second `any`, no column on the right, no resolved test, no path).
|
|
12
|
+
*/
|
|
13
|
+
export declare function childRows(schema: DatabaseSchema, parentId: string, rows: AnyRows, style: SqlStyle): ChildRows;
|
|
14
|
+
/**
|
|
15
|
+
* `buildWhere`'s `inline` for a raw read of `parentId`, whose FROM names the row as `parent` (already quoted): the
|
|
16
|
+
* child is read from its own relation, as the parent is, with no scope between.
|
|
17
|
+
*/
|
|
18
|
+
export declare function inlineRows(schema: DatabaseSchema, parentId: string, parent: string, style: SqlStyle, relation: (table: TableMeta) => string): (rows: AnyRows) => InlineRows;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* An `any`/`no` condition read where no view is: a table read by a handler's `db`, a loader's, a policy's. The
|
|
3
|
+
* child is found the way a view finds it (the reference they point at the row through, `child.column`), and the
|
|
4
|
+
* test is written as an EXISTS whose join is built here once, for the two readers that need it — a raw adapter,
|
|
5
|
+
* which reads the child as it reads the parent (unscoped), and a scoped read, which reads it through the reader's
|
|
6
|
+
* row scope (scopedReads.ts) — so the two cannot drift on the join, its operators or what they refuse.
|
|
7
|
+
*/
|
|
8
|
+
import { RequestFault } from './refusal.js';
|
|
9
|
+
import { backrefsTo } from './viewPaths.js';
|
|
10
|
+
import { findTable } from './schema.js';
|
|
11
|
+
/** The alias the child rows stand under inside their EXISTS: the parent is named by its own relation. */
|
|
12
|
+
const CHILD = '_tw_any';
|
|
13
|
+
const refused = () => new RequestFault('This read cannot ask about the rows that point at it.');
|
|
14
|
+
/**
|
|
15
|
+
* The child an `any` names, from the table it points at. Refused, never guessed, when the reference is not one the
|
|
16
|
+
* catalog holds, is stitched to another connection, or the child's conditions say more than a child's own columns
|
|
17
|
+
* compared with values (no second `any`, no column on the right, no resolved test, no path).
|
|
18
|
+
*/
|
|
19
|
+
export function childRows(schema, parentId, rows, style) {
|
|
20
|
+
if (!rows || typeof rows !== 'object' || typeof rows.via !== 'string')
|
|
21
|
+
throw refused();
|
|
22
|
+
const parent = findTable(schema, parentId);
|
|
23
|
+
const match = backrefsTo(schema, parentId).find((b) => `${b.child}.${b.fk.from.columns.join('+')}` === rows.via);
|
|
24
|
+
const child = match && findTable(schema, match.child);
|
|
25
|
+
if (!parent || !match || !child || match.fk.stitched)
|
|
26
|
+
throw refused();
|
|
27
|
+
for (const group of rows.filter?.groups ?? []) {
|
|
28
|
+
for (const c of group) {
|
|
29
|
+
if (c.any !== undefined || c.other !== undefined || c.via !== undefined || c.equality !== undefined)
|
|
30
|
+
throw refused();
|
|
31
|
+
if (typeof c.column !== 'string' || !child.columns.some((column) => column.name === c.column))
|
|
32
|
+
throw new RequestFault('Unknown column in a related-rows test.');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const { fk } = match;
|
|
36
|
+
if (fk.from.columns.length !== fk.to.columns.length)
|
|
37
|
+
throw refused();
|
|
38
|
+
const typeOf = (table, name) => table.columns.find((column) => column.name === name)?.type ?? '';
|
|
39
|
+
return {
|
|
40
|
+
child,
|
|
41
|
+
join: (alias, ref) => fk.to.columns
|
|
42
|
+
.map((to, at) => {
|
|
43
|
+
const from = fk.from.columns[at];
|
|
44
|
+
/* The database's own operator where both sides are built-in types, as a column compared with a column is
|
|
45
|
+
(filter.ts): a bare `=` resolves through the search_path. */
|
|
46
|
+
const op = style.builtin?.(typeOf(child, from)) && style.builtin?.(typeOf(parent, to)) ? 'OPERATOR(pg_catalog.=)' : '=';
|
|
47
|
+
return `${alias}.${style.quote(from)} ${op} ${ref}.${style.quote(to)}`;
|
|
48
|
+
})
|
|
49
|
+
.join(' AND '),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* `buildWhere`'s `inline` for a raw read of `parentId`, whose FROM names the row as `parent` (already quoted): the
|
|
54
|
+
* child is read from its own relation, as the parent is, with no scope between.
|
|
55
|
+
*/
|
|
56
|
+
export function inlineRows(schema, parentId, parent, style, relation) {
|
|
57
|
+
return (rows) => {
|
|
58
|
+
const { child, join } = childRows(schema, parentId, rows, style);
|
|
59
|
+
const alias = style.quote(CHILD);
|
|
60
|
+
return {
|
|
61
|
+
relation: relation(child),
|
|
62
|
+
alias,
|
|
63
|
+
on: join(alias, parent),
|
|
64
|
+
column: (name) => `${alias}.${style.quote(name)}`,
|
|
65
|
+
/* Typed as the scoped read types them, so a child's comparison names the built-in operator there too. */
|
|
66
|
+
types: (name) => child.columns.find((column) => column.name === name)?.type,
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -176,7 +176,9 @@ interface CompiledBreakdown {
|
|
|
176
176
|
* then measures, then the filter, then the sort — so aliases are handed out in
|
|
177
177
|
* first-use order and the same breakdown always compiles to the same text.
|
|
178
178
|
*/
|
|
179
|
-
export declare function compileBreakdown(schema: DatabaseSchema, def: BreakdownDef, style: SqlStyle, offset?: number | unknown, scope?: RowScope
|
|
179
|
+
export declare function compileBreakdown(schema: DatabaseSchema, def: BreakdownDef, style: SqlStyle, offset?: number | unknown, scope?: RowScope,
|
|
180
|
+
/** The catalog a row scope's `via` targets resolve in, where the reader's own is pruned (rowScope.ts ScopedRelations). */
|
|
181
|
+
policy?: DatabaseSchema): CompiledBreakdown;
|
|
180
182
|
/** Who says a catalog cannot change, beside the scope catalog's own captures. */
|
|
181
183
|
export declare function keepBreakdownsAgainst(fixed: (schema: DatabaseSchema) => boolean): void;
|
|
182
184
|
/** For tests: how many breakdowns were compiled rather than found. */
|
|
@@ -196,7 +198,7 @@ export declare function withBreakdownComputed(schema: DatabaseSchema, def: Break
|
|
|
196
198
|
* of the base table. Without the limit and offset, which is the whole point of
|
|
197
199
|
* asking.
|
|
198
200
|
*/
|
|
199
|
-
export declare function compileBreakdownCount(schema: DatabaseSchema, def: BreakdownDef, style: SqlStyle, scope?: RowScope, limit?: number): {
|
|
201
|
+
export declare function compileBreakdownCount(schema: DatabaseSchema, def: BreakdownDef, style: SqlStyle, scope?: RowScope, limit?: number, policy?: DatabaseSchema): {
|
|
200
202
|
text: string;
|
|
201
203
|
params: unknown[];
|
|
202
204
|
};
|
package/dist/shared/breakdown.js
CHANGED
|
@@ -95,7 +95,9 @@ const isDateish = (type) => /date|time/i.test(type ?? '');
|
|
|
95
95
|
* then measures, then the filter, then the sort — so aliases are handed out in
|
|
96
96
|
* first-use order and the same breakdown always compiles to the same text.
|
|
97
97
|
*/
|
|
98
|
-
export function compileBreakdown(schema, def, style, offset = 0, scope
|
|
98
|
+
export function compileBreakdown(schema, def, style, offset = 0, scope,
|
|
99
|
+
/** The catalog a row scope's `via` targets resolve in, where the reader's own is pruned (rowScope.ts ScopedRelations). */
|
|
100
|
+
policy = schema) {
|
|
99
101
|
/* A breakdown compiles to the same statement against the same catalog, and an App's enter asks for each of its
|
|
100
102
|
rail's and dashboard's breakdowns several times — against the reader's pruned catalog and the full one for
|
|
101
103
|
the access check (columnGrants.ts), under the reader's scope to run it, and again for the SQL pane — each a
|
|
@@ -103,10 +105,11 @@ export function compileBreakdown(schema, def, style, offset = 0, scope) {
|
|
|
103
105
|
whose text says all of it (breakdownText), and only where compiling read neither the clock nor the reader
|
|
104
106
|
(query.ts readsOf): a statement that wrote a request's instant, day or email is that request's, and is never
|
|
105
107
|
handed to another. */
|
|
106
|
-
|
|
108
|
+
// A scope read through another catalog than the one it is kept against is no key of it: compiled afresh.
|
|
109
|
+
const text = typeof offset === 'number' && policy === schema && fixedCatalog(schema) ? breakdownText(def) : undefined;
|
|
107
110
|
if (text === undefined) {
|
|
108
111
|
breakdownsCompiled++;
|
|
109
|
-
return compileBreakdownFresh(schema, def, style, offset, scope);
|
|
112
|
+
return compileBreakdownFresh(schema, def, style, offset, scope, policy);
|
|
110
113
|
}
|
|
111
114
|
let byStyle = breakdownsKept.get(schema);
|
|
112
115
|
if (!byStyle)
|
|
@@ -122,7 +125,7 @@ export function compileBreakdown(schema, def, style, offset = 0, scope) {
|
|
|
122
125
|
}
|
|
123
126
|
else {
|
|
124
127
|
breakdownsCompiled++;
|
|
125
|
-
const read = readsOf(() => compileBreakdownFresh(schema, def, style, offset, scope));
|
|
128
|
+
const read = readsOf(() => compileBreakdownFresh(schema, def, style, offset, scope, policy));
|
|
126
129
|
if (read.clock !== 0 || read.reader)
|
|
127
130
|
return read.value;
|
|
128
131
|
found = read.value;
|
|
@@ -184,7 +187,7 @@ const scopeOrdinal = (scope) => {
|
|
|
184
187
|
scopeOrdinals.set(scope, (ordinal = ++scopesSeen));
|
|
185
188
|
return ordinal;
|
|
186
189
|
};
|
|
187
|
-
function compileBreakdownFresh(schema, def, style, offset, scope) {
|
|
190
|
+
function compileBreakdownFresh(schema, def, style, offset, scope, policy) {
|
|
188
191
|
// The App's soft keys, where this catalog is the database's own (declaredReferences.ts).
|
|
189
192
|
schema = withViewReferences(schema, def.references);
|
|
190
193
|
const base = findTable(schema, def.base);
|
|
@@ -203,7 +206,7 @@ function compileBreakdownFresh(schema, def, style, offset, scope) {
|
|
|
203
206
|
if (!def.measures?.length) {
|
|
204
207
|
throw new PathError('A breakdown needs something to measure — `count`, or `sum total`.', '', '', 0);
|
|
205
208
|
}
|
|
206
|
-
const relations = new ScopedRelations(schema, style, scope);
|
|
209
|
+
const relations = new ScopedRelations(schema, style, scope, policy);
|
|
207
210
|
const baseRelation = relations.table(base.id);
|
|
208
211
|
const plan = new JoinPlan(schema, base, style, relations.table);
|
|
209
212
|
/* The view compiler's paths, so a rollup — the base's or through a
|
|
@@ -516,8 +519,8 @@ export function withBreakdownComputed(schema, def) {
|
|
|
516
519
|
* of the base table. Without the limit and offset, which is the whole point of
|
|
517
520
|
* asking.
|
|
518
521
|
*/
|
|
519
|
-
export function compileBreakdownCount(schema, def, style, scope, limit) {
|
|
520
|
-
const inner = compileBreakdown(schema, def, style, 0, scope);
|
|
522
|
+
export function compileBreakdownCount(schema, def, style, scope, limit, policy = schema) {
|
|
523
|
+
const inner = compileBreakdown(schema, def, style, 0, scope, policy);
|
|
521
524
|
/* The LIMIT/OFFSET line is the last one and carries the last two
|
|
522
525
|
parameters. Dropped by rebuilding rather than by string surgery would be
|
|
523
526
|
nicer; it is the last line by construction, and the test says so. */
|
package/dist/shared/filter.d.ts
CHANGED
|
@@ -40,6 +40,40 @@ export interface Condition {
|
|
|
40
40
|
* and everywhere else it is refused.
|
|
41
41
|
*/
|
|
42
42
|
equality?: ConditionEquality;
|
|
43
|
+
/**
|
|
44
|
+
* The rows of another relation hold a match for this row's key: a row scope's `via` (rowScope.ts), already written as
|
|
45
|
+
* the `EXISTS (…)` over the target's scoped relation, which binds nothing of its own (a target's filter is a CTE
|
|
46
|
+
* there). SQL text, so only a builder that asks for it (`buildWhere`'s `via`) compiles it, and everywhere else it is
|
|
47
|
+
* refused, never dropped.
|
|
48
|
+
*/
|
|
49
|
+
via?: ConditionVia;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The child rows of an `any` condition, as a builder that writes the test inline resolved them (`buildWhere`'s
|
|
53
|
+
* `inline`): the relation they are read from, the alias it stands under, the join to the row asked about, and how
|
|
54
|
+
* the child's own columns are written. The child's conditions are bound by `buildWhere`, in the statement's order.
|
|
55
|
+
*/
|
|
56
|
+
export interface InlineRows {
|
|
57
|
+
readonly relation: string;
|
|
58
|
+
readonly alias: string;
|
|
59
|
+
/** The join to the row asked about, written with the alias and the row's own name. */
|
|
60
|
+
readonly on: string;
|
|
61
|
+
/** One of the child's own columns, qualified; refuses a name the child does not hold. */
|
|
62
|
+
readonly column: (name: string) => string;
|
|
63
|
+
/** A child column's catalog type, where the style names its operators by schema. */
|
|
64
|
+
readonly types?: (name: string) => string | undefined;
|
|
65
|
+
}
|
|
66
|
+
/** Whether a filter asks about the rows that point at the one read (`any`/`no`) anywhere. */
|
|
67
|
+
export declare const asksAboutRows: (filter: Filter | undefined) => boolean;
|
|
68
|
+
/**
|
|
69
|
+
* A filter whose `any`/`no` children are named in another namespace: a layer that renames the tables of a request
|
|
70
|
+
* (an App's resources to the database's, a loader's local names to the catalog's) renames the child as it renames the
|
|
71
|
+
* table, since the child is a table of the same read. A child the rename does not know throws, as a table would.
|
|
72
|
+
*/
|
|
73
|
+
export declare function renamedAnyRows(filter: Filter | undefined, rename: (table: string) => string): Filter | undefined;
|
|
74
|
+
/** A relationship test (`Condition.via`) as the builder that resolved it wrote it. */
|
|
75
|
+
interface ConditionVia {
|
|
76
|
+
readonly text: string;
|
|
43
77
|
}
|
|
44
78
|
/** A foreign key's comparison of one column (`Condition.equality`): each part SQL as the catalog gave it. */
|
|
45
79
|
export interface ConditionEquality {
|
|
@@ -253,5 +287,10 @@ exists?: (rows: AnyRows) => string,
|
|
|
253
287
|
references?: boolean,
|
|
254
288
|
/** The catalog's type of a column the filter names, where the style names its operators by schema
|
|
255
289
|
* (SqlStyle.builtin): an App's scoped reads. Absent, or undefined for a name, the operator as written. */
|
|
256
|
-
types?: (name: string) => string | undefined
|
|
290
|
+
types?: (name: string) => string | undefined,
|
|
291
|
+
/** Whether a condition may carry a resolved relationship test (`Condition.via`): a row scope's own relations only. */
|
|
292
|
+
via?: boolean,
|
|
293
|
+
/** How an `any` condition is written when its child's values are bound here, in order, with the rest of the
|
|
294
|
+
filter: a read that resolves the child itself (anyRows.ts). Absent, and without `exists`, one is refused. */
|
|
295
|
+
inline?: (rows: AnyRows) => InlineRows): SqlFragment;
|
|
257
296
|
export {};
|
package/dist/shared/filter.js
CHANGED
|
@@ -5,6 +5,27 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { LIKE_ESCAPE, likePattern } from './like.js';
|
|
7
7
|
import { RequestFault } from './refusal.js';
|
|
8
|
+
/** Whether a filter asks about the rows that point at the one read (`any`/`no`) anywhere. */
|
|
9
|
+
export const asksAboutRows = (filter) => filter?.groups.some((group) => group.some((condition) => condition.any !== undefined)) === true;
|
|
10
|
+
/**
|
|
11
|
+
* A filter whose `any`/`no` children are named in another namespace: a layer that renames the tables of a request
|
|
12
|
+
* (an App's resources to the database's, a loader's local names to the catalog's) renames the child as it renames the
|
|
13
|
+
* table, since the child is a table of the same read. A child the rename does not know throws, as a table would.
|
|
14
|
+
*/
|
|
15
|
+
export function renamedAnyRows(filter, rename) {
|
|
16
|
+
if (!filter || !asksAboutRows(filter))
|
|
17
|
+
return filter;
|
|
18
|
+
return {
|
|
19
|
+
...filter,
|
|
20
|
+
groups: filter.groups.map((group) => group.map((condition) => {
|
|
21
|
+
if (condition.any === undefined)
|
|
22
|
+
return condition;
|
|
23
|
+
const { via } = condition.any;
|
|
24
|
+
const at = via.lastIndexOf('.');
|
|
25
|
+
return { ...condition, any: { ...condition.any, via: `${rename(via.slice(0, at))}${via.slice(at)}` } };
|
|
26
|
+
})),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
8
29
|
/* A type as the scope catalog names one (postgresScopeCatalog.ts sqlType): a chain of identifiers, each a word or
|
|
9
30
|
quoted, then at most a modifier of one or two integers and the brackets of an array — `pg_catalog."numeric"(10,2)`,
|
|
10
31
|
`"s"."my type"`, `pg_catalog."varchar"(3)[]`. Nothing else, so no text can end the cast and start an expression;
|
|
@@ -216,7 +237,12 @@ exists,
|
|
|
216
237
|
references = false,
|
|
217
238
|
/** The catalog's type of a column the filter names, where the style names its operators by schema
|
|
218
239
|
* (SqlStyle.builtin): an App's scoped reads. Absent, or undefined for a name, the operator as written. */
|
|
219
|
-
types
|
|
240
|
+
types,
|
|
241
|
+
/** Whether a condition may carry a resolved relationship test (`Condition.via`): a row scope's own relations only. */
|
|
242
|
+
via = false,
|
|
243
|
+
/** How an `any` condition is written when its child's values are bound here, in order, with the rest of the
|
|
244
|
+
filter: a read that resolves the child itself (anyRows.ts). Absent, and without `exists`, one is refused. */
|
|
245
|
+
inline) {
|
|
220
246
|
if (!filter?.groups.length)
|
|
221
247
|
return { text: '', params: [] };
|
|
222
248
|
const params = [];
|
|
@@ -251,14 +277,37 @@ types) {
|
|
|
251
277
|
if (c.collation !== undefined && !COMPARISONS.has(c.op)) {
|
|
252
278
|
throw new RequestFault('Only a comparison can compare text by bytes.');
|
|
253
279
|
}
|
|
280
|
+
/* Before the column is written: a relationship test names its own columns and binds nothing. */
|
|
281
|
+
if (c.via !== undefined) {
|
|
282
|
+
if (!via || typeof c.via.text !== 'string')
|
|
283
|
+
throw new RequestFault('This read cannot ask about a related row.');
|
|
284
|
+
return c.via.text;
|
|
285
|
+
}
|
|
254
286
|
/* Before the column is written: child rows are no column of this row. Their
|
|
255
287
|
values are bound where their relation is planned (rowScope.ts `filtered`),
|
|
256
288
|
so the EXISTS carries no parameter here and the numbering is untouched. */
|
|
257
289
|
if (c.any !== undefined) {
|
|
258
|
-
if (!exists || typeof c.any !== 'object' || c.any === null)
|
|
290
|
+
if ((!exists && !inline) || typeof c.any !== 'object' || c.any === null)
|
|
259
291
|
throw new RequestFault('This read cannot ask about the rows that point at it.');
|
|
292
|
+
let text;
|
|
293
|
+
if (exists)
|
|
294
|
+
text = exists(c.any);
|
|
295
|
+
else {
|
|
296
|
+
/* The child's own values are bound here, after everything before it and before everything after: the
|
|
297
|
+
text and the parameters are written in one order, which is the only thing a `$n` needs. */
|
|
298
|
+
const rows = inline(c.any);
|
|
299
|
+
let narrowed = '';
|
|
300
|
+
if (c.any.filter?.groups.length) {
|
|
301
|
+
/* No hooks: a second `any` inside, a column on the right and a resolved test are refused, never dropped. */
|
|
302
|
+
const inner = buildWhere(c.any.filter, style, index, rows.column, undefined, undefined, undefined, false, rows.types);
|
|
303
|
+
params.push(...inner.params);
|
|
304
|
+
index += inner.params.length;
|
|
305
|
+
narrowed = inner.text ? ` AND (${inner.text.slice(' WHERE '.length)})` : '';
|
|
306
|
+
}
|
|
307
|
+
text = `EXISTS (SELECT 1 FROM ${rows.relation} ${rows.alias} WHERE ${rows.on}${narrowed})`;
|
|
308
|
+
}
|
|
260
309
|
/* `no task …` is the same rows asked the other way: none of them matches. */
|
|
261
|
-
return c.op === 'isNull' ? `NOT ${
|
|
310
|
+
return c.op === 'isNull' ? `NOT ${text}` : text;
|
|
262
311
|
}
|
|
263
312
|
/* Before the column is written: a record's tags are no column. */
|
|
264
313
|
if (c.op === 'has') {
|
|
@@ -375,7 +424,8 @@ function sameCondition(a, b) {
|
|
|
375
424
|
a.collation !== b.collation ||
|
|
376
425
|
a.other !== b.other ||
|
|
377
426
|
a.any !== b.any ||
|
|
378
|
-
a.equality !== b.equality
|
|
427
|
+
a.equality !== b.equality ||
|
|
428
|
+
a.via?.text !== b.via?.text)
|
|
379
429
|
return false;
|
|
380
430
|
if (Array.isArray(a.value) || Array.isArray(b.value)) {
|
|
381
431
|
return (Array.isArray(a.value) &&
|
|
@@ -1,15 +1,30 @@
|
|
|
1
|
-
import { type Filter, type SqlFragment, type SqlStyle } from './filter.js';
|
|
1
|
+
import { type Condition, type Filter, type SqlFragment, type SqlStyle } from './filter.js';
|
|
2
2
|
import { type DatabaseSchema } from './schema.js';
|
|
3
3
|
/** Internal, server-owned compiler input; never a member of a request definition.
|
|
4
4
|
* Exact catalog IDs only. With a scope present, missing resources deny rows.
|
|
5
5
|
* `true` explicitly allows all rows; filters address local columns, not paths.
|
|
6
6
|
* This primitive alone does not enable App authorization or tenant isolation.
|
|
7
7
|
*/
|
|
8
|
-
export interface RowScopeFilter
|
|
8
|
+
export interface RowScopeFilter {
|
|
9
|
+
groups: Array<Array<Condition | ViaCondition>>;
|
|
9
10
|
/** Private SQLite comparison metadata, never SQL or a request filter field.
|
|
10
11
|
* Applies BINARY collation to these local columns; LIKE keeps its own semantics. */
|
|
11
12
|
readonly binary?: readonly string[];
|
|
12
13
|
}
|
|
14
|
+
/** The rows of another resource this row's key must be among: the ones this reader may see there. */
|
|
15
|
+
export interface RowScopeVia {
|
|
16
|
+
readonly resource: string;
|
|
17
|
+
/** The target's key; equal in length to the condition's `column` (a composite key is compared pairwise). */
|
|
18
|
+
readonly column: string | readonly string[];
|
|
19
|
+
/** Narrows the target's rows further (never wider): groups are ORed, conditions in one ANDed, literal conditions only. */
|
|
20
|
+
readonly where?: readonly (readonly Condition[])[];
|
|
21
|
+
}
|
|
22
|
+
/** `column in <the rows of via.resource this reader may see>`: compiled to an EXISTS over the target's scoped relation. */
|
|
23
|
+
export interface ViaCondition {
|
|
24
|
+
readonly column: string | readonly string[];
|
|
25
|
+
readonly op: 'in';
|
|
26
|
+
readonly via: RowScopeVia;
|
|
27
|
+
}
|
|
13
28
|
export type RowScope = Readonly<Record<string, boolean | RowScopeFilter>>;
|
|
14
29
|
/** Composition must budget expanded data against the same capture limits. */
|
|
15
30
|
export declare const ROW_SCOPE_LIMITS: Readonly<{
|
|
@@ -20,6 +35,19 @@ export declare const ROW_SCOPE_LIMITS: Readonly<{
|
|
|
20
35
|
string: 16384;
|
|
21
36
|
characters: 262144;
|
|
22
37
|
}>;
|
|
38
|
+
/**
|
|
39
|
+
* `via('support_case_id', 'support_case')`: the row shows when its `support_case_id` is the `id` of a `support_case` row
|
|
40
|
+
* the reader may see. The target's key defaults to `id`; a composite key is arrays of equal length
|
|
41
|
+
* (`via(['tenant_id', 'case_no'], 'support_case', ['tenant_id', 'no'])`). `where` narrows the target's rows further
|
|
42
|
+
* (`[[{ column: 'status', op: '=', value: 'open' }]]`): never wider, since it filters the scoped relation.
|
|
43
|
+
*/
|
|
44
|
+
export declare function via(column: string | readonly string[], resource: string, key?: string | readonly string[], where?: RowScopeVia['where']): ViaCondition;
|
|
45
|
+
/** A rule with every `via` target renamed by `rename` (a resource's id to its physical table's): the condition's own columns are the table's, and stay. */
|
|
46
|
+
export declare function renameViaTargets(rule: RowScope[string], rename: (resource: string) => string): RowScope[string];
|
|
47
|
+
/** A `via` whose target key, or whose own column, no index leads: an EXISTS per row is then a scan per row. Advice for
|
|
48
|
+
* the operator, never a refusal: slow, not wrong. Read off the catalog's own index list, so a table whose adapter does
|
|
49
|
+
* not report indexes (`indexed` absent) is not judged. One line per rule, in policy order. */
|
|
50
|
+
export declare function rowScopeViaAdvisories(schema: DatabaseSchema, scope: RowScope): string[];
|
|
23
51
|
/** Copy before validation so accessors, prototypes and later mutation cannot
|
|
24
52
|
* change what was checked. Bounds apply to the entire scope, not each table.
|
|
25
53
|
* Frozen nested values stay private; Readonly<Record> alone is not a snapshot.
|
|
@@ -53,8 +81,20 @@ export declare class ScopedRelations {
|
|
|
53
81
|
private readonly params;
|
|
54
82
|
private finished;
|
|
55
83
|
private ordinal;
|
|
56
|
-
|
|
84
|
+
/** The catalog a `via` target resolves against: the policy's own, whole. A read's catalog is pruned to what the
|
|
85
|
+
* reader may read, and a membership table they may not read (or a column of one) is missing there; reading it
|
|
86
|
+
* through a `via` only decides whether a row shows, which is what a policy author is allowed to decide. */
|
|
87
|
+
private readonly whole;
|
|
88
|
+
private recursive;
|
|
89
|
+
private readonly building;
|
|
90
|
+
constructor(schema: DatabaseSchema, style: SqlStyle, scope?: RowScope, policy?: DatabaseSchema);
|
|
91
|
+
/** A table's meta, from the read's catalog and failing that the policy's (a `via` target the reader cannot read). */
|
|
92
|
+
private meta;
|
|
93
|
+
private fresh;
|
|
94
|
+
private related;
|
|
95
|
+
/** A table's relation under the scope; a table this read's catalog does not hold is refused, though a `via` may read it. */
|
|
57
96
|
table: (id: string) => string;
|
|
97
|
+
private relation;
|
|
58
98
|
private readonly narrowed;
|
|
59
99
|
/**
|
|
60
100
|
* A table's rows narrowed by an authored filter over its own columns — the
|
|
@@ -68,5 +108,6 @@ export declare class ScopedRelations {
|
|
|
68
108
|
* narrows within the reader's rows, never beside them.
|
|
69
109
|
*/
|
|
70
110
|
filtered: (id: string, filter: Filter, written?: Readonly<Record<string, (own: (column: string) => string) => string>>) => string;
|
|
111
|
+
private narrow;
|
|
71
112
|
finish(): SqlFragment;
|
|
72
113
|
}
|