uql-orm 0.114.4 → 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.
Files changed (84) hide show
  1. package/dist/cockroachdb/cockroachDialect.d.ts +2 -0
  2. package/dist/cockroachdb/cockroachDialect.js +5 -0
  3. package/dist/d1/d1Querier.d.ts +2 -4
  4. package/dist/d1/d1Querier.js +0 -4
  5. package/dist/d1/d1SqliteDialect.js +1 -1
  6. package/dist/dialect/abstractDialect.d.ts +17 -8
  7. package/dist/dialect/abstractDialect.js +30 -16
  8. package/dist/dialect/abstractSqlDialect.d.ts +11 -2
  9. package/dist/dialect/abstractSqlDialect.js +17 -2
  10. package/dist/dialect/mysqlLikeSqlDialect.js +2 -0
  11. package/dist/dialect/operators.d.ts +2 -2
  12. package/dist/dialect/pgLikeSqlDialect.d.ts +8 -1
  13. package/dist/dialect/pgLikeSqlDialect.js +20 -0
  14. package/dist/entity/columnOptionsBuilder.d.ts +2 -1
  15. package/dist/entity/columnOptionsBuilder.js +3 -0
  16. package/dist/entity/metadata/definedFixture/author.d.ts +1 -1
  17. package/dist/entity/metadata/definedFixture/book.d.ts +2 -2
  18. package/dist/entity/metadata/definedFixture/folder.d.ts +1 -1
  19. package/dist/entity/metadata/definition.js +69 -30
  20. package/dist/http/api.d.ts +1 -1
  21. package/dist/http/wireQuery.js +1 -1
  22. package/dist/index.d.ts +2 -0
  23. package/dist/index.js +2 -0
  24. package/dist/migrate/ddl/tableDdl.d.ts +5 -0
  25. package/dist/migrate/ddl/tableDdl.js +9 -0
  26. package/dist/migrate/drift/driftDetector.js +2 -1
  27. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +3 -1
  28. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +7 -1
  29. package/dist/migrate/introspection/postgresIntrospector.d.ts +3 -1
  30. package/dist/migrate/introspection/postgresIntrospector.js +14 -1
  31. package/dist/migrate/introspection/tableSchemaAST.js +1 -0
  32. package/dist/migrate/policySql.d.ts +12 -0
  33. package/dist/migrate/policySql.js +74 -0
  34. package/dist/migrate/schemaChange.js +1 -0
  35. package/dist/migrate/schemaGenerator.d.ts +2 -0
  36. package/dist/migrate/schemaGenerator.js +37 -11
  37. package/dist/mongodb/mongoDialect.js +3 -1
  38. package/dist/mongodb/mongodbQuerier.d.ts +0 -2
  39. package/dist/mongodb/mongodbQuerier.js +0 -4
  40. package/dist/mssql/mssqlDialect.js +2 -0
  41. package/dist/neon/neonQuerier.d.ts +1 -2
  42. package/dist/nestjs/uqlContextInterceptor.d.ts +1 -1
  43. package/dist/nestjs/uqlContextInterceptor.js +1 -1
  44. package/dist/nestjs/uqlModule.d.ts +2 -2
  45. package/dist/nestjs/uqlModule.js +1 -1
  46. package/dist/querier/abstractQuerier.d.ts +31 -4
  47. package/dist/querier/abstractQuerier.js +120 -52
  48. package/dist/querier/abstractQuerierPool.js +2 -0
  49. package/dist/querier/abstractSqlQuerier.d.ts +16 -6
  50. package/dist/querier/abstractSqlQuerier.js +66 -6
  51. package/dist/querier/queryError.js +1 -0
  52. package/dist/schema/schemaAST.js +1 -0
  53. package/dist/schema/schemaASTBuilder.d.ts +3 -1
  54. package/dist/schema/schemaASTBuilder.js +1 -0
  55. package/dist/schema/schemaASTDiffer.js +4 -2
  56. package/dist/schema/types.d.ts +12 -4
  57. package/dist/sqlite/bindValues.d.ts +1 -1
  58. package/dist/sqlite/hranaQuerier.d.ts +2 -2
  59. package/dist/sqlite/sqliteDialect.js +2 -0
  60. package/dist/turso/tursoSessionQuerier.d.ts +2 -2
  61. package/dist/type/column.d.ts +10 -0
  62. package/dist/type/definedEntity.d.ts +9 -4
  63. package/dist/type/dialect.d.ts +13 -0
  64. package/dist/type/entity.d.ts +10 -0
  65. package/dist/type/index.d.ts +1 -0
  66. package/dist/type/index.js +1 -0
  67. package/dist/type/migration.d.ts +5 -1
  68. package/dist/type/policy.d.ts +91 -0
  69. package/dist/type/policy.js +1 -0
  70. package/dist/type/querier.d.ts +14 -0
  71. package/dist/type/querierPool.d.ts +2 -2
  72. package/dist/type/query.d.ts +18 -31
  73. package/dist/type/query.js +2 -0
  74. package/dist/util/contextRefs.d.ts +6 -0
  75. package/dist/util/contextRefs.js +5 -0
  76. package/dist/util/dialect.util.d.ts +10 -11
  77. package/dist/util/dialect.util.js +70 -56
  78. package/dist/util/fieldOption.util.js +1 -0
  79. package/dist/util/policy.util.d.ts +100 -0
  80. package/dist/util/policy.util.js +234 -0
  81. package/dist/util/uqlError.d.ts +2 -5
  82. package/dist/util/uqlError.js +2 -5
  83. package/package.json +1 -1
  84. package/skills/uql-orm/SKILL.md +23 -13
