mythix 2.11.3 → 2.12.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/README.md +1 -1
- package/package.json +4 -4
- package/src/application.d.ts +4 -1
- package/src/cli/cli-utils.d.ts +6 -3
- package/src/cli/cli-utils.js +8 -3
- package/src/cli/generators/migration-generator.js +117 -24
- package/src/cli/migrate-command.js +4 -4
- package/src/cli/shell-command.js +1 -1
- package/src/controllers/controller-base.d.ts +6 -2
- package/src/controllers/controller-base.js +7 -2
- package/src/http-server/http-server.js +2 -2
- package/src/models/model-module.js +3 -3
- package/src/models/model.d.ts +4 -1
- package/src/models/model.js +9 -4
- package/src/modules/database-module.js +1 -1
- package/src/tasks/task-base.d.ts +6 -2
- package/src/tasks/task-base.js +7 -2
- package/src/tasks/task-module.js +8 -12
- package/src/utils/test-utils.js +4 -4
package/README.md
CHANGED
|
@@ -416,7 +416,7 @@ class Application extends Mythix.Application {
|
|
|
416
416
|
|
|
417
417
|
Tasks are cron-like tasks that run at a given frequency, delay, or scheduled time. They can be defined using the `Mythix.defineTask` method, and should be placed inside `./app/tasks`, in files with names that have a `-task.js` suffix.
|
|
418
418
|
|
|
419
|
-
Like most `mythix` classes, you can call `this.getApplication()` to get the application instance, `this.getLogger()` to get the tasks logger, `this.getModel(name)` or `this.getModels()` to get application models, and `this.
|
|
419
|
+
Like most `mythix` classes, you can call `this.getApplication()` to get the application instance, `this.getLogger()` to get the tasks logger, `this.getModel(name)` or `this.getModels()` to get application models, and `this.getConnection()` to get the database connection for the running application.
|
|
420
420
|
|
|
421
421
|
Example task:
|
|
422
422
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mythix",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.12.0",
|
|
4
4
|
"description": "Mythix is a NodeJS web-app framework",
|
|
5
5
|
"main": "src/index",
|
|
6
6
|
"scripts": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"homepage": "https://github.com/th317erd/mythix#readme",
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@spothero/eslint-plugin-spothero": "github:spothero/eslint-plugin-spothero",
|
|
23
|
-
"@types/node": "^18.11.
|
|
23
|
+
"@types/node": "^18.11.10",
|
|
24
24
|
"colors": "^1.4.0",
|
|
25
25
|
"diff": "^5.1.0",
|
|
26
26
|
"eslint": "^8.28.0",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"express": "^4.18.2",
|
|
34
34
|
"express-busboy": "github:th317erd/express-busboy#0754a570d7979097b31e48655b80d3fcd628d4e4",
|
|
35
35
|
"form-data": "^4.0.0",
|
|
36
|
-
"luxon": "^3.1.
|
|
36
|
+
"luxon": "^3.1.1",
|
|
37
37
|
"micromatch": "^4.0.5",
|
|
38
|
-
"mythix-orm": "^1.
|
|
38
|
+
"mythix-orm": "^1.12.0",
|
|
39
39
|
"nife": "^1.12.1",
|
|
40
40
|
"prompts": "^2.4.2"
|
|
41
41
|
}
|
package/src/application.d.ts
CHANGED
|
@@ -66,7 +66,10 @@ export declare class Application {
|
|
|
66
66
|
// From DatabaseModule
|
|
67
67
|
public getDBConfig(): GenericObject;
|
|
68
68
|
public getDBTablePrefix(): string | null;
|
|
69
|
-
public
|
|
69
|
+
public getConnection(connection?: ConnectionBase): ConnectionBase;
|
|
70
|
+
|
|
71
|
+
// Deprecated
|
|
72
|
+
public getDBConnection(connection?: ConnectionBase): ConnectionBase;
|
|
70
73
|
|
|
71
74
|
// From FileWatcherModule
|
|
72
75
|
public autoReload(enable?: boolean, shuttingDown?: boolean): Promise<void>;
|
package/src/cli/cli-utils.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { Logger } from '../logger';
|
|
|
7
7
|
|
|
8
8
|
export declare type CommandClass = typeof CommandBase;
|
|
9
9
|
|
|
10
|
-
export declare type CommandClasses = { [
|
|
10
|
+
export declare type CommandClasses = { [key: string]: CommandClass };
|
|
11
11
|
|
|
12
12
|
export declare type FileFilterCallback = (fullFileName: string, fileName: string, stats?: Stats) => boolean;
|
|
13
13
|
|
|
@@ -16,7 +16,7 @@ export declare class CommandBase {
|
|
|
16
16
|
declare public static commandName: string;
|
|
17
17
|
declare public static applicationConfig?: GenericObject | (() => GenericObject);
|
|
18
18
|
declare public static commandArguments?: () => { help: HelpInterface, runner: Runner };
|
|
19
|
-
declare public static runtimeArguments?: { [
|
|
19
|
+
declare public static runtimeArguments?: { [key: string]: Array<string> };
|
|
20
20
|
public static execute(): Promise<void>;
|
|
21
21
|
|
|
22
22
|
declare public application: Application;
|
|
@@ -26,7 +26,10 @@ export declare class CommandBase {
|
|
|
26
26
|
getOptions(): GenericObject;
|
|
27
27
|
getApplication(): Application;
|
|
28
28
|
getLogger(): Logger;
|
|
29
|
-
|
|
29
|
+
getConnection(connection?: ConnectionBase): ConnectionBase;
|
|
30
|
+
|
|
31
|
+
// Deprecated
|
|
32
|
+
getDBConnection(connection?: ConnectionBase): ConnectionBase;
|
|
30
33
|
|
|
31
34
|
spawnCommand(
|
|
32
35
|
command: string,
|
package/src/cli/cli-utils.js
CHANGED
|
@@ -48,9 +48,14 @@ class CommandBase {
|
|
|
48
48
|
return app.getLogger();
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
getConnection(connection) {
|
|
52
52
|
let application = this.getApplication();
|
|
53
|
-
return application.
|
|
53
|
+
return application.getConnection(connection);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Deprecated
|
|
57
|
+
getDBConnection(connection) {
|
|
58
|
+
return this.getConnection(connection);
|
|
54
59
|
}
|
|
55
60
|
|
|
56
61
|
spawnCommand(command, args, options) {
|
|
@@ -290,7 +295,7 @@ function defineCommand(_commandName, definer, _parent) {
|
|
|
290
295
|
return await commandInstance.execute.call(commandInstance, commandOptions, commandContext);
|
|
291
296
|
};
|
|
292
297
|
|
|
293
|
-
let dbConnection = (typeof application.
|
|
298
|
+
let dbConnection = (typeof application.getConnection === 'function') ? application.getConnection() : undefined;
|
|
294
299
|
let result;
|
|
295
300
|
|
|
296
301
|
if (dbConnection && typeof dbConnection.createContext === 'function')
|
|
@@ -17,11 +17,11 @@ const MIGRATION_ID = '${migrationID}';
|
|
|
17
17
|
|
|
18
18
|
module.exports = {
|
|
19
19
|
MIGRATION_ID,
|
|
20
|
-
up: async function(connection) {
|
|
21
|
-
${upCode}
|
|
20
|
+
up: async function(connection, application) {
|
|
21
|
+
${upCode || ''}
|
|
22
22
|
},
|
|
23
|
-
down: async function(connection) {
|
|
24
|
-
${downCode}
|
|
23
|
+
down: async function(connection, application) {
|
|
24
|
+
${downCode || ''}
|
|
25
25
|
},
|
|
26
26
|
};
|
|
27
27
|
`;
|
|
@@ -36,12 +36,13 @@ class GenerateMigrationCommand extends CommandBase {
|
|
|
36
36
|
'@usage': 'mythix-cli generate migration {operation} {operation args} [options]',
|
|
37
37
|
'@title': 'Generate a migration file',
|
|
38
38
|
'@examples': [
|
|
39
|
+
'mythix-cli generate migration add blank --name "my migration"',
|
|
39
40
|
'mythix-cli generate migration add model User',
|
|
40
41
|
'mythix-cli generate migration rename model User AdminUser',
|
|
41
|
-
'mythix-cli generate migration
|
|
42
|
+
'mythix-cli generate migration drop model User',
|
|
42
43
|
'mythix-cli generate migration add field User:age',
|
|
43
44
|
'mythix-cli generate migration rename field User:age yearsLived',
|
|
44
|
-
'mythix-cli generate migration
|
|
45
|
+
'mythix-cli generate migration drop field User:age',
|
|
45
46
|
],
|
|
46
47
|
'-n={name} | -n {name} | --name={name} | --name {name}': 'Specify the name of the generated migration file',
|
|
47
48
|
'-o={path} | -o {path} | --output={path} | --output {path}': 'Specify the name of the generated migration file',
|
|
@@ -60,9 +61,9 @@ class GenerateMigrationCommand extends CommandBase {
|
|
|
60
61
|
|| store('outputPath', Path.resolve(applicationOptions.migrationsPath));
|
|
61
62
|
|
|
62
63
|
let result = $(
|
|
63
|
-
|
|
64
|
+
/^(add|rename|drop)$/i,
|
|
64
65
|
({ store }, parsedResult) => {
|
|
65
|
-
store({ operation: parsedResult.value });
|
|
66
|
+
store({ operation: parsedResult.value.toLowerCase() });
|
|
66
67
|
return true;
|
|
67
68
|
},
|
|
68
69
|
{ formatParsedResult: (result) => ({ value: result[0] }) },
|
|
@@ -72,18 +73,25 @@ class GenerateMigrationCommand extends CommandBase {
|
|
|
72
73
|
return false;
|
|
73
74
|
|
|
74
75
|
result = $(
|
|
75
|
-
|
|
76
|
+
/^(models?|fields?|blank)$/i,
|
|
76
77
|
({ store }, parsedResult) => {
|
|
77
|
-
|
|
78
|
+
let entity = parsedResult.value.toLowerCase();
|
|
79
|
+
if (entity === 'models')
|
|
80
|
+
entity = 'model';
|
|
81
|
+
|
|
82
|
+
if (entity === 'fields')
|
|
83
|
+
entity = 'field';
|
|
84
|
+
|
|
85
|
+
store({ entity });
|
|
78
86
|
return true;
|
|
79
87
|
},
|
|
80
88
|
{ formatParsedResult: (result) => ({ value: result[0] }) },
|
|
81
89
|
);
|
|
82
90
|
|
|
83
|
-
if (!result
|
|
91
|
+
if (!result)
|
|
84
92
|
return false;
|
|
85
93
|
|
|
86
|
-
if (fetch('
|
|
94
|
+
if (fetch('entity') === 'blank' && Nife.isEmpty(fetch('name'))) {
|
|
87
95
|
console.log('Error: Blank templates require a name. Use the "--name {name}" argument to specify a name.\n');
|
|
88
96
|
return false;
|
|
89
97
|
}
|
|
@@ -113,13 +121,17 @@ class GenerateMigrationCommand extends CommandBase {
|
|
|
113
121
|
|
|
114
122
|
let name = [
|
|
115
123
|
args.operation,
|
|
116
|
-
args.entity,
|
|
124
|
+
(args.remaining.length > 1) ? `${args.entity}s` : args.entity,
|
|
117
125
|
...args.remaining,
|
|
118
126
|
];
|
|
119
127
|
|
|
120
128
|
return `${args.version}-${this.formatMigrationName(name.join('-'))}.js`;
|
|
121
129
|
}
|
|
122
130
|
|
|
131
|
+
generateMigration(migrationID, upCode, downCode) {
|
|
132
|
+
return generateMigration(migrationID, upCode, downCode);
|
|
133
|
+
}
|
|
134
|
+
|
|
123
135
|
async execute(args, fullArgs) {
|
|
124
136
|
args.version = this.getRevisionNumber();
|
|
125
137
|
args.remaining = fullArgs._remaining;
|
|
@@ -172,12 +184,18 @@ class GenerateMigrationCommand extends CommandBase {
|
|
|
172
184
|
return str.trim().replace(/^/gm, ws);
|
|
173
185
|
}
|
|
174
186
|
|
|
175
|
-
|
|
187
|
+
operationAddBlank(args) {
|
|
188
|
+
return this.generateMigration(
|
|
189
|
+
args.version,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
addOrDropModels(args, reverseOperation) {
|
|
176
194
|
if (Nife.isEmpty(args.remaining))
|
|
177
195
|
throw new ValidationError('No model name(s) provided. Try this instead: \n\nmythix-cli generate migration add models \'{model name}\' ...');
|
|
178
196
|
|
|
179
197
|
let application = this.getApplication();
|
|
180
|
-
let connection = application.
|
|
198
|
+
let connection = application.getConnection();
|
|
181
199
|
let queryGenerator = connection.getQueryGenerator();
|
|
182
200
|
let statements = [];
|
|
183
201
|
let reverseStatements = [];
|
|
@@ -187,22 +205,97 @@ class GenerateMigrationCommand extends CommandBase {
|
|
|
187
205
|
let modelName = modelNames[i];
|
|
188
206
|
let Model = connection.getModel(modelName);
|
|
189
207
|
|
|
190
|
-
|
|
191
|
-
|
|
208
|
+
// Create table
|
|
209
|
+
let createTable = queryGenerator.generateCreateTableStatement(Model, { ifNotExists: true });
|
|
210
|
+
statements.push(` // Create "${Model.getTableName()}" table\n await connection.query(\`\n${this.tabIn(createTable, 3)}\`,\n { logger: console },\n );`);
|
|
211
|
+
|
|
212
|
+
// Create indexes and constraints
|
|
213
|
+
let trailingStatements = Nife.toArray(queryGenerator.generateCreateTableStatementOuterTail(Model)).filter(Boolean);
|
|
214
|
+
if (Nife.isNotEmpty(trailingStatements)) {
|
|
215
|
+
for (let i = 0, il = trailingStatements.length; i < il; i++) {
|
|
216
|
+
let trailingStatement = trailingStatements[i];
|
|
217
|
+
statements.push(` await connection.query(\n \`${trailingStatement}\`,\n { logger: console },\n );`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
192
220
|
|
|
193
221
|
let dropTable = queryGenerator.generateDropTableStatement(Model, { cascade: true });
|
|
194
|
-
reverseStatements.push(` // Drop ${
|
|
222
|
+
reverseStatements.push(` // Drop "${Model.getTableName()}" table\n await connection.query(\n \`${dropTable.trim()}\`,\n { logger: console },\n );`);
|
|
195
223
|
}
|
|
196
224
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
225
|
+
if (reverseOperation) {
|
|
226
|
+
return this.generateMigration(
|
|
227
|
+
args.version,
|
|
228
|
+
reverseStatements.reverse().join('\n\n'),
|
|
229
|
+
statements.join('\n\n'),
|
|
230
|
+
);
|
|
231
|
+
} else {
|
|
232
|
+
return this.generateMigration(
|
|
233
|
+
args.version,
|
|
234
|
+
statements.join('\n\n'),
|
|
235
|
+
reverseStatements.reverse().join('\n\n'),
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
addOrDropFields(args, reverseOperation) {
|
|
241
|
+
if (Nife.isEmpty(args.remaining))
|
|
242
|
+
throw new ValidationError('No field name(s) provided. Try this instead: \n\nmythix-cli generate migration add fields \'{model name}:{field name}\' ...');
|
|
243
|
+
|
|
244
|
+
let application = this.getApplication();
|
|
245
|
+
let connection = application.getConnection();
|
|
246
|
+
let queryGenerator = connection.getQueryGenerator();
|
|
247
|
+
let statements = [];
|
|
248
|
+
let reverseStatements = [];
|
|
249
|
+
|
|
250
|
+
let fields = args.remaining.map((fieldName) => {
|
|
251
|
+
let field = connection.getField(fieldName);
|
|
252
|
+
if (!field)
|
|
253
|
+
throw new ValidationError(`Unable to find requested field: "${fieldName}"`);
|
|
254
|
+
|
|
255
|
+
return field;
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
for (let i = 0, il = fields.length; i < il; i++) {
|
|
259
|
+
let field = fields[i];
|
|
260
|
+
let Model = field.Model;
|
|
261
|
+
|
|
262
|
+
// Create table
|
|
263
|
+
let createColumn = queryGenerator.generateAddColumnStatement(field, { ifNotExists: true });
|
|
264
|
+
statements.push(` // Add "${Model.getTableName()}"."${field.columnName}" column\n await connection.query(\n \`${createColumn}\`,\n { logger: console },\n );`);
|
|
265
|
+
|
|
266
|
+
let dropColumn = queryGenerator.generateDropColumnStatement(field, { cascade: true, ifExists: true });
|
|
267
|
+
reverseStatements.push(` // Drop "${Model.getTableName()}"."${field.columnName}" column\n await connection.query(\n \`${dropColumn.trim()}\`,\n { logger: console },\n );`);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (reverseOperation) {
|
|
271
|
+
return this.generateMigration(
|
|
272
|
+
args.version,
|
|
273
|
+
reverseStatements.reverse().join('\n\n'),
|
|
274
|
+
statements.join('\n\n'),
|
|
275
|
+
);
|
|
276
|
+
} else {
|
|
277
|
+
return this.generateMigration(
|
|
278
|
+
args.version,
|
|
279
|
+
statements.join('\n\n'),
|
|
280
|
+
reverseStatements.reverse().join('\n\n'),
|
|
281
|
+
);
|
|
282
|
+
}
|
|
202
283
|
}
|
|
203
284
|
|
|
204
285
|
operationAddModel(args) {
|
|
205
|
-
return this.
|
|
286
|
+
return this.addOrDropModels(args, false);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
operationDropModel(args) {
|
|
290
|
+
return this.addOrDropModels(args, true);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
operationAddField(args) {
|
|
294
|
+
return this.addOrDropFields(args, false);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
operationDropField(args) {
|
|
298
|
+
return this.addOrDropFields(args, true);
|
|
206
299
|
}
|
|
207
300
|
}
|
|
208
301
|
|
|
@@ -62,16 +62,16 @@ module.exports = defineCommand('migrate', ({ Parent }) => {
|
|
|
62
62
|
let fileName = Path.basename(fullFileName);
|
|
63
63
|
let migrationFileTS = fileName.substring(0, TIMESTAMP_LENGTH);
|
|
64
64
|
|
|
65
|
-
if (BigInt(migrationFileTS)
|
|
65
|
+
if (BigInt(migrationFileTS) > revision)
|
|
66
66
|
return true;
|
|
67
67
|
|
|
68
68
|
return false;
|
|
69
69
|
});
|
|
70
70
|
|
|
71
71
|
if (index < 0)
|
|
72
|
-
return
|
|
72
|
+
return [];
|
|
73
73
|
|
|
74
|
-
return migrationFiles.slice((isRollback) ? index : index
|
|
74
|
+
return migrationFiles.slice((isRollback) ? index - 1 : index);
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
async executeMigration(application, dbConnection, migrationFileName, useTransaction, rollback) {
|
|
@@ -147,7 +147,7 @@ module.exports = defineCommand('migrate', ({ Parent }) => {
|
|
|
147
147
|
|
|
148
148
|
let application = this.getApplication();
|
|
149
149
|
let applicationOptions = application.getOptions();
|
|
150
|
-
let dbConnection = application.
|
|
150
|
+
let dbConnection = application.getConnection();
|
|
151
151
|
let migrationsPath = applicationOptions.migrationsPath;
|
|
152
152
|
let migrationFiles = this.getMigrationFiles(migrationsPath);
|
|
153
153
|
let useTransaction = args.transaction;
|
package/src/cli/shell-command.js
CHANGED
|
@@ -63,7 +63,7 @@ module.exports = defineCommand('shell', ({ Parent }) => {
|
|
|
63
63
|
interactiveShell.setupHistory(Path.join(OS.homedir(), `.${appName}-${environment}-history`), () => {});
|
|
64
64
|
|
|
65
65
|
interactiveShell.context.UUIDV4 = UUIDV4;
|
|
66
|
-
interactiveShell.context.connection = (typeof application.
|
|
66
|
+
interactiveShell.context.connection = (typeof application.getConnection === 'function') ? application.getConnection() : null;
|
|
67
67
|
interactiveShell.context.application = application;
|
|
68
68
|
interactiveShell.context.Nife = Nife;
|
|
69
69
|
|
|
@@ -7,7 +7,7 @@ import { HTTPBaseError } from '../http-server/http-errors';
|
|
|
7
7
|
|
|
8
8
|
export declare type ControllerClass = typeof ControllerBase;
|
|
9
9
|
|
|
10
|
-
export declare type ControllerClasses = { [
|
|
10
|
+
export declare type ControllerClasses = { [key: string]: ControllerClass };
|
|
11
11
|
|
|
12
12
|
export declare interface ControllerContext {
|
|
13
13
|
params: GenericObject;
|
|
@@ -37,7 +37,11 @@ export declare class ControllerBase {
|
|
|
37
37
|
public getLogger(): Logger;
|
|
38
38
|
public getModel(name?: string): ModelClass | undefined;
|
|
39
39
|
public getModels(): Models;
|
|
40
|
-
public
|
|
40
|
+
public getConnection(connection?: ConnectionBase): ConnectionBase;
|
|
41
|
+
|
|
42
|
+
// Deprecated
|
|
43
|
+
public getDBConnection(connection?: ConnectionBase): ConnectionBase;
|
|
44
|
+
|
|
41
45
|
public prepareToThrowError(ErrorClass: Function, args: Array<any>): HTTPBaseError;
|
|
42
46
|
public throwNotFoundError(...args: Array<any>): void;
|
|
43
47
|
public throwBadRequestError(...args: Array<any>): void;
|
|
@@ -97,9 +97,14 @@ class ControllerBase {
|
|
|
97
97
|
return application.getModels();
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
getConnection(connection) {
|
|
101
101
|
let application = this.getApplication();
|
|
102
|
-
return application.
|
|
102
|
+
return application.getConnection(connection);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Deprecated
|
|
106
|
+
getDBConnection(connection) {
|
|
107
|
+
return this.getConnection(connection);
|
|
103
108
|
}
|
|
104
109
|
|
|
105
110
|
prepareToThrowError(ErrorClass, args) {
|
|
@@ -308,7 +308,7 @@ class HTTPServer {
|
|
|
308
308
|
};
|
|
309
309
|
|
|
310
310
|
let application = this.getApplication();
|
|
311
|
-
let dbConnection = (typeof application.
|
|
311
|
+
let dbConnection = (typeof application.getConnection === 'function') ? application.getConnection() : undefined;
|
|
312
312
|
|
|
313
313
|
if (dbConnection && typeof dbConnection.createContext === 'function')
|
|
314
314
|
return await dbConnection.createContext(executeRequest, dbConnection, dbConnection);
|
|
@@ -386,7 +386,7 @@ class HTTPServer {
|
|
|
386
386
|
return await controllerInstance.handleOutgoingResponse(controllerResult, request, response, context);
|
|
387
387
|
};
|
|
388
388
|
|
|
389
|
-
let dbConnection = (typeof application.
|
|
389
|
+
let dbConnection = (typeof application.getConnection === 'function') ? application.getConnection() : undefined;
|
|
390
390
|
if (dbConnection && typeof dbConnection.createContext === 'function')
|
|
391
391
|
await dbConnection.createContext(handleOutgoing, dbConnection, dbConnection);
|
|
392
392
|
else
|
|
@@ -75,7 +75,7 @@ class ModelModule extends BaseModule {
|
|
|
75
75
|
|
|
76
76
|
loadModels(modelsPath) {
|
|
77
77
|
let application = this.getApplication();
|
|
78
|
-
let connection = (typeof application.
|
|
78
|
+
let connection = (typeof application.getConnection === 'function') ? application.getConnection() : null;
|
|
79
79
|
let dbConfig = (typeof application.getDBConfig === 'function') ? application.getDBConfig() : null;
|
|
80
80
|
let modelFiles = this.getModelFilePaths(modelsPath);
|
|
81
81
|
let models = {};
|
|
@@ -103,7 +103,7 @@ class ModelModule extends BaseModule {
|
|
|
103
103
|
|
|
104
104
|
getModel(modelName) {
|
|
105
105
|
let application = this.getApplication();
|
|
106
|
-
let connection = (typeof application.
|
|
106
|
+
let connection = (typeof application.getConnection === 'function') ? application.getConnection() : null;
|
|
107
107
|
if (!connection)
|
|
108
108
|
return;
|
|
109
109
|
|
|
@@ -112,7 +112,7 @@ class ModelModule extends BaseModule {
|
|
|
112
112
|
|
|
113
113
|
getModels() {
|
|
114
114
|
let application = this.getApplication();
|
|
115
|
-
let connection = (typeof application.
|
|
115
|
+
let connection = (typeof application.getConnection === 'function') ? application.getConnection() : null;
|
|
116
116
|
if (!connection)
|
|
117
117
|
return {};
|
|
118
118
|
|
package/src/models/model.d.ts
CHANGED
|
@@ -13,5 +13,8 @@ export declare class Model extends _Model {
|
|
|
13
13
|
|
|
14
14
|
public getApplication(): Application;
|
|
15
15
|
public getLogger(): Logger;
|
|
16
|
-
public
|
|
16
|
+
public getConnection(connection?: ConnectionBase): ConnectionBase;
|
|
17
|
+
|
|
18
|
+
// Deprecated
|
|
19
|
+
public getDBConnection(connection?: ConnectionBase): ConnectionBase;
|
|
17
20
|
}
|
package/src/models/model.js
CHANGED
|
@@ -34,7 +34,7 @@ class Model extends _Model {
|
|
|
34
34
|
return application.getLogger();
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
getConnection(connection) {
|
|
38
38
|
if (connection)
|
|
39
39
|
return connection;
|
|
40
40
|
|
|
@@ -42,18 +42,23 @@ class Model extends _Model {
|
|
|
42
42
|
if (!application)
|
|
43
43
|
return null;
|
|
44
44
|
|
|
45
|
-
if (typeof application.
|
|
46
|
-
return application.
|
|
45
|
+
if (typeof application.getConnection === 'function')
|
|
46
|
+
return application.getConnection();
|
|
47
47
|
|
|
48
48
|
return null;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
// Deprecated
|
|
52
|
+
getDBConnection(connection) {
|
|
53
|
+
return this.getConnection(connection);
|
|
54
|
+
}
|
|
55
|
+
|
|
51
56
|
static _getConnection(_connection) {
|
|
52
57
|
let connection = super._getConnection(_connection);
|
|
53
58
|
if (connection)
|
|
54
59
|
return connection;
|
|
55
60
|
|
|
56
|
-
return this.getApplication().
|
|
61
|
+
return this.getApplication().getConnection();
|
|
57
62
|
}
|
|
58
63
|
}
|
|
59
64
|
|
package/src/tasks/task-base.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Application } from '../application';
|
|
|
3
3
|
import { Logger } from '../logger';
|
|
4
4
|
import { ConnectionBase, ModelClass, Models } from 'mythix-orm';
|
|
5
5
|
|
|
6
|
-
export declare type Tasks = { [
|
|
6
|
+
export declare type Tasks = { [key: string]: TaskBase };
|
|
7
7
|
|
|
8
8
|
export declare type TaskClass = typeof TaskBase;
|
|
9
9
|
|
|
@@ -37,7 +37,11 @@ export declare class TaskBase {
|
|
|
37
37
|
public getNumberOfWorkers(): number;
|
|
38
38
|
public getModel(name: string): ModelClass;
|
|
39
39
|
public getModels(): Models;
|
|
40
|
-
public
|
|
40
|
+
public getConnection(connection?: ConnectionBase): ConnectionBase;
|
|
41
|
+
|
|
42
|
+
// Deprecated
|
|
43
|
+
public getDBConnection(connection?: ConnectionBase): ConnectionBase;
|
|
44
|
+
|
|
41
45
|
public getFrequency(taskIndex?: number): number;
|
|
42
46
|
public getStartDelay(taskIndex?: number): number;
|
|
43
47
|
}
|
package/src/tasks/task-base.js
CHANGED
|
@@ -100,9 +100,14 @@ class TaskBase {
|
|
|
100
100
|
return application.getModels();
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
getConnection(connection) {
|
|
104
104
|
let application = this.getApplication();
|
|
105
|
-
return application.
|
|
105
|
+
return application.getConnection(connection);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Deprecated
|
|
109
|
+
getDBConnection(connection) {
|
|
110
|
+
return this.getConnection(connection);
|
|
106
111
|
}
|
|
107
112
|
|
|
108
113
|
getFrequency(taskIndex) {
|
package/src/tasks/task-module.js
CHANGED
|
@@ -81,7 +81,7 @@ class TaskModule extends BaseModule {
|
|
|
81
81
|
loadTasks(tasksPath) {
|
|
82
82
|
let application = this.getApplication();
|
|
83
83
|
let taskFiles = this.getTaskFilePaths(tasksPath);
|
|
84
|
-
let connection = (typeof application.
|
|
84
|
+
let connection = (typeof application.getConnection === 'function') ? application.getConnection() : null;
|
|
85
85
|
let dbConfig = (typeof application.getDBConfig === 'function') ? application.getDBConfig() : null;
|
|
86
86
|
let tasks = {};
|
|
87
87
|
let args = { application, connection, dbConfig };
|
|
@@ -126,22 +126,18 @@ class TaskModule extends BaseModule {
|
|
|
126
126
|
promise.reject(error);
|
|
127
127
|
};
|
|
128
128
|
|
|
129
|
-
const _runTask = () => {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
errorResult,
|
|
136
|
-
);
|
|
137
|
-
} else {
|
|
138
|
-
promise.resolve(result);
|
|
129
|
+
const _runTask = async () => {
|
|
130
|
+
try {
|
|
131
|
+
let result = await taskInstance.execute(lastTime, currentTime, diff);
|
|
132
|
+
successResult(result);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
errorResult(error);
|
|
139
135
|
}
|
|
140
136
|
};
|
|
141
137
|
|
|
142
138
|
const runTask = async () => {
|
|
143
139
|
let application = this.getApplication();
|
|
144
|
-
let dbConnection = (typeof application.
|
|
140
|
+
let dbConnection = (typeof application.getConnection === 'function') ? application.getConnection() : undefined;
|
|
145
141
|
|
|
146
142
|
if (dbConnection && typeof dbConnection.createContext === 'function')
|
|
147
143
|
await dbConnection.createContext(_runTask, dbConnection, dbConnection);
|
package/src/utils/test-utils.js
CHANGED
|
@@ -115,8 +115,8 @@ function createTestApplication(ApplicationClass) {
|
|
|
115
115
|
async start(...args) {
|
|
116
116
|
let result = await super.start(...args);
|
|
117
117
|
|
|
118
|
-
if (typeof this.
|
|
119
|
-
let connection = this.
|
|
118
|
+
if (typeof this.getConnection === 'function') {
|
|
119
|
+
let connection = this.getConnection();
|
|
120
120
|
await this.createAllTables(connection);
|
|
121
121
|
}
|
|
122
122
|
|
|
@@ -142,7 +142,7 @@ function createTestApplication(ApplicationClass) {
|
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
async truncateAllTables(exclude) {
|
|
145
|
-
let connection = this.
|
|
145
|
+
let connection = this.getConnection();
|
|
146
146
|
let models = this.getModels();
|
|
147
147
|
let modelNames = Object.keys(models);
|
|
148
148
|
|
|
@@ -173,7 +173,7 @@ function createTestApplication(ApplicationClass) {
|
|
|
173
173
|
try {
|
|
174
174
|
let Klass = callback.call(this, OriginalModel);
|
|
175
175
|
|
|
176
|
-
let connection = this.
|
|
176
|
+
let connection = this.getConnection();
|
|
177
177
|
connection.registerModel(Klass);
|
|
178
178
|
|
|
179
179
|
return await runner.call(this, Klass);
|