mythix 3.0.0 → 4.0.2

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 (128) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +27 -30
  3. package/{src → lib}/application.d.ts +25 -39
  4. package/{src/application.js → lib/application.mjs} +110 -135
  5. package/lib/cli/cli-utils.d.ts +21 -0
  6. package/lib/cli/cli-utils.mjs +319 -0
  7. package/lib/cli/command-base.d.ts +33 -0
  8. package/lib/cli/command-base.mjs +118 -0
  9. package/lib/cli/command-executor.mjs +14 -0
  10. package/lib/cli/commands/deploy-command.mjs +1010 -0
  11. package/lib/cli/commands/generators/generate-command.mjs +117 -0
  12. package/lib/cli/commands/generators/index.mjs +2 -0
  13. package/{src/cli/generators/migration-generator.js → lib/cli/commands/generators/migration-generator.mjs} +11 -13
  14. package/lib/cli/commands/index.mjs +22 -0
  15. package/lib/cli/commands/migrate-command.mjs +184 -0
  16. package/lib/cli/commands/routes-command.mjs +40 -0
  17. package/lib/cli/commands/serve-command.mjs +63 -0
  18. package/lib/cli/commands/shell-command.mjs +95 -0
  19. package/lib/cli/index.mjs +3 -0
  20. package/{src → lib}/controllers/controller-base.d.ts +1 -1
  21. package/{src/controllers/controller-base.js → lib/controllers/controller-base.mjs} +3 -9
  22. package/{src → lib}/controllers/controller-module.d.ts +3 -3
  23. package/lib/controllers/controller-module.mjs +70 -0
  24. package/lib/controllers/generate-client-api-interface-spec.mjs +44 -0
  25. package/{src → lib}/controllers/generate-client-api-interface.d.ts +1 -0
  26. package/{src/controllers/generate-client-api-interface.js → lib/controllers/generate-client-api-interface.mjs} +39 -16
  27. package/{src → lib}/controllers/index.d.ts +2 -3
  28. package/lib/controllers/index.mjs +4 -0
  29. package/lib/controllers/routes/index.mjs +4 -0
  30. package/{src/controllers/routes/route-capture.js → lib/controllers/routes/route-capture.mjs} +2 -8
  31. package/{src/controllers/routes/route-endpoint.js → lib/controllers/routes/route-endpoint.mjs} +8 -8
  32. package/{src/controllers/routes/route-scope-base.js → lib/controllers/routes/route-scope-base.mjs} +19 -15
  33. package/{src/controllers/routes/route-scope.js → lib/controllers/routes/route-scope.mjs} +10 -12
  34. package/{src/http-server/http-errors.js → lib/http/http-errors.mjs} +9 -20
  35. package/{src/utils/http-interface.js → lib/http/http-interface.mjs} +6 -14
  36. package/{src/http-server → lib/http}/http-server-module.d.ts +2 -2
  37. package/{src/http-server/http-server-module.js → lib/http/http-server-module.mjs} +7 -11
  38. package/{src/http-server/http-server.js → lib/http/http-server.mjs} +17 -21
  39. package/lib/http/index.d.ts +4 -0
  40. package/lib/http/index.mjs +5 -0
  41. package/lib/index.d.ts +30 -0
  42. package/lib/index.mjs +31 -0
  43. package/lib/logger-spec.mjs +121 -0
  44. package/{src → lib}/logger.d.ts +1 -3
  45. package/{src/logger.js → lib/logger.mjs} +10 -24
  46. package/lib/models/index.d.ts +2 -0
  47. package/lib/models/index.mjs +2 -0
  48. package/{src/models/migration-model.js → lib/models/migration-model.mjs} +3 -11
  49. package/lib/models/model.d.ts +10 -0
  50. package/lib/models/model.mjs +28 -0
  51. package/{src → lib}/modules/database-module.d.ts +2 -2
  52. package/{src/modules/database-module.js → lib/modules/database-module.mjs} +19 -14
  53. package/lib/modules/index.d.ts +2 -0
  54. package/lib/modules/index.mjs +2 -0
  55. package/{src/modules/base-module.d.ts → lib/modules/module-base.d.ts} +3 -10
  56. package/{src/modules/base-module.js → lib/modules/module-base.mjs} +16 -6
  57. package/lib/tasks/index.d.ts +2 -0
  58. package/lib/tasks/index.mjs +2 -0
  59. package/{src → lib}/tasks/task-base.d.ts +4 -14
  60. package/lib/tasks/task-base.mjs +73 -0
  61. package/{src → lib}/tasks/task-module.d.ts +3 -3
  62. package/{src/tasks/task-module.js → lib/tasks/task-module.mjs} +43 -98
  63. package/{src/utils/config-utils.js → lib/utils/config-utils.mjs} +2 -10
  64. package/lib/utils/crypto-utils-spec.mjs +24 -0
  65. package/{src/utils/crypto-utils.js → lib/utils/crypto-utils.mjs} +16 -38
  66. package/lib/utils/file-utils-spec.mjs +10 -0
  67. package/{src/utils/file-utils.js → lib/utils/file-utils.mjs} +4 -11
  68. package/{src/utils/http-utils.js → lib/utils/http-utils.mjs} +3 -10
  69. package/{src → lib}/utils/index.d.ts +1 -1
  70. package/lib/utils/index.mjs +6 -0
  71. package/lib/utils/mime-utils-spec.mjs +171 -0
  72. package/{src/utils/mime-utils.js → lib/utils/mime-utils.mjs} +5 -14
  73. package/{src/utils/test-utils.js → lib/utils/test-utils.mjs} +14 -42
  74. package/package.json +5 -5
  75. package/src/cli/cli-utils.d.ts +0 -80
  76. package/src/cli/cli-utils.js +0 -547
  77. package/src/cli/command-executor.js +0 -31
  78. package/src/cli/deploy-command.js +0 -1010
  79. package/src/cli/generators/generate-command.js +0 -149
  80. package/src/cli/index.js +0 -5
  81. package/src/cli/migrate-command.js +0 -181
  82. package/src/cli/routes-command.js +0 -40
  83. package/src/cli/serve-command.js +0 -63
  84. package/src/cli/shell-command.js +0 -96
  85. package/src/controllers/controller-module.js +0 -126
  86. package/src/controllers/controller-utils.d.ts +0 -19
  87. package/src/controllers/controller-utils.js +0 -24
  88. package/src/controllers/index.js +0 -19
  89. package/src/controllers/routes/index.js +0 -31
  90. package/src/http-server/index.d.ts +0 -3
  91. package/src/http-server/index.js +0 -16
  92. package/src/index.d.ts +0 -49
  93. package/src/index.js +0 -47
  94. package/src/models/index.d.ts +0 -4
  95. package/src/models/index.js +0 -17
  96. package/src/models/model-module.d.ts +0 -9
  97. package/src/models/model-module.js +0 -130
  98. package/src/models/model-utils.d.ts +0 -20
  99. package/src/models/model-utils.js +0 -46
  100. package/src/models/model.d.ts +0 -20
  101. package/src/models/model.js +0 -65
  102. package/src/modules/file-watcher-module.d.ts +0 -13
  103. package/src/modules/file-watcher-module.js +0 -220
  104. package/src/modules/index.d.ts +0 -3
  105. package/src/modules/index.js +0 -11
  106. package/src/tasks/index.d.ts +0 -3
  107. package/src/tasks/index.js +0 -11
  108. package/src/tasks/task-base.js +0 -117
  109. package/src/tasks/task-utils.d.ts +0 -46
  110. package/src/tasks/task-utils.js +0 -130
  111. package/src/utils/index.js +0 -25
  112. /package/{src → lib}/cli/index.d.ts +0 -0
  113. /package/{src → lib}/controllers/routes/index.d.ts +0 -0
  114. /package/{src → lib}/controllers/routes/route-capture.d.ts +0 -0
  115. /package/{src → lib}/controllers/routes/route-endpoint.d.ts +0 -0
  116. /package/{src → lib}/controllers/routes/route-scope-base.d.ts +0 -0
  117. /package/{src → lib}/controllers/routes/route-scope.d.ts +0 -0
  118. /package/{src/http-server → lib/http}/http-errors.d.ts +0 -0
  119. /package/{src/utils → lib/http}/http-interface.d.ts +0 -0
  120. /package/{src/http-server → lib/http}/http-server.d.ts +0 -0
  121. /package/{src → lib}/interfaces/common.ts +0 -0
  122. /package/{src → lib}/models/migration-model.d.ts +0 -0
  123. /package/{src → lib}/utils/config-utils.d.ts +0 -0
  124. /package/{src → lib}/utils/crypto-utils.d.ts +0 -0
  125. /package/{src → lib}/utils/file-utils.d.ts +0 -0
  126. /package/{src → lib}/utils/http-utils.d.ts +0 -0
  127. /package/{src → lib}/utils/mime-utils.d.ts +0 -0
  128. /package/{src → lib}/utils/test-utils.d.ts +0 -0
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Wyatt Greenway
3
+ Copyright (c) 2023 Wyatt Greenway
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -45,17 +45,18 @@ To create your own `mythix` application, you simply need to inherit from the `My
45
45
  Example:
46
46
 
47
47
  ```javascript
