@protobase/query 0.129.1 → 0.130.1
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.d.ts +7 -1
- package/dist/index.js +6 -1
- package/dist/table-scope/organization-source.d.ts +18 -0
- package/dist/table-scope/organization-source.js +12 -0
- package/dist/table-scope/raw-tables.d.ts +15 -0
- package/dist/table-scope/raw-tables.js +60 -0
- package/dist/table-scope/scope-condition.d.ts +10 -0
- package/dist/table-scope/scope-condition.js +34 -0
- package/dist/table-scope/scope-insert.js +90 -0
- package/dist/table-scope/scope-plugin.d.ts +17 -0
- package/dist/table-scope/scope-plugin.js +26 -0
- package/dist/table-scope/scope-transformer.js +175 -0
- package/dist/table-scope/scoped-db.d.ts +28 -0
- package/dist/table-scope/scoped-db.js +65 -0
- package/dist/table-scope/scoped-tables.d.ts +21 -0
- package/dist/table-scope/scoped-tables.js +33 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -30,4 +30,10 @@ import { lookupRead } from "./linked-read.js";
|
|
|
30
30
|
import { LookupCoverage, lookupQueries, uncoveredLookupMatch } from "./lookup-coverage.js";
|
|
31
31
|
import { readsParent } from "./reads-parent.js";
|
|
32
32
|
import { SeekRequest, seekToPosition } from "./seek-to-position.js";
|
|
33
|
-
|
|
33
|
+
import { ScopedTable, ScopedTables } from "./table-scope/scoped-tables.js";
|
|
34
|
+
import { Organization, ScopeRefused } from "./table-scope/scope-condition.js";
|
|
35
|
+
import { EveryOrganization, OrganizationSource, ScopeOrganization, everyOrganization, isEveryOrganization } from "./table-scope/organization-source.js";
|
|
36
|
+
import { ScopedDb, scopedDb } from "./table-scope/scoped-db.js";
|
|
37
|
+
import { ScopePlugin } from "./table-scope/scope-plugin.js";
|
|
38
|
+
import { scoped, unscoped } from "./table-scope/raw-tables.js";
|
|
39
|
+
export { type ColumnUse, type CompileOptions, type CoverageQuery, type Db, type EveryOrganization, 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 Organization, type OrganizationSource, type ParentRead, type PerValue, QueryError, type QueryErrorCode, type RelatedPage, type RelatedPageRequest, type RelatedRowsQuery, type ScanRequests, type ScanResults, type Scope, type ScopeOrganization, ScopePlugin, ScopeRefused, type ScopedDb, type ScopedTable, type ScopedTables, type ScrollMode, type SeekRequest, type SeriesOptions, type SeriesPoint, type SplitPoint, type SplitSeriesOptions, type TableIndex, type TableIndexes, type UncoveredColumn, aggregateScan, buildListQuery, buildRelatedRows, cellsAcross, checkMapPosition, columnRef, compileFilter, countUpTo, estimateCount, everyOrganization, exactCount, facetCounts, groupTotals, histogram, indexServes, isEveryOrganization, listPerValue, listQuery, lookupQueries, lookupRead, lookupReadOf, mapBuckets, mapCellPixels, maxLimit, maxMapBuckets, maxMapZoom, parentCondition, readIdentity, readsParent, relatedPage, relatedRows, scoped, scopedDb, scrollMode, seekToPosition, series, splitSeries, tableIndexes, tenantCondition, uncoveredColumns, uncoveredFileColumns, uncoveredLookupMatch, unscoped, withLookupRead, withoutField };
|
package/dist/index.js
CHANGED
|
@@ -27,4 +27,9 @@ import { lookupRead } from "./linked-read.js";
|
|
|
27
27
|
import { lookupQueries, uncoveredLookupMatch } from "./lookup-coverage.js";
|
|
28
28
|
import { readsParent } from "./reads-parent.js";
|
|
29
29
|
import { seekToPosition } from "./seek-to-position.js";
|
|
30
|
-
|
|
30
|
+
import { everyOrganization, isEveryOrganization } from "./table-scope/organization-source.js";
|
|
31
|
+
import { ScopeRefused } from "./table-scope/scope-condition.js";
|
|
32
|
+
import { scoped, unscoped } from "./table-scope/raw-tables.js";
|
|
33
|
+
import { ScopePlugin } from "./table-scope/scope-plugin.js";
|
|
34
|
+
import { scopedDb } from "./table-scope/scoped-db.js";
|
|
35
|
+
export { QueryError, ScopePlugin, ScopeRefused, aggregateScan, buildListQuery, buildRelatedRows, cellsAcross, checkMapPosition, columnRef, compileFilter, countUpTo, estimateCount, everyOrganization, exactCount, facetCounts, groupTotals, histogram, indexServes, isEveryOrganization, listPerValue, listQuery, lookupQueries, lookupRead, lookupReadOf, mapBuckets, mapCellPixels, maxLimit, maxMapBuckets, maxMapZoom, parentCondition, readIdentity, readsParent, relatedPage, relatedRows, scoped, scopedDb, scrollMode, seekToPosition, series, splitSeries, tableIndexes, tenantCondition, uncoveredColumns, uncoveredFileColumns, uncoveredLookupMatch, unscoped, withLookupRead, withoutField };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Organization } from "./scope-condition.js";
|
|
2
|
+
//#region src/table-scope/organization-source.d.ts
|
|
3
|
+
/** Every organization's rows, on purpose: `unscoped` says why, for whoever reviews it. */
|
|
4
|
+
export type EveryOrganization = {
|
|
5
|
+
readonly unscoped: string;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Where the scope finds the organization a query works in, read as each query is built, such as from the request it
|
|
9
|
+
* runs for: an organization, none, or every organization (`EveryOrganization`).
|
|
10
|
+
*/
|
|
11
|
+
export type OrganizationSource = () => Organization | EveryOrganization;
|
|
12
|
+
/** The scope's organization: a fixed one, or one an `OrganizationSource` gives at the time. */
|
|
13
|
+
export type ScopeOrganization = Organization | OrganizationSource;
|
|
14
|
+
/** Whether a query works in every organization. */
|
|
15
|
+
export declare const isEveryOrganization: (organization: Organization | EveryOrganization) => organization is EveryOrganization;
|
|
16
|
+
/** Every organization's rows, saying why; refused without a reason. */
|
|
17
|
+
export declare const everyOrganization: (reason: string) => EveryOrganization;
|
|
18
|
+
//#endregion
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/table-scope/organization-source.ts
|
|
2
|
+
/** The organization a query works in now. */
|
|
3
|
+
const currentOrganization = (organization) => typeof organization === "function" ? organization() : organization;
|
|
4
|
+
/** Whether a query works in every organization. */
|
|
5
|
+
const isEveryOrganization = (organization) => typeof organization === "object" && organization !== null;
|
|
6
|
+
/** Every organization's rows, saying why; refused without a reason. */
|
|
7
|
+
const everyOrganization = (reason) => {
|
|
8
|
+
if (typeof reason !== "string" || reason.trim() === "") throw new Error("every organization needs a reason: say why this work reads every organization");
|
|
9
|
+
return { unscoped: reason };
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
export { currentOrganization, everyOrganization, isEveryOrganization };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import "./scoped-tables.js";
|
|
2
|
+
import { RawNode, SelectionNode, TableNode, WhereNode } from "kysely";
|
|
3
|
+
//#region src/table-scope/raw-tables.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A scoped table in raw `sql`, as the organization's rows: `sql\`select id from ${scoped('auth.member')} as m\``. The
|
|
6
|
+
* table is `schema.table` or `table`, as `sql.table` takes it. Without the scope on the database it runs on, the
|
|
7
|
+
* query fails.
|
|
8
|
+
*/
|
|
9
|
+
export declare const scoped: (table: string) => import("kysely").RawBuilder<unknown>;
|
|
10
|
+
/**
|
|
11
|
+
* A scoped table in raw `sql` with every organization's rows, on purpose: `reason` says why, for whoever reviews it.
|
|
12
|
+
* `db.unscoped(reason)` is the same for a whole query.
|
|
13
|
+
*/
|
|
14
|
+
export declare const unscoped: (reason: string, table: string) => import("kysely").RawBuilder<unknown>;
|
|
15
|
+
//#endregion
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ScopeRefused } from "./scope-condition.js";
|
|
2
|
+
import { scopedTableNamed } from "./scoped-tables.js";
|
|
3
|
+
import { IdentifierNode, RawNode, SelectAllNode, SelectQueryNode, SelectionNode, TableNode, WhereNode, sql } from "kysely";
|
|
4
|
+
//#region src/table-scope/raw-tables.ts
|
|
5
|
+
const scopedFragments = ["scoped(", ")"];
|
|
6
|
+
const unscopedFragments = ["/* every organization: unscoped() */ ", ""];
|
|
7
|
+
/**
|
|
8
|
+
* A scoped table in raw `sql`, as the organization's rows: `sql\`select id from ${scoped('auth.member')} as m\``. The
|
|
9
|
+
* table is `schema.table` or `table`, as `sql.table` takes it. Without the scope on the database it runs on, the
|
|
10
|
+
* query fails.
|
|
11
|
+
*/
|
|
12
|
+
const scoped = (table) => sql`scoped(${sql.table(table)})`;
|
|
13
|
+
/**
|
|
14
|
+
* A scoped table in raw `sql` with every organization's rows, on purpose: `reason` says why, for whoever reviews it.
|
|
15
|
+
* `db.unscoped(reason)` is the same for a whole query.
|
|
16
|
+
*/
|
|
17
|
+
const unscoped = (reason, table) => {
|
|
18
|
+
if (reason.trim() === "") throw new Error("unscoped needs a reason: say why this query reads every organization");
|
|
19
|
+
const parts = Object.assign([...unscopedFragments], { raw: [...unscopedFragments] });
|
|
20
|
+
return sql(parts, sql.table(table));
|
|
21
|
+
};
|
|
22
|
+
const fragmentsAre = (node, fragments) => node.sqlFragments.length === fragments.length && node.sqlFragments.every((part, i) => part === fragments[i]);
|
|
23
|
+
const tableOf = (node) => {
|
|
24
|
+
if (node && RawNode.is(node) && fragmentsAre(node, ["", ""]) && node.parameters.length === 1) return tableOf(node.parameters[0]);
|
|
25
|
+
return node && TableNode.is(node) ? node : void 0;
|
|
26
|
+
};
|
|
27
|
+
/** The table `scoped(t)` wraps, when the node is one. */
|
|
28
|
+
const scopedMark = (node) => fragmentsAre(node, scopedFragments) && node.parameters.length === 1 ? tableOf(node.parameters[0]) : void 0;
|
|
29
|
+
/** Whether the node is `unscoped(reason, t)`. */
|
|
30
|
+
const isUnscopedMark = (node) => fragmentsAre(node, unscopedFragments);
|
|
31
|
+
/** `(select * from <table> where <condition>)`, what `scoped(t)` prints. */
|
|
32
|
+
const scopedRows = (table, condition) => ({
|
|
33
|
+
...SelectQueryNode.createFrom([table]),
|
|
34
|
+
selections: [SelectionNode.create(SelectAllNode.create())],
|
|
35
|
+
where: WhereNode.create(condition)
|
|
36
|
+
});
|
|
37
|
+
const idsOf = (node) => {
|
|
38
|
+
if (node.parameters.length === 0 || !node.parameters.every(IdentifierNode.is)) return;
|
|
39
|
+
const inner = node.sqlFragments.slice(1, -1);
|
|
40
|
+
if (node.sqlFragments[0] !== "" || node.sqlFragments.at(-1) !== "" || !inner.every((part) => part === ".")) return;
|
|
41
|
+
return node.parameters.map(({ name }) => name);
|
|
42
|
+
};
|
|
43
|
+
const idNamesScoped = (scope, ids, shadowing) => {
|
|
44
|
+
if (ids.length === 1 && shadowing.has(ids[0])) return false;
|
|
45
|
+
return scopedTableNamed(scope, ids.length >= 2 ? ids[0] : void 0, ids.length >= 2 ? ids[1] : ids[0]) !== void 0;
|
|
46
|
+
};
|
|
47
|
+
const refusal = (name) => new ScopeRefused(`Raw SQL names the scoped table ${name}: write scoped('${name}') for the organization's rows, or unscoped('<reason>', '${name}') for every organization's`);
|
|
48
|
+
/** Refuses raw SQL that names a scoped table through `sql.table` or `sql.id`, outside `scoped()` and `unscoped()`. */
|
|
49
|
+
const checkRawTables = (scope, node, shadowing) => {
|
|
50
|
+
const ids = idsOf(node);
|
|
51
|
+
if (ids && idNamesScoped(scope, ids, shadowing)) throw refusal(ids.join("."));
|
|
52
|
+
for (const parameter of node.parameters) {
|
|
53
|
+
if (!TableNode.is(parameter)) continue;
|
|
54
|
+
const { schema, identifier } = parameter.table;
|
|
55
|
+
if (schema === void 0 && shadowing.has(identifier.name)) continue;
|
|
56
|
+
if (scopedTableNamed(scope, schema?.name, identifier.name)) throw refusal(schema ? `${schema.name}.${identifier.name}` : identifier.name);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
//#endregion
|
|
60
|
+
export { checkRawTables, isUnscopedMark, scoped, scopedMark, scopedRows, unscoped };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import "./scoped-tables.js";
|
|
2
|
+
import { OnNode, TableNode, WhereNode } from "kysely";
|
|
3
|
+
//#region src/table-scope/scope-condition.d.ts
|
|
4
|
+
/** The organization a query works in; undefined when it works in none, which sees no scoped row and writes none. */
|
|
5
|
+
export type Organization = string | number | undefined;
|
|
6
|
+
/** Thrown for a query the scope cannot keep to its organization, before it reaches the database. */
|
|
7
|
+
export declare class ScopeRefused extends Error {
|
|
8
|
+
name: string;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AndNode, BinaryOperationNode, ColumnNode, OnNode, OperatorNode, OrNode, ParensNode, RawNode, ReferenceNode, ValueNode, WhereNode } from "kysely";
|
|
2
|
+
//#region src/table-scope/scope-condition.ts
|
|
3
|
+
/** Thrown for a query the scope cannot keep to its organization, before it reaches the database. */
|
|
4
|
+
var ScopeRefused = class extends Error {
|
|
5
|
+
name = "ScopeRefused";
|
|
6
|
+
};
|
|
7
|
+
/** `<qualifier>.<column> = <organization>`, or `false` without an organization. */
|
|
8
|
+
const scopeCondition = (scoped, qualifier, organization) => organization === void 0 ? RawNode.createWithSql("false") : BinaryOperationNode.create(ReferenceNode.create(ColumnNode.create(scoped.column), qualifier), OperatorNode.create("="), ValueNode.create(organization));
|
|
9
|
+
const sameTable = (a, b) => a.table.identifier.name === b.table.identifier.name && a.table.schema?.name === b.table.schema?.name;
|
|
10
|
+
const conjuncts = (node) => {
|
|
11
|
+
if (AndNode.is(node)) return OrNode.is(node.left) || OrNode.is(node.right) ? [node] : [...conjuncts(node.left), ...conjuncts(node.right)];
|
|
12
|
+
if (ParensNode.is(node)) return conjuncts(node.node);
|
|
13
|
+
return [node];
|
|
14
|
+
};
|
|
15
|
+
const isScopeCondition = (term, scoped, qualifier, organization) => {
|
|
16
|
+
if (organization === void 0) return RawNode.is(term) && term.sqlFragments.length === 1 && term.sqlFragments[0] === "false";
|
|
17
|
+
if (!BinaryOperationNode.is(term) || !OperatorNode.is(term.operator) || term.operator.operator !== "=") return false;
|
|
18
|
+
const { leftOperand: left, rightOperand: right } = term;
|
|
19
|
+
return ReferenceNode.is(left) && ColumnNode.is(left.column) && left.column.column.name === scoped.column && left.table !== void 0 && sameTable(left.table, qualifier) && ValueNode.is(right) && isOrganization(right.value, organization);
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Whether a condition already holds the scope's for this table, as Kysely runs the plugins again over a query it
|
|
23
|
+
* already transformed, such as a subquery built on the same database: the scope then adds nothing.
|
|
24
|
+
*/
|
|
25
|
+
const holdsScope = (condition, scoped, qualifier, organization) => condition !== void 0 && conjuncts(condition).some((term) => isScopeCondition(term, scoped, qualifier, organization));
|
|
26
|
+
const and = (existing, condition) => AndNode.create(ParensNode.create(existing), condition);
|
|
27
|
+
const andWhere = (where, condition) => where ? WhereNode.create(and(where.where, condition)) : WhereNode.create(condition);
|
|
28
|
+
const andOn = (on, condition) => on ? OnNode.create(and(on.on, condition)) : OnNode.create(condition);
|
|
29
|
+
/** Whether a value a query writes is the organization itself, as a literal: anything else is refused. */
|
|
30
|
+
const isOrganization = (value, organization) => organization !== void 0 && (typeof value === "string" || typeof value === "number") && String(value) === String(organization);
|
|
31
|
+
/** The value a node writes, when it is a literal. */
|
|
32
|
+
const literalOf = (node) => ValueNode.is(node) ? { value: node.value } : void 0;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { ScopeRefused, andOn, andWhere, holdsScope, isOrganization, literalOf, scopeCondition };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { ScopeRefused, andWhere, holdsScope, isOrganization, literalOf, scopeCondition } from "./scope-condition.js";
|
|
2
|
+
import { AliasNode, ColumnNode, DefaultInsertValueNode, IdentifierNode, InsertQueryNode, PrimitiveValueListNode, ReferenceNode, SelectAllNode, SelectQueryNode, SelectionNode, TableNode, ValueListNode, ValueNode, ValuesNode } from "kysely";
|
|
3
|
+
//#region src/table-scope/scope-insert.ts
|
|
4
|
+
const tableName = (table) => [table.table.schema?.name, table.table.identifier.name].filter(Boolean).join(".");
|
|
5
|
+
const updatedColumn = (column) => {
|
|
6
|
+
if (ColumnNode.is(column)) return column.column.name;
|
|
7
|
+
if (ReferenceNode.is(column) && ColumnNode.is(column.column)) return column.column.column.name;
|
|
8
|
+
};
|
|
9
|
+
/** Refuses a write of the organization column with anything but the organization itself, as a literal. */
|
|
10
|
+
const checkColumnUpdates = (updates, scoped, organization, table) => {
|
|
11
|
+
for (const update of updates ?? []) {
|
|
12
|
+
const column = updatedColumn(update.column);
|
|
13
|
+
if (column === void 0) throw new ScopeRefused(`An update of ${tableName(table)} sets a column it cannot name: set each column by its name`);
|
|
14
|
+
if (column !== scoped.column) continue;
|
|
15
|
+
const written = literalOf(update.value);
|
|
16
|
+
if (written && isOrganization(written.value, organization)) continue;
|
|
17
|
+
throw new ScopeRefused(`An update sets ${tableName(table)}.${scoped.column} to another organization than the one it works in`);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const scopeValues = (values, at, organization, refuse) => ValuesNode.create(values.values.map((row) => {
|
|
21
|
+
if (PrimitiveValueListNode.is(row)) {
|
|
22
|
+
if (at < 0) return PrimitiveValueListNode.create([...row.values, organization]);
|
|
23
|
+
if (!isOrganization(row.values[at], organization)) refuse();
|
|
24
|
+
return row;
|
|
25
|
+
}
|
|
26
|
+
if (!ValueListNode.is(row)) throw new Error("Not implemented: an insert row that is no list of values");
|
|
27
|
+
if (at < 0) return ValueListNode.create([...row.values, ValueNode.create(organization)]);
|
|
28
|
+
if (DefaultInsertValueNode.is(row.values[at])) return ValueListNode.create(row.values.map((value, i) => i === at ? ValueNode.create(organization) : value));
|
|
29
|
+
const written = literalOf(row.values[at]);
|
|
30
|
+
if (!written || !isOrganization(written.value, organization)) refuse();
|
|
31
|
+
return row;
|
|
32
|
+
}));
|
|
33
|
+
const writesOrganization = (select, at, scoped, organization) => {
|
|
34
|
+
const selections = select.selections ?? [];
|
|
35
|
+
if (at !== selections.length - 1 || selections.some(({ selection }) => SelectAllNode.is(selection) || ReferenceNode.is(selection) && SelectAllNode.is(selection.column))) return false;
|
|
36
|
+
const written = selections[at]?.selection;
|
|
37
|
+
if (!written || !AliasNode.is(written) || !IdentifierNode.is(written.alias) || written.alias.name !== scoped.column) return false;
|
|
38
|
+
const value = literalOf(written.node);
|
|
39
|
+
return value !== void 0 && isOrganization(value.value, organization);
|
|
40
|
+
};
|
|
41
|
+
const scopeSelect = (select, at, scoped, organization, table) => {
|
|
42
|
+
if (select.setOperations?.length) throw new Error("Not implemented: insert … select of a union into a scoped table");
|
|
43
|
+
if (at >= 0 && writesOrganization(select, at, scoped, organization)) return select;
|
|
44
|
+
if (at >= 0) throw new ScopeRefused(`An insert into ${tableName(table)} selects its ${scoped.column}: leave the column out, the scope writes the organization`);
|
|
45
|
+
if (!select.selections?.length) throw new Error("Not implemented: insert … select without a column list into a scoped table");
|
|
46
|
+
const column = SelectionNode.create(AliasNode.create(ValueNode.create(organization), IdentifierNode.create(scoped.column)));
|
|
47
|
+
return {
|
|
48
|
+
...select,
|
|
49
|
+
selections: [...select.selections, column]
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* An insert into a scoped table, in the organization: every row gets its organization, a row naming another one is
|
|
54
|
+
* refused, and an upsert updates a row of the organization only. Without an organization, it is refused.
|
|
55
|
+
*/
|
|
56
|
+
const scopeInsert = (node, scoped, organization, table) => {
|
|
57
|
+
if (organization === void 0) throw new ScopeRefused(`An insert into ${tableName(table)} works in no organization`);
|
|
58
|
+
const refuse = () => {
|
|
59
|
+
throw new ScopeRefused(`An insert into ${tableName(table)} names another organization than the one it works in`);
|
|
60
|
+
};
|
|
61
|
+
const columns = node.columns;
|
|
62
|
+
if (node.values !== void 0 && columns === void 0) throw new Error(`Not implemented: an insert into ${tableName(table)} without its column list`);
|
|
63
|
+
const at = (columns ?? []).findIndex((column) => column.column.name === scoped.column);
|
|
64
|
+
const own = at < 0 ? [...columns ?? [], ColumnNode.create(scoped.column)] : columns;
|
|
65
|
+
const values = () => {
|
|
66
|
+
if (node.values === void 0) return ValuesNode.create([ValueListNode.create([ValueNode.create(organization)])]);
|
|
67
|
+
if (ValuesNode.is(node.values)) return scopeValues(node.values, at, organization, refuse);
|
|
68
|
+
if (SelectQueryNode.is(node.values)) return scopeSelect(node.values, at, scoped, organization, table);
|
|
69
|
+
throw new Error(`Not implemented: an insert into ${tableName(table)} of values that are neither rows nor a select`);
|
|
70
|
+
};
|
|
71
|
+
const onConflict = node.onConflict && scopeConflict(node.onConflict, scoped, organization, table);
|
|
72
|
+
return InsertQueryNode.cloneWith(node, {
|
|
73
|
+
columns: own,
|
|
74
|
+
values: values(),
|
|
75
|
+
defaultValues: false,
|
|
76
|
+
...onConflict && { onConflict }
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
const scopeConflict = (node, scoped, organization, table) => {
|
|
80
|
+
if (node.doNothing) return node;
|
|
81
|
+
checkColumnUpdates(node.updates, scoped, organization, table);
|
|
82
|
+
const row = TableNode.create(table.table.identifier.name);
|
|
83
|
+
if (holdsScope(node.updateWhere?.where, scoped, row, organization)) return node;
|
|
84
|
+
return {
|
|
85
|
+
...node,
|
|
86
|
+
updateWhere: andWhere(node.updateWhere, scopeCondition(scoped, row, organization))
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
//#endregion
|
|
90
|
+
export { checkColumnUpdates, scopeInsert };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ScopedTables } from "./scoped-tables.js";
|
|
2
|
+
import { ScopeOrganization } from "./organization-source.js";
|
|
3
|
+
import { KyselyPlugin, PluginTransformQueryArgs, PluginTransformResultArgs, QueryResult, RootOperationNode, UnknownRow } from "kysely";
|
|
4
|
+
//#region src/table-scope/scope-plugin.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Keeps each query on a scoped table to `organization` (see `ScopeTransformer`); with none, a query sees no scoped
|
|
7
|
+
* row and an insert into one is refused. An `OrganizationSource` is read as each query is built: a query it gives
|
|
8
|
+
* every organization for passes as it is, and its raw `scoped(t)` fails as on a database without the scope.
|
|
9
|
+
*/
|
|
10
|
+
export declare class ScopePlugin implements KyselyPlugin {
|
|
11
|
+
readonly scope: ScopedTables;
|
|
12
|
+
readonly organization: ScopeOrganization;
|
|
13
|
+
constructor(scope: ScopedTables, organization: ScopeOrganization);
|
|
14
|
+
transformQuery({ node }: PluginTransformQueryArgs): RootOperationNode;
|
|
15
|
+
transformResult({ result }: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>>;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { currentOrganization, isEveryOrganization } from "./organization-source.js";
|
|
2
|
+
import { ScopeTransformer } from "./scope-transformer.js";
|
|
3
|
+
//#region src/table-scope/scope-plugin.ts
|
|
4
|
+
/**
|
|
5
|
+
* Keeps each query on a scoped table to `organization` (see `ScopeTransformer`); with none, a query sees no scoped
|
|
6
|
+
* row and an insert into one is refused. An `OrganizationSource` is read as each query is built: a query it gives
|
|
7
|
+
* every organization for passes as it is, and its raw `scoped(t)` fails as on a database without the scope.
|
|
8
|
+
*/
|
|
9
|
+
var ScopePlugin = class {
|
|
10
|
+
scope;
|
|
11
|
+
organization;
|
|
12
|
+
constructor(scope, organization) {
|
|
13
|
+
this.scope = scope;
|
|
14
|
+
this.organization = organization;
|
|
15
|
+
}
|
|
16
|
+
transformQuery({ node }) {
|
|
17
|
+
const organization = currentOrganization(this.organization);
|
|
18
|
+
if (isEveryOrganization(organization)) return node;
|
|
19
|
+
return new ScopeTransformer(this.scope, organization).transformNode(node);
|
|
20
|
+
}
|
|
21
|
+
async transformResult({ result }) {
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export { ScopePlugin };
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { andOn, andWhere, holdsScope, scopeCondition } from "./scope-condition.js";
|
|
2
|
+
import { scopedTableOf, tableItem } from "./scoped-tables.js";
|
|
3
|
+
import { checkRawTables, isUnscopedMark, scopedMark, scopedRows } from "./raw-tables.js";
|
|
4
|
+
import { checkColumnUpdates, scopeInsert } from "./scope-insert.js";
|
|
5
|
+
import { OperationNodeTransformer, RawNode } from "kysely";
|
|
6
|
+
//#region src/table-scope/scope-transformer.ts
|
|
7
|
+
const filteredInOn = /* @__PURE__ */ new Set([
|
|
8
|
+
"InnerJoin",
|
|
9
|
+
"LeftJoin",
|
|
10
|
+
"LateralInnerJoin",
|
|
11
|
+
"LateralLeftJoin"
|
|
12
|
+
]);
|
|
13
|
+
const filteredInWhere = /* @__PURE__ */ new Set(["CrossJoin", "LateralCrossJoin"]);
|
|
14
|
+
const cteNames = (node) => (node?.expressions ?? []).map((expression) => expression.name.table.table.identifier.name);
|
|
15
|
+
/**
|
|
16
|
+
* Keeps every query on a scoped table to one organization, at the select, update or delete that reads the table: a
|
|
17
|
+
* table in its `from`, `using` or as its target is filtered in its `where`, a joined one in the join's `on`. A
|
|
18
|
+
* subquery, a common table expression or `exists` filters its own tables; a name a common table expression takes is
|
|
19
|
+
* not the table. Inserts write the organization; raw SQL may name a scoped table only through `scoped()` or `unscoped()`.
|
|
20
|
+
*/
|
|
21
|
+
var ScopeTransformer = class extends OperationNodeTransformer {
|
|
22
|
+
scope;
|
|
23
|
+
organization;
|
|
24
|
+
#frames = [];
|
|
25
|
+
constructor(scope, organization) {
|
|
26
|
+
super();
|
|
27
|
+
this.scope = scope;
|
|
28
|
+
this.organization = organization;
|
|
29
|
+
}
|
|
30
|
+
#shadowing() {
|
|
31
|
+
return new Set(this.#frames.flatMap((frame) => [...frame]));
|
|
32
|
+
}
|
|
33
|
+
#inLevel(withNode, work) {
|
|
34
|
+
this.#frames.push(new Set(cteNames(withNode)));
|
|
35
|
+
const done = work();
|
|
36
|
+
this.#frames.pop();
|
|
37
|
+
return done;
|
|
38
|
+
}
|
|
39
|
+
#scoped(node) {
|
|
40
|
+
return scopedTableOf(this.scope, node, this.#shadowing());
|
|
41
|
+
}
|
|
42
|
+
#scopedTarget(node) {
|
|
43
|
+
return scopedTableOf(this.scope, node, /* @__PURE__ */ new Set());
|
|
44
|
+
}
|
|
45
|
+
#filterTarget(where, node) {
|
|
46
|
+
const item = node && tableItem(node);
|
|
47
|
+
if (node && !item) throw new Error("Not implemented: a write to anything but one table");
|
|
48
|
+
const scoped = item && this.#scopedTarget(item.table);
|
|
49
|
+
if (!scoped || holdsScope(where?.where, scoped, item.qualifier, this.organization)) return where;
|
|
50
|
+
return andWhere(where, scopeCondition(scoped, item.qualifier, this.organization));
|
|
51
|
+
}
|
|
52
|
+
#filterItems(where, items) {
|
|
53
|
+
let filtered = where;
|
|
54
|
+
for (const node of items) {
|
|
55
|
+
const item = tableItem(node);
|
|
56
|
+
const scoped = item && this.#scoped(item.table);
|
|
57
|
+
if (scoped && !holdsScope(filtered?.where, scoped, item.qualifier, this.organization)) filtered = andWhere(filtered, scopeCondition(scoped, item.qualifier, this.organization));
|
|
58
|
+
}
|
|
59
|
+
return filtered;
|
|
60
|
+
}
|
|
61
|
+
#filterJoins(where, joins, ownsScoped) {
|
|
62
|
+
let filtered = where;
|
|
63
|
+
const scopedJoins = (joins ?? []).map((join) => {
|
|
64
|
+
const item = tableItem(join.table);
|
|
65
|
+
const scoped = item && this.#scoped(item.table);
|
|
66
|
+
if (!scoped) {
|
|
67
|
+
if (ownsScoped && (join.joinType === "RightJoin" || join.joinType === "FullJoin")) throw new Error("Not implemented: a right or full join in a query of a scoped table");
|
|
68
|
+
return join;
|
|
69
|
+
}
|
|
70
|
+
const condition = scopeCondition(scoped, item.qualifier, this.organization);
|
|
71
|
+
if (filteredInOn.has(join.joinType)) return holdsScope(join.on?.on, scoped, item.qualifier, this.organization) ? join : {
|
|
72
|
+
...join,
|
|
73
|
+
on: andOn(join.on, condition)
|
|
74
|
+
};
|
|
75
|
+
if (filteredInWhere.has(join.joinType)) {
|
|
76
|
+
if (!holdsScope(filtered?.where, scoped, item.qualifier, this.organization)) filtered = andWhere(filtered, condition);
|
|
77
|
+
return join;
|
|
78
|
+
}
|
|
79
|
+
throw new Error(`Not implemented: a ${join.joinType} of a scoped table`);
|
|
80
|
+
});
|
|
81
|
+
return {
|
|
82
|
+
where: filtered,
|
|
83
|
+
joins: joins && scopedJoins
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
#ownsScoped(items, joins, targets) {
|
|
87
|
+
return [...items, ...(joins ?? []).map((join) => join.table)].some((node) => {
|
|
88
|
+
const item = tableItem(node);
|
|
89
|
+
return item !== void 0 && this.#scoped(item.table) !== void 0;
|
|
90
|
+
}) || targets.some((node) => this.#scopedTarget(tableItem(node).table) !== void 0);
|
|
91
|
+
}
|
|
92
|
+
#filterLevel(where, items, joins, targets = []) {
|
|
93
|
+
const targeted = targets.reduce((filtered, target) => this.#filterTarget(filtered, target), where);
|
|
94
|
+
return this.#filterJoins(this.#filterItems(targeted, items), joins, this.#ownsScoped(items, joins, targets));
|
|
95
|
+
}
|
|
96
|
+
transformSelectQuery(node, queryId) {
|
|
97
|
+
return this.#inLevel(node.with, () => {
|
|
98
|
+
const done = super.transformSelectQuery(node, queryId);
|
|
99
|
+
const { where, joins } = this.#filterLevel(done.where, done.from?.froms ?? [], done.joins);
|
|
100
|
+
return {
|
|
101
|
+
...done,
|
|
102
|
+
...where && { where },
|
|
103
|
+
...joins && { joins }
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
transformUpdateQuery(node, queryId) {
|
|
108
|
+
return this.#inLevel(node.with, () => {
|
|
109
|
+
const done = super.transformUpdateQuery(node, queryId);
|
|
110
|
+
const target = done.table && tableItem(done.table);
|
|
111
|
+
if (done.table && !target) throw new Error("Not implemented: an update of anything but one table");
|
|
112
|
+
const scoped = target && this.#scopedTarget(target.table);
|
|
113
|
+
if (scoped) checkColumnUpdates(done.updates, scoped, this.organization, target.table);
|
|
114
|
+
const { where, joins } = this.#filterLevel(done.where, done.from?.froms ?? [], done.joins, done.table ? [done.table] : []);
|
|
115
|
+
return {
|
|
116
|
+
...done,
|
|
117
|
+
...where && { where },
|
|
118
|
+
...joins && { joins }
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
transformDeleteQuery(node, queryId) {
|
|
123
|
+
return this.#inLevel(node.with, () => {
|
|
124
|
+
const done = super.transformDeleteQuery(node, queryId);
|
|
125
|
+
const { where, joins } = this.#filterLevel(done.where, done.using?.tables ?? [], done.joins, done.from.froms);
|
|
126
|
+
return {
|
|
127
|
+
...done,
|
|
128
|
+
...where && { where },
|
|
129
|
+
...joins && { joins }
|
|
130
|
+
};
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
transformInsertQuery(node, queryId) {
|
|
134
|
+
return this.#inLevel(node.with, () => {
|
|
135
|
+
const done = super.transformInsertQuery(node, queryId);
|
|
136
|
+
const scoped = done.into && this.#scopedTarget(done.into);
|
|
137
|
+
return scoped ? scopeInsert(done, scoped, this.organization, done.into) : done;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
transformMergeQuery(node, queryId) {
|
|
141
|
+
return this.#inLevel(node.with, () => {
|
|
142
|
+
const into = node.into && tableItem(node.into);
|
|
143
|
+
const using = node.using && tableItem(node.using.table);
|
|
144
|
+
if (into && this.#scopedTarget(into.table) || using && this.#scoped(using.table)) throw new Error("Not implemented: merge into or using a scoped table");
|
|
145
|
+
return super.transformMergeQuery(node, queryId);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
transformWith(node, queryId) {
|
|
149
|
+
const own = this.#frames.pop();
|
|
150
|
+
const names = cteNames(node);
|
|
151
|
+
const expressions = node.expressions.map((expression, i) => {
|
|
152
|
+
this.#frames.push(new Set(node.recursive ? names : names.slice(0, i)));
|
|
153
|
+
const done = this.transformCommonTableExpression(expression, queryId);
|
|
154
|
+
this.#frames.pop();
|
|
155
|
+
return done;
|
|
156
|
+
});
|
|
157
|
+
this.#frames.push(own);
|
|
158
|
+
return {
|
|
159
|
+
...node,
|
|
160
|
+
expressions
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
transformRaw(node, queryId) {
|
|
164
|
+
const table = scopedMark(node);
|
|
165
|
+
if (table) {
|
|
166
|
+
const scoped = this.#scoped(table);
|
|
167
|
+
return RawNode.create(["", ""], [scoped ? scopedRows(table, scopeCondition(scoped, table, this.organization)) : table]);
|
|
168
|
+
}
|
|
169
|
+
if (isUnscopedMark(node)) return node;
|
|
170
|
+
checkRawTables(this.scope, node, this.#shadowing());
|
|
171
|
+
return super.transformRaw(node, queryId);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
//#endregion
|
|
175
|
+
export { ScopeTransformer };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Db } from "../db.js";
|
|
2
|
+
import { ScopedTables } from "./scoped-tables.js";
|
|
3
|
+
import { Organization } from "./scope-condition.js";
|
|
4
|
+
import { EveryOrganization, ScopeOrganization } from "./organization-source.js";
|
|
5
|
+
import { Kysely, KyselyPlugin } from "kysely";
|
|
6
|
+
//#region src/table-scope/scoped-db.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* The database kept to one organization (`ScopePlugin`): a query on a scoped table sees and writes that
|
|
9
|
+
* organization's rows only. `unscoped(reason)` is the same database without the scope, its schema and other plugins
|
|
10
|
+
* kept, for work that means every organization's rows, saying why; `withoutPlugins()` is refused, as it would drop the
|
|
11
|
+
* scope without a reason. What it makes keeps the scope and the refusal: `withSchema`, `withPlugin`, `withTables`,
|
|
12
|
+
* `with`, and the database a transaction or connection gives its callback.
|
|
13
|
+
*/
|
|
14
|
+
export type ScopedDb = {
|
|
15
|
+
withSchema(schema: string): ScopedDb;
|
|
16
|
+
withPlugin(plugin: KyselyPlugin): ScopedDb;
|
|
17
|
+
withTables<T extends Record<string, Record<string, any>>>(): ScopedDb;
|
|
18
|
+
} & Kysely<any> & {
|
|
19
|
+
/** The organization it works in now; undefined for none, `{ unscoped }` while its source gives every organization. */
|
|
20
|
+
readonly organization: Organization | EveryOrganization;
|
|
21
|
+
unscoped(reason: string): Db;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* `db` kept to `organization` on the scoped tables of `scope`: a fixed one, or an `OrganizationSource` read as each
|
|
25
|
+
* query is built, for a database made once that serves many organizations, such as from the request a query runs for.
|
|
26
|
+
*/
|
|
27
|
+
export declare const scopedDb: (db: Db, scope: ScopedTables, organization: ScopeOrganization) => ScopedDb;
|
|
28
|
+
//#endregion
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { currentOrganization } from "./organization-source.js";
|
|
2
|
+
import { ScopePlugin } from "./scope-plugin.js";
|
|
3
|
+
import { ConnectionBuilder, ControlledTransactionBuilder, Kysely, QueryCreator, TransactionBuilder, WithSchemaPlugin } from "kysely";
|
|
4
|
+
//#region src/table-scope/scoped-db.ts
|
|
5
|
+
const refuseWithoutPlugins = () => {
|
|
6
|
+
throw new Error("withoutPlugins() would drop the organization scope: use db.unscoped('<reason>')");
|
|
7
|
+
};
|
|
8
|
+
const withoutScope = (instance) => instance.getExecutor().plugins.filter((plugin) => !(plugin instanceof ScopePlugin)).reduce((done, plugin) => done.withPlugin(plugin), instance.withoutPlugins());
|
|
9
|
+
/**
|
|
10
|
+
* `db` with the scope's plugin where it sees the names its queries write: after a schema `withSchema` gives, as
|
|
11
|
+
* Kysely runs that first, and before the database's other plugins, so one that renames a table, such as to the name it
|
|
12
|
+
* has in the database, renames the scope's condition with it.
|
|
13
|
+
*/
|
|
14
|
+
const withScope = (db, plugin) => {
|
|
15
|
+
const plugins = db.getExecutor().plugins;
|
|
16
|
+
const schemas = plugins.filter((one) => one instanceof WithSchemaPlugin);
|
|
17
|
+
const others = plugins.filter((one) => !(one instanceof WithSchemaPlugin));
|
|
18
|
+
return [
|
|
19
|
+
...schemas,
|
|
20
|
+
plugin,
|
|
21
|
+
...others
|
|
22
|
+
].reduce((done, one) => done.withPlugin(one), db.withoutPlugins());
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* `db` kept to `organization` on the scoped tables of `scope`: a fixed one, or an `OrganizationSource` read as each
|
|
26
|
+
* query is built, for a database made once that serves many organizations, such as from the request a query runs for.
|
|
27
|
+
*/
|
|
28
|
+
const scopedDb = (db, scope, organization) => {
|
|
29
|
+
const unscopedOf = (instance) => (reason) => {
|
|
30
|
+
if (typeof reason !== "string" || reason.trim() === "") throw new Error("db.unscoped needs a reason: say why this work reads every organization");
|
|
31
|
+
return withoutScope(instance);
|
|
32
|
+
};
|
|
33
|
+
const guard = (value) => {
|
|
34
|
+
if (value instanceof Kysely) return guardDb(value);
|
|
35
|
+
if (value instanceof QueryCreator) return guarded(value, {});
|
|
36
|
+
if (value instanceof TransactionBuilder || value instanceof ConnectionBuilder || value instanceof ControlledTransactionBuilder) return guardBuilder(value);
|
|
37
|
+
return value;
|
|
38
|
+
};
|
|
39
|
+
const guarded = (target, own) => new Proxy(target, { get(on, property) {
|
|
40
|
+
if (property === "withoutPlugins") return refuseWithoutPlugins;
|
|
41
|
+
if (property in own) return own[property];
|
|
42
|
+
const value = Reflect.get(on, property, on);
|
|
43
|
+
if (typeof value !== "function") return value;
|
|
44
|
+
return new Proxy(value, { apply: (method, _, args) => guard(Reflect.apply(method, on, args)) });
|
|
45
|
+
} });
|
|
46
|
+
const guardDb = (instance) => guarded(instance, {
|
|
47
|
+
unscoped: unscopedOf(instance),
|
|
48
|
+
get organization() {
|
|
49
|
+
return currentOrganization(organization);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
const guardBuilder = (builder) => new Proxy(builder, { get(on, property) {
|
|
53
|
+
const value = Reflect.get(on, property, on);
|
|
54
|
+
if (typeof value !== "function") return value;
|
|
55
|
+
if (property !== "execute") return (...args) => guard(value.apply(on, args));
|
|
56
|
+
return (callback, ...rest) => {
|
|
57
|
+
const wrapped = typeof callback === "function" ? (handle) => callback(guard(handle)) : callback;
|
|
58
|
+
const result = value.apply(on, [wrapped, ...rest]);
|
|
59
|
+
return result instanceof Promise ? result.then(guard) : result;
|
|
60
|
+
};
|
|
61
|
+
} });
|
|
62
|
+
return guardDb(scope.tables.length === 0 ? db : withScope(db, new ScopePlugin(scope, organization)));
|
|
63
|
+
};
|
|
64
|
+
//#endregion
|
|
65
|
+
export { scopedDb };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { TableNode } from "kysely";
|
|
2
|
+
//#region src/table-scope/scoped-tables.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* A table whose rows each belong to one organization: its schema, undefined for a table the search path finds, its
|
|
5
|
+
* name, and the column that holds the organization.
|
|
6
|
+
*/
|
|
7
|
+
export type ScopedTable = {
|
|
8
|
+
schema?: string;
|
|
9
|
+
table: string;
|
|
10
|
+
column: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The scoped tables, looked up by the name a query gives. `searchSchema` is the schema the search path finds an
|
|
14
|
+
* unqualified name in, `public` by default as Postgres's own search path has it: a name qualified with it is the same
|
|
15
|
+
* table as the unqualified one.
|
|
16
|
+
*/
|
|
17
|
+
export type ScopedTables = {
|
|
18
|
+
tables: readonly ScopedTable[];
|
|
19
|
+
searchSchema?: string;
|
|
20
|
+
};
|
|
21
|
+
//#endregion
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AliasNode, IdentifierNode, TableNode } from "kysely";
|
|
2
|
+
//#region src/table-scope/scoped-tables.ts
|
|
3
|
+
const schemaOf = (schema, searchSchema) => schema ?? searchSchema;
|
|
4
|
+
/**
|
|
5
|
+
* The scoped table a name stands for, or undefined. An unqualified name is the scoped table only when that table's
|
|
6
|
+
* schema is the one the search path finds, so a project's own `public.member` is never taken for `auth.member`.
|
|
7
|
+
*/
|
|
8
|
+
const scopedTableNamed = ({ tables, searchSchema = "public" }, schema, table) => tables.find((scoped) => scoped.table === table && schemaOf(scoped.schema, searchSchema) === schemaOf(schema, searchSchema));
|
|
9
|
+
/**
|
|
10
|
+
* The scoped table of a table node, unless an unqualified name is one of the common table expressions in `shadowing`.
|
|
11
|
+
* The target of an insert, update or delete is always a table, so it is looked up with no shadowing.
|
|
12
|
+
*/
|
|
13
|
+
const scopedTableOf = (scope, node, shadowing) => {
|
|
14
|
+
const schema = node.table.schema?.name;
|
|
15
|
+
const table = node.table.identifier.name;
|
|
16
|
+
if (schema === void 0 && shadowing.has(table)) return;
|
|
17
|
+
return scopedTableNamed(scope, schema, table);
|
|
18
|
+
};
|
|
19
|
+
/** The table of a from item, plain or aliased; undefined for anything else, such as a subquery. */
|
|
20
|
+
const tableItem = (node) => {
|
|
21
|
+
if (TableNode.is(node)) return {
|
|
22
|
+
table: node,
|
|
23
|
+
qualifier: node
|
|
24
|
+
};
|
|
25
|
+
if (!AliasNode.is(node) || !TableNode.is(node.node)) return;
|
|
26
|
+
if (!IdentifierNode.is(node.alias)) throw new Error("Not implemented: a table aliased by anything but a name");
|
|
27
|
+
return {
|
|
28
|
+
table: node.node,
|
|
29
|
+
qualifier: TableNode.create(node.alias.name)
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
//#endregion
|
|
33
|
+
export { scopedTableNamed, scopedTableOf, tableItem };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/query",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.130.1",
|
|
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.
|
|
32
|
+
"@protobase/schema": "0.130.1",
|
|
33
33
|
"kysely": "^0.29.6"
|
|
34
34
|
}
|
|
35
35
|
}
|