@@ -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 trigger the database lacks, critical since rows are written otherwise than the entity says, 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
- // A rebuild drops every trigger with its table; an alter, only a kept one naming a column it moves.
166
- const triggers = (diff.triggers ?? []).filter(({ from, to }) => rebuild || !from || !to || to.statements.some((statement) => namesAny(statement, moved)));
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
- ...(tableDiff?.triggers ?? []),
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 (in particular `security: true` ones) apply even to a bare `$populate: { rel: true }`,
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 ?? {});
@@ -53,8 +53,6 @@ export declare class MongodbQuerier extends AbstractQuerier implements MongoQuer
53
53
  /** Every read and write goes through here, which makes it where a released querier is caught. */
54
54
  collection<E extends Document>(entity: Type<E>): import("mongodb").Collection<E>;
55
55
  get db(): import("mongodb").Db;
56
- /** Runs `write` as its steps: a multi-step write is atomic only inside a transaction the caller opens. */
57
- protected atomically<T>(write: () => Promise<T>): Promise<T>;
58
56
  protected openTransaction(_opts?: TransactionOptions): Promise<void>;
59
57
  /**
60
58
  * The driver owns the transaction state here and settles it in its own `finally`, so a failed commit
@@ -243,10 +243,6 @@ export class MongodbQuerier extends AbstractQuerier {
243
243
  get db() {
244
244
  return this.conn.db();
245
245
  }
246
- /** Runs `write` as its steps: a multi-step write is atomic only inside a transaction the caller opens. */
247
- atomically(write) {
248
- return write();
249
- }
250
246
  async openTransaction(_opts) {
251
247
  this.logger.logInfo('startTransaction');
252
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 '../querier/index.js';
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/`security` filters (multi-tenancy, RLS)
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/`security` filters (multi-tenancy, RLS)
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 parameterized /
12
- * `security` filters (multi-tenancy, RLS) are scoped automatically. Derive tenant/auth from a
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>;
@@ -93,7 +93,7 @@ let UqlModule = (() => {
93
93
  },
94
94
  ];
95
95
  if (contextOf) {
96
- // Scope every request to its context so multi-tenancy / security filters apply automatically.
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] };
@@ -151,6 +151,24 @@ export declare abstract class AbstractQuerier implements Querier {
151
151
  * and the insert half of a read upsert. Answers what `insert` reports.
152
152
  */
153
153
  private insertRows;
154
+ /**
155
+ * {@link guardWrite} under a pool holding writes to the policies, answering how the write of `rows` runs: alone, or
156
+ * with the rows it wrote read back against the check they could not settle, in a transaction undoing it where one
157
+ * does not hold. Where the database holds the policies the rows still get their pinned fields and an early refusal,
158
+ * and nothing is read back. A checked write is refused where this querier holds no transaction to undo it in.
159
+ */
160
+ private guarded;
161
+ /**
162
+ * {@link guardWrite} where this pool reads written rows back, answering the check to read them against; else
163
+ * {@link pinUpserted}'s pinning alone, where it guards writes at all.
164
+ */
165
+ private guardRows;
166
+ /** {@link pinWrite} where this pool guards writes: an upsert's branches check what they write, or its database does. */
167
+ private pinUpserted;
168
+ /** Whether {@link atomically} undoes every step of a write that fails partway: a transaction holds them. */
169
+ private undoesSteps;
170
+ /** Refuses, undoing the write, where any row `ids` names does not hold `check` once written. */
171
+ private holdToCheck;
154
172
  /** Writes `rows`, its columns only, and onto each one the key the database generated for it, where it can tell. */
155
173
  protected abstract internalInsertMany<E extends object>(entity: Type<E>, rows: EntityData<E>[]): Promise<void>;
156
174
  updateOneById<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, id: EntityId<E>, payload: UpdateWrite<E>, opts?: QueryOptions & WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['affected']>;
@@ -187,7 +205,10 @@ export declare abstract class AbstractQuerier implements Querier {
187
205
  * still costs one statement. Best effort by nature - the row can change again while we ask.
188
206
  */
189
207
  private throwStaleVersion;
190
- /** What every update does to its row before its statement: writes the to-ones it holds, fills and guards it. */
208
+ /**
209
+ * What every update does to its row before its statement: writes the to-ones it holds, fills and guards it.
210
+ * Answers how its write runs, as {@link guarded} does.
211
+ */
191
212
  private prepareUpdate;
192
213
  /** The UPDATE, skipped where the payload writes no column, reporting `unwritten` instead. */
193
214
  private updateColumns;
@@ -229,7 +250,13 @@ export declare abstract class AbstractQuerier implements Querier {
229
250
  $entity: Type<E>;
230
251
  }, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['written'][]>;
231
252
  upsertMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, q: QueryUpsert<E>, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['written'][]>;
232
- /** Upserts `rows`, each naming every conflict key, reading first where {@link upsertsByReading} says so: the id each got. */
253
+ /**
254
+ * Whether an upsert has to read before it writes: `ON CONFLICT` updates a row the querier holds to policies whatever
255
+ * tenant it belongs to, writes no relation, and cannot tell a found row it skipped for a stale version. A database
256
+ * holding the policies itself holds the conflicting row to them in the one statement.
257
+ */
258
+ private upsertsByReading;
259
+ /** Upserts `rows`, each naming every conflict key, reading first where `reads`: the id each got. */
233
260
  private upsertIds;
234
261
  /**
235
262
  * The id of each of `rows` the engine's upsert reports, or {@link withKept} reads back. A row matched on its own key
@@ -260,8 +287,8 @@ export declare abstract class AbstractQuerier implements Querier {
260
287
  /**
261
288
  * An upsert as a read, then writes: the rows the conflict names are read through the filters, those found update
262
289
  * by id as `updateOne` does, a versioned one at its version, and the rest insert, so either branch writes its
263
- * relations and a guarded row stays its tenant's. Inside a transaction a found row is held until the write, and
264
- * a row inserted concurrently fails on its key. See architecture/security-filter-writes.md.
290
+ * relations and a policied row stays its tenant's. Inside a transaction a found row is held until the write, and
291
+ * a row inserted concurrently fails on its key. See architecture/row-level-security.md.
265
292
  */
266
293
  private readThenUpsert;
267
294
  /**