48
- const Path = require('path');
49
- const Mythix = require('mythix');
50
- const getRoutes = require('./routes');
51
- const { authMiddleware } = require('./middleware');
52
-
53
- class Application extends Mythix.Application {
54
- static APP_NAME = 'my_app_name';
48
+ import Path from 'node:path';
49
+ import Mythix from 'mythix';
50
+ import getRoutes from './routes/index.mjs';
51
+ import { authMiddleware } from './middleware.mjs';
52
+
53
+ export class Application extends Mythix.Application {
54
+ static getName() {
55
+ return 'my_app_name';
56
+ }
55
57
 
56
58
  constructor(_opts) {
57
59
  var opts = Object.assign({
58
- rootPath: Path.resolve(__dirname),
59
60
  httpServer: {
60
61
  middleware: [
61
62
  authMiddleware,
@@ -70,10 +71,6 @@ class Application extends Mythix.Application {
70
71
  return getRoutes.apply(this, args);
71
72
  }
72
73
  }
73
-
74
- module.exports = {
75
- Application,
76
- };
77
74
  ```
78
75
 
79
76
  ## Starting your server
@@ -120,9 +117,9 @@ Once you have your new controller file created, you simply need to define your c
120
117
  For example, a simple demo controller can be created by creating a new controller file named `./app/controllers/hello-world-controller.js` and placing the following contents in this file:
121
118
 
122
119
  ```javascript
123
- const { defineController } = require('mythix');
120
+ import { defineController } from 'mythix';
124
121
 
125
- module.exports = defineController('HelloWorld', ({ Parent }) => {
122
+ export const HelloWorld = defineController('HelloWorld', ({ Parent }) => {
126
123
  return class HelloWorldController extends Parent {
127
124
  async greet(params, query, body, models) {
128
125
  return 'Hello World!';
@@ -136,7 +133,7 @@ Now, all you need to do is add your new controller to the routes:
136
133
  Simply modify `./app/routes.js` to have the following content:
137
134
 
138
135
  ```
139
- module.exports = function getRoutes({ path }) {
136
+ export function getRoutes({ path }) {
140
137
  path('api', ({ path }) => {
141
138
  path('v1', ({ endpoint }) => {
142
139
  endpoint('greet', {
@@ -183,7 +180,7 @@ Routes are defined using methods. The method `getRoutes` is called on your appli
183
180
  Example:
184
181
 
185
182
  ```javascript
186
- module.exports = function({ path }) {
183
+ export function getRoutes({ path }) {
187
184
  path('api', ({ path }) => {
188
185
  path('v1', ({ endpoint, capture }) => {
189
186
  path('user', ({ endpoint, capture }) => {
@@ -246,9 +243,9 @@ Model definition files need to be placed in `./app/models`, and need to have a `
246
243
  Example:
247
244
 
248
245
  ```javascript
249
- const { defineModel } = require('mythix');
246
+ import { defineModel } from 'mythix';
250
247
 
251
- module.exports = defineModel('Product', ({ Parent, Types }) => {
248
+ export const Product = defineModel('Product', ({ Parent, Types }) => {
252
249
  return class Product extends Parent {
253
250
  // Define the model fields, using mythix-orm
254
251
  static fields = {
@@ -311,9 +308,9 @@ Any file placed in `./app/commands/` with a `-command.js` suffix will be loaded
311
308
  Example:
312
309
 
313
310
  ```javascript
314
- const { defineCommand } = require('mythix');
311
+ import { defineCommand } from 'mythix';
315
312
 
316
- module.exports = defineCommand('deploy', ({ Parent }) => {
313
+ export const Deploy = defineCommand('deploy', ({ Parent }) => {
317
314
  return class DeployCommand extends Parent {
318
315
  static runtimeArguments = {
319
316
  'node': [ '--inspect' ],
@@ -411,7 +408,7 @@ const {
411
408
  TaskBase,
412
409
  } = require('mythix');
413
410
 
414
- module.exports = defineTask('CustomTask', ({ application, Parent, time }) => {
411
+ export const CustomTask = defineTask('CustomTask', ({ application, Parent, time }) => {
415
412
  const workerCount = application.getConfigValue('tasks.CustomTask.workers', 1, 'integer');
416
413
 
417
414
  return class CustomTask extends Parent {
@@ -519,9 +516,9 @@ The return value will be a controller class, inherited from `Mythix.ControllerBa
519
516
  #### Example
520
517
 
521
518
  ```javascript
522
- const { defineController } = require('mythix');
519
+ import { defineController } from 'mythix';
523
520
 
524
- module.exports = defineController(
521
+ export const HelloWorld = defineController(
525
522
  'HelloWorld',
526
523
  ({ Parent, application }) => {
527
524
 
@@ -575,9 +572,9 @@ The return value will be a model class, inherited from `Mythix.ModelBase`.
575
572
  #### Example
576
573
 
577
574
  ```javascript
578
- const { defineModel } = require('mythix');
575
+ import { defineModel } from 'mythix';
579
576
 
580
- module.exports = defineModel('Product', ({ Parent, Types }) => {
577
+ export const Product = defineModel('Product', ({ Parent, Types }) => {
581
578
  return class Product extends Parent {
582
579
  // Define the model fields, using mythix-orm
583
580
  static fields = {
@@ -629,7 +626,7 @@ module.exports = defineModel('Product', ({ Parent, Types }) => {
629
626
 
630
627
  #### Description
631
628
 
632
- **Note: This section is outdated... command arguments have been changed to use the [cmded](https://www.npmjs.com/package/cmded) module. Refer to `mythix` [built-in commands](https://github.com/th317erd/mythix/blob/main/src/cli/deploy-command.js) for examples on the new command line argument interface.**
629
+ **Note: This section is outdated... command arguments have been changed to use the [cmded](https://www.npmjs.com/package/cmded) module. Refer to `mythix` [built-in commands](https://github.com/th317erd/mythix/blob/main/lib/cli/deploy-command.js) for examples on the new command line argument interface.**
633
630
 
634
631
  Create a new command class, giving your command the name specified by the `commandName` argument (all lower-case). The `definer` method will be invoked immediately upon the call to `Mythix.defineCommand`, and is expected to return a new controller class that inherits from `context.Parent`. `context.Parent` by default (if no `ParentCommandClassNameToInheritFrom` argument is specified) will be `Mythix.CommandBase`.
635
632
 
@@ -656,9 +653,9 @@ The return value will be a command class, inherited from `Mythix.CommandBase`.
656
653
  #### Example
657
654
 
658
655
  ```javascript
659
- const { defineCommand } = require('mythix');
656
+ import { defineCommand } from 'mythix';
660
657
 
661
- module.exports = defineCommand('deploy', ({ Parent }) => {
658
+ export const Deploy = defineCommand('deploy', ({ Parent }) => {
662
659
  return class DeployCommand extends Parent {
663
660
  static definition = 'Deploy application to servers';
664
661
  static commandArguments = '<-target:string(Target server to deploy to)';
@@ -708,9 +705,9 @@ The return value will be a task class, inherited from `Mythix.TaskBase`.
708
705
  #### Example
709
706
 
710
707
  ```javascript
711
- const { defineTask } = require('mythix');
708
+ import { defineTask } from 'mythix';
712
709
 
713
- module.exports = defineTask('CustomTask', ({ Parent, time }) => {
710
+ export const CustomTask = defineTask('CustomTask', ({ Parent, time }) => {
714
711
  return class CustomTask extends Parent {
715
712
  static workers = 4;
716
713
  static frequency = time.days(1);
@@ -1,50 +1,39 @@
1
1
  import { ConnectionBase, ModelClass, Models } from 'mythix-orm';
2
2
  import { ControllerClass } from './controllers/controller-base';
3
3
  import { RouteScope } from './controllers/routes/route-scope';
4
- import { HTTPServer } from './http-server/http-server';
4
+ import { HTTPServer } from './http/http-server';
5
5
  import { GenericObject } from './interfaces/common';
6
6
  import { Logger, LoggerClass, LoggerOptions } from './logger';
7
- import { Modules, ModuleClasses, BaseModuleClass } from './modules/base-module';
7
+ import { ModuleBase } from './modules/module-base';
8
+ import { TaskBase } from './tasks/task-base';
9
+ import { CommandClasses } from './cli/command-base';
8
10
 
9
11
  export declare type ApplicationClass = typeof Application;
10
12
 
13
+ export declare interface ModuleDictionary {
14
+ [key: string]: typeof ModuleBase;
15
+ }
16
+
11
17
  export declare interface ApplicationOptions {
12
- cli: boolean;
13
- environment: string;
14
- appName: string;
15
- rootPath: string;
16
- configPath: string;
17
- migrationsPath: string;
18
- modelsPath: string;
19
- seedersPath: string;
20
- controllersPath: string;
21
- templatesPath: string;
22
- commandsPath: string;
23
- tasksPath: string;
24
- modules: ModuleClasses;
25
- autoReload: boolean;
26
- exitOnShutdown: number | null;
27
- runTasks: boolean;
28
- testMode: boolean;
29
- noInternalMigrationTable: boolean;
30
- logger: LoggerOptions;
31
- database: boolean | GenericObject;
32
- httpServer: boolean | GenericObject;
33
- tempPath: string;
34
- routeParserTypes: { [key: string]: (value: string, param: GenericObject, index?: number) => any };
18
+ bindToProcessSignals?: boolean;
19
+ cli?: boolean;
20
+ database?: boolean | GenericObject;
21
+ environment?: string;
22
+ exitOnShutdown?: number | null;
23
+ httpServer?: boolean | GenericObject;
24
+ logger?: LoggerOptions;
25
+ tasks?: boolean | { [key: string]: TaskBase } | Array<TaskBase>;
26
+ tempPath?: string;
35
27
  }
36
28
 
37
29
  export declare class Application {
38
- declare public static APP_NAME: string;
39
-
40
- public static getDefaultModules(): ModuleClasses;
41
- public static findModuleIndex(modules: ModuleClasses, moduleKlass: BaseModuleClass): number;
42
- public static replaceModule(modules: ModuleClasses, moduleKlass: BaseModuleClass, replacementModuleKlass: BaseModuleClass): ModuleClasses;
30
+ public static getName(): string;
31
+ public static getCommandList(): CommandClasses;
32
+ public static getModules(): ModuleDictionary;
43
33
 
44
34
  public constructor(options?: ApplicationOptions);
45
35
  public getTempPath(): string | null;
46
- public getModules(): Modules;
47
- public initializeModules(modules: ModuleClasses): Promise<void>;
36
+ public initializeModules(modules: ModuleDictionary): Promise<void>;
48
37
  public startAllModules(options: ApplicationOptions): Promise<void>;
49
38
  public stopAllModules(options: ApplicationOptions): Promise<Array<Error>>;
50
39
  public bindToProcessSignals(): void;
@@ -68,17 +57,14 @@ export declare class Application {
68
57
  public getDBTablePrefix(): string | null;
69
58
  public getConnection(connection?: ConnectionBase): ConnectionBase;
70
59
 
71
- // Deprecated
72
- public getDBConnection(connection?: ConnectionBase): ConnectionBase;
73
-
74
- // From FileWatcherModule
75
- public autoReload(enable?: boolean, shuttingDown?: boolean): Promise<void>;
76
-
77
60
  // From ModelModule
78
61
  public getModel(modelName?: string): ModelClass | undefined;
79
62
  public getModels(): Models;
63
+ public getAppModelClasses(connection: ConnectionBase): { [key: string]: ModelClass };
64
+ public bindModels(connection: ConnectionBase, models: { [key: string]: ModelClass }): { [key: string]: ModelClass };
80
65
 
81
66
  // From ControllerModule
67
+ public getAppControllerClasses(): { [key: string]: ControllerClass };
82
68
  public getController(name: string): { controller: ControllerClass, controllerMethod: string | undefined };
83
69
 
84
70
  // From HTTPServerModule
@@ -88,7 +74,7 @@ export declare class Application {
88
74
  declare public isStarted: boolean;
89
75
  declare public isStopping: boolean;
90
76
  declare public options: ApplicationOptions;
91
- declare public moduleInstances: Modules;
77
+ declare public moduleInstances: Array<ModuleBase>;
92
78
  declare public config: GenericObject;
93
79
  declare public logger: Logger;
94
80
  }
@@ -1,100 +1,39 @@
1
- 'use strict';
2
-
3
- /* global process, __dirname */
4
-
5
- const Nife = require('nife');
6
- const Path = require('path');
7
- const FileSystem = require('fs');
8
- const EventEmitter = require('events');
9
- const OS = require('os');
10
- const { Logger } = require('./logger');
11
- const { DatabaseModule } = require('./modules/database-module');
12
- const { ModelModule } = require('./models/model-module');
13
- const { HTTPServerModule } = require('./http-server/http-server-module');
14
- const { ControllerModule } = require('./controllers/controller-module');
15
- const { TaskModule } = require('./tasks/task-module');
16
- const { FileWatcherModule } = require('./modules/file-watcher-module');
17
- const { wrapConfig } = require('./utils');
18
- const ControllerRoutes = require('./controllers/routes');
19
-
20
- // Trace what is requesting the application exit
21
-
22
- // (function doExit(_exit) {
23
- // process.exit = function() {
24
- // console.trace('EXIT');
25
- // return _exit.apply(process, arguments);
26
- // };
27
- // })(process.exit);
28
-
29
- class Application extends EventEmitter {
30
- static APP_NAME = 'mythix';
31
-
32
- static getDefaultModules() {
33
- return [
34
- DatabaseModule,
35
- ModelModule,
36
- HTTPServerModule,
37
- ControllerModule,
38
- TaskModule,
39
- FileWatcherModule,
40
- ];
1
+ import Path from 'node:path';
2
+ import FileSystem from 'node:fs';
3
+ import OS from 'node:os';
4
+ import Nife from 'nife';
5
+ import { Logger } from './logger.mjs';
6
+ import { DatabaseModule } from './modules/database-module.mjs';
7
+ import { HTTPServerModule } from './http/http-server-module.mjs';
8
+ import { ControllerModule } from './controllers/controller-module.mjs';
9
+ import { TaskModule } from './tasks/task-module.mjs';
10
+ import { wrapConfig } from './utils/config-utils.mjs';
11
+ import * as ControllerRoutes from './controllers/routes/index.mjs';
12
+ import { COMMANDS } from './cli/commands/index.mjs';
13
+ import { Migration } from './models/migration-model.mjs';
14
+
15
+ export class Application {
16
+ static getName() {
17
+ return 'mythix';
41
18
  }
42
19
 
43
- static findModuleIndex(modules, moduleKlass) {
44
- let index = -1;
45
-
46
- if (typeof moduleKlass.getModuleName === 'function') {
47
- let moduleKlassName = moduleKlass.getModuleName();
48
- index = modules.findIndex((thisModuleClass) => {
49
- return (thisModuleClass.getModuleName() === moduleKlassName);
50
- });
51
- } else {
52
- index = modules.findIndex((thisModuleClass) => thisModuleClass === moduleKlass);
53
- }
54
-
55
- return index;
20
+ static getCommandList() {
21
+ return COMMANDS;
56
22
  }
57
23
 
58
- static replaceModule(modules, moduleKlass, replacementModuleKlass) {
59
- let index = this.findModuleIndex(modules, moduleKlass);
60
- if (index >= 0)
61
- modules[index] = replacementModuleKlass;
62
- else
63
- throw new Error('Application::replaceModule: Unable to find and replace requested module');
64
-
65
- return modules;
24
+ static getModules() {
25
+ return {
26
+ database: DatabaseModule,
27
+ httpServer: HTTPServerModule,
28
+ controllers: ControllerModule,
29
+ tasks: TaskModule,
30
+ };
66
31
  }
67
32
 
68
33
  constructor(_opts) {
69
- super();
70
-
71
- let ROOT_PATH = (_opts && _opts.rootPath) ? _opts.rootPath : Path.resolve(__dirname);
72
-
73
34
  let opts = Nife.extend(true, {
74
- environment: (process.env.NODE_ENV || 'development'),
75
- appName: this.constructor.APP_NAME,
76
- rootPath: ROOT_PATH,
77
- configPath: Path.resolve(ROOT_PATH, 'config'),
78
- migrationsPath: Path.resolve(ROOT_PATH, 'migrations'),
79
- modelsPath: Path.resolve(ROOT_PATH, 'models'),
80
- seedersPath: Path.resolve(ROOT_PATH, 'seeders'),
81
- controllersPath: Path.resolve(ROOT_PATH, 'controllers'),
82
- templatesPath: Path.resolve(ROOT_PATH, 'templates'),
83
- commandsPath: Path.resolve(ROOT_PATH, 'commands'),
84
- tasksPath: Path.resolve(ROOT_PATH, 'tasks'),
85
- autoReload: ((_opts && _opts.environment) || process.env.NODE_ENV || 'development') === 'development',
86
- exitOnShutdown: null,
87
- runTasks: true,
88
- testMode: false,
89
- noInternalMigrationTable: false,
90
- logger: {
91
- rootPath: ROOT_PATH,
92
- },
93
- database: {},
94
- httpServer: {
95
- routeParserTypes: undefined,
96
- middleware: null,
97
- },
35
+ environment: (process.env.NODE_ENV || 'development'),
36
+ exitOnShutdown: null,
98
37
  }, _opts || {});
99
38
 
100
39
  Object.defineProperties(this, {
@@ -129,7 +68,7 @@ class Application extends EventEmitter {
129
68
  writable: false,
130
69
  enumerable: false,
131
70
  configurable: true,
132
- value: this.loadConfig(opts.configPath),
71
+ value: wrapConfig(Object.assign({}, opts.config || {}, { environment: opts.environment })),
133
72
  },
134
73
  });
135
74
 
@@ -145,17 +84,15 @@ class Application extends EventEmitter {
145
84
  },
146
85
  });
147
86
 
148
- this.bindToProcessSignals();
87
+ if (opts.bindToProcessSignals !== false)
88
+ this.bindToProcessSignals();
149
89
 
150
90
  if (Nife.isEmpty(opts.tempPath))
151
91
  opts.tempPath = Path.resolve(OS.tmpdir(), this.getApplicationName().replace(/[^\w-]/g, ''), ('' + process.pid));
152
-
153
- opts.modules = this.constructor.getDefaultModules(this, opts);
154
92
  }
155
93
 
156
94
  getApplicationName() {
157
- let options = this.getOptions();
158
- return options.appName || this.constructor.APP_NAME;
95
+ return this.constructor.getName();
159
96
  }
160
97
 
161
98
  getTempPath() {
@@ -163,21 +100,25 @@ class Application extends EventEmitter {
163
100
  return options.tempPath;
164
101
  }
165
102
 
166
- getModules() {
103
+ getModuleInstances() {
167
104
  return this.moduleInstances || [];
168
105
  }
169
106
 
170
107
  async initializeModules(_modules) {
171
108
  // Shutdown modules, if any are active
172
- let stopPromises = this.getModules().map((moduleInstance) => moduleInstance.stop());
173
- await Promise.all(stopPromises);
109
+ await this.stopAllModules(this.getOptions());
174
110
 
175
- let modules = _modules || [];
111
+ let modules = _modules || {};
112
+ let moduleNames = Object.keys(modules);
176
113
  let moduleInstances = [];
177
114
  let options = this.getOptions();
178
115
 
179
- for (let i = 0, il = modules.length; i < il; i++) {
180
- let ModuleClass = modules[i];
116
+ for (let i = 0, il = moduleNames.length; i < il; i++) {
117
+ let moduleName = moduleNames[i];
118
+ let ModuleClass = modules[moduleName];
119
+ if (typeof ModuleClass !== 'function')
120
+ throw new TypeError(`Specified module "${moduleName}" is not a module class`);
121
+
181
122
  if (typeof ModuleClass.shouldUse === 'function' && ModuleClass.shouldUse.call(ModuleClass, this, options) === false)
182
123
  continue;
183
124
 
@@ -189,7 +130,7 @@ class Application extends EventEmitter {
189
130
  }
190
131
 
191
132
  async startAllModules(options) {
192
- let moduleInstances = this.getModules();
133
+ let moduleInstances = this.getModuleInstances();
193
134
 
194
135
  for (let i = 0, il = moduleInstances.length; i < il; i++) {
195
136
  let moduleInstance = moduleInstances[i];
@@ -198,7 +139,7 @@ class Application extends EventEmitter {
198
139
  }
199
140
 
200
141
  async stopAllModules(options) {
201
- let moduleInstances = this.getModules();
142
+ let moduleInstances = this.getModuleInstances();
202
143
  let errors = [];
203
144
 
204
145
  for (let i = moduleInstances.length - 1; i >= 0; i--) {
@@ -211,8 +152,11 @@ class Application extends EventEmitter {
211
152
  }
212
153
  }
213
154
 
214
- if (errors.length > 0)
215
- return errors;
155
+ if (errors.length > 0) {
156
+ let error = new AggregateError('Errors were encountered while shutting down modules');
157
+ error.errors = errors;
158
+ throw error;
159
+ }
216
160
  }
217
161
 
218
162
  bindToProcessSignals() {
@@ -235,25 +179,6 @@ class Application extends EventEmitter {
235
179
  return this;
236
180
  }
237
181
 
238
- loadConfig(configPath) {
239
- let appOptions = this.getOptions();
240
- let environment = (appOptions.environment || process.NODE_ENV || 'development');
241
-
242
- if (!configPath)
243
- return wrapConfig(Object.assign({}, { environment }));
244
-
245
- try {
246
- let config = require(configPath);
247
- if (config.__esModule)
248
- config = config['default'];
249
-
250
- return wrapConfig(Object.assign({}, config || {}, { environment }));
251
- } catch (error) {
252
- this.getLogger().error(`Error while trying to load application configuration ${configPath}: `, error);
253
- throw error;
254
- }
255
- }
256
-
257
182
  getConfigValue(key, defaultValue, type) {
258
183
  let result = this.config.ENV(key, defaultValue);
259
184
 
@@ -273,6 +198,49 @@ class Application extends EventEmitter {
273
198
  return this;
274
199
  }
275
200
 
201
+ getAppControllerClasses() {
202
+ return {};
203
+ }
204
+
205
+ getAppModelClasses() {
206
+ return {
207
+ Migration,
208
+ };
209
+ }
210
+
211
+ bindModels(connection, models) {
212
+ const generateBoundModelClass = (connection, modelName, ModelClass) => {
213
+ if (typeof ModelClass !== 'function')
214
+ return;
215
+
216
+ const app = this;
217
+
218
+ return class BoundModel extends ModelClass {
219
+ static getApplication() {
220
+ return app;
221
+ }
222
+
223
+ static getModelName() {
224
+ return modelName;
225
+ }
226
+ };
227
+ };
228
+
229
+ let modelNames = Object.keys(models);
230
+ let boundModels = [];
231
+
232
+ for (let i = 0, il = modelNames.length; i < il; i++) {
233
+ let modelName = modelNames[i];
234
+ let ModelClass = generateBoundModelClass(connection, modelName, models[modelName]);
235
+ if (!ModelClass)
236
+ continue;
237
+
238
+ boundModels[modelName] = ModelClass;
239
+ }
240
+
241
+ return boundModels;
242
+ }
243
+
276
244
  _getRoutes() {
277
245
  let routeScope = new ControllerRoutes.RouteScope();
278
246
  this.getRoutes(routeScope);
@@ -284,8 +252,7 @@ class Application extends EventEmitter {
284
252
  }
285
253
 
286
254
  getCustomRouteParserTypes() {
287
- let options = this.getOptions();
288
- return options.routeParserTypes;
255
+ return [];
289
256
  }
290
257
 
291
258
  createLogger(loggerOpts, LoggerClass) {
@@ -299,21 +266,34 @@ class Application extends EventEmitter {
299
266
  return this.logger;
300
267
  }
301
268
 
269
+ getModel(name) {
270
+ if (typeof this.getConnection !== 'function')
271
+ return;
272
+
273
+ let connection = this.getConnection();
274
+ return connection.getModel(name);
275
+ }
276
+
277
+ getModels() {
278
+ if (typeof this.getConnection !== 'function')
279
+ return {};
280
+
281
+ let connection = this.getConnection();
282
+ return connection.getModels();
283
+ }
284
+
302
285
  async start() {
303
286
  let options = this.getOptions();
304
-
305
287
  let tempDir = this.getTempPath();
306
288
  if (Nife.isNotEmpty(tempDir)) {
307
289
  if (!FileSystem.existsSync(tempDir))
308
290
  FileSystem.mkdirSync(tempDir, { recursive: true });
309
291
  }
310
292
 
311
- await this.initializeModules(options.modules);
293
+ await this.initializeModules(this.constructor.getModules());
312
294
  await this.startAllModules(options);
313
295
 
314
296
  this.isStarted = true;
315
-
316
- this.emit('start');
317
297
  }
318
298
 
319
299
  async stop(exitCode) {
@@ -352,11 +332,8 @@ class Application extends EventEmitter {
352
332
 
353
333
  this.getLogger().info('Shut down complete!');
354
334
 
355
- this.emit('stop');
356
-
357
335
  if (options.exitOnShutdown != null || exitCode != null) {
358
336
  let code = (exitCode != null) ? exitCode : options.exitOnShutdown;
359
- this.emit('exit', code);
360
337
  process.exit(code);
361
338
  }
362
339
  } catch (error) {
@@ -365,5 +342,3 @@ class Application extends EventEmitter {
365
342
  }
366
343
  }
367
344
  }
368
-
369
- module.exports = { Application };
@@ -0,0 +1,21 @@
1
+ import { Application, ApplicationClass, ApplicationOptions } from '../application';
2
+ import { GenericObject } from '../interfaces/common';
3
+ import { CommandClass } from './command-base';
4
+
5
+ export declare function loadMythixConfig(
6
+ mythixConfigPath: string,
7
+ appRootPath: string
8
+ ): Promise<GenericObject>;
9
+
10
+ export declare function createApplication(
11
+ applicationClass: ApplicationClass,
12
+ options?: ApplicationOptions
13
+ ): Application;
14
+
15
+ export declare function executeCommand(
16
+ config: GenericObject,
17
+ appOptions: ApplicationOptions,
18
+ commandContext: GenericObject,
19
+ CommandKlass: CommandClass,
20
+ argv: Array<string>
21
+ ): Promise<undefined>;