uql-orm 0.99.0 → 0.100.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.
Files changed (174) hide show
  1. package/LICENSE.md +22 -0
  2. package/README.md +1 -1
  3. package/dist/browser/uql-browser.min.js +2 -2
  4. package/dist/browser/uql-browser.min.js.map +5 -5
  5. package/dist/bunSql/bunSql.util.js +2 -2
  6. package/dist/bunSql/bunSqlQuerier.js +0 -1
  7. package/dist/bunSql/bunSqlQuerierPool.js +1 -1
  8. package/dist/cockroachdb/cockroachDialect.js +1 -0
  9. package/dist/context/context.browser.d.ts +5 -0
  10. package/dist/context/context.browser.js +21 -11
  11. package/dist/context/context.d.ts +5 -0
  12. package/dist/context/context.js +8 -3
  13. package/dist/dialect/abstractDialect.js +4 -2
  14. package/dist/dialect/abstractSqlDialect.d.ts +5 -5
  15. package/dist/dialect/abstractSqlDialect.js +26 -18
  16. package/dist/dialect/aliases.d.ts +0 -2
  17. package/dist/dialect/aliases.js +0 -2
  18. package/dist/dialect/hydrateColumn.d.ts +3 -3
  19. package/dist/dialect/hydrateColumn.js +1 -0
  20. package/dist/dialect/index.js +0 -2
  21. package/dist/dialect/jsonSql.d.ts +2 -3
  22. package/dist/dialect/jsonSql.js +4 -6
  23. package/dist/dialect/mysqlLikeSqlDialect.d.ts +1 -1
  24. package/dist/dialect/mysqlLikeSqlDialect.js +5 -3
  25. package/dist/dialect/operators.d.ts +2 -3
  26. package/dist/dialect/operators.js +3 -4
  27. package/dist/dialect/pgLikeSqlDialect.js +12 -12
  28. package/dist/entity/decorator/members.d.ts +4 -12
  29. package/dist/entity/metadata/definition.js +8 -5
  30. package/dist/http/handler.d.ts +6 -6
  31. package/dist/http/handler.js +20 -5
  32. package/dist/index.d.ts +6 -5
  33. package/dist/index.js +6 -5
  34. package/dist/{maria → mariadb}/mariadbQuerier.js +1 -1
  35. package/dist/migrate/builder/columnBuilder.d.ts +3 -3
  36. package/dist/migrate/builder/columnBuilder.js +1 -1
  37. package/dist/migrate/builder/index.d.ts +0 -3
  38. package/dist/migrate/builder/index.js +0 -3
  39. package/dist/migrate/builder/migrationBuilder.d.ts +6 -6
  40. package/dist/migrate/builder/migrationBuilder.js +7 -7
  41. package/dist/migrate/builder/tableBuilder.d.ts +25 -25
  42. package/dist/migrate/builder/tableBuilder.js +5 -5
  43. package/dist/migrate/builder/types.d.ts +37 -37
  44. package/dist/migrate/cli-config.d.ts +11 -1
  45. package/dist/migrate/cli-config.js +20 -6
  46. package/dist/migrate/cli.js +9 -18
  47. package/dist/migrate/codegen/entityCodeGenerator.js +1 -1
  48. package/dist/migrate/codegen/migrationFile.d.ts +7 -7
  49. package/dist/migrate/codegen/migrationFile.js +11 -12
  50. package/dist/migrate/ddl/indexCapabilities.d.ts +2 -0
  51. package/dist/migrate/ddl/indexCapabilities.js +6 -0
  52. package/dist/migrate/ddl/indexDdl.d.ts +8 -0
  53. package/dist/migrate/ddl/indexDdl.js +22 -6
  54. package/dist/migrate/generator/definitionToNode.d.ts +1 -1
  55. package/dist/migrate/generator/definitionToNode.js +1 -1
  56. package/dist/migrate/index.d.ts +1 -3
  57. package/dist/migrate/index.js +0 -3
  58. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +11 -15
  59. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +13 -15
  60. package/dist/migrate/introspection/mssqlIntrospector.d.ts +5 -5
  61. package/dist/migrate/introspection/mssqlIntrospector.js +24 -28
  62. package/dist/migrate/introspection/mysqlIntrospector.d.ts +5 -5
  63. package/dist/migrate/introspection/mysqlIntrospector.js +31 -37
  64. package/dist/migrate/introspection/postgresIntrospector.d.ts +13 -13
  65. package/dist/migrate/introspection/postgresIntrospector.js +35 -39
  66. package/dist/migrate/introspection/registry.js +1 -1
  67. package/dist/migrate/introspection/sqliteIntrospector.d.ts +6 -4
  68. package/dist/migrate/introspection/sqliteIntrospector.js +15 -12
  69. package/dist/migrate/migrationLock.d.ts +38 -0
  70. package/dist/migrate/migrationLock.js +103 -0
  71. package/dist/migrate/migrationTarget.d.ts +6 -2
  72. package/dist/migrate/migrationTarget.js +30 -14
  73. package/dist/migrate/migrator.d.ts +39 -23
  74. package/dist/migrate/migrator.js +110 -74
  75. package/dist/migrate/schemaGenerator.d.ts +3 -1
  76. package/dist/migrate/schemaGenerator.js +14 -5
  77. package/dist/migrate/storage/databaseStorage.d.ts +28 -20
  78. package/dist/migrate/storage/databaseStorage.js +36 -54
  79. package/dist/migrate/storage/index.d.ts +0 -1
  80. package/dist/migrate/storage/index.js +0 -1
  81. package/dist/{mongo → mongodb}/index.d.ts +0 -1
  82. package/dist/{mongo → mongodb}/index.js +0 -1
  83. package/dist/{mongo → mongodb}/mongoCommand.d.ts +17 -1
  84. package/dist/{mongo → mongodb}/mongoCommand.js +15 -2
  85. package/dist/{mongo → mongodb}/mongoDialect.js +8 -12
  86. package/dist/{mongo → mongodb}/mongoIntrospector.js +12 -3
  87. package/dist/{mongo → mongodb}/mongoMigrationStorage.d.ts +5 -5
  88. package/dist/{mongo → mongodb}/mongoMigrationStorage.js +8 -12
  89. package/dist/{mongo → mongodb}/mongoQuerier.d.ts +3 -1
  90. package/dist/{mongo → mongodb}/mongoSchemaGenerator.d.ts +11 -1
  91. package/dist/{mongo → mongodb}/mongoSchemaGenerator.js +56 -11
  92. package/dist/{mongo → mongodb}/mongodbQuerier.d.ts +7 -18
  93. package/dist/{mongo → mongodb}/mongodbQuerier.js +44 -78
  94. package/dist/mssql/mssqlDialect.d.ts +6 -4
  95. package/dist/mssql/mssqlDialect.js +20 -10
  96. package/dist/mssql/mssqlQuerier.d.ts +2 -2
  97. package/dist/mssql/mssqlQuerier.js +3 -3
  98. package/dist/mssql/mssqlQuerierPool.d.ts +1 -1
  99. package/dist/mssql/mssqlQuerierPool.js +2 -2
  100. package/dist/mysql/mysql2Querier.js +1 -5
  101. package/dist/namingStrategy/defaultNamingStrategy.d.ts +0 -1
  102. package/dist/namingStrategy/defaultNamingStrategy.js +0 -3
  103. package/dist/pglite/pgliteDialect.d.ts +3 -0
  104. package/dist/pglite/pgliteDialect.js +3 -1
  105. package/dist/pglite/pgliteQuerier.d.ts +2 -0
  106. package/dist/pglite/pgliteQuerier.js +4 -0
  107. package/dist/postgres/postgresDialect.d.ts +3 -4
  108. package/dist/postgres/postgresDialect.js +5 -6
  109. package/dist/querier/abstractQuerier.d.ts +119 -36
  110. package/dist/querier/abstractQuerier.js +465 -161
  111. package/dist/querier/abstractQuerierPool.d.ts +5 -6
  112. package/dist/querier/abstractQuerierPool.js +20 -6
  113. package/dist/querier/abstractSqlQuerier.d.ts +15 -10
  114. package/dist/querier/abstractSqlQuerier.js +54 -47
  115. package/dist/querier/abstractSqlQuerierPool.d.ts +5 -3
  116. package/dist/querier/abstractSqlQuerierPool.js +7 -4
  117. package/dist/querier/transaction.d.ts +46 -0
  118. package/dist/querier/transaction.js +56 -0
  119. package/dist/schema/canonicalType.js +2 -2
  120. package/dist/schema/schemaASTDiffer.d.ts +11 -3
  121. package/dist/schema/schemaASTDiffer.js +20 -8
  122. package/dist/sqlite/nodeSqliteQuerierPool.d.ts +1 -1
  123. package/dist/sqlite/nodeSqliteQuerierPool.js +1 -1
  124. package/dist/sqlite/sqliteDialect.d.ts +1 -1
  125. package/dist/sqlite/sqliteDialect.js +3 -1
  126. package/dist/sqlite/sqliteQuerier.d.ts +2 -0
  127. package/dist/sqlite/sqliteQuerier.js +4 -0
  128. package/dist/sqlite/sqliteQuerierPool.d.ts +4 -4
  129. package/dist/sqlite/sqliteQuerierPool.js +1 -1
  130. package/dist/type/dialect.d.ts +16 -2
  131. package/dist/type/entity.d.ts +32 -32
  132. package/dist/type/migration.d.ts +10 -27
  133. package/dist/type/namingStrategy.d.ts +0 -4
  134. package/dist/type/querier.d.ts +18 -10
  135. package/dist/type/query.d.ts +7 -22
  136. package/dist/type/queryRaw.d.ts +11 -6
  137. package/dist/type/queryRaw.js +2 -3
  138. package/dist/type/universalQuerier.d.ts +26 -28
  139. package/dist/type/utility.d.ts +7 -2
  140. package/dist/util/dialect.util.d.ts +2 -2
  141. package/dist/util/dialect.util.js +4 -1
  142. package/dist/util/field.util.d.ts +12 -0
  143. package/dist/util/field.util.js +24 -0
  144. package/dist/util/hook.util.d.ts +3 -2
  145. package/dist/util/query.util.d.ts +5 -0
  146. package/dist/util/query.util.js +34 -1
  147. package/dist/util/raw.d.ts +8 -2
  148. package/dist/util/raw.js +22 -0
  149. package/dist/util/relationQuery.util.d.ts +2 -0
  150. package/dist/util/relationQuery.util.js +4 -0
  151. package/dist/util/sql.util.d.ts +0 -5
  152. package/dist/util/sql.util.js +3 -9
  153. package/dist/util/uqlError.d.ts +0 -6
  154. package/dist/util/uqlError.js +0 -5
  155. package/package.json +20 -26
  156. package/skills/uql-orm/SKILL.md +13 -10
  157. package/dist/migrate/storage/jsonStorage.d.ts +0 -14
  158. package/dist/migrate/storage/jsonStorage.js +0 -33
  159. /package/dist/{maria → mariadb}/index.d.ts +0 -0
  160. /package/dist/{maria → mariadb}/index.js +0 -0
  161. /package/dist/{maria → mariadb}/mariaDialect.d.ts +0 -0
  162. /package/dist/{maria → mariadb}/mariaDialect.js +0 -0
  163. /package/dist/{maria → mariadb}/mariadbQuerier.d.ts +0 -0
  164. /package/dist/{maria → mariadb}/mariadbQuerierPool.d.ts +0 -0
  165. /package/dist/{maria → mariadb}/mariadbQuerierPool.js +0 -0
  166. /package/dist/{mongo → mongodb}/mongoDialect.d.ts +0 -0
  167. /package/dist/{mongo → mongodb}/mongoIntrospector.d.ts +0 -0
  168. /package/dist/{mongo → mongodb}/mongoQuerier.js +0 -0
  169. /package/dist/{mongo → mongodb}/mongodbQuerierPool.d.ts +0 -0
  170. /package/dist/{mongo → mongodb}/mongodbQuerierPool.js +0 -0
  171. /package/dist/{mongo → mongodb}/textLanguage.d.ts +0 -0
  172. /package/dist/{mongo → mongodb}/textLanguage.js +0 -0
  173. /package/dist/{mongo → mongodb}/vectorDistance.d.ts +0 -0
  174. /package/dist/{mongo → mongodb}/vectorDistance.js +0 -0
