uql-orm 0.98.0 → 0.100.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 (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 +9 -0
  45. package/dist/migrate/cli-config.js +14 -4
  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 +47 -37
  74. package/dist/migrate/migrator.js +154 -131
  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,69 +1,51 @@
1
- import { currentTimestamp } from '../../util/raw.js';
2
- import { withSqlQuerierForMigrations } from '../acquireQuerierForMigrations.js';
3
- import { TableBuilder } from '../builder/tableBuilder.js';
1
+ import { currentTimestamp, raw } from '../../util/raw.js';
2
+ import { TableDefinitionBuilder } from '../builder/tableBuilder.js';
4
3
  import { SqlSchemaGenerator } from '../schemaGenerator.js';
5
4
  /** Where executed migrations are recorded when the config does not name a table. */
6
5
  export const DEFAULT_MIGRATIONS_TABLE = 'uql_migrations';
7
6
  /**
8
- * Stores migration state in a database table.
9
- * Uses the querier's dialect for escaping and placeholders.
7
+ * The journal record a run holds the lock by where the engine has no named lock (`migrationLock.ts`). No
8
+ * file name holds a slash, so no migration is named so.
10
9
  */
10
+ export const LOCK_RECORD = 'uql/lock';
11
+ /**
12
+ * Creates the journal `tableName` where it is missing, rendered by the schema generator rather than written
13
+ * out: SQL Server has no `CREATE TABLE IF NOT EXISTS`, and its `TIMESTAMP` is a row version that takes no
14
+ * default. Run under the migration lock, so two runs never race to create it.
15
+ */
16
+ export async function createMigrationsTable(querier, tableName) {
17
+ const table = new TableDefinitionBuilder(tableName);
18
+ table.string('name', { length: 255, primaryKey: true });
19
+ table.timestamptz('executed_at', { defaultValue: currentTimestamp });
20
+ const generator = new SqlSchemaGenerator(querier.dialect);
21
+ for (const sql of generator.generateCreateTableFromDefinition(table.build(), { ifNotExists: true })) {
22
+ await querier.run(raw.text(sql));
23
+ }
24
+ }
25
+ /** Records the migrations run in a table, on the querier each call is handed, which the lock created. */
11
26
  export class DatabaseMigrationStorage {
12
- pool;
13
27
  tableName;
14
- storageInitialized = false;
15
- constructor(pool, options = {}) {
16
- this.pool = pool;
28
+ constructor(options = {}) {
17
29
  this.tableName = options.tableName ?? DEFAULT_MIGRATIONS_TABLE;
18
30
  }
19
- /** Creates the table on first use, once. */
20
- async ensureStorage() {
21
- if (this.storageInitialized) {
22
- return;
23
- }
24
- await withSqlQuerierForMigrations(this.pool, 'DatabaseMigrationStorage', async (querier) => {
25
- await this.createTableIfNotExists(querier);
26
- this.storageInitialized = true;
27
- });
31
+ /** The names recorded, the lock's left out. */
32
+ async executed(querier) {
33
+ const { table, name } = journalIds(querier, this.tableName);
34
+ const rows = await querier.all `SELECT ${name} FROM ${table} WHERE ${name} <> ${LOCK_RECORD} ORDER BY ${name} ASC`;
35
+ return rows.map((row) => row.name);
28
36
  }
29
- /**
30
- * Rendered by the schema generator rather than written out, so each engine gets its own spelling: SQL
31
- * Server has no `CREATE TABLE IF NOT EXISTS`, and its `TIMESTAMP` is a row version that takes no default.
32
- */
33
- async createTableIfNotExists(querier) {
34
- const table = new TableBuilder(this.tableName);
35
- table.string('name', { length: 255, primaryKey: true });
36
- table.timestamptz('executed_at', { defaultValue: currentTimestamp });
37
- const generator = new SqlSchemaGenerator(querier.dialect);
38
- for (const sql of generator.generateCreateTableFromDefinition(table.build(), { ifNotExists: true })) {
39
- await querier.run(sql);
40
- }
41
- }
42
- async executed() {
43
- await this.ensureStorage();
44
- return withSqlQuerierForMigrations(this.pool, 'DatabaseMigrationStorage', async (querier) => {
45
- const { dialect } = querier;
46
- const sql = /*sql*/ `SELECT ${dialect.escapeId('name')} FROM ${dialect.escapeId(this.tableName)} ORDER BY ${dialect.escapeId('name')} ASC`;
47
- const rows = await querier.all(sql);
48
- return rows.map((row) => row.name);
49
- });
50
- }
51
- /**
52
- * Log a migration as executed - uses provided querier (within transaction)
53
- */
37
+ /** Records `migrationName` as run, on the querier that ran it, inside its transaction where there is one. */
54
38
  async logWithQuerier(querier, migrationName) {
55
- await this.ensureStorage();
56
- const { dialect } = querier;
57
- const sql = /*sql*/ `INSERT INTO ${dialect.escapeId(this.tableName)} (${dialect.escapeId('name')}) VALUES (${dialect.placeholder(1)})`;
58
- await querier.run(sql, [migrationName]);
39
+ const { table, name } = journalIds(querier, this.tableName);
40
+ await querier.run `INSERT INTO ${table} (${name}) VALUES (${migrationName})`;
59
41
  }
60
- /**
61
- * Unlog a migration - uses provided querier (within transaction)
62
- */
42
+ /** Removes the record of `migrationName`, on the querier that reverted it. */
63
43
  async unlogWithQuerier(querier, migrationName) {
64
- await this.ensureStorage();
65
- const { dialect } = querier;
66
- const sql = /*sql*/ `DELETE FROM ${dialect.escapeId(this.tableName)} WHERE ${dialect.escapeId('name')} = ${dialect.placeholder(1)}`;
67
- await querier.run(sql, [migrationName]);
44
+ const { table, name } = journalIds(querier, this.tableName);
45
+ await querier.run `DELETE FROM ${table} WHERE ${name} = ${migrationName}`;
68
46
  }
69
47
  }
48
+ /** The journal `tableName` and its one column, escaped as `querier`'s engine spells them. */
49
+ export function journalIds({ dialect }, tableName) {
50
+ return { table: raw.text(dialect.escapeId(tableName)), name: raw.text(dialect.escapeId('name')) };
51
+ }
@@ -1,2 +1 @@
1
1
  export * from './databaseStorage.js';
2
- export * from './jsonStorage.js';
@@ -1,2 +1 @@
1
1
  export * from './databaseStorage.js';
2
- export * from './jsonStorage.js';
@@ -2,6 +2,5 @@ export * from './mongoDialect.js';
2
2
  export * from './mongodbQuerier.js';
3
3
  export * from './mongodbQuerierPool.js';
4
4
  export * from './mongoIntrospector.js';
5
- export * from './mongoMigrationStorage.js';
6
5
  export { isMongoQuerier, type MongoQuerier } from './mongoQuerier.js';
7
6
  export * from './mongoSchemaGenerator.js';
@@ -2,6 +2,5 @@ export * from './mongoDialect.js';
2
2
  export * from './mongodbQuerier.js';
3
3
  export * from './mongodbQuerierPool.js';
4
4
  export * from './mongoIntrospector.js';
5
- export * from './mongoMigrationStorage.js';
6
5
  export { isMongoQuerier } from './mongoQuerier.js';
7
6
  export * from './mongoSchemaGenerator.js';
@@ -1,3 +1,6 @@
1
+ import type { CheckSchema } from '../schema/types.js';
2
+ /** A collection validator in query-filter syntax: every document written has to match it, `{}` none. */
3
+ export type MongoValidator = Readonly<Record<string, unknown>>;
1
4
  /** The direction, or `'text'`, of one field in a MongoDB index key spec. */
2
5
  export type MongoIndexKey = Record<string, 1 | -1 | 'text'>;
3
6
  /** What a `createIndex` command hands the driver beside its key spec. */
@@ -32,6 +35,11 @@ export type MongoSearchIndex = {
32
35
  export type MongoCommand = {
33
36
  readonly action: 'createCollection';
34
37
  readonly name: string;
38
+ readonly validator?: MongoValidator;
39
+ } | {
40
+ readonly action: 'collMod';
41
+ readonly name: string;
42
+ readonly validator: MongoValidator;
35
43
  } | {
36
44
  readonly action: 'dropCollection';
37
45
  readonly name: string;
@@ -58,6 +66,8 @@ export type MongoCommand = {
58
66
  readonly collection: string;
59
67
  readonly name: string;
60
68
  };
69
+ /** A collection's validator as the check it is, named for its JSON, so an edited one is a new name. */
70
+ export declare function validatorCheck(collection: string, validator: MongoValidator): CheckSchema;
61
71
  export declare function serializeMongoCommand(command: MongoCommand): string;
62
72
  /**
63
73
  * What executing these commands needs of a database handle. Declared structurally rather than as the
@@ -65,7 +75,13 @@ export declare function serializeMongoCommand(command: MongoCommand): string;
65
75
  * satisfy it with a plain object.
66
76
  */
67
77
  export type MongoCommandTarget = {
68
- createCollection(name: string): Promise<unknown>;
78
+ createCollection(name: string, options?: {
79
+ readonly validator: MongoValidator;
80
+ }): Promise<unknown>;
81
+ command(command: {
82
+ readonly collMod: string;
83
+ readonly validator: MongoValidator;
84
+ }): Promise<unknown>;
69
85
  renameCollection(from: string, to: string): Promise<unknown>;
70
86
  collection(name: string): {
71
87
  drop(): Promise<unknown>;
@@ -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. */