uql-orm 0.63.0 → 0.64.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/migrate/builder/expressions.js +1 -15
- package/dist/migrate/builder/migrationBuilder.d.ts +6 -28
- package/dist/migrate/builder/migrationBuilder.js +9 -83
- package/dist/migrate/builder/types.d.ts +14 -24
- package/dist/migrate/cli.d.ts +1 -6
- package/dist/migrate/cli.js +3 -10
- package/dist/migrate/codegen/migrationFile.d.ts +9 -1
- package/dist/migrate/codegen/migrationFile.js +2 -1
- package/dist/migrate/ddl/indexDdl.d.ts +4 -2
- package/dist/migrate/ddl/indexDdl.js +16 -15
- package/dist/migrate/generator/mongoCommand.d.ts +9 -9
- package/dist/migrate/generator/mongoCommand.js +1 -1
- package/dist/migrate/generator/mongoSchemaGenerator.d.ts +21 -26
- package/dist/migrate/generator/mongoSchemaGenerator.js +103 -80
- package/dist/migrate/index.d.ts +1 -1
- package/dist/migrate/index.js +1 -1
- package/dist/migrate/indexPredicate.d.ts +8 -0
- package/dist/migrate/indexPredicate.js +52 -0
- package/dist/migrate/migrationTarget.d.ts +23 -0
- package/dist/migrate/migrationTarget.js +48 -0
- package/dist/migrate/migrator.d.ts +17 -45
- package/dist/migrate/migrator.js +80 -179
- package/dist/migrate/schemaGenerator.d.ts +12 -13
- package/dist/migrate/schemaGenerator.js +43 -9
- package/dist/mongo/mongoDialect.d.ts +6 -1
- package/dist/schema/schemaASTBuilder.d.ts +2 -0
- package/dist/schema/schemaASTBuilder.js +9 -22
- package/dist/type/migration.d.ts +11 -40
- package/dist/util/ddlExpression.util.d.ts +3 -1
- package/dist/util/ddlExpression.util.js +14 -0
- package/dist/util/sqlLiteral.js +5 -3
- package/package.json +1 -1
- package/dist/migrate/schemaGeneratorAsync.d.ts +0 -7
- package/dist/migrate/schemaGeneratorAsync.js +0 -12
|
@@ -99,21 +99,7 @@ function defaultLiteral(value, dialect) {
|
|
|
99
99
|
if (typeof value === 'boolean') {
|
|
100
100
|
return dialect.booleanLiteral === 'native' ? (value ? 'TRUE' : 'FALSE') : value ? '1' : '0';
|
|
101
101
|
}
|
|
102
|
-
|
|
103
|
-
return dialect.escape(ddlTimestamp(value));
|
|
104
|
-
}
|
|
105
|
-
if (typeof value === 'object') {
|
|
106
|
-
return dialect.escape(JSON.stringify(value));
|
|
107
|
-
}
|
|
108
|
-
return dialect.escape(value);
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* `YYYY-MM-DD HH:mm:ss.SSS` in UTC. Not `toISOString`, whose `T` and `Z` MySQL rejects outright
|
|
112
|
-
* ("Invalid default value"), and not `escape`'s local-time form, which would make the DDL depend on
|
|
113
|
-
* the machine that generated it.
|
|
114
|
-
*/
|
|
115
|
-
function ddlTimestamp(date) {
|
|
116
|
-
return date.toISOString().replace('T', ' ').replace('Z', '');
|
|
102
|
+
return dialect.escape(typeof value === 'object' && !(value instanceof Date) ? JSON.stringify(value) : value);
|
|
117
103
|
}
|
|
118
104
|
function expressionSql(expression, dialect) {
|
|
119
105
|
const { expressions } = DIALECT_DEFAULTS[dialect.dialectName];
|
|
@@ -1,13 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Migration Builder
|
|
3
|
-
*
|
|
4
|
-
* Provides type-safe migration operations with two modes:
|
|
5
|
-
* - OperationRecorder: Record operations only (for code generation)
|
|
6
|
-
* - MigrationBuilder: Execute DDL operations (for integration tests/runtime)
|
|
7
|
-
*/
|
|
8
1
|
import type { ForeignKeyAction } from '../../schema/types.js';
|
|
9
2
|
import type { IndexColumnInput, IndexOptions } from '../../type/index.js';
|
|
10
|
-
import type {
|
|
3
|
+
import type { SchemaGenerator } from '../../type/migration.js';
|
|
11
4
|
import type { AnyMigrationOperation, IAlterTableBuilder, IColumnBuilder, IColumnFactory, IMigrationBuilder, ITableBuilder } from './types.js';
|
|
12
5
|
type ForeignKeyTarget = {
|
|
13
6
|
table: string;
|
|
@@ -49,28 +42,13 @@ export declare class OperationRecorder implements IMigrationBuilder {
|
|
|
49
42
|
getOperations(): AnyMigrationOperation[];
|
|
50
43
|
}
|
|
51
44
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```typescript
|
|
57
|
-
* const builder = new MigrationBuilder(querier);
|
|
58
|
-
*
|
|
59
|
-
* await builder.createTable('users', (t) => {
|
|
60
|
-
* t.id();
|
|
61
|
-
* t.string('name');
|
|
62
|
-
* t.timestamps();
|
|
63
|
-
* });
|
|
64
|
-
* ```
|
|
45
|
+
* Records each operation, then runs the statements `generator` writes for it through `run`. Build one
|
|
46
|
+
* for a querier with `migrationBuilderFor`.
|
|
65
47
|
*/
|
|
66
48
|
export declare class MigrationBuilder extends OperationRecorder {
|
|
67
|
-
private readonly
|
|
68
|
-
private readonly
|
|
69
|
-
constructor(
|
|
70
|
-
/** The recorder's sink, plus the statements the operation turns into. */
|
|
49
|
+
private readonly generator;
|
|
50
|
+
private readonly run;
|
|
51
|
+
constructor(generator: SchemaGenerator, run: (statement: string) => Promise<unknown>);
|
|
71
52
|
protected record(operation: AnyMigrationOperation): Promise<void>;
|
|
72
|
-
private getCreateTableStatements;
|
|
73
|
-
private execute;
|
|
74
|
-
private operationToSql;
|
|
75
53
|
}
|
|
76
54
|
export {};
|
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Migration Builder
|
|
3
|
-
*
|
|
4
|
-
* Provides type-safe migration operations with two modes:
|
|
5
|
-
* - OperationRecorder: Record operations only (for code generation)
|
|
6
|
-
* - MigrationBuilder: Execute DDL operations (for integration tests/runtime)
|
|
7
|
-
*/
|
|
8
1
|
import { indexNameParts, normalizeIndexColumn } from '../../util/index.js';
|
|
9
2
|
import { derivedIndexName } from '../../util/sql.util.js';
|
|
10
|
-
import { createSchemaGenerator } from '../schemaGenerator.js';
|
|
11
|
-
import { splitSqlStatements } from './splitSqlStatements.js';
|
|
12
3
|
import { TableBuilder } from './tableBuilder.js';
|
|
13
4
|
/**
|
|
14
5
|
* One `createIndex` operation. Shared because the alter-table builder, the recorder and the
|
|
@@ -239,86 +230,21 @@ export class OperationRecorder {
|
|
|
239
230
|
}
|
|
240
231
|
}
|
|
241
232
|
/**
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
* @example
|
|
246
|
-
* ```typescript
|
|
247
|
-
* const builder = new MigrationBuilder(querier);
|
|
248
|
-
*
|
|
249
|
-
* await builder.createTable('users', (t) => {
|
|
250
|
-
* t.id();
|
|
251
|
-
* t.string('name');
|
|
252
|
-
* t.timestamps();
|
|
253
|
-
* });
|
|
254
|
-
* ```
|
|
233
|
+
* Records each operation, then runs the statements `generator` writes for it through `run`. Build one
|
|
234
|
+
* for a querier with `migrationBuilderFor`.
|
|
255
235
|
*/
|
|
256
236
|
export class MigrationBuilder extends OperationRecorder {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
constructor(
|
|
237
|
+
generator;
|
|
238
|
+
run;
|
|
239
|
+
constructor(generator, run) {
|
|
260
240
|
super();
|
|
261
|
-
this.
|
|
262
|
-
|
|
263
|
-
if (!generator) {
|
|
264
|
-
throw new TypeError(`Could not find a schema generator for dialect: ${querier.dialect.dialectName}`);
|
|
265
|
-
}
|
|
266
|
-
this.sqlGenerator = generator;
|
|
241
|
+
this.generator = generator;
|
|
242
|
+
this.run = run;
|
|
267
243
|
}
|
|
268
|
-
/** The recorder's sink, plus the statements the operation turns into. */
|
|
269
244
|
async record(operation) {
|
|
270
245
|
await super.record(operation);
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
getCreateTableStatements(operation) {
|
|
274
|
-
return this.sqlGenerator.generateCreateTableFromDefinition(operation.table);
|
|
275
|
-
}
|
|
276
|
-
async execute(operation) {
|
|
277
|
-
if (operation.type === 'createTable') {
|
|
278
|
-
for (const statement of this.getCreateTableStatements(operation)) {
|
|
279
|
-
await this.querier.run(statement);
|
|
280
|
-
}
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
const sql = this.operationToSql(operation);
|
|
284
|
-
if (sql) {
|
|
285
|
-
for (const statement of splitSqlStatements(sql)) {
|
|
286
|
-
await this.querier.run(statement);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
operationToSql(operation) {
|
|
291
|
-
switch (operation.type) {
|
|
292
|
-
case 'createTable':
|
|
293
|
-
// One line per statement in preview; execute() runs each separately (#87).
|
|
294
|
-
return this.getCreateTableStatements(operation).join('\n');
|
|
295
|
-
case 'dropTable':
|
|
296
|
-
return this.sqlGenerator.generateDropTable(operation.tableName, {
|
|
297
|
-
ifExists: operation.ifExists,
|
|
298
|
-
cascade: operation.cascade,
|
|
299
|
-
});
|
|
300
|
-
case 'renameTable':
|
|
301
|
-
return this.sqlGenerator.generateRenameTableSql(operation.oldName, operation.newName);
|
|
302
|
-
case 'addColumn':
|
|
303
|
-
return this.sqlGenerator.generateAddColumnSql(operation.tableName, operation.column);
|
|
304
|
-
case 'dropColumn':
|
|
305
|
-
return this.sqlGenerator.generateDropColumnSql(operation.tableName, operation.columnName);
|
|
306
|
-
case 'renameColumn':
|
|
307
|
-
return this.sqlGenerator.generateRenameColumnSql(operation.tableName, operation.oldName, operation.newName);
|
|
308
|
-
case 'alterColumn':
|
|
309
|
-
return this.sqlGenerator.generateAlterColumnSql(operation.tableName, operation.columnName, operation.changes);
|
|
310
|
-
case 'createIndex':
|
|
311
|
-
return this.sqlGenerator.generateCreateIndexFromDefinition(operation.tableName, operation.index);
|
|
312
|
-
case 'dropIndex':
|
|
313
|
-
return this.sqlGenerator.generateDropIndex(operation.tableName, operation.indexName);
|
|
314
|
-
case 'addForeignKey':
|
|
315
|
-
return this.sqlGenerator.generateAddForeignKeySql(operation.tableName, operation.foreignKey);
|
|
316
|
-
case 'dropForeignKey':
|
|
317
|
-
return this.sqlGenerator.generateDropForeignKeySql(operation.tableName, operation.constraintName);
|
|
318
|
-
case 'raw':
|
|
319
|
-
return operation.sql;
|
|
320
|
-
default:
|
|
321
|
-
return undefined;
|
|
246
|
+
for (const statement of this.generator.generateOperation(operation)) {
|
|
247
|
+
await this.run(statement);
|
|
322
248
|
}
|
|
323
249
|
}
|
|
324
250
|
}
|
|
@@ -116,20 +116,10 @@ export interface TableDefinition {
|
|
|
116
116
|
/** Table comment */
|
|
117
117
|
comment?: string;
|
|
118
118
|
}
|
|
119
|
-
/**
|
|
120
|
-
* Type of migration operation.
|
|
121
|
-
*/
|
|
122
|
-
export type MigrationOperationType = 'createTable' | 'dropTable' | 'renameTable' | 'alterTable' | 'addColumn' | 'dropColumn' | 'alterColumn' | 'renameColumn' | 'createIndex' | 'dropIndex' | 'addForeignKey' | 'dropForeignKey' | 'raw';
|
|
123
|
-
/**
|
|
124
|
-
* Base migration operation.
|
|
125
|
-
*/
|
|
126
|
-
export interface MigrationOperation {
|
|
127
|
-
type: MigrationOperationType;
|
|
128
|
-
}
|
|
129
119
|
/**
|
|
130
120
|
* Create table operation.
|
|
131
121
|
*/
|
|
132
|
-
export interface CreateTableOperation
|
|
122
|
+
export interface CreateTableOperation {
|
|
133
123
|
type: 'createTable';
|
|
134
124
|
table: TableDefinition;
|
|
135
125
|
ifNotExists?: boolean;
|
|
@@ -137,7 +127,7 @@ export interface CreateTableOperation extends MigrationOperation {
|
|
|
137
127
|
/**
|
|
138
128
|
* Drop table operation.
|
|
139
129
|
*/
|
|
140
|
-
export interface DropTableOperation
|
|
130
|
+
export interface DropTableOperation {
|
|
141
131
|
type: 'dropTable';
|
|
142
132
|
tableName: string;
|
|
143
133
|
ifExists?: boolean;
|
|
@@ -146,7 +136,7 @@ export interface DropTableOperation extends MigrationOperation {
|
|
|
146
136
|
/**
|
|
147
137
|
* Rename table operation.
|
|
148
138
|
*/
|
|
149
|
-
export interface RenameTableOperation
|
|
139
|
+
export interface RenameTableOperation {
|
|
150
140
|
type: 'renameTable';
|
|
151
141
|
oldName: string;
|
|
152
142
|
newName: string;
|
|
@@ -154,7 +144,7 @@ export interface RenameTableOperation extends MigrationOperation {
|
|
|
154
144
|
/**
|
|
155
145
|
* Add column operation.
|
|
156
146
|
*/
|
|
157
|
-
export interface AddColumnOperation
|
|
147
|
+
export interface AddColumnOperation {
|
|
158
148
|
type: 'addColumn';
|
|
159
149
|
tableName: string;
|
|
160
150
|
column: FullColumnDefinition;
|
|
@@ -162,7 +152,7 @@ export interface AddColumnOperation extends MigrationOperation {
|
|
|
162
152
|
/**
|
|
163
153
|
* Drop column operation.
|
|
164
154
|
*/
|
|
165
|
-
export interface DropColumnOperation
|
|
155
|
+
export interface DropColumnOperation {
|
|
166
156
|
type: 'dropColumn';
|
|
167
157
|
tableName: string;
|
|
168
158
|
columnName: string;
|
|
@@ -170,7 +160,7 @@ export interface DropColumnOperation extends MigrationOperation {
|
|
|
170
160
|
/**
|
|
171
161
|
* Alter column operation.
|
|
172
162
|
*/
|
|
173
|
-
export interface AlterColumnOperation
|
|
163
|
+
export interface AlterColumnOperation {
|
|
174
164
|
type: 'alterColumn';
|
|
175
165
|
tableName: string;
|
|
176
166
|
columnName: string;
|
|
@@ -179,7 +169,7 @@ export interface AlterColumnOperation extends MigrationOperation {
|
|
|
179
169
|
/**
|
|
180
170
|
* Rename column operation.
|
|
181
171
|
*/
|
|
182
|
-
export interface RenameColumnOperation
|
|
172
|
+
export interface RenameColumnOperation {
|
|
183
173
|
type: 'renameColumn';
|
|
184
174
|
tableName: string;
|
|
185
175
|
oldName: string;
|
|
@@ -188,7 +178,7 @@ export interface RenameColumnOperation extends MigrationOperation {
|
|
|
188
178
|
/**
|
|
189
179
|
* Create index operation.
|
|
190
180
|
*/
|
|
191
|
-
export interface CreateIndexOperation
|
|
181
|
+
export interface CreateIndexOperation {
|
|
192
182
|
type: 'createIndex';
|
|
193
183
|
tableName: string;
|
|
194
184
|
index: IndexDefinition;
|
|
@@ -197,7 +187,7 @@ export interface CreateIndexOperation extends MigrationOperation {
|
|
|
197
187
|
/**
|
|
198
188
|
* Drop index operation.
|
|
199
189
|
*/
|
|
200
|
-
export interface DropIndexOperation
|
|
190
|
+
export interface DropIndexOperation {
|
|
201
191
|
type: 'dropIndex';
|
|
202
192
|
tableName: string;
|
|
203
193
|
indexName: string;
|
|
@@ -206,7 +196,7 @@ export interface DropIndexOperation extends MigrationOperation {
|
|
|
206
196
|
/**
|
|
207
197
|
* Add foreign key operation.
|
|
208
198
|
*/
|
|
209
|
-
export interface AddForeignKeyOperation
|
|
199
|
+
export interface AddForeignKeyOperation {
|
|
210
200
|
type: 'addForeignKey';
|
|
211
201
|
tableName: string;
|
|
212
202
|
foreignKey: ForeignKeySchema;
|
|
@@ -214,7 +204,7 @@ export interface AddForeignKeyOperation extends MigrationOperation {
|
|
|
214
204
|
/**
|
|
215
205
|
* Drop foreign key operation.
|
|
216
206
|
*/
|
|
217
|
-
export interface DropForeignKeyOperation
|
|
207
|
+
export interface DropForeignKeyOperation {
|
|
218
208
|
type: 'dropForeignKey';
|
|
219
209
|
tableName: string;
|
|
220
210
|
constraintName: string;
|
|
@@ -222,7 +212,7 @@ export interface DropForeignKeyOperation extends MigrationOperation {
|
|
|
222
212
|
/**
|
|
223
213
|
* Raw SQL operation (escape hatch).
|
|
224
214
|
*/
|
|
225
|
-
export interface RawSqlOperation
|
|
215
|
+
export interface RawSqlOperation {
|
|
226
216
|
type: 'raw';
|
|
227
217
|
sql: string;
|
|
228
218
|
}
|
|
@@ -387,7 +377,7 @@ export interface IAlterTableBuilder {
|
|
|
387
377
|
* Interface for the main migration builder.
|
|
388
378
|
*/
|
|
389
379
|
export interface IMigrationBuilder {
|
|
390
|
-
/** Create a
|
|
380
|
+
/** Create a table as `callback` declares it; on MongoDB a collection, whose callback declares only indexes. */
|
|
391
381
|
createTable(name: string, callback: (table: ITableBuilder) => void): Promise<void>;
|
|
392
382
|
/** Drop a table */
|
|
393
383
|
dropTable(name: string, options?: {
|
|
@@ -420,6 +410,6 @@ export interface IMigrationBuilder {
|
|
|
420
410
|
}): Promise<void>;
|
|
421
411
|
/** Drop a foreign key */
|
|
422
412
|
dropForeignKey(tableName: string, constraintName: string): Promise<void>;
|
|
423
|
-
/** Execute raw SQL */
|
|
413
|
+
/** Execute raw SQL, the escape hatch for anything the builder does not model. */
|
|
424
414
|
raw(sql: string): Promise<void>;
|
|
425
415
|
}
|
package/dist/migrate/cli.d.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import type {
|
|
3
|
-
import type { Config, MigratorDialect } from '../type/index.js';
|
|
2
|
+
import type { Config } from '../type/index.js';
|
|
4
3
|
import { Migrator } from './migrator.js';
|
|
5
|
-
import { createSchemaGeneratorAsync } from './schemaGeneratorAsync.js';
|
|
6
|
-
/** Sync helper for SQL dialects only; returns `undefined` for MongoDB - use {@link createSchemaGeneratorAsync}. */
|
|
7
|
-
export declare function getSchemaGenerator(dialect: MigratorDialect, defaultForeignKeyAction?: ForeignKeyAction): import("./schemaGenerator.js").SqlSchemaGenerator | undefined;
|
|
8
|
-
export { createSchemaGeneratorAsync };
|
|
9
4
|
export declare function main(args?: string[]): Promise<void>;
|
|
10
5
|
export declare function runUp(migrator: Migrator, args: string[]): Promise<void>;
|
|
11
6
|
export declare function runDown(migrator: Migrator, args: string[]): Promise<void>;
|
package/dist/migrate/cli.js
CHANGED
|
@@ -7,14 +7,8 @@ import { createEntityCodeGenerator } from './codegen/entityCodeGenerator.js';
|
|
|
7
7
|
import { entityTypesSource } from './codegen/entityTypes.js';
|
|
8
8
|
import { detectDrift } from './drift/driftDetector.js';
|
|
9
9
|
import { Migrator } from './migrator.js';
|
|
10
|
-
import { buildEntityAST
|
|
11
|
-
import { createSchemaGeneratorAsync } from './schemaGeneratorAsync.js';
|
|
10
|
+
import { buildEntityAST } from './schemaGenerator.js';
|
|
12
11
|
import { DEFAULT_MIGRATIONS_TABLE } from './storage/databaseStorage.js';
|
|
13
|
-
/** Sync helper for SQL dialects only; returns `undefined` for MongoDB - use {@link createSchemaGeneratorAsync}. */
|
|
14
|
-
export function getSchemaGenerator(dialect, defaultForeignKeyAction) {
|
|
15
|
-
return createSchemaGenerator(dialect, defaultForeignKeyAction);
|
|
16
|
-
}
|
|
17
|
-
export { createSchemaGeneratorAsync };
|
|
18
12
|
export async function main(args = process.argv.slice(2)) {
|
|
19
13
|
let customPath;
|
|
20
14
|
const filteredArgs = [];
|
|
@@ -42,7 +36,6 @@ export async function main(args = process.argv.slice(2)) {
|
|
|
42
36
|
defaultForeignKeyAction: config.defaultForeignKeyAction,
|
|
43
37
|
};
|
|
44
38
|
const migrator = new Migrator(config.pool, options);
|
|
45
|
-
await migrator.ensureSchemaGenerator();
|
|
46
39
|
switch (command) {
|
|
47
40
|
case 'up':
|
|
48
41
|
await runUp(migrator, filteredArgs.slice(1));
|
|
@@ -259,13 +252,13 @@ export async function runDriftCheck(migrator, config) {
|
|
|
259
252
|
console.error('No entities configured. Add entities to your uql config.');
|
|
260
253
|
process.exit(1);
|
|
261
254
|
}
|
|
262
|
-
else if (!migrator.schemaIntrospector
|
|
255
|
+
else if (!migrator.schemaIntrospector) {
|
|
263
256
|
console.error('No introspector available. Check your pool configuration.');
|
|
264
257
|
process.exit(1);
|
|
265
258
|
}
|
|
266
259
|
else {
|
|
267
260
|
console.log('\nChecking for schema drift...');
|
|
268
|
-
const expectedAST = buildEntityAST(migrator.
|
|
261
|
+
const expectedAST = buildEntityAST(await migrator.getSchemaGenerator(), config.entities);
|
|
269
262
|
// Build actual schema from database
|
|
270
263
|
const actualAST = await migrator.schemaIntrospector.introspect();
|
|
271
264
|
// Detect drift. The dialect renders canonical types as SQL - without it every type formats as
|
|
@@ -28,14 +28,22 @@ export declare function emitMongoCommandCalls(statements: string[]): string;
|
|
|
28
28
|
export declare const EMPTY_MANUAL_MIGRATION_UP_INNER = " // Add your migration logic here.\n // Use one await querier.run(\"...\") per SQL statement when possible (same style as generate:entities).\n // Example (Postgres):\n // await querier.run(\"CREATE TABLE \\\"users\\\" (\\\"id\\\" SERIAL PRIMARY KEY);\");\n";
|
|
29
29
|
/** Body for `down` in a manual (empty) migration scaffold. */
|
|
30
30
|
export declare const EMPTY_MANUAL_MIGRATION_DOWN_INNER = " // Add your rollback logic here.\n // await querier.run(\"DROP TABLE IF EXISTS \\\"users\\\";\");\n";
|
|
31
|
-
/** How a migration on
|
|
31
|
+
/** How a migration on one querier is scaffolded empty, and how a generated statement is spelled in it. */
|
|
32
|
+
export type MigrationSource = {
|
|
33
|
+
readonly querier: MigrationQuerierType;
|
|
34
|
+
readonly emptyUp: string;
|
|
35
|
+
readonly emptyDown: string;
|
|
36
|
+
emit(statements: string[]): string;
|
|
37
|
+
};
|
|
32
38
|
export declare const migrationSource: {
|
|
33
39
|
SqlQuerier: {
|
|
40
|
+
querier: "SqlQuerier";
|
|
34
41
|
emptyUp: string;
|
|
35
42
|
emptyDown: string;
|
|
36
43
|
emit: typeof emitSqlRunCalls;
|
|
37
44
|
};
|
|
38
45
|
MongoQuerier: {
|
|
46
|
+
querier: "MongoQuerier";
|
|
39
47
|
emptyUp: string;
|
|
40
48
|
emptyDown: string;
|
|
41
49
|
emit: typeof emitMongoCommandCalls;
|
|
@@ -24,14 +24,15 @@ export const EMPTY_MANUAL_MIGRATION_UP_INNER = ` // Add your migration logic
|
|
|
24
24
|
export const EMPTY_MANUAL_MIGRATION_DOWN_INNER = ` // Add your rollback logic here.
|
|
25
25
|
// await querier.run("DROP TABLE IF EXISTS \\"users\\";");
|
|
26
26
|
`;
|
|
27
|
-
/** How a migration on each querier is scaffolded empty, and how a generated statement is spelled in it. */
|
|
28
27
|
export const migrationSource = {
|
|
29
28
|
SqlQuerier: {
|
|
29
|
+
querier: 'SqlQuerier',
|
|
30
30
|
emptyUp: EMPTY_MANUAL_MIGRATION_UP_INNER,
|
|
31
31
|
emptyDown: EMPTY_MANUAL_MIGRATION_DOWN_INNER,
|
|
32
32
|
emit: emitSqlRunCalls,
|
|
33
33
|
},
|
|
34
34
|
MongoQuerier: {
|
|
35
|
+
querier: 'MongoQuerier',
|
|
35
36
|
emptyUp: ` // Add your migration logic here, through the database handle.
|
|
36
37
|
// await querier.db.collection('users').updateMany({}, { $set: { active: true } });
|
|
37
38
|
`,
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { AbstractSqlDialect } from '../../dialect/abstractSqlDialect.js';
|
|
2
2
|
import { type IndexType } from '../../schema/types.js';
|
|
3
3
|
import { type IndexColumnSchema, type IndexFeature, type IndexJsonArray, type IndexSchema } from '../../type/index.js';
|
|
4
|
+
/** Refuses an index of a type `types` lacks, `hints` naming what to declare instead. */
|
|
5
|
+
export declare function assertIndexType(index: IndexSchema, types: ReadonlySet<IndexType>, dialectName: string, hints?: ReadonlyMap<IndexType, string>): void;
|
|
6
|
+
/** Refuses an index asking for a feature `features` lacks. */
|
|
7
|
+
export declare function assertIndexFeatures(index: IndexSchema, features: ReadonlySet<IndexFeature>, dialectName: string): void;
|
|
4
8
|
/**
|
|
5
9
|
* `CREATE INDEX` for SQL dialects: the statement and the fragments each engine spells differently.
|
|
6
10
|
* The migrator's rather than the dialect's, since {@link SqlSchemaGenerator} is the only thing that
|
|
@@ -28,8 +32,6 @@ export declare class IndexDdl<D extends AbstractSqlDialect = AbstractSqlDialect>
|
|
|
28
32
|
protected readonly indexTypes: ReadonlySet<IndexType>;
|
|
29
33
|
/** What to declare instead of a type this dialect lacks, appended to its refusal. */
|
|
30
34
|
protected readonly indexTypeHints: ReadonlyMap<IndexType, string>;
|
|
31
|
-
private assertIndexType;
|
|
32
|
-
private assertIndexFeatures;
|
|
33
35
|
/**
|
|
34
36
|
* Index types this dialect spells as a keyword of their own (`FULLTEXT INDEX`, `VECTOR INDEX`)
|
|
35
37
|
* rather than as an access method after the table. One table drives both, so a type that is a
|
|
@@ -17,6 +17,20 @@ const INDEX_FEATURE_PROBES = {
|
|
|
17
17
|
opsClass: (index) => index.entries.some((entry) => entry.opsClass !== undefined),
|
|
18
18
|
include: (index) => Boolean(index.include?.length),
|
|
19
19
|
};
|
|
20
|
+
/** Refuses an index of a type `types` lacks, `hints` naming what to declare instead. */
|
|
21
|
+
export function assertIndexType(index, types, dialectName, hints = new Map()) {
|
|
22
|
+
if (index.type && !types.has(index.type)) {
|
|
23
|
+
throw new TypeError(`${dialectName} has no ${index.type} index (index "${index.name}")` + (hints.get(index.type) ?? ''));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Refuses an index asking for a feature `features` lacks. */
|
|
27
|
+
export function assertIndexFeatures(index, features, dialectName) {
|
|
28
|
+
for (const feature of getKeys(INDEX_FEATURE_PROBES)) {
|
|
29
|
+
if (INDEX_FEATURE_PROBES[feature](index) && !features.has(feature)) {
|
|
30
|
+
throw new TypeError(`${dialectName} does not support ${INDEX_FEATURE_LABELS[feature]} (index "${index.name}")`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
20
34
|
/**
|
|
21
35
|
* `CREATE INDEX` for SQL dialects: the statement and the fragments each engine spells differently.
|
|
22
36
|
* The migrator's rather than the dialect's, since {@link SqlSchemaGenerator} is the only thing that
|
|
@@ -30,8 +44,8 @@ export class IndexDdl {
|
|
|
30
44
|
this.dialect = dialect;
|
|
31
45
|
}
|
|
32
46
|
getCreateIndexStatement(tableName, index, opts = {}) {
|
|
33
|
-
|
|
34
|
-
|
|
47
|
+
assertIndexType(index, this.indexTypes, this.dialect.dialectName, this.indexTypeHints);
|
|
48
|
+
assertIndexFeatures(index, this.indexFeatures, this.dialect.dialectName);
|
|
35
49
|
const unique = index.unique ? 'UNIQUE ' : '';
|
|
36
50
|
const ifNotExists = (opts.ifNotExists ?? this.dialect.features.indexIfNotExists) ? 'IF NOT EXISTS ' : '';
|
|
37
51
|
const columns = index.entries.map((entry) => this.indexColumn(entry, index)).join(', ');
|
|
@@ -57,19 +71,6 @@ export class IndexDdl {
|
|
|
57
71
|
indexTypes = new Set(INDEX_TYPES);
|
|
58
72
|
/** What to declare instead of a type this dialect lacks, appended to its refusal. */
|
|
59
73
|
indexTypeHints = new Map();
|
|
60
|
-
assertIndexType(index) {
|
|
61
|
-
if (index.type && !this.indexTypes.has(index.type)) {
|
|
62
|
-
throw new TypeError(`${this.dialect.dialectName} has no ${index.type} index (index "${index.name}")` +
|
|
63
|
-
(this.indexTypeHints.get(index.type) ?? ''));
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
assertIndexFeatures(index) {
|
|
67
|
-
for (const feature of getKeys(INDEX_FEATURE_PROBES)) {
|
|
68
|
-
if (INDEX_FEATURE_PROBES[feature](index) && !this.indexFeatures.has(feature)) {
|
|
69
|
-
throw new TypeError(`${this.dialect.dialectName} does not support ${INDEX_FEATURE_LABELS[feature]} (index "${index.name}")`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
74
|
/**
|
|
74
75
|
* Index types this dialect spells as a keyword of their own (`FULLTEXT INDEX`, `VECTOR INDEX`)
|
|
75
76
|
* rather than as an access method after the table. One table drives both, so a type that is a
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
/** The direction, or `'text'`, of one field in a MongoDB index key spec. */
|
|
2
2
|
export type MongoIndexKey = Record<string, 1 | -1 | 'text'>;
|
|
3
|
+
/** What a `createIndex` command hands the driver beside its key spec. */
|
|
4
|
+
export type MongoIndexOptions = {
|
|
5
|
+
readonly unique: boolean;
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly partialFilterExpression?: Readonly<Record<string, unknown>>;
|
|
8
|
+
};
|
|
3
9
|
/**
|
|
4
10
|
* `SchemaGenerator` yields one string per statement, so {@link MongoSchemaGenerator} emits its
|
|
5
11
|
* commands as JSON and this is their schema.
|
|
6
12
|
*
|
|
7
13
|
* Declared next to the generator that writes them because the migrator used to restate the shape
|
|
8
14
|
* inline from `JSON.parse`, with `cmd.name!` assertions and a bare `action: string` - where a command
|
|
9
|
-
* it had no branch for
|
|
15
|
+
* it had no branch for was silently a no-op.
|
|
10
16
|
*/
|
|
11
17
|
export type MongoCommand = {
|
|
12
18
|
readonly action: 'createCollection';
|
|
@@ -23,10 +29,7 @@ export type MongoCommand = {
|
|
|
23
29
|
readonly collection: string;
|
|
24
30
|
readonly name: string;
|
|
25
31
|
readonly key: MongoIndexKey;
|
|
26
|
-
readonly options:
|
|
27
|
-
readonly unique: boolean;
|
|
28
|
-
readonly name: string;
|
|
29
|
-
};
|
|
32
|
+
readonly options: MongoIndexOptions;
|
|
30
33
|
} | {
|
|
31
34
|
readonly action: 'dropIndex';
|
|
32
35
|
readonly collection: string;
|
|
@@ -43,10 +46,7 @@ export type MongoCommandTarget = {
|
|
|
43
46
|
renameCollection(from: string, to: string): Promise<unknown>;
|
|
44
47
|
collection(name: string): {
|
|
45
48
|
drop(): Promise<unknown>;
|
|
46
|
-
createIndex(key: MongoIndexKey, options:
|
|
47
|
-
unique: boolean;
|
|
48
|
-
name: string;
|
|
49
|
-
}): Promise<unknown>;
|
|
49
|
+
createIndex(key: MongoIndexKey, options: MongoIndexOptions): Promise<unknown>;
|
|
50
50
|
dropIndex(name: string): Promise<unknown>;
|
|
51
51
|
};
|
|
52
52
|
};
|
|
@@ -27,7 +27,7 @@ export function runMongoCommand(db, statement) {
|
|
|
27
27
|
return db.collection(command.collection).dropIndex(command.name);
|
|
28
28
|
default:
|
|
29
29
|
// Unreachable for a command this module produced; a hand-written statement lands here rather
|
|
30
|
-
// than being silently skipped
|
|
30
|
+
// than being silently skipped.
|
|
31
31
|
throw unsupportedMongoCommand(statement);
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { ForeignKeyAction,
|
|
3
|
-
import type
|
|
4
|
-
import type {
|
|
5
|
-
export declare class MongoSchemaGenerator extends
|
|
1
|
+
import { MongoDialect } from '../../mongo/mongoDialect.js';
|
|
2
|
+
import type { ForeignKeyAction, TableNode } from '../../schema/types.js';
|
|
3
|
+
import { type CreateSchemaOptions, type EntityWhereMeta, type IndexSchema, type NamingStrategy, type SchemaDiff, type SchemaGenerator, type Type } from '../../type/index.js';
|
|
4
|
+
import type { AnyMigrationOperation } from '../builder/types.js';
|
|
5
|
+
export declare class MongoSchemaGenerator extends MongoDialect implements SchemaGenerator {
|
|
6
6
|
protected readonly defaultForeignKeyAction?: ForeignKeyAction | undefined;
|
|
7
|
-
readonly dialectName = "mongodb";
|
|
8
|
-
protected readonly featureDefaults: import("../../type/dialect.js").DialectFeatures;
|
|
9
|
-
readonly insertIdSource: InsertIdSource;
|
|
10
7
|
constructor(namingStrategy?: NamingStrategy, defaultForeignKeyAction?: ForeignKeyAction | undefined);
|
|
11
8
|
/** A collection has no SQL to render a check, a computed column or an index expression into. */
|
|
12
9
|
compileDdl(): string;
|
|
@@ -19,13 +16,18 @@ export declare class MongoSchemaGenerator extends AbstractDialect implements Sch
|
|
|
19
16
|
generateDropSchema(entities: readonly Type<object>[]): string[];
|
|
20
17
|
private selected;
|
|
21
18
|
/**
|
|
22
|
-
* The indexes
|
|
23
|
-
*
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
* The indexes an entity declares, as the collection would hold them. One owner because two paths need
|
|
20
|
+
* it: creating a collection, and working out which of its indexes are missing.
|
|
21
|
+
*/
|
|
22
|
+
private indexesOf;
|
|
23
|
+
/** One declared index, its members resolved to document paths and its `where` to a filter document. */
|
|
24
|
+
private indexSchema;
|
|
25
|
+
/**
|
|
26
|
+
* The JSON of the document `partialFilterExpression` takes, refused where the predicate reaches past
|
|
27
|
+
* what that holds or what a migration carries as JSON.
|
|
26
28
|
*/
|
|
27
|
-
|
|
28
|
-
generateCreateTable<E>(entity: Type<E>, _options?: {
|
|
29
|
+
compileIndexPredicate(where: EntityWhereMeta<object>, entity: Type<object>, indexName: string): string;
|
|
30
|
+
generateCreateTable<E extends object>(entity: Type<E>, _options?: {
|
|
29
31
|
ifNotExists?: boolean;
|
|
30
32
|
}): string[];
|
|
31
33
|
generateDropTable(tableName: string): string;
|
|
@@ -35,20 +37,13 @@ export declare class MongoSchemaGenerator extends AbstractDialect implements Sch
|
|
|
35
37
|
* MongoDB's key spec is where its index options live: `-1` for a descending entry and `'text'` for a
|
|
36
38
|
* full-text index, which is what `$text` needs since a text index declares its own fields.
|
|
37
39
|
*
|
|
38
|
-
* @remarks The SQL-only
|
|
39
|
-
*
|
|
40
|
-
*
|
|
40
|
+
* @remarks The SQL-only options are refused by the checks the SQL dialects refuse each other's with - a
|
|
41
|
+
* silently weaker index is worse than a clear failure. `where` is the filter's JSON, as
|
|
42
|
+
* {@link compileIndexPredicate} writes it.
|
|
41
43
|
*/
|
|
42
44
|
generateCreateIndex(tableName: string, index: IndexSchema): string;
|
|
43
45
|
generateDropIndex(tableName: string, indexName: string): string;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
ifNotExists?: boolean;
|
|
47
|
-
}): string[];
|
|
48
|
-
generateCreateIndexFromNode(index: IndexNode): string;
|
|
49
|
-
generateCreateTableFromDefinition(table: TableDefinition, _options?: {
|
|
50
|
-
ifNotExists?: boolean;
|
|
51
|
-
}): string[];
|
|
52
|
-
generateRenameTableSql(oldName: string, newName: string): string;
|
|
46
|
+
/** A collection and its indexes, which is all a document store has: a column, a constraint or SQL throws. */
|
|
47
|
+
generateOperation(operation: AnyMigrationOperation): string[];
|
|
53
48
|
diffSchema(entity: Type<object>, currentTable: TableNode | undefined): SchemaDiff | undefined;
|
|
54
49
|
}
|