@@ -1,4 +1,9 @@
1
+ import { ownedCheck } from '../util/ddlExpression.util.js';
1
2
  import { UqlUsageError } from '../util/uqlError.js';
3
+ /** A collection's validator as the check it is, named for its JSON, so an edited one is a new name. */
4
+ export function validatorCheck(collection, validator) {
5
+ return ownedCheck(collection, 'validator', JSON.stringify(validator));
6
+ }
2
7
  export function serializeMongoCommand(command) {
3
8
  return JSON.stringify(command);
4
9
  }
@@ -17,7 +22,11 @@ export function runMongoCommand(db, statement) {
17
22
  const command = parseMongoCommand(statement);
18
23
  switch (command.action) {
19
24
  case 'createCollection':
20
- return db.createCollection(command.name);
25
+ return command.validator
26
+ ? db.createCollection(command.name, { validator: command.validator })
27
+ : db.createCollection(command.name);
28
+ case 'collMod':
29
+ return db.command({ collMod: command.name, validator: command.validator });
21
30
  case 'dropCollection':
22
31
  return db.collection(command.name).drop();
23
32
  case 'renameCollection':
@@ -42,7 +51,11 @@ export function mongoCommandSource(statement, db) {
42
51
  const literal = JSON.stringify;
43
52
  switch (command.action) {
44
53
  case 'createCollection':
45
- return `${db}.createCollection(${literal(command.name)})`;
54
+ return command.validator
55
+ ? `${db}.createCollection(${literal(command.name)}, ${literal({ validator: command.validator })})`
56
+ : `${db}.createCollection(${literal(command.name)})`;
57
+ case 'collMod':
58
+ return `${db}.command(${literal({ collMod: command.name, validator: command.validator })})`;
46
59
  case 'dropCollection':
47
60
  return `${db}.collection(${literal(command.name)}).drop()`;
48
61
  case 'renameCollection':
@@ -6,7 +6,7 @@ import { aggregateColumnField, groupPathField, resolveGroupJoins, relationSortTe
6
6
  import { assertSoleId, fieldOf, getMeta, relationOf, soleIdOf } from '../entity/index.js';
7
7
  import { COUNT_RESULT_KEY } from '../type/query.js';
8
8
  import { QueryRaw } from '../type/queryRaw.js';
9
- import { aggregateOf, isSelectList, assertAggregateColumns, assertNonNegativeInteger, columnFamily, countedRelations, entityName, fieldUpdateOf, fillOnFields, filterFieldKeys, findVectorIndex, findVectorSort, getKeys, getRelationRequestSummary, hasKeys, isFieldUpdateOp, isJsonObject, isJsonUpdateOp, isOperatorMap, isOperatorObject, isRecord, isVectorSearch, normalizeScalarFieldSelection, parentJoins, parseGroupMap, parseRelationAtKey, parseRelationSize, parseSortDirection, rankedTextSearch, someKey, targetKeyColumns, textSortOf, vectorDistanceOf, } from '../util/index.js';
9
+ import { aggregateOf, isSelectList, assertAggregateColumns, assertNoUndefined, assertNonNegativeInteger, columnFamily, countedRelations, entityName, fieldUpdateOf, fillOnFields, filterFieldKeys, findVectorIndex, findVectorSort, getKeys, holdsForeignKey, jsonKey, jsonPathKeys, getRelationRequestSummary, hasKeys, isFieldUpdateOp, isJsonObject, isJsonUpdateOp, isOperatorMap, isOperatorObject, isRecord, isVectorSearch, normalizeScalarFieldSelection, parentJoins, parseGroupMap, parseRelationAtKey, parseRelationSize, parseSortDirection, rankedTextSearch, someKey, targetKeyColumns, textSortOf, vectorDistanceOf, } from '../util/index.js';
10
10
  import { UqlUsageError } from '../util/uqlError.js';
11
11
  import { decodeBigIntsExcept } from '../util/wideNumber.js';
12
12
  import { textLanguage } from './textLanguage.js';
@@ -23,7 +23,7 @@ export const mongoDialectFeatures = {
23
23
  vectorSupportsLength: false,
24
24
  vectorBytes: false,
25
25
  supportsTimestamptz: false,
26
- stringSizing: 'bounded-text',
26
+ stringSizing: 'boundedText',
27
27
  supportsUnsigned: false,
28
28
  serverSideCursors: false,
29
29
  correlatedWrites: false,
@@ -78,9 +78,7 @@ function compareCount(count, size) {
78
78
  if (typeof size === 'number') {
79
79
  return { $eq: [count, size] };
80
80
  }
81
- const comparisons = Object.entries(size)
82
- .filter(([, bound]) => bound !== undefined)
83
- .flatMap(([op, bound]) => op === '$between' && Array.isArray(bound)
81
+ const comparisons = Object.entries(size).flatMap(([op, bound]) => op === '$between' && Array.isArray(bound)
84
82
  ? [{ $gte: [count, bound[0]] }, { $lte: [count, bound[1]] }]
85
83
  : [{ [op]: [count, bound] }]);
86
84
  if (!comparisons.length) {
@@ -707,7 +705,7 @@ export class MongoDialect extends AbstractDialect {
707
705
  assertReadable(meta, key);
708
706
  return this.columnOf(meta, key);
709
707
  }
710
- return this.columnOf(meta, key.slice(0, dot)) + key.slice(dot);
708
+ return `${this.columnOf(meta, key.slice(0, dot))}.${jsonPathKeys(key.slice(dot + 1)).join('.')}`;
711
709
  }
712
710
  aggregationPipeline(entity, q, opts) {
713
711
  // Sorted as a field, which goes either way where a `$meta` sort only descends.
@@ -875,7 +873,7 @@ export class MongoDialect extends AbstractDialect {
875
873
  * direction joins the parent's `_id` to the target's FK.
876
874
  */
877
875
  joinKeys(meta, relMeta, relOpts) {
878
- if (relOpts.cardinality === 'm1') {
876
+ if (holdsForeignKey(relOpts)) {
879
877
  // The target's side, not this entity's: a lookup matches one `localField` against one
880
878
  // `foreignField`, so a composite target would join on its first column alone and gather the
881
879
  // rows of every key that agrees on it. The other branch is refused by `columnOf` below, whose
@@ -1012,6 +1010,7 @@ export class MongoDialect extends AbstractDialect {
1012
1010
  set[key] = value;
1013
1011
  continue;
1014
1012
  }
1013
+ [...getKeys(value.$set), ...getKeys(value.$push), ...getKeys(value.$pull), ...(value.$unset ?? [])].forEach(jsonKey);
1015
1014
  for (const [path, v] of Object.entries(value.$set ?? {})) {
1016
1015
  set[`${key}.${path}`] = v;
1017
1016
  }
@@ -1217,9 +1216,7 @@ export class MongoDialect extends AbstractDialect {
1217
1216
  * the fields it reads, so a relation aggregate among them is on the document first.
1218
1217
  */
1219
1218
  whereExpression(meta, where, named) {
1220
- const terms = getKeys(where)
1221
- .filter((key) => where[key] !== undefined)
1222
- .map((key) => {
1219
+ const terms = getKeys(where).map((key) => {
1223
1220
  if (isGroupOp(key)) {
1224
1221
  const { join, negate } = GROUP_OPS[key];
1225
1222
  const clauses = groupClauses(key, where[key]).map((clause) => {
@@ -1278,9 +1275,8 @@ export class MongoDialect extends AbstractDialect {
1278
1275
  }
1279
1276
  buildHavingFilter(having) {
1280
1277
  const filter = {};
1278
+ assertNoUndefined(having, '$having');
1281
1279
  for (const [alias, condition] of Object.entries(having)) {
1282
- if (condition === undefined)
1283
- continue;
1284
1280
  // Classified exactly as the SQL side classifies a `$where`/`$having` value, so the two agree
1285
1281
  // on identical input. Keeping only numbers and objects dropped a string or boolean without a
1286
1282
  // word, handing back every group instead of the filtered ones.
@@ -1,4 +1,5 @@
1
1
  import { createTableNode, SchemaAST } from '../schema/schemaAST.js';
2
+ import { validatorCheck } from './mongoCommand.js';
2
3
  import { withMongoQuerierForMigrations } from './mongoQuerier.js';
3
4
  import { textConfigOf } from './textLanguage.js';
4
5
  /** What a server without Atlas Search answers a search index command with. */
@@ -25,9 +26,12 @@ export class MongoSchemaIntrospector {
25
26
  }
26
27
  async getTableSchema(tableName) {
27
28
  return this.withDb(async (db) => {
28
- if (!(await hasCollection(db, tableName))) {
29
+ const [info] = await db.listCollections({ name: tableName, type: 'collection' }, { nameOnly: false }).toArray();
30
+ if (!info) {
29
31
  return undefined;
30
32
  }
33
+ // Annotated: the driver types a collection's options as `Document`, whose members are `any`.
34
+ const validator = info.options?.['validator'];
31
35
  // Annotated: the driver types a `listIndexes` entry as `any`, and its `indexes()` leaves `name` optional.
32
36
  const collection = db.collection(tableName);
33
37
  const indexes = await collection.listIndexes().toArray();
@@ -35,6 +39,7 @@ export class MongoSchemaIntrospector {
35
39
  return {
36
40
  name: tableName,
37
41
  columns: [],
42
+ ...(validator && { checks: [validatorCheck(tableName, validator)] }),
38
43
  indexes: [
39
44
  ...indexes.map(({ name, key, unique, weights, default_language }) => ({
40
45
  name,
@@ -94,11 +99,15 @@ async function hasCollection(db, name) {
94
99
  const collections = await db.listCollections({ name, type: 'collection' }, { nameOnly: true }).toArray();
95
100
  return collections.length > 0;
96
101
  }
97
- /** Mongo has no columns to read, so a table's are the fields its indexes name, one node per field. */
98
102
  /** `listIndexes` reports keys, uniqueness and text weights; a `partialFilterExpression` is no SQL predicate. */
99
103
  const INDEX_FACETS = new Set(['textIndex']);
100
- function buildTable({ name, indexes = [] }) {
104
+ /**
105
+ * Mongo has no columns to read, so a table's are the fields its indexes name, one node per field. Its
106
+ * validator is its one check.
107
+ */
108
+ function buildTable({ name, indexes = [], checks = [] }) {
101
109
  const table = createTableNode(name, undefined, INDEX_FACETS);
110
+ table.checks.push(...checks);
102
111
  for (const index of indexes) {
103
112
  for (const { column } of index.entries) {
104
113
  if (!table.columns.has(column)) {
@@ -1,16 +1,16 @@
1
- import type { MigrationStorage, QuerierPool } from '../type/index.js';
2
- import { type MongoQuerier } from './mongoQuerier.js';
1
+ import { type MigrationStorage } from '../migrate/storage/databaseStorage.js';
2
+ import type { MongoQuerier } from './mongoQuerier.js';
3
3
  /**
4
4
  * Stores migration state in a MongoDB collection, named as the SQL table would be, which MongoDB creates
5
5
  * on its first insert.
6
6
  */
7
7
  export declare class MongoMigrationStorage implements MigrationStorage {
8
- private readonly pool;
9
8
  private readonly collectionName;
10
- constructor(pool: QuerierPool, options?: {
9
+ constructor(options?: {
11
10
  tableName?: string;
12
11
  });
13
- executed(): Promise<string[]>;
12
+ /** The names recorded, the lock's left out. */
13
+ executed(querier: MongoQuerier): Promise<string[]>;
14
14
  logWithQuerier(querier: MongoQuerier, migrationName: string): Promise<void>;
15
15
  unlogWithQuerier(querier: MongoQuerier, migrationName: string): Promise<void>;
16
16
  private collection;
@@ -1,23 +1,19 @@
1
- import { DEFAULT_MIGRATIONS_TABLE } from '../migrate/storage/databaseStorage.js';
2
- import { withMongoQuerierForMigrations } from './mongoQuerier.js';
1
+ import { DEFAULT_MIGRATIONS_TABLE, LOCK_RECORD } from '../migrate/storage/databaseStorage.js';
3
2
  /**
4
3
  * Stores migration state in a MongoDB collection, named as the SQL table would be, which MongoDB creates
5
4
  * on its first insert.
6
5
  */
7
6
  export class MongoMigrationStorage {
8
- pool;
9
7
  collectionName;
10
- constructor(pool, options = {}) {
11
- this.pool = pool;
8
+ constructor(options = {}) {
12
9
  this.collectionName = options.tableName ?? DEFAULT_MIGRATIONS_TABLE;
13
10
  }
14
- executed() {
15
- return withMongoQuerierForMigrations(this.pool, 'MongoMigrationStorage', async (querier) => {
16
- const documents = await this.collection(querier)
17
- .find({}, { sort: { _id: 1 } })
18
- .toArray();
19
- return documents.map((document) => document._id);
20
- });
11
+ /** The names recorded, the lock's left out. */
12
+ async executed(querier) {
13
+ const documents = await this.collection(querier)
14
+ .find({ _id: { $ne: LOCK_RECORD } }, { sort: { _id: 1 } })
15
+ .toArray();
16
+ return documents.map((document) => document._id);
21
17
  }
22
18
  async logWithQuerier(querier, migrationName) {
23
19
  await this.collection(querier).insertOne({ _id: migrationName, executed_at: new Date() });
@@ -1,4 +1,4 @@
1
- import type { Db } from 'mongodb';
1
+ import type { ClientSession, Db } from 'mongodb';
2
2
  import { type Querier, type QuerierPool } from '../type/index.js';
3
3
  /**
4
4
  * Extended querier interface for MongoDB execution.
@@ -8,6 +8,8 @@ export interface MongoQuerier extends Querier {
8
8
  * The MongoDB database instance.
9
9
  */
10
10
  readonly db: Db;
11
+ /** The open transaction's session, for a raw call through `db` to run inside it: `{ session: querier.session }`. */
12
+ readonly session: ClientSession | undefined;
11
13
  }
12
14
  /**
13
15
  * Type guard for a querier over a MongoDB database. A handle alone does not tell: the SQLite, D1 and
@@ -23,7 +23,17 @@ export declare class MongoSchemaGenerator extends MongoDialect implements Schema
23
23
  * what that holds or what a migration carries as JSON.
24
24
  */
25
25
  private indexFilter;
26
- /** A collection's indexes: each dropped, then each created, an alter as both. */
26
+ /**
27
+ * The validator enforcing an entity's checks and then its enums, none where it declares neither. An enum is
28
+ * `$in` its values or `null`, so a missing or null value passes, as SQL's `CHECK` passes NULL.
29
+ */
30
+ private validatorOf;
31
+ /** A check's filter, refused where it is SQL or holds what a migration cannot carry as JSON. */
32
+ private checkFilter;
33
+ /**
34
+ * A collection's validator, which a diff holds as its one check, then its indexes: each dropped, then each
35
+ * created, an alter as both. A changed validator is a `collMod` to the new one, a removed one to `{}`.
36
+ */
27
37
  generateAlterTable(diff: SchemaDiff): string[];
28
38
  /** An index as MongoDB's key spec (`-1` descending, `'text'` full-text), refusing the SQL-only options. */
29
39
  generateCreateIndex(tableName: string, index: IndexSchema): string;
@@ -9,8 +9,9 @@ import { QueryRaw, } from '../type/index.js';
9
9
  import { indexDistance, unsupportedVectorMetric } from '../type/vector.js';
10
10
  import { declaredIndexes, declaredIndexName, renderIndexColumn } from '../util/ddlExpression.util.js';
11
11
  import { fulltextConfig, fulltextWeights } from '../util/dialect.util.js';
12
+ import { definedEntries, hasKeys } from '../util/object.util.js';
12
13
  import { UqlUsageError } from '../util/uqlError.js';
13
- import { serializeMongoCommand } from './mongoCommand.js';
14
+ import { serializeMongoCommand, validatorCheck } from './mongoCommand.js';
14
15
  import { MongoDialect } from './mongoDialect.js';
15
16
  import { textLanguage } from './textLanguage.js';
16
17
  /** The index types a key spec can say, a plain key or `'text'`, and Atlas's vector search index. */
@@ -39,16 +40,16 @@ export class MongoSchemaGenerator extends MongoDialect {
39
40
  .filter((meta) => !wanted || wanted.has(this.resolveTableName(meta)))
40
41
  .flatMap((meta) => {
41
42
  const name = this.resolveTableName(meta);
42
- return this.createCollection(name, this.indexesOf(meta, name));
43
+ return this.createCollection(name, this.indexesOf(meta, name), this.validatorOf(meta, name));
43
44
  });
44
45
  }
45
46
  generateDropSchema(entities) {
46
47
  return entities.map((entity) => serializeMongoCommand({ action: 'dropCollection', name: this.resolveTableName(getMeta(entity)) }));
47
48
  }
48
49
  /** A collection, and one `createIndex` command for each of its indexes, as SQL's `[CREATE TABLE, ...CREATE INDEX]`. */
49
- createCollection(name, indexes) {
50
+ createCollection(name, indexes, validator) {
50
51
  return [
51
- serializeMongoCommand({ action: 'createCollection', name }),
52
+ serializeMongoCommand({ action: 'createCollection', name, validator }),
52
53
  ...indexes.map((index) => this.generateCreateIndex(name, index)),
53
54
  ];
54
55
  }
@@ -93,9 +94,45 @@ export class MongoSchemaGenerator extends MongoDialect {
93
94
  }
94
95
  return JSON.stringify(filter);
95
96
  }
96
- /** A collection's indexes: each dropped, then each created, an alter as both. */
97
+ /**
98
+ * The validator enforcing an entity's checks and then its enums, none where it declares neither. An enum is
99
+ * `$in` its values or `null`, so a missing or null value passes, as SQL's `CHECK` passes NULL.
100
+ */
101
+ validatorOf(meta, collectionName) {
102
+ const clauses = [
103
+ ...(meta.checks ?? []).map(({ where }) => this.checkFilter(where, meta.entity, collectionName)),
104
+ ...definedEntries(meta.fields).flatMap(([key, field]) => field.enum ? [{ [this.columnOf(meta, key)]: { $in: [...field.enum, null] } }] : []),
105
+ ].filter(hasKeys);
106
+ return clauses.length > 1 ? { $and: clauses } : clauses[0];
107
+ }
108
+ /** A check's filter, refused where it is SQL or holds what a migration cannot carry as JSON. */
109
+ checkFilter(where, entity, collectionName) {
110
+ if (where instanceof QueryRaw) {
111
+ throw new UqlUsageError(`mongodb does not support checks from a SQL predicate (collection "${collectionName}")`);
112
+ }
113
+ const filter = this.renderFilter(entity, where);
114
+ const refused = refusedFilterValue(filter, true);
115
+ if (refused) {
116
+ throw new UqlUsageError(`mongodb does not support ${refused} in a check (collection "${collectionName}")`);
117
+ }
118
+ return filter;
119
+ }
120
+ /**
121
+ * A collection's validator, which a diff holds as its one check, then its indexes: each dropped, then each
122
+ * created, an alter as both. A changed validator is a `collMod` to the new one, a removed one to `{}`.
123
+ */
97
124
  generateAlterTable(diff) {
125
+ const [validator] = sides(diff.checks, 'to');
98
126
  return [
127
+ ...(diff.checks?.length
128
+ ? [
129
+ serializeMongoCommand({
130
+ action: 'collMod',
131
+ name: diff.tableName,
132
+ validator: validator ? JSON.parse(validator.expression) : {},
133
+ }),
134
+ ]
135
+ : []),
99
136
  ...sides(diff.indexes, 'from').map((index) => dropIndex(diff.tableName, index.name, index.type)),
100
137
  ...sides(diff.indexes, 'to').map((index) => this.generateCreateIndex(diff.tableName, index)),
101
138
  ];
@@ -182,9 +219,13 @@ export class MongoSchemaGenerator extends MongoDialect {
182
219
  if (!currentTable) {
183
220
  return { tableName: collectionName, type: 'create' };
184
221
  }
222
+ const declared = this.validatorOf(meta, collectionName);
223
+ const desired = declared && validatorCheck(collectionName, declared);
224
+ const [current] = currentTable.checks;
225
+ const checks = desired?.name === current?.name ? [] : [{ from: current, to: desired }];
185
226
  const { changes } = indexChanges(collectionName, this.indexesOf(meta, collectionName), currentTable.indexes, currentTable.indexFacets);
186
227
  const indexes = changes.map(({ from, to }) => ({ from: from && indexNodeToSchema(from), to }));
187
- return indexes.length ? { tableName: collectionName, type: 'alter', indexes } : undefined;
228
+ return indexes.length || checks.length ? { tableName: collectionName, type: 'alter', checks, indexes } : undefined;
188
229
  }
189
230
  }
190
231
  /** A collection has no SQL to render a check, a computed column or an index expression into. */
@@ -195,19 +236,23 @@ function refuseSql() {
195
236
  function dropIndex(collection, name, type) {
196
237
  return serializeMongoCommand({ action: type === 'vectorSearch' ? 'dropSearchIndex' : 'dropIndex', collection, name });
197
238
  }
198
- /** The first value `partialFilterExpression` refuses, `null`, or a migration cannot carry as JSON, such as a `Date`. */
199
- function refusedFilterValue(value) {
239
+ /**
240
+ * The first value a migration cannot carry as JSON, such as a `Date`, or `null` unless `holdsNull`, which a
241
+ * validator does and `partialFilterExpression` does not.
242
+ */
243
+ function refusedFilterValue(value, holdsNull = false) {
244
+ const refusedIn = (values) => values.map((it) => refusedFilterValue(it, holdsNull)).find(Boolean);
200
245
  if (value === null) {
201
- return 'null';
246
+ return holdsNull ? undefined : 'null';
202
247
  }
203
248
  if (Array.isArray(value)) {
204
- return value.map(refusedFilterValue).find(Boolean);
249
+ return refusedIn(value);
205
250
  }
206
251
  if (typeof value !== 'object') {
207
252
  return typeof value === 'bigint' ? 'a bigint' : undefined;
208
253
  }
209
254
  if (Object.getPrototypeOf(value) === Object.prototype) {
210
- return Object.values(value).map(refusedFilterValue).find(Boolean);
255
+ return refusedIn(Object.values(value));
211
256
  }
212
257
  const typeName = value.constructor.name;
213
258
  return `${/^[aeiou]/i.test(typeName) ? 'an' : 'a'} ${typeName}`;
@@ -1,13 +1,15 @@
1
- import type { Document, MongoClient } from 'mongodb';
1
+ import type { ClientSession, Document, MongoClient } from 'mongodb';
2
2
  import { AbstractQuerier } from '../querier/index.js';
3
3
  import type { EntityData, ExtraOptions, PrimaryKey, Query, QueryAggMap, QueryAggregate, QueryAggregateResult, QueryConflictPaths, QueryPage, QueryGroupMap, QueryOptions, QuerySearch, TransactionOptions, Type, UpdatePayload } from '../type/index.js';
4
4
  import type { MongoDialect } from './mongoDialect.js';
5
5
  import type { MongoQuerier } from './mongoQuerier.js';
6
6
  export declare class MongodbQuerier extends AbstractQuerier implements MongoQuerier {
7
+ #private;
7
8
  readonly dialect: MongoDialect;
8
9
  readonly conn: MongoClient;
9
10
  readonly extra?: ExtraOptions | undefined;
10
- private session?;
11
+ /** The open transaction's session, for a raw call through `db` to run inside it: `{ session: querier.session }`. */
12
+ get session(): ClientSession | undefined;
11
13
  constructor(dialect: MongoDialect, conn: MongoClient, extra?: ExtraOptions | undefined);
12
14
  private execute;
13
15
  protected internalFindMany<E extends Document>(entity: Type<E>, q: Query<E>, opts?: QueryOptions): Promise<E[]>;
@@ -48,8 +50,8 @@ export declare class MongodbQuerier extends AbstractQuerier implements MongoQuer
48
50
  internalInsertMany<E extends Document>(entity: Type<E>, rows: EntityData<E>[]): Promise<void>;
49
51
  internalUpdateMany<E extends Document>(entity: Type<E>, qm: QuerySearch<E>, payload: UpdatePayload<E>, opts?: QueryOptions): Promise<number>;
50
52
  /**
51
- * `_id` is immutable, so a key the payload names can only be written on the insert branch of an
52
- * upsert; in `$set` it would refuse every matched document. Everything else updates either way.
53
+ * What an upsert writes, as SQL's does: a found document takes the payload and the `onUpdate` fills, and only
54
+ * an inserted one the `onInsert` fills and the key, which is immutable, so in `$set` it would refuse every match.
53
55
  */
54
56
  private upsertUpdate;
55
57
  private buildConflictFilter;
@@ -59,20 +61,7 @@ export declare class MongodbQuerier extends AbstractQuerier implements MongoQuer
59
61
  * a concurrent one included, then takes `update`.
60
62
  */
61
63
  private upsertWithUpdate;
62
- /** The id and whether it was created, read off the document a `findOneAndUpdate` upsert wrote. */
63
- private upserted;
64
- protected internalUpsertOne<E extends Document>(entity: Type<E>, conflictPaths: QueryConflictPaths<E>, payload: E, update?: UpdatePayload<E>): Promise<{
65
- ids: (PrimaryKey | undefined)[];
66
- changes: number;
67
- created: boolean;
68
- }>;
69
- protected internalUpsertMany<E extends Document>(entity: Type<E>, conflictPaths: QueryConflictPaths<E>, payload: E[], update?: UpdatePayload<E>): Promise<{
70
- ids: (PrimaryKey | undefined)[];
71
- changes: number;
72
- } | {
73
- changes: number;
74
- ids?: undefined;
75
- }>;
64
+ protected internalUpsertMany<E extends Document>(entity: Type<E>, conflictPaths: QueryConflictPaths<E>, payload: E[], update?: UpdatePayload<E>): Promise<(PrimaryKey | undefined)[]>;
76
65
  protected internalDeleteMany<E extends Document>(entity: Type<E>, qm: QuerySearch<E>, opts?: QueryOptions): Promise<number>;
77
66
  get hasOpenTransaction(): boolean;
78
67
  /** Every read and write goes through here, which makes it where a released querier is caught. */
@@ -15,7 +15,7 @@ function asksForNoRows(q) {
15
15
  /**
16
16
  * MongoDB runs no trigger within a write (Atlas Database Triggers fire after the commit), so a write to
17
17
  * an entity declaring one - a stamp included - would skip it silently. Refused instead, as a query naming
18
- * SQL is. The why, in `architecture/triggers.md`.
18
+ * SQL is.
19
19
  */
20
20
  function refuseTriggers(entity) {
21
21
  if (hasTriggers(getMeta(entity))) {
@@ -27,7 +27,11 @@ export class MongodbQuerier extends AbstractQuerier {
27
27
  dialect;
28
28
  conn;
29
29
  extra;
30
- session;
30
+ #session;
31
+ /** The open transaction's session, for a raw call through `db` to run inside it: `{ session: querier.session }`. */
32
+ get session() {
33
+ return this.#session;
34
+ }
31
35
  constructor(dialect, conn, extra) {
32
36
  super(extra);
33
37
  this.dialect = dialect;
@@ -36,7 +40,7 @@ export class MongodbQuerier extends AbstractQuerier {
36
40
  }
37
41
  async execute(task) {
38
42
  return this.serialize(async () => {
39
- return task(this.session);
43
+ return task(this.#session);
40
44
  });
41
45
  }
42
46
  async internalFindMany(entity, q, opts) {
@@ -68,7 +72,7 @@ export class MongodbQuerier extends AbstractQuerier {
68
72
  ? this.buildVectorPipeline(entity, q, vectorSort, opts)
69
73
  : this.readsThroughPipeline(entity, q) && this.dialect.aggregationPipeline(entity, q, opts);
70
74
  return pipeline
71
- ? this.collection(entity).aggregate(pipeline, { session: this.session })
75
+ ? this.collection(entity).aggregate(pipeline, { session: this.#session })
72
76
  : this.buildFindCursor(entity, q, opts);
73
77
  }
74
78
  /**
@@ -91,7 +95,7 @@ export class MongodbQuerier extends AbstractQuerier {
91
95
  }
92
96
  /** Build a MongoDB FindCursor with filter, projection, sort, skip, and limit from the query. */
93
97
  buildFindCursor(entity, q, opts) {
94
- const cursor = this.collection(entity).find({}, { session: this.session });
98
+ const cursor = this.collection(entity).find({}, { session: this.#session });
95
99
  const filter = this.dialect.where(entity, q.$where, opts);
96
100
  if (hasKeys(filter)) {
97
101
  cursor.filter(filter);
@@ -201,17 +205,19 @@ export class MongodbQuerier extends AbstractQuerier {
201
205
  });
202
206
  }
203
207
  /**
204
- * `_id` is immutable, so a key the payload names can only be written on the insert branch of an
205
- * upsert; in `$set` it would refuse every matched document. Everything else updates either way.
208
+ * What an upsert writes, as SQL's does: a found document takes the payload and the `onUpdate` fills, and only
209
+ * an inserted one the `onInsert` fills and the key, which is immutable, so in `$set` it would refuse every match.
206
210
  */
207
- upsertUpdate(persistable) {
208
- const { _id, ...rest } = persistable;
211
+ upsertUpdate(meta, payload) {
212
+ const updated = this.dialect.getPersistable(meta, clone(payload), 'onUpdate');
213
+ const inserted = this.dialect.getPersistable(meta, clone(payload), 'onInsert');
214
+ const insertOnly = Object.fromEntries(Object.entries(inserted).filter(([column]) => !(column in updated)));
209
215
  const update = {};
210
- if (hasKeys(rest)) {
211
- update['$set'] = rest;
216
+ if (hasKeys(updated)) {
217
+ update['$set'] = updated;
212
218
  }
213
- if (_id !== undefined) {
214
- update['$setOnInsert'] = { _id };
219
+ if (hasKeys(insertOnly)) {
220
+ update['$setOnInsert'] = insertOnly;
215
221
  }
216
222
  return update;
217
223
  }
@@ -226,77 +232,37 @@ export class MongodbQuerier extends AbstractQuerier {
226
232
  async upsertWithUpdate(entity, conflictPaths, row, update) {
227
233
  const persistable = this.dialect.getPersistable(getMeta(entity), clone(row), 'onInsert');
228
234
  const res = await this.execute((session) => this.collection(entity).findOneAndUpdate(this.buildConflictFilter(entity, conflictPaths, row), { $setOnInsert: persistable }, { upsert: true, returnDocument: 'after', includeResultMetadata: true, session }));
229
- const { id, created } = this.upserted(res);
230
- // An empty `update` leaves a found document as it is, as `DO NOTHING` does.
231
- const updates = !created && hasKeys(update);
232
- if (updates) {
235
+ // `updatedExisting` is false where the document was inserted; an empty `update` leaves a found one
236
+ // as it is, as `DO NOTHING` does.
237
+ if (res.lastErrorObject?.['updatedExisting'] && hasKeys(update)) {
233
238
  await this.internalUpdateMany(entity, { $where: whereEach(getKeys(conflictPaths), (key) => row[key]) }, update);
234
239
  }
235
- return { id, created, changes: created || updates ? 1 : 0 };
236
- }
237
- /** The id and whether it was created, read off the document a `findOneAndUpdate` upsert wrote. */
238
- upserted(res) {
239
- const id = this.dialect.fromWireId(res.value?._id);
240
- // `updatedExisting` is false when a new document was inserted (upserted).
241
- return { id, created: res.lastErrorObject?.['updatedExisting'] === false };
242
- }
243
- async internalUpsertOne(entity, conflictPaths, payload, update) {
244
- refuseTriggers(entity);
245
- if (update) {
246
- const { id, created, changes } = await this.upsertWithUpdate(entity, conflictPaths, payload, update);
247
- return { ids: [id], changes, created };
248
- }
249
- return this.timed('upsertOne', undefined, async () => {
250
- payload = clone(payload);
251
- const meta = getMeta(entity);
252
- const persistable = this.dialect.getPersistable(meta, payload, 'onInsert');
253
- const filter = this.buildConflictFilter(entity, conflictPaths, payload);
254
- const update = this.upsertUpdate(persistable);
255
- const res = await this.execute((session) => this.collection(entity).findOneAndUpdate(filter, update, {
256
- upsert: true,
257
- returnDocument: 'after',
258
- includeResultMetadata: true,
259
- session,
260
- }));
261
- // Read off the document as written, which carries its `_id` on either branch.
262
- const { id, created } = this.upserted(res);
263
- return { ids: [id], changes: 1, created };
264
- });
240
+ return this.dialect.fromWireId(res.value?._id);
265
241
  }
266
242
  async internalUpsertMany(entity, conflictPaths, payload, update) {
267
243
  refuseTriggers(entity);
268
244
  if (update) {
269
245
  const ids = [];
270
- let changes = 0;
271
246
  for (const row of payload) {
272
- const written = await this.upsertWithUpdate(entity, conflictPaths, row, update);
273
- ids.push(written.id);
274
- changes += written.changes;
247
+ ids.push(await this.upsertWithUpdate(entity, conflictPaths, row, update));
275
248
  }
276
- return { ids, changes };
249
+ return ids;
277
250
  }
278
251
  return this.timed('upsertMany', undefined, async () => {
279
- if (!payload?.length) {
280
- return { changes: 0 };
252
+ if (!payload.length) {
253
+ return [];
281
254
  }
282
255
  const meta = getMeta(entity);
283
256
  // Asked before `getPersistable` fills an `onInsert` key into rows it may only update.
284
257
  const unnamed = payload.map((row) => !namesKey(meta, row));
285
- payload = clone(payload);
286
- const operations = payload.map((item) => {
287
- const persistable = this.dialect.getPersistable(meta, item, 'onInsert');
288
- const filter = this.buildConflictFilter(entity, conflictPaths, item);
289
- const update = this.upsertUpdate(persistable);
290
- return {
291
- updateOne: {
292
- filter,
293
- update,
294
- upsert: true,
295
- },
296
- };
297
- });
258
+ const operations = payload.map((item) => ({
259
+ updateOne: {
260
+ filter: this.buildConflictFilter(entity, conflictPaths, item),
261
+ update: this.upsertUpdate(meta, item),
262
+ upsert: true,
263
+ },
264
+ }));
298
265
  const res = await this.execute((session) => this.collection(entity).bulkWrite(operations, { session }));
299
- const changes = res.upsertedCount + res.modifiedCount;
300
266
  // `upsertedIds` names only the documents inserted, keyed by operation index, so each lands on
301
267
  // its own row; an updated document's `_id` is read back by the conflict fields instead.
302
268
  const reported = payload.map((_, index) => {
@@ -305,7 +271,7 @@ export class MongodbQuerier extends AbstractQuerier {
305
271
  });
306
272
  const unplaced = reported.some((id, index) => id === undefined && unnamed[index]);
307
273
  const found = unplaced ? await this.idsByConflict(entity, conflictPaths, payload) : [];
308
- return { changes, ids: reported.map((id, index) => id ?? found[index]) };
274
+ return reported.map((id, index) => id ?? found[index]);
309
275
  });
310
276
  }
311
277
  async internalDeleteMany(entity, qm, opts = {}) {
@@ -332,7 +298,7 @@ export class MongodbQuerier extends AbstractQuerier {
332
298
  });
333
299
  }
334
300
  get hasOpenTransaction() {
335
- return !!this.session?.inTransaction();
301
+ return !!this.#session?.inTransaction();
336
302
  }
337
303
  /** Every read and write goes through here, which makes it where a released querier is caught. */
338
304
  collection(entity) {
@@ -346,24 +312,24 @@ export class MongodbQuerier extends AbstractQuerier {
346
312
  return this.conn.db();
347
313
  }
348
314
  async openTransaction(_opts) {
349
- this.logger.logInfo('beginTransaction');
350
- await this.session?.endSession();
351
- this.session = this.conn.startSession();
352
- this.session.startTransaction();
315
+ this.logger.logInfo('startTransaction');
316
+ await this.#session?.endSession();
317
+ this.#session = this.conn.startSession();
318
+ this.#session.startTransaction();
353
319
  }
354
320
  /**
355
321
  * The driver owns the transaction state here and settles it in its own `finally`, so a failed commit
356
322
  * or abort still leaves `inTransaction()` false and the querier releasable.
357
323
  */
358
324
  async endTransaction(commit) {
359
- this.logger.logInfo(commit ? 'commitTransaction' : 'rollbackTransaction');
360
- await (commit ? this.session?.commitTransaction() : this.session?.abortTransaction());
325
+ this.logger.logInfo(commit ? 'commitTransaction' : 'abortTransaction');
326
+ await (commit ? this.#session?.commitTransaction() : this.#session?.abortTransaction());
361
327
  }
362
328
  async internalRelease() {
363
- const session = this.session;
329
+ const session = this.#session;
364
330
  // Cleared first, so a failing `endSession` cannot leave the querier holding a session it already
365
331
  // tried to end.
366
- this.session = undefined;
332
+ this.#session = undefined;
367
333
  await session?.endSession();
368
334
  }
369
335
  }