mythix 2.6.2 → 2.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mythix",
3
- "version": "2.6.2",
3
+ "version": "2.7.0",
4
4
  "description": "Mythix is a NodeJS web-app framework",
5
5
  "main": "src/index",
6
6
  "scripts": {
@@ -35,7 +35,7 @@
35
35
  "form-data": "^4.0.0",
36
36
  "luxon": "^3.0.4",
37
37
  "micromatch": "^4.0.5",
38
- "mythix-orm": "^1.7.2",
38
+ "mythix-orm": "^1.8.0",
39
39
  "nife": "^1.12.1",
40
40
  "prompts": "^2.4.2"
41
41
  }
@@ -274,7 +274,18 @@ function defineCommand(_commandName, definer, _parent) {
274
274
 
275
275
  let commandOptions = commandContext[commandName] || {};
276
276
  let commandInstance = new Klass(application, commandOptions);
277
- let result = await commandInstance.execute.call(commandInstance, commandOptions, commandContext);
277
+
278
+ const doExecuteCommand = async () => {
279
+ return await commandInstance.execute.call(commandInstance, commandOptions, commandContext);
280
+ };
281
+
282
+ let dbConnection = (typeof application.getDBConnection === 'function') ? application.getDBConnection() : undefined;
283
+ let result;
284
+
285
+ if (dbConnection && typeof dbConnection.createContext === 'function')
286
+ result = await dbConnection.createContext(doExecuteCommand, dbConnection, dbConnection);
287
+ else
288
+ result = await doExecuteCommand();
278
289
 
279
290
  await application.stop(result || 0);
280
291
  } catch (error) {
@@ -301,22 +301,32 @@ class HTTPServer {
301
301
  }
302
302
 
303
303
  async sendRequestToController(request, response, context) {
304
- let controllerInstance = context.controllerInstance;
304
+ const executeRequest = async () => {
305
+ let controllerInstance = context.controllerInstance;
305
306
 
306
- // Compile query params
307
- context.query = this.compileQueryParams(context.route, context.query, (context.route && context.route.queryParams));
307
+ // Compile query params
308
+ context.query = this.compileQueryParams(context.route, context.query, (context.route && context.route.queryParams));
308
309
 
309
- let route = context.route;
310
+ let route = context.route;
310
311
 
311
- // Execute middleware if any exists
312
- let middleware = (typeof controllerInstance.getMiddleware === 'function') ? controllerInstance.getMiddleware.call(controllerInstance, context) : [];
313
- if (route && Nife.instanceOf(route.middleware, 'array') && Nife.isNotEmpty(route.middleware))
314
- middleware = route.middleware.concat((middleware) ? middleware : []);
312
+ // Execute middleware if any exists
313
+ let middleware = (typeof controllerInstance.getMiddleware === 'function') ? controllerInstance.getMiddleware.call(controllerInstance, context) : [];
314
+ if (route && Nife.instanceOf(route.middleware, 'array') && Nife.isNotEmpty(route.middleware))
315
+ middleware = route.middleware.concat((middleware) ? middleware : []);
315
316
 
316
- if (Nife.isNotEmpty(middleware))
317
- await this.executeMiddleware(middleware, request, response);
317
+ if (Nife.isNotEmpty(middleware))
318
+ await this.executeMiddleware(middleware, request, response);
318
319
 
319
- return await controllerInstance.handleIncomingRequest.apply(controllerInstance, [ request, response, context ]);
320
+ return await controllerInstance.handleIncomingRequest.apply(controllerInstance, [ request, response, context ]);
321
+ };
322
+
323
+ let application = this.getApplication();
324
+ let dbConnection = (typeof application.getDBConnection === 'function') ? application.getDBConnection() : undefined;
325
+
326
+ if (dbConnection && typeof dbConnection.createContext === 'function')
327
+ return await dbConnection.createContext(executeRequest, dbConnection, dbConnection);
328
+ else
329
+ return await executeRequest();
320
330
  }
321
331
 
322
332
  async baseRouter(request, response, next) {
@@ -361,10 +371,19 @@ class HTTPServer {
361
371
 
362
372
  let controllerResult = await this.sendRequestToController(request, response, context);
363
373
 
364
- if (!(response.finished || response.statusMessage))
365
- await controllerInstance.handleOutgoingResponse(controllerResult, request, response, context);
366
- else if (!response.finished)
374
+ if (!(response.finished || response.statusMessage)) {
375
+ const handleOutgoing = async () => {
376
+ return await controllerInstance.handleOutgoingResponse(controllerResult, request, response, context);
377
+ };
378
+
379
+ let dbConnection = (typeof application.getDBConnection === 'function') ? application.getDBConnection() : undefined;
380
+ if (dbConnection && typeof dbConnection.createContext === 'function')
381
+ await dbConnection.createContext(handleOutgoing, dbConnection, dbConnection);
382
+ else
383
+ await handleOutgoing();
384
+ } else if (!response.finished) {
367
385
  response.end();
386
+ }
368
387
 
369
388
  let statusCode = response.statusCode || 200;
370
389
  let requestTime = Nife.now() - startTime;
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
- const { Types } = require('mythix-orm');
3
+ const { Types, Utils } = require('mythix-orm');
4
4
  const { Model: ModelBase } = require('./model');
5
5
 
6
- function _setupModel(modelName, _Model, { application, connection }) {
6
+ function _setupModel(modelName, _Model, { application, connection: applicationConnection }) {
7
7
  let Model = _Model;
8
8
  let tableName = Model.getTableName();
9
9
  let tablePrefix = application.getDBTablePrefix();
@@ -19,7 +19,15 @@ function _setupModel(modelName, _Model, { application, connection }) {
19
19
  if (_connection)
20
20
  return _connection;
21
21
 
22
- return connection;
22
+ let { connection } = Model.getModelContext();
23
+ if (connection)
24
+ return connection;
25
+
26
+ connection = Utils.getContextValue('connection');
27
+ if (connection)
28
+ return connection;
29
+
30
+ return applicationConnection;
23
31
  };
24
32
 
25
33
  return { [modelName]: Model };
@@ -76,7 +76,7 @@ class DatabaseModule extends BaseModule {
76
76
 
77
77
  if (Nife.isEmpty(databaseConfig)) {
78
78
  this.getLogger().error(`Error: database connection for "${this.getConfigValue('environment')}" not defined`);
79
- return;
79
+ return {};
80
80
  }
81
81
 
82
82
  databaseConfig.logging = (this.getLogger().isDebugLevel()) ? this.getLogger().log.bind(this.getLogger()) : false;
@@ -126,7 +126,7 @@ class TaskModule extends BaseModule {
126
126
  promise.reject(error);
127
127
  };
128
128
 
129
- const runTask = () => {
129
+ const _runTask = () => {
130
130
  let result = taskInstance.execute(lastTime, currentTime, diff);
131
131
 
132
132
  if (Nife.instanceOf(result, 'promise')) {
@@ -139,6 +139,16 @@ class TaskModule extends BaseModule {
139
139
  }
140
140
  };
141
141
 
142
+ const runTask = async () => {
143
+ let application = this.getApplication();
144
+ let dbConnection = (typeof application.getDBConnection === 'function') ? application.getDBConnection() : undefined;
145
+
146
+ if (dbConnection && typeof dbConnection.createContext === 'function')
147
+ await dbConnection.createContext(_runTask, dbConnection, dbConnection);
148
+ else
149
+ await _runTask();
150
+ };
151
+
142
152
  let taskName = TaskKlass.taskName;
143
153
  let promise = Nife.createResolvable();
144
154
  let taskInstance = taskInfo.taskInstance;
@@ -64,6 +64,18 @@ function createTestApplication(ApplicationClass) {
64
64
  return defaultModules;
65
65
  }
66
66
 
67
+ createDatabaseConnection() {
68
+ const NOOP = () => {};
69
+
70
+ return {
71
+ isStarted: () => true,
72
+ start: NOOP,
73
+ stop: NOOP,
74
+ registerModels: NOOP,
75
+ getModels: () => ({}),
76
+ };
77
+ }
78
+
67
79
  constructor(_opts) {
68
80
  let opts = Nife.extend(true, {
69
81
  environment: 'test',