uql-orm 0.114.3 → 0.115.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cockroachdb/cockroachDialect.d.ts +2 -0
- package/dist/cockroachdb/cockroachDialect.js +5 -0
- package/dist/d1/d1Querier.d.ts +2 -4
- package/dist/d1/d1Querier.js +0 -4
- package/dist/d1/d1SqliteDialect.js +1 -1
- package/dist/dialect/abstractDialect.d.ts +17 -8
- package/dist/dialect/abstractDialect.js +30 -16
- package/dist/dialect/abstractSqlDialect.d.ts +11 -2
- package/dist/dialect/abstractSqlDialect.js +17 -2
- package/dist/dialect/mysqlLikeSqlDialect.js +2 -0
- package/dist/dialect/operators.d.ts +2 -2
- package/dist/dialect/pgLikeSqlDialect.d.ts +8 -1
- package/dist/dialect/pgLikeSqlDialect.js +20 -0
- package/dist/entity/columnOptionsBuilder.d.ts +2 -1
- package/dist/entity/columnOptionsBuilder.js +3 -0
- package/dist/entity/metadata/definedFixture/author.d.ts +1 -1
- package/dist/entity/metadata/definedFixture/book.d.ts +2 -2
- package/dist/entity/metadata/definedFixture/folder.d.ts +1 -1
- package/dist/entity/metadata/definition.js +69 -30
- package/dist/http/api.d.ts +1 -1
- package/dist/http/wireQuery.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/migrate/ddl/tableDdl.d.ts +5 -0
- package/dist/migrate/ddl/tableDdl.js +9 -0
- package/dist/migrate/drift/driftDetector.js +2 -1
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +3 -1
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +7 -1
- package/dist/migrate/introspection/postgresIntrospector.d.ts +3 -1
- package/dist/migrate/introspection/postgresIntrospector.js +14 -1
- package/dist/migrate/introspection/tableSchemaAST.js +1 -0
- package/dist/migrate/policySql.d.ts +12 -0
- package/dist/migrate/policySql.js +74 -0
- package/dist/migrate/schemaChange.js +1 -0
- package/dist/migrate/schemaGenerator.d.ts +2 -0
- package/dist/migrate/schemaGenerator.js +37 -11
- package/dist/mongodb/mongoDialect.js +3 -1
- package/dist/mongodb/mongodbQuerier.d.ts +2 -5
- package/dist/mongodb/mongodbQuerier.js +14 -26
- package/dist/mssql/mssqlDialect.js +2 -0
- package/dist/neon/neonQuerier.d.ts +1 -2
- package/dist/nestjs/uqlContextInterceptor.d.ts +1 -1
- package/dist/nestjs/uqlContextInterceptor.js +1 -1
- package/dist/nestjs/uqlModule.d.ts +2 -2
- package/dist/nestjs/uqlModule.js +1 -1
- package/dist/querier/abstractQuerier.d.ts +68 -30
- package/dist/querier/abstractQuerier.js +249 -168
- package/dist/querier/abstractQuerierPool.js +2 -0
- package/dist/querier/abstractSqlQuerier.d.ts +22 -13
- package/dist/querier/abstractSqlQuerier.js +75 -27
- package/dist/querier/queryError.js +1 -0
- package/dist/schema/schemaAST.js +1 -0
- package/dist/schema/schemaASTBuilder.d.ts +3 -1
- package/dist/schema/schemaASTBuilder.js +1 -0
- package/dist/schema/schemaASTDiffer.js +4 -2
- package/dist/schema/types.d.ts +12 -4
- package/dist/sqlite/bindValues.d.ts +1 -1
- package/dist/sqlite/hranaQuerier.d.ts +2 -2
- package/dist/sqlite/sqliteDialect.js +2 -0
- package/dist/turso/tursoSessionQuerier.d.ts +2 -2
- package/dist/type/column.d.ts +10 -0
- package/dist/type/definedEntity.d.ts +9 -4
- package/dist/type/dialect.d.ts +13 -0
- package/dist/type/entity.d.ts +10 -0
- package/dist/type/index.d.ts +1 -0
- package/dist/type/index.js +1 -0
- package/dist/type/migration.d.ts +5 -1
- package/dist/type/policy.d.ts +91 -0
- package/dist/type/policy.js +1 -0
- package/dist/type/querier.d.ts +16 -2
- package/dist/type/querierPool.d.ts +2 -2
- package/dist/type/query.d.ts +20 -35
- package/dist/type/query.js +2 -0
- package/dist/type/universalQuerier.d.ts +6 -6
- package/dist/util/contextRefs.d.ts +6 -0
- package/dist/util/contextRefs.js +5 -0
- package/dist/util/dialect.util.d.ts +10 -16
- package/dist/util/dialect.util.js +70 -64
- package/dist/util/fieldOption.util.js +1 -0
- package/dist/util/policy.util.d.ts +100 -0
- package/dist/util/policy.util.js +234 -0
- package/dist/util/uqlError.d.ts +2 -5
- package/dist/util/uqlError.js +2 -5
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +24 -14
|
@@ -60,6 +60,15 @@ export class TableDdl {
|
|
|
60
60
|
alterClauses(column, _from) {
|
|
61
61
|
throw rebuildRefusal(this.dialect, `Altering the column "${column.name}"`);
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* What switches `table`'s row-level security on, forced so its owner is held too, or off: on with its first policy,
|
|
65
|
+
* off with its last, since forced with none it shows no row to anyone.
|
|
66
|
+
*/
|
|
67
|
+
rowSecurity(table, on) {
|
|
68
|
+
return this.alterTable(table, on
|
|
69
|
+
? ['ENABLE ROW LEVEL SECURITY', 'FORCE ROW LEVEL SECURITY']
|
|
70
|
+
: ['NO FORCE ROW LEVEL SECURITY', 'DISABLE ROW LEVEL SECURITY']);
|
|
71
|
+
}
|
|
63
72
|
/** One `ALTER TABLE` making every clause, and none for no clause. */
|
|
64
73
|
alterTable(table, clauses) {
|
|
65
74
|
return clauses.length ? [`ALTER TABLE ${this.dialect.escapeId(table)} ${clauses.join(', ')};`] : [];
|
|
@@ -14,6 +14,7 @@ export function detectDrift(expectedAST, actualAST, options = {}) {
|
|
|
14
14
|
...detectPrimaryKeyDrifts(diff),
|
|
15
15
|
...detectOwnedDrifts('Check', diff.checks),
|
|
16
16
|
...detectOwnedDrifts('Trigger', diff.triggers),
|
|
17
|
+
...detectOwnedDrifts('Policy', diff.policies),
|
|
17
18
|
...diff.relationships.map(relationshipDrift),
|
|
18
19
|
];
|
|
19
20
|
const count = (severity) => drifts.filter((drift) => drift.severity === severity).length;
|
|
@@ -41,7 +42,7 @@ function detectPrimaryKeyDrifts(diff) {
|
|
|
41
42
|
}));
|
|
42
43
|
}
|
|
43
44
|
/**
|
|
44
|
-
* A check or
|
|
45
|
+
* A check, trigger or policy the database lacks, critical since rows are written otherwise than the entity says, or
|
|
45
46
|
* one uql installed that nothing declares.
|
|
46
47
|
*/
|
|
47
48
|
function detectOwnedDrifts(kind, changes) {
|
|
@@ -2,7 +2,7 @@ import type { AbstractSqlDialect } from '../../dialect/index.js';
|
|
|
2
2
|
import type { IndexFacet } from '../../schema/indexDifferences.js';
|
|
3
3
|
import type { SchemaAST } from '../../schema/schemaAST.js';
|
|
4
4
|
import { SqlExpression } from '../../schema/sqlExpression.js';
|
|
5
|
-
import type { CheckSchema, ColumnDefault, TriggerSchema } from '../../schema/types.js';
|
|
5
|
+
import type { CheckSchema, ColumnDefault, PolicySchema, TriggerSchema } from '../../schema/types.js';
|
|
6
6
|
import { type ForeignKeyAction } from '../../schema/types.js';
|
|
7
7
|
import type { ColumnRenames, ColumnSchema, Except, ForeignKeySchema, IndexSchema, PrimaryKeySchema, QuerierPool, QuerySql, RawRow, SchemaIntrospector, SqlQuerier, StoredDefinition, TableSchema } from '../../type/index.js';
|
|
8
8
|
/**
|
|
@@ -78,6 +78,8 @@ export declare abstract class AbstractSqlSchemaIntrospector implements SchemaInt
|
|
|
78
78
|
* what uql wrote, which is exactly what a rollback restores.
|
|
79
79
|
*/
|
|
80
80
|
protected abstract triggersQuery(tableName: string): QuerySql;
|
|
81
|
+
/** The row-level policies uql installed on the table: none, but where the engine holds rows to policies. */
|
|
82
|
+
protected getPolicies(_read: TableRowReader, _tableName: string): Promise<PolicySchema[]>;
|
|
81
83
|
/** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
|
|
82
84
|
protected getDefinition(_read: TableRowReader, _tableName: string): Promise<StoredDefinition[] | undefined>;
|
|
83
85
|
/**
|
|
@@ -60,13 +60,14 @@ export class AbstractSqlSchemaIntrospector {
|
|
|
60
60
|
if (!exists) {
|
|
61
61
|
return undefined;
|
|
62
62
|
}
|
|
63
|
-
const [columns, indexes, foreignKeys, primaryKey, checks, triggers, definition] = await Promise.all([
|
|
63
|
+
const [columns, indexes, foreignKeys, primaryKey, checks, triggers, policies, definition] = await Promise.all([
|
|
64
64
|
this.getColumns(read, tableName),
|
|
65
65
|
this.getIndexes(read, tableName),
|
|
66
66
|
this.getForeignKeys(read, tableName),
|
|
67
67
|
this.getPrimaryKey(read, tableName),
|
|
68
68
|
this.getChecks(read, tableName),
|
|
69
69
|
this.getTriggers(read, tableName),
|
|
70
|
+
this.getPolicies(read, tableName),
|
|
70
71
|
this.getDefinition(read, tableName),
|
|
71
72
|
]);
|
|
72
73
|
const unique = uniqueColumns(indexes);
|
|
@@ -82,6 +83,7 @@ export class AbstractSqlSchemaIntrospector {
|
|
|
82
83
|
foreignKeys,
|
|
83
84
|
checks,
|
|
84
85
|
triggers,
|
|
86
|
+
policies,
|
|
85
87
|
definition,
|
|
86
88
|
};
|
|
87
89
|
});
|
|
@@ -111,6 +113,10 @@ export class AbstractSqlSchemaIntrospector {
|
|
|
111
113
|
const rows = await read(this.triggersQuery(tableName));
|
|
112
114
|
return rows.flatMap(({ name, requires, definition }) => isOwnedName(name) ? [{ name, statements: [requires, definition].filter((sql) => typeof sql === 'string') }] : []);
|
|
113
115
|
}
|
|
116
|
+
/** The row-level policies uql installed on the table: none, but where the engine holds rows to policies. */
|
|
117
|
+
async getPolicies(_read, _tableName) {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
114
120
|
/** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
|
|
115
121
|
async getDefinition(_read, _tableName) {
|
|
116
122
|
return undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { IndexFacet } from '../../schema/indexDifferences.js';
|
|
2
|
-
import { type CheckSchema, type ColumnDefault } from '../../schema/types.js';
|
|
2
|
+
import { type CheckSchema, type ColumnDefault, type PolicySchema } from '../../schema/types.js';
|
|
3
3
|
import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema, QuerySql } from '../../type/index.js';
|
|
4
4
|
import { AbstractSqlSchemaIntrospector, type ReadColumn, type TableRowReader } from './abstractSqlSchemaIntrospector.js';
|
|
5
5
|
/**
|
|
@@ -13,6 +13,8 @@ export declare class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrosp
|
|
|
13
13
|
*/
|
|
14
14
|
protected readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
15
15
|
protected triggersQuery(tableName: string): QuerySql;
|
|
16
|
+
/** Each policy uql installed, as the statement recreating it from what `pg_policies` reprints. */
|
|
17
|
+
protected getPolicies(read: TableRowReader, tableName: string): Promise<PolicySchema[]>;
|
|
16
18
|
/** `pg_get_expr` answers NULL for a table dropped mid-read, where `pg_get_constraintdef` raises. */
|
|
17
19
|
protected getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
|
|
18
20
|
protected getTableNamesQuery(): QuerySql;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { INDEX_TYPES } from '../../schema/types.js';
|
|
1
|
+
import { INDEX_TYPES, } from '../../schema/types.js';
|
|
2
2
|
import { isVectorIndexType } from '../../type/vector.js';
|
|
3
3
|
import { sql } from '../../util/sql.js';
|
|
4
|
+
import { isOwnedName } from '../../util/sql.util.js';
|
|
4
5
|
import { AbstractSqlSchemaIntrospector, } from './abstractSqlSchemaIntrospector.js';
|
|
5
6
|
/**
|
|
6
7
|
* PostgreSQL schema introspector
|
|
@@ -28,6 +29,18 @@ export class PostgresSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
28
29
|
WHERE NOT t.tgisinternal AND n.nspname = ${this.schemaExpr} AND c.relname = ${tableName}
|
|
29
30
|
`;
|
|
30
31
|
}
|
|
32
|
+
/** Each policy uql installed, as the statement recreating it from what `pg_policies` reprints. */
|
|
33
|
+
async getPolicies(read, tableName) {
|
|
34
|
+
const rows = await read(sql `
|
|
35
|
+
SELECT policyname AS name,
|
|
36
|
+
'CREATE POLICY ' || quote_ident(policyname) || ' ON ' || quote_ident(schemaname) || '.' || quote_ident(tablename)
|
|
37
|
+
|| ' AS ' || permissive || ' FOR ' || cmd || ' TO ' || array_to_string(roles, ', ')
|
|
38
|
+
|| coalesce(' USING (' || qual || ')', '') || coalesce(' WITH CHECK (' || with_check || ')', '') AS definition
|
|
39
|
+
FROM pg_policies
|
|
40
|
+
WHERE schemaname = ${this.schemaExpr} AND tablename = ${tableName}
|
|
41
|
+
`);
|
|
42
|
+
return rows.flatMap(({ name, definition }) => (isOwnedName(name) ? [{ name, statements: [definition] }] : []));
|
|
43
|
+
}
|
|
31
44
|
/** `pg_get_expr` answers NULL for a table dropped mid-read, where `pg_get_constraintdef` raises. */
|
|
32
45
|
async getChecks(read, tableName) {
|
|
33
46
|
const rows = await read(sql `
|
|
@@ -50,6 +50,7 @@ function tableNode(schema, { dialectName, schema: namespace, indexFacets }) {
|
|
|
50
50
|
table.primaryKey = schema.primaryKey;
|
|
51
51
|
table.checks.push(...(schema.checks ?? []));
|
|
52
52
|
table.triggers.push(...(schema.triggers ?? []));
|
|
53
|
+
table.policies.push(...(schema.policies ?? []));
|
|
53
54
|
table.definition = schema.definition;
|
|
54
55
|
return table;
|
|
55
56
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AbstractSqlDialect } from '../dialect/index.js';
|
|
2
|
+
import type { PolicySchema } from '../schema/types.js';
|
|
3
|
+
import { type EntityMeta, type FieldMeta } from '../type/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* The policies a native pool's database holds `meta` to, each named for its table and label and ending in a hash of
|
|
6
|
+
* its own SQL, as a trigger is: one `FOR ALL` where it holds every statement, else one per statement. The table's
|
|
7
|
+
* row-level security is switched with its first policy and its last, as `TableDdl.rowSecurity` says. See
|
|
8
|
+
* architecture/row-level-security.md.
|
|
9
|
+
*/
|
|
10
|
+
export declare function renderPolicies(dialect: AbstractSqlDialect, meta: EntityMeta<object>, columnType: (field: FieldMeta) => string): PolicySchema[];
|
|
11
|
+
/** What removes a policy from `table`, a name qualified where its schema is. */
|
|
12
|
+
export declare function dropPolicy(dialect: AbstractSqlDialect, table: string, name: string): string;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { readsRelations } from '../dialect/abstractDialect.js';
|
|
2
|
+
import { QuerySql, } from '../type/index.js';
|
|
3
|
+
import { entityName } from '../util/object.util.js';
|
|
4
|
+
import { asWhere, bindContext, POLICY_OPS, policiesOf } from '../util/policy.util.js';
|
|
5
|
+
import { ownedName } from '../util/sql.util.js';
|
|
6
|
+
import { UqlUsageError } from '../util/uqlError.js';
|
|
7
|
+
/** The command each statement a policy holds is, as `CREATE POLICY ... FOR` names it. */
|
|
8
|
+
const COMMANDS = {
|
|
9
|
+
read: 'SELECT',
|
|
10
|
+
insert: 'INSERT',
|
|
11
|
+
update: 'UPDATE',
|
|
12
|
+
delete: 'DELETE',
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* The policies a native pool's database holds `meta` to, each named for its table and label and ending in a hash of
|
|
16
|
+
* its own SQL, as a trigger is: one `FOR ALL` where it holds every statement, else one per statement. The table's
|
|
17
|
+
* row-level security is switched with its first policy and its last, as `TableDdl.rowSecurity` says. See
|
|
18
|
+
* architecture/row-level-security.md.
|
|
19
|
+
*/
|
|
20
|
+
export function renderPolicies(dialect, meta, columnType) {
|
|
21
|
+
if (!dialect.policies.database || !meta.policies?.length) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
const table = dialect.resolveTableAlias(meta);
|
|
25
|
+
const target = dialect.escapedTableName(meta);
|
|
26
|
+
const read = (ref, field) => {
|
|
27
|
+
if (!field) {
|
|
28
|
+
throw new UqlUsageError(`a policy of '${entityName(meta)}' compares context key '${ref.$context}' with no field`);
|
|
29
|
+
}
|
|
30
|
+
const sql = dialect.contextRead(ref.$context, columnType(field));
|
|
31
|
+
return new QuerySql(() => sql);
|
|
32
|
+
};
|
|
33
|
+
return policiesOf(meta.policies).flatMap((policy, position) => {
|
|
34
|
+
const label = policy.name ?? `${policy.effect}_${position}`;
|
|
35
|
+
const predicate = (where) => {
|
|
36
|
+
const bound = asWhere(meta, bindContext(meta, where, read));
|
|
37
|
+
const { rowSecurity } = dialect.features;
|
|
38
|
+
if (!(rowSecurity && rowSecurity.subqueries) && readsRelations(meta, bound)) {
|
|
39
|
+
throw new UqlUsageError(`${dialect.engineName} takes no subquery in a policy, so '${entityName(meta)}' policy '${label}' cannot read a relation there: leave policies unset on this pool, for uql to hold them`);
|
|
40
|
+
}
|
|
41
|
+
return ([...whenSql(dialect, policy.when), dialect.compileDdl(bound, meta.entity)].filter(Boolean).join(' AND ') ||
|
|
42
|
+
'true');
|
|
43
|
+
};
|
|
44
|
+
const commands = POLICY_OPS.every((op) => policy.ops.includes(op))
|
|
45
|
+
? [['ALL', undefined]]
|
|
46
|
+
: policy.ops.map((op) => [COMMANDS[op], op]);
|
|
47
|
+
return commands.map(([command, op]) => {
|
|
48
|
+
const using = command === 'INSERT' ? undefined : predicate(policy.where);
|
|
49
|
+
const check = writes(command) ? predicate(policy.check ?? policy.where) : undefined;
|
|
50
|
+
const create = (name) => [
|
|
51
|
+
`CREATE POLICY ${dialect.escapeId(name)} ON ${target} AS ${policy.effect === 'allow' ? 'PERMISSIVE' : 'RESTRICTIVE'} FOR ${command} TO PUBLIC`,
|
|
52
|
+
...(using === undefined ? [] : [`USING (${using})`]),
|
|
53
|
+
...(check === undefined ? [] : [`WITH CHECK (${check})`]),
|
|
54
|
+
].join(' ');
|
|
55
|
+
const name = ownedName(table, op ? `${label}_${op}` : label, create(''));
|
|
56
|
+
return { name, statements: [create(name)] };
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/** Whether `command` writes rows, which `WITH CHECK` holds. */
|
|
61
|
+
function writes(command) {
|
|
62
|
+
return command === 'ALL' || command === 'INSERT' || command === 'UPDATE';
|
|
63
|
+
}
|
|
64
|
+
/** What removes a policy from `table`, a name qualified where its schema is. */
|
|
65
|
+
export function dropPolicy(dialect, table, name) {
|
|
66
|
+
return `DROP POLICY IF EXISTS ${dialect.escapeId(name)} ON ${dialect.escapeId(table)}`;
|
|
67
|
+
}
|
|
68
|
+
/** A `when` as the context comparisons it is, each read as text, as the binding sends every value. */
|
|
69
|
+
function whenSql(dialect, when) {
|
|
70
|
+
return Object.entries(when ?? {}).map(([key, value]) => {
|
|
71
|
+
const values = [value].flat().map((it) => dialect.escape(String(it)));
|
|
72
|
+
return values.length ? `${dialect.contextRead(key, 'text')} IN (${values.join(', ')})` : 'false';
|
|
73
|
+
});
|
|
74
|
+
}
|
|
@@ -83,6 +83,7 @@ export function reverseDiff(diff) {
|
|
|
83
83
|
foreignKeys: diff.foreignKeys?.map((change) => putBack(change, (foreignKey) => ({ ...foreignKey, columns: foreignKey.columns.map(nameOf) }))),
|
|
84
84
|
checks: diff.checks?.map(swap),
|
|
85
85
|
triggers: diff.triggers?.map(swap),
|
|
86
|
+
policies: diff.policies?.map(swap),
|
|
86
87
|
renamedColumns,
|
|
87
88
|
rebuild: diff.rebuild && { from: diff.rebuild.to, to: diff.rebuild.from },
|
|
88
89
|
};
|
|
@@ -58,6 +58,8 @@ export declare class SqlSchemaGenerator implements SchemaGenerator {
|
|
|
58
58
|
* MySQL refuses to rename or drop under one. An alter is its drop, then its add.
|
|
59
59
|
*/
|
|
60
60
|
generateAlterTable(diff: SchemaDiff): string[];
|
|
61
|
+
/** What switches `tableName`'s row-level security as `policies` change it, as `[off, on]`: on with its first, off with its last. */
|
|
62
|
+
private rowSecurityChange;
|
|
61
63
|
/** `CONSTRAINT <name> CHECK (...)`, in a `CREATE TABLE`, after `ADD`, or inline where a column brings it. */
|
|
62
64
|
private checkConstraint;
|
|
63
65
|
private addCheckSql;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getMeta } from '../entity/index.js';
|
|
2
|
-
import { canonicalToSql, engineType, isVectorCategory, suppliedKeySql } from '../schema/canonicalType.js';
|
|
2
|
+
import { canonicalToSql, engineType, isVectorCategory, resolveColumnCanonicalType, suppliedKeySql, } from '../schema/canonicalType.js';
|
|
3
3
|
import { indexChanges } from '../schema/indexDifferences.js';
|
|
4
4
|
import { matchByKey } from '../schema/matchByKey.js';
|
|
5
5
|
import { buildSchemaAST } from '../schema/schemaASTBuilder.js';
|
|
@@ -16,6 +16,7 @@ import { rebuildTable } from './ddl/tableRebuild.js';
|
|
|
16
16
|
import { columnForeignKey, columnIndex, bareColumn, renderIndexDefinition, tableDefinitionToNode, } from './generator/definitionToNode.js';
|
|
17
17
|
import { indexNodeToSchema } from './generator/indexNodeToSchema.js';
|
|
18
18
|
import { assertIndexPredicate } from './indexPredicate.js';
|
|
19
|
+
import { dropPolicy, renderPolicies } from './policySql.js';
|
|
19
20
|
import { added, alterations, dropped, needsRebuild, newlyRequired, nonEmpty, sides } from './schemaChange.js';
|
|
20
21
|
import { dropTrigger, dropTriggerBody, namesAny, renderTrigger, stampTriggers } from './triggerSql.js';
|
|
21
22
|
/** The SQL schema generator, one for every SQL engine, its spellings taken from the dialect and its DDL classes. */
|
|
@@ -69,6 +70,7 @@ export class SqlSchemaGenerator {
|
|
|
69
70
|
},
|
|
70
71
|
defaultForeignKeyAction: this.defaultForeignKeyAction,
|
|
71
72
|
renderTriggers: (meta) => [...(meta.triggers ?? []), ...stampTriggers(this.dialect, meta)].map((trigger, i) => renderTrigger(this.dialect, meta, trigger, i)),
|
|
73
|
+
renderPolicies: (meta) => renderPolicies(this.dialect, meta, (field) => canonicalToSql(resolveColumnCanonicalType(field), this.dialect)),
|
|
72
74
|
textScoreIndexes: this.dialect.features.textScoreIndexes,
|
|
73
75
|
vectorIndexRequiresNotNull: this.features.vectorIndexRequiresNotNull,
|
|
74
76
|
});
|
|
@@ -90,6 +92,12 @@ export class SqlSchemaGenerator {
|
|
|
90
92
|
: tables.flatMap((table) => table.outgoingRelations.map((relation) => this.addForeignKeySql(qualifyName(table.name, table.schema), foreignKeyOf(relation))))),
|
|
91
93
|
// Triggers last: each needs its own table, and a body may read any other the same schema just made.
|
|
92
94
|
...terminated(tables.flatMap((table) => table.triggers.flatMap((trigger) => trigger.statements))),
|
|
95
|
+
// Policies after them, a membership read through a table the same schema just made included.
|
|
96
|
+
...tables.flatMap((table) => {
|
|
97
|
+
const policies = table.policies.map((to) => ({ to }));
|
|
98
|
+
const [, on] = this.rowSecurityChange(qualifyName(table.name, table.schema), policies);
|
|
99
|
+
return [...on, ...terminated(sides(policies, 'to').flatMap((policy) => policy.statements))];
|
|
100
|
+
}),
|
|
93
101
|
];
|
|
94
102
|
}
|
|
95
103
|
/**
|
|
@@ -162,10 +170,19 @@ export class SqlSchemaGenerator {
|
|
|
162
170
|
...sides(columns, 'from').map(({ name }) => name),
|
|
163
171
|
...(diff.renamedColumns ?? []).flatMap(({ from, to }) => [from, to]),
|
|
164
172
|
];
|
|
165
|
-
//
|
|
166
|
-
|
|
173
|
+
// Postgres refuses to retype or drop a column a trigger or a policy reads: a rebuild drops every one with its
|
|
174
|
+
// table, an alter takes off only a kept one naming a column it moves, and puts it back last.
|
|
175
|
+
const cycled = (changes = []) => changes.filter(({ from, to }) => rebuild || !from || !to || to.statements.some((statement) => namesAny(statement, moved)));
|
|
176
|
+
const triggers = cycled(diff.triggers);
|
|
167
177
|
const triggerDrops = terminated(sides(triggers, 'from').flatMap((trigger) => dropTrigger(this.dialect, tableName, trigger.name)));
|
|
168
178
|
const triggerCreates = terminated(sides(triggers, 'to').flatMap((trigger) => trigger.statements));
|
|
179
|
+
const policies = cycled(diff.policies);
|
|
180
|
+
const [off, on] = this.rowSecurityChange(tableName, diff.policies);
|
|
181
|
+
const policyDrops = [
|
|
182
|
+
...terminated(sides(policies, 'from').map((policy) => dropPolicy(this.dialect, tableName, policy.name))),
|
|
183
|
+
...off,
|
|
184
|
+
];
|
|
185
|
+
const policyCreates = [...on, ...terminated(sides(policies, 'to').flatMap((policy) => policy.statements))];
|
|
169
186
|
if (rebuild) {
|
|
170
187
|
return [
|
|
171
188
|
...triggerDrops,
|
|
@@ -176,6 +193,7 @@ export class SqlSchemaGenerator {
|
|
|
176
193
|
const target = this.escapeId(tableName);
|
|
177
194
|
const [dropKey, addKey] = this.tableDdl.replacePrimaryKey(tableName, primaryKey?.from, primaryKey?.to);
|
|
178
195
|
return [
|
|
196
|
+
...policyDrops,
|
|
179
197
|
...triggerDrops,
|
|
180
198
|
...sides(diff.foreignKeys, 'from').map((foreignKey) => this.tableDdl.dropForeignKey(tableName, constraintNameOf(tableName, foreignKey))),
|
|
181
199
|
...dropKey,
|
|
@@ -194,8 +212,17 @@ export class SqlSchemaGenerator {
|
|
|
194
212
|
...sides(diff.foreignKeys, 'to').map((foreignKey) => this.addForeignKeySql(tableName, foreignKey)),
|
|
195
213
|
...sides(diff.checks, 'to').map((check) => this.addCheckSql(tableName, check)),
|
|
196
214
|
...triggerCreates,
|
|
215
|
+
...policyCreates,
|
|
197
216
|
];
|
|
198
217
|
}
|
|
218
|
+
/** What switches `tableName`'s row-level security as `policies` change it, as `[off, on]`: on with its first, off with its last. */
|
|
219
|
+
rowSecurityChange(tableName, policies = []) {
|
|
220
|
+
const had = sides(policies, 'from').length > 0;
|
|
221
|
+
const has = sides(policies, 'to').length > 0;
|
|
222
|
+
return had === has
|
|
223
|
+
? [[], []]
|
|
224
|
+
: [had ? this.tableDdl.rowSecurity(tableName, false) : [], has ? this.tableDdl.rowSecurity(tableName, true) : []];
|
|
225
|
+
}
|
|
199
226
|
/** `CONSTRAINT <name> CHECK (...)`, in a `CREATE TABLE`, after `ADD`, or inline where a column brings it. */
|
|
200
227
|
checkConstraint(check) {
|
|
201
228
|
return `CONSTRAINT ${this.escapeId(check.name)} CHECK (${check.expression})`;
|
|
@@ -274,14 +301,9 @@ export class SqlSchemaGenerator {
|
|
|
274
301
|
}))),
|
|
275
302
|
foreignKeys: nonEmpty(foreignKeys),
|
|
276
303
|
checks: nonEmpty(tableDiff?.checks ?? []),
|
|
277
|
-
// A kept trigger as both sides, which the alter cycles only where a column it names moves under it.
|
|
278
|
-
triggers: nonEmpty([
|
|
279
|
-
|
|
280
|
-
...matchByKey(desired.triggers, currentTable.triggers, (it) => it.name).matched.map(([to, from]) => ({
|
|
281
|
-
from,
|
|
282
|
-
to,
|
|
283
|
-
})),
|
|
284
|
-
]),
|
|
304
|
+
// A kept trigger or policy as both sides, which the alter cycles only where a column it names moves under it.
|
|
305
|
+
triggers: nonEmpty([...(tableDiff?.triggers ?? []), ...kept(desired.triggers, currentTable.triggers)]),
|
|
306
|
+
policies: nonEmpty([...(tableDiff?.policies ?? []), ...kept(desired.policies, currentTable.policies)]),
|
|
285
307
|
renamedColumns: nonEmpty(renamedColumns ?? []),
|
|
286
308
|
};
|
|
287
309
|
if (!(tableDiff || alter.indexes || alter.foreignKeys || alter.renamedColumns)) {
|
|
@@ -469,6 +491,10 @@ export class SqlSchemaGenerator {
|
|
|
469
491
|
export function dropsForeignKeysFirst(features, options) {
|
|
470
492
|
return !(options.cascade && features.dropTableCascade) && !features.rebuildsTables;
|
|
471
493
|
}
|
|
494
|
+
/** Each of `desired` the database has too, matched by name, as both sides of a change. */
|
|
495
|
+
function kept(desired, current) {
|
|
496
|
+
return matchByKey(desired, current, (it) => it.name).matched.map(([to, from]) => ({ from, to }));
|
|
497
|
+
}
|
|
472
498
|
/** Statements as a migration runs them, each ended. */
|
|
473
499
|
function terminated(statements) {
|
|
474
500
|
return statements.map((sql) => `${sql};`);
|
|
@@ -99,6 +99,8 @@ export const mongoDialectFeatures = {
|
|
|
99
99
|
rowLocks: false, // its concurrency control is atomic document updates, plus a transaction the caller opens
|
|
100
100
|
reportsWrites: [],
|
|
101
101
|
nullsSortLowest: true,
|
|
102
|
+
rowSecurity: false,
|
|
103
|
+
transactions: false,
|
|
102
104
|
};
|
|
103
105
|
/** What `toWireId` converts: the hex spelling of an `ObjectId`, and nothing looser. */
|
|
104
106
|
const HEX_24 = /^[0-9a-f]{24}$/i;
|
|
@@ -1050,7 +1052,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1050
1052
|
continue;
|
|
1051
1053
|
}
|
|
1052
1054
|
// Unconditional, not gated by an explicit relation-level `$where`: the related entity's own
|
|
1053
|
-
// filters
|
|
1055
|
+
// filters and policies apply even to a bare `$populate: { rel: true }`,
|
|
1054
1056
|
// and the caller's bypass never reaches them, exactly like the SQL dialects' JOIN ON-clause filters.
|
|
1055
1057
|
assertNoRelationText(join.path, join.query);
|
|
1056
1058
|
const relationFilter = this.where(join.entity, join.query.$where ?? {});
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { ClientSession, Document, MongoClient } from 'mongodb';
|
|
2
|
-
import type { UpsertedId } from '../querier/abstractQuerier.js';
|
|
3
2
|
import { AbstractQuerier } from '../querier/index.js';
|
|
4
|
-
import type { EntityData, ExtraOptions, Query, QueryAggregate, QueryUpsert, QueryPage, StatementOptions, QuerySearch, RawRow, TransactionOptions, Type, UpdatePayload } from '../type/index.js';
|
|
3
|
+
import type { EntityData, ExtraOptions, WrittenId, Query, QueryAggregate, QueryUpsert, QueryPage, StatementOptions, QuerySearch, RawRow, TransactionOptions, Type, UpdatePayload } from '../type/index.js';
|
|
5
4
|
import type { MongoDialect } from './mongoDialect.js';
|
|
6
5
|
import type { MongoQuerier } from './mongoQuerier.js';
|
|
7
6
|
export declare class MongodbQuerier extends AbstractQuerier implements MongoQuerier {
|
|
@@ -48,14 +47,12 @@ export declare class MongodbQuerier extends AbstractQuerier implements MongoQuer
|
|
|
48
47
|
* Updating a found document `apart`, it writes only an inserted one.
|
|
49
48
|
*/
|
|
50
49
|
private upsertUpdate;
|
|
51
|
-
protected internalUpsertMany<E extends Document>(entity: Type<E>, q: QueryUpsert<E>, payload: E[]): Promise<(
|
|
50
|
+
protected internalUpsertMany<E extends Document>(entity: Type<E>, q: QueryUpsert<E>, payload: E[]): Promise<(WrittenId<E> | undefined)[]>;
|
|
52
51
|
protected internalDeleteMany<E extends Document>(entity: Type<E>, qm: QuerySearch<E>, opts?: StatementOptions): Promise<number>;
|
|
53
52
|
get hasOpenTransaction(): boolean;
|
|
54
53
|
/** Every read and write goes through here, which makes it where a released querier is caught. */
|
|
55
54
|
collection<E extends Document>(entity: Type<E>): import("mongodb").Collection<E>;
|
|
56
55
|
get db(): import("mongodb").Db;
|
|
57
|
-
/** Runs `write` as its steps: a multi-step write is atomic only inside a transaction the caller opens. */
|
|
58
|
-
protected atomically<T>(write: () => Promise<T>): Promise<T>;
|
|
59
56
|
protected openTransaction(_opts?: TransactionOptions): Promise<void>;
|
|
60
57
|
/**
|
|
61
58
|
* The driver owns the transaction state here and settles it in its own `finally`, so a failed commit
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { AGGREGATE_VALUE_ALIAS } from '../dialect/aliases.js';
|
|
2
2
|
import { hasRequiredJoin } from '../dialect/queryJoins.js';
|
|
3
|
-
import { getMeta,
|
|
3
|
+
import { getMeta, soleIdOf } from '../entity/index.js';
|
|
4
4
|
import { AbstractQuerier } from '../querier/index.js';
|
|
5
|
-
import { clone, deletionOf, foundRowWhere, foundRowAssigned,
|
|
5
|
+
import { clone, deletionOf, foundRowWhere, foundRowAssigned, hasKeys, hasTriggers, withDeleted, } from '../util/index.js';
|
|
6
6
|
import { UqlUsageError } from '../util/uqlError.js';
|
|
7
7
|
/**
|
|
8
8
|
* `$limit: 0` asks for no rows, the way it does on every SQL dialect - but MongoDB reads `limit(0)`
|
|
@@ -183,33 +183,25 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
183
183
|
}
|
|
184
184
|
async internalUpsertMany(entity, q, payload) {
|
|
185
185
|
refuseTriggers(entity);
|
|
186
|
-
if (!payload.length) {
|
|
187
|
-
return [];
|
|
188
|
-
}
|
|
189
186
|
// A found document is updated apart from the insert when the upsert has an update of its own, as `$setOnInsert`
|
|
190
187
|
// and `$inc` on one field conflict in one operation, or a `$where`. The two batches are not atomic on their own:
|
|
191
188
|
// a transaction the caller opens makes them so.
|
|
192
189
|
const apart = q.$update !== undefined || q.$where !== undefined;
|
|
193
190
|
const meta = getMeta(entity);
|
|
194
191
|
const { $conflict } = q;
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
filter: this.dialect.where(entity, foundRowWhere({ $conflict }, row), withDeleted()),
|
|
204
|
-
update: this.upsertUpdate(meta, row, apart),
|
|
205
|
-
upsert: true,
|
|
206
|
-
},
|
|
207
|
-
});
|
|
192
|
+
// A soft-deleted document conflicts as any other does, its unique key holding across both.
|
|
193
|
+
const upserts = payload.map((row) => ({
|
|
194
|
+
updateOne: {
|
|
195
|
+
filter: this.dialect.where(entity, foundRowWhere({ $conflict }, row), withDeleted()),
|
|
196
|
+
update: this.upsertUpdate(meta, row, apart),
|
|
197
|
+
upsert: true,
|
|
198
|
+
},
|
|
199
|
+
}));
|
|
208
200
|
const res = await this.execute('upsertMany', (session) => this.collection(entity).bulkWrite(upserts, { session }));
|
|
209
|
-
// `upsertedIds`
|
|
210
|
-
//
|
|
201
|
+
// `upsertedIds` names only the documents inserted, keyed by operation index, so each lands on its own row; an
|
|
202
|
+
// updated document's `_id` is read back by the conflict fields instead.
|
|
211
203
|
const reported = payload.map((_, index) => {
|
|
212
|
-
const id = res.upsertedIds[index]
|
|
204
|
+
const id = res.upsertedIds[index];
|
|
213
205
|
return id === undefined ? undefined : this.dialect.fromWireId(id);
|
|
214
206
|
});
|
|
215
207
|
const updates = apart
|
|
@@ -223,7 +215,7 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
223
215
|
if (updates.length) {
|
|
224
216
|
await this.execute('upsertMany', (session) => this.collection(entity).bulkWrite(updates, { session }));
|
|
225
217
|
}
|
|
226
|
-
return
|
|
218
|
+
return reported;
|
|
227
219
|
}
|
|
228
220
|
async internalDeleteMany(entity, qm, opts = {}) {
|
|
229
221
|
refuseTriggers(entity);
|
|
@@ -251,10 +243,6 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
251
243
|
get db() {
|
|
252
244
|
return this.conn.db();
|
|
253
245
|
}
|
|
254
|
-
/** Runs `write` as its steps: a multi-step write is atomic only inside a transaction the caller opens. */
|
|
255
|
-
atomically(write) {
|
|
256
|
-
return write();
|
|
257
|
-
}
|
|
258
246
|
async openTransaction(_opts) {
|
|
259
247
|
this.logger.logInfo('startTransaction');
|
|
260
248
|
await this.#session?.endSession();
|
|
@@ -51,6 +51,8 @@ const MSSQL_FEATURES = {
|
|
|
51
51
|
deferrable: false,
|
|
52
52
|
},
|
|
53
53
|
namedLocks: 'transaction',
|
|
54
|
+
rowSecurity: false,
|
|
55
|
+
transactions: true,
|
|
54
56
|
};
|
|
55
57
|
/** The `type` `OPENJSON` reports for each JSON kind. */
|
|
56
58
|
const OPENJSON_TYPE = { null: 0, string: 1, number: 2, boolean: 3, array: 4, object: 5 };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { type PgAnyClient, PgQuerier } from '../postgres/pgQuerier.js';
|
|
2
|
-
import type { BoundStatement } from '../
|
|
3
|
-
import type { ExtraOptions, TransactionOptions } from '../type/index.js';
|
|
2
|
+
import type { BoundStatement, ExtraOptions, TransactionOptions } from '../type/index.js';
|
|
4
3
|
import type { SqlExecution } from '../util/sql.util.js';
|
|
5
4
|
import type { NeonDialect } from './neonDialect.js';
|
|
6
5
|
/** Neon over HTTP, what a statement goes through outside a session: one statement, or a list as one transaction. */
|
|
@@ -2,7 +2,7 @@ import { type CallHandler, type ExecutionContext, type NestInterceptor } from '@
|
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
3
|
import type { UqlContext } from '../type/index.js';
|
|
4
4
|
/**
|
|
5
|
-
* Runs each request inside `withContext`, so parameterized
|
|
5
|
+
* Runs each request inside `withContext`, so policies and parameterized filters
|
|
6
6
|
* are scoped automatically for every query in the request - including relations, cascades, and the
|
|
7
7
|
* queries a transaction runs. Wired for you by {@link UqlModule.forRoot} when you pass `contextOf`.
|
|
8
8
|
*/
|
|
@@ -36,7 +36,7 @@ import { Injectable } from '@nestjs/common';
|
|
|
36
36
|
import { from, Observable, switchMap } from 'rxjs';
|
|
37
37
|
import { withContext } from '../context/context.js';
|
|
38
38
|
/**
|
|
39
|
-
* Runs each request inside `withContext`, so parameterized
|
|
39
|
+
* Runs each request inside `withContext`, so policies and parameterized filters
|
|
40
40
|
* are scoped automatically for every query in the request - including relations, cascades, and the
|
|
41
41
|
* queries a transaction runs. Wired for you by {@link UqlModule.forRoot} when you pass `contextOf`.
|
|
42
42
|
*/
|
|
@@ -8,8 +8,8 @@ type UqlModuleCommon<Req> = {
|
|
|
8
8
|
readonly global?: boolean;
|
|
9
9
|
/**
|
|
10
10
|
* Derive the ambient {@link UqlContext} (e.g. `{ tenantId, userId }`) from each HTTP request.
|
|
11
|
-
* When set, a global interceptor runs every request inside `withContext`, so
|
|
12
|
-
*
|
|
11
|
+
* When set, a global interceptor runs every request inside `withContext`, so policies (multi-tenancy,
|
|
12
|
+
* row-level security) and parameterized filters are scoped automatically. Derive tenant/auth from a
|
|
13
13
|
* verified source (session, JWT) - never trust the client.
|
|
14
14
|
*/
|
|
15
15
|
readonly contextOf?: (request: Req) => UqlContext | undefined | Promise<UqlContext | undefined>;
|
package/dist/nestjs/uqlModule.js
CHANGED
|
@@ -93,7 +93,7 @@ let UqlModule = (() => {
|
|
|
93
93
|
},
|
|
94
94
|
];
|
|
95
95
|
if (contextOf) {
|
|
96
|
-
// Scope every request to its context so
|
|
96
|
+
// Scope every request to its context so policies apply automatically.
|
|
97
97
|
providers.push({ provide: APP_INTERCEPTOR, useValue: new UqlContextInterceptor(contextOf) });
|
|
98
98
|
}
|
|
99
99
|
return { module: UqlModule, global, imports, providers, exports: [UQL_QUERIER_POOL] };
|