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/lib/index.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ export * from './application.mjs';
2
+ export * from './logger.mjs';
3
+
4
+ export * as Utils from './utils/index.mjs';
5
+ export {
6
+ CryptoUtils,
7
+ FileUtils,
8
+ HTTPUtils,
9
+ MimeUtils,
10
+ TestUtils,
11
+ } from './utils/index.mjs';
12
+
13
+ export * as Modules from './modules/index.mjs';
14
+ export { ModuleBase } from './modules/index.mjs';
15
+
16
+ export * as Tasks from './tasks/index.mjs';
17
+ export { TaskBase } from './tasks/index.mjs';
18
+
19
+ export * as Models from './models/index.mjs';
20
+ export { Model } from './models/index.mjs';
21
+
22
+ export * as HTTP from './http/index.mjs';
23
+
24
+ export * as Controllers from './controllers/index.mjs';
25
+ export { ControllerBase } from './controllers/index.mjs';
26
+
27
+ export * as CLI from './cli/index.mjs';
28
+ export { CommandBase, Commands } from './cli/index.mjs';
29
+
30
+ export { Types, Utils as MythixORMUtils } from 'mythix-orm';
package/lib/index.mjs ADDED
@@ -0,0 +1,31 @@
1
+ export * from './application.mjs';
2
+ export * from './logger.mjs';
3
+
4
+ export * as Utils from './utils/index.mjs';
5
+ export {
6
+ CryptoUtils,
7
+ FileUtils,
8
+ HTTPUtils,
9
+ MimeUtils,
10
+ TestUtils,
11
+ } from './utils/index.mjs';
12
+
13
+ export * as Modules from './modules/index.mjs';
14
+ export { ModuleBase } from './modules/index.mjs';
15
+
16
+ export * as Tasks from './tasks/index.mjs';
17
+ export { TaskBase } from './tasks/index.mjs';
18
+
19
+ export * as Models from './models/index.mjs';
20
+ export { Model } from './models/index.mjs';
21
+
22
+ export * as HTTP from './http/index.mjs';
23
+ export { HTTPErrors } from './http/index.mjs';
24
+
25
+ export * as Controllers from './controllers/index.mjs';
26
+ export { ControllerBase } from './controllers/index.mjs';
27
+
28
+ export * as CLI from './cli/index.mjs';
29
+ export { CommandBase, Commands } from './cli/index.mjs';
30
+
31
+ export { Types, Utils as MythixORMUtils } from 'mythix-orm';
@@ -0,0 +1,121 @@
1
+ /* eslint-disable no-magic-numbers */
2
+ /* global jasmine */
3
+
4
+ import { Logger } from './logger.mjs';
5
+
6
+ describe('Logger', () => {
7
+ let fakeConsole;
8
+ let consoleOutput;
9
+ let logger;
10
+
11
+ beforeAll(() => {
12
+ jasmine.clock().install();
13
+ jasmine.clock().mockDate(new Date(2000, 0, 1, 12, 0, 0, 0));
14
+ });
15
+
16
+ beforeEach(() => {
17
+ const createConsoleMethod = (name) => {
18
+ return function(...args) {
19
+ if (!consoleOutput[name])
20
+ consoleOutput[name] = [];
21
+
22
+ consoleOutput[name].push(args);
23
+ };
24
+ };
25
+
26
+ consoleOutput = {};
27
+
28
+ fakeConsole = {
29
+ debug: createConsoleMethod('debug'),
30
+ error: createConsoleMethod('error'),
31
+ info: createConsoleMethod('info'),
32
+ log: createConsoleMethod('log'),
33
+ warn: createConsoleMethod('warn'),
34
+ };
35
+
36
+ logger = new Logger({ writer: fakeConsole, pid: 777 });
37
+ });
38
+
39
+ it('works', () => {
40
+ logger.error('Testing 123', true, 3.14, 'Hello World!', { test: 'stuff' }, [ 'wow', 1, 2, 3 ]);
41
+ expect(consoleOutput.error[0]).toEqual([ 'E, [2000-01-01T19:00:00.000Z #777] -- : Testing 123 true 3.14 Hello World! {"test":"stuff"} ["wow",1,2,3]' ]);
42
+
43
+ logger.log('Testing 123', true, 3.14, 'Hello World!', { test: 'stuff' }, [ 'wow', 1, 2, 3 ]);
44
+ expect(consoleOutput.log[0]).toEqual([ 'L, [2000-01-01T19:00:00.000Z #777] -- : Testing 123 true 3.14 Hello World! {"test":"stuff"} ["wow",1,2,3]' ]);
45
+
46
+ logger.warn('Testing 123', true, 3.14, 'Hello World!', { test: 'stuff' }, [ 'wow', 1, 2, 3 ]);
47
+ expect(consoleOutput.warn[0]).toEqual([ 'W, [2000-01-01T19:00:00.000Z #777] -- : Testing 123 true 3.14 Hello World! {"test":"stuff"} ["wow",1,2,3]' ]);
48
+
49
+ logger.info('Testing 123', true, 3.14, 'Hello World!', { test: 'stuff' }, [ 'wow', 1, 2, 3 ]);
50
+ expect(consoleOutput.info[0]).toEqual([ 'I, [2000-01-01T19:00:00.000Z #777] -- : Testing 123 true 3.14 Hello World! {"test":"stuff"} ["wow",1,2,3]' ]);
51
+
52
+ logger.debug('Testing 123', true, 3.14, 'Hello World!', { test: 'stuff' }, [ 'wow', 1, 2, 3 ]);
53
+ expect(consoleOutput.debug[0]).toEqual([ 'D, [2000-01-01T19:00:00.000Z #777] -- : Testing 123 true 3.14 Hello World! {"test":"stuff"} ["wow",1,2,3]' ]);
54
+ });
55
+
56
+ it('can set a log level', () => {
57
+ const writeAll = () => {
58
+ consoleOutput = {};
59
+
60
+ logger.error('test');
61
+ logger.log('test');
62
+ logger.warn('test');
63
+ logger.info('test');
64
+ logger.debug('test');
65
+ };
66
+
67
+ // Level 0 (no messages)
68
+ logger.setLevel(0);
69
+ writeAll();
70
+ expect(consoleOutput.error).toBe(undefined);
71
+ expect(consoleOutput.log).toBe(undefined);
72
+ expect(consoleOutput.warn).toBe(undefined);
73
+ expect(consoleOutput.info).toBe(undefined);
74
+ expect(consoleOutput.debug).toBe(undefined);
75
+
76
+ // Level 1 (only errors)
77
+ logger.setLevel(Logger.LEVEL_ERROR);
78
+ writeAll();
79
+ expect(consoleOutput.error).toEqual([ [ 'E, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
80
+ expect(consoleOutput.log).toBe(undefined);
81
+ expect(consoleOutput.warn).toBe(undefined);
82
+ expect(consoleOutput.info).toBe(undefined);
83
+ expect(consoleOutput.debug).toBe(undefined);
84
+
85
+ // Level 2 (error, and log)
86
+ logger.setLevel(Logger.LEVEL_LOG);
87
+ writeAll();
88
+ expect(consoleOutput.error).toEqual([ [ 'E, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
89
+ expect(consoleOutput.log).toEqual([ [ 'L, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
90
+ expect(consoleOutput.warn).toBe(undefined);
91
+ expect(consoleOutput.info).toBe(undefined);
92
+ expect(consoleOutput.debug).toBe(undefined);
93
+
94
+ // Level 3 (error, log, and warn)
95
+ logger.setLevel(Logger.LEVEL_WARN);
96
+ writeAll();
97
+ expect(consoleOutput.error).toEqual([ [ 'E, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
98
+ expect(consoleOutput.log).toEqual([ [ 'L, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
99
+ expect(consoleOutput.warn).toEqual([ [ 'W, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
100
+ expect(consoleOutput.info).toBe(undefined);
101
+ expect(consoleOutput.debug).toBe(undefined);
102
+
103
+ // Level 3 (error, log, warn, and info)
104
+ logger.setLevel(Logger.LEVEL_INFO);
105
+ writeAll();
106
+ expect(consoleOutput.error).toEqual([ [ 'E, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
107
+ expect(consoleOutput.log).toEqual([ [ 'L, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
108
+ expect(consoleOutput.warn).toEqual([ [ 'W, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
109
+ expect(consoleOutput.info).toEqual([ [ 'I, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
110
+ expect(consoleOutput.debug).toBe(undefined);
111
+
112
+ // Level 4 (error, log, warn, info, and debug)
113
+ logger.setLevel(Logger.LEVEL_DEBUG);
114
+ writeAll();
115
+ expect(consoleOutput.error).toEqual([ [ 'E, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
116
+ expect(consoleOutput.log).toEqual([ [ 'L, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
117
+ expect(consoleOutput.warn).toEqual([ [ 'W, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
118
+ expect(consoleOutput.info).toEqual([ [ 'I, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
119
+ expect(consoleOutput.debug).toEqual([ [ 'D, [2000-01-01T19:00:00.000Z #777] -- : test' ] ]);
120
+ });
121
+ });
@@ -6,14 +6,13 @@ export declare interface LoggerWriter {
6
6
  log: (...args: Array<any>) => void;
7
7
  }
8
8
 
9
- export declare type ErrorStackFormatterMethod = (rootPath: string, error: Error) => void;
9
+ export declare type ErrorStackFormatterMethod = (error: Error) => void;
10
10
 
11
11
  export declare type LoggerClass = typeof Logger;
12
12
 
13
13
  export declare interface LoggerOptions {
14
14
  level?: number;
15
15
  writer?: string | LoggerWriter | null;
16
- rootPath?: string;
17
16
  errorStackFormatter?: ErrorStackFormatterMethod;
18
17
  }
19
18
 
@@ -29,7 +28,6 @@ export declare class Logger {
29
28
  declare protected _customWriter: string;
30
29
  declare protected _pid: string;
31
30
  declare protected _formatter: string;
32
- declare protected _rootPath: string;
33
31
  declare protected _errorStackFormatter: string;
34
32
 
35
33
  constructor(options?: LoggerOptions);
@@ -1,9 +1,6 @@
1
1
  /* eslint-disable lines-between-class-members */
2
- 'use strict';
3
2
 
4
- /* global process */
5
-
6
- const FileSystem = require('fs');
3
+ import FileSystem from 'node:fs';
7
4
 
8
5
  const LEVEL_ERROR = 1;
9
6
  const LEVEL_LOG = 2;
@@ -11,7 +8,7 @@ const LEVEL_WARN = 3;
11
8
  const LEVEL_INFO = 4;
12
9
  const LEVEL_DEBUG = 5;
13
10
 
14
- function errorStackToString(rootPath, error) {
11
+ function errorStackToString(error) {
15
12
  return ('\n -> ' + error.stack.split(/\n+/).slice(1).map((part) => `${part.replace(/^\s+at\s+/, '')}\n`).join(' -> ')).trimEnd();
16
13
  }
17
14
 
@@ -34,8 +31,8 @@ function logToWriter(type, ..._args) {
34
31
 
35
32
  if (arg instanceof Error) {
36
33
  let formattedStack = (typeof this._errorStackFormatter === 'function')
37
- ? this._errorStackFormatter.call(this, this._rootPath, arg)
38
- : errorStackToString.call(this, this._rootPath, arg);
34
+ ? this._errorStackFormatter.call(this, arg)
35
+ : errorStackToString.call(this, arg);
39
36
 
40
37
  arg = `${arg.name}: ${arg.message}: ${formattedStack}`;
41
38
  } else if (arg && typeof arg.valueOf() === 'function') {
@@ -68,10 +65,10 @@ function logToWriter(type, ..._args) {
68
65
  let content = args.join(' ');
69
66
  let output = `${type.charAt(0).toUpperCase()}, [${(new Date()).toISOString()} #${this._pid}] -- : ${(typeof formatter === 'function') ? formatter(content) : content}`;
70
67
 
71
- writeToWriterObject.call(this, (!writer) ? console : writer, type, output);
68
+ writeToWriterObject.call(this, writer, type, output);
72
69
  }
73
70
 
74
- class Logger {
71
+ export class Logger {
75
72
  static LEVEL_ERROR = LEVEL_ERROR;
76
73
  static LEVEL_LOG = LEVEL_LOG;
77
74
  static LEVEL_WARN = LEVEL_WARN;
@@ -80,13 +77,11 @@ class Logger {
80
77
 
81
78
  constructor(_opts) {
82
79
  let opts = Object.assign({
83
- level: LEVEL_INFO,
80
+ level: LEVEL_DEBUG,
84
81
  writer: null,
85
- rootPath: process.cwd(),
86
82
  errorStackFormatter: null,
87
- }, _opts || {}, {
88
- pid: process.pid,
89
- });
83
+ pid: process.pid,
84
+ }, _opts || {});
90
85
 
91
86
  // If string, assume file path
92
87
  let customWriter = false;
@@ -111,7 +106,7 @@ class Logger {
111
106
  writable: false,
112
107
  enumerable: false,
113
108
  configurable: false,
114
- value: opts.writer,
109
+ value: opts.writer || console,
115
110
  },
116
111
  '_customWriter': {
117
112
  writable: false,
@@ -131,12 +126,6 @@ class Logger {
131
126
  configurable: false,
132
127
  value: opts.formatter,
133
128
  },
134
- '_rootPath': {
135
- writable: false,
136
- enumerable: false,
137
- configurable: false,
138
- value: opts.rootPath,
139
- },
140
129
  '_errorStackFormatter': {
141
130
  writable: false,
142
131
  enumerable: false,
@@ -160,7 +149,6 @@ class Logger {
160
149
  writer: this._writer,
161
150
  pid: this._pid,
162
151
  formatter: this._formatter,
163
- rootPath: this._rootPath,
164
152
  errorStackFormatter: this._errorStackFormatter,
165
153
  }, extraOpts || {}));
166
154
  }
@@ -225,5 +213,3 @@ class Logger {
225
213
  });
226
214
  }
227
215
  }
228
-
229
- module.exports = { Logger };
@@ -0,0 +1,2 @@
1
+ export * from './model';
2
+ export * from './migration-model';
@@ -0,0 +1,2 @@
1
+ export * from './model.mjs';
2
+ export * from './migration-model.mjs';
@@ -1,12 +1,9 @@
1
- 'use strict';
2
-
3
- const { Types } = require('mythix-orm');
4
- const { registerModel } = require('./model-utils');
5
- const { Model } = require('./model');
1
+ import { Types } from 'mythix-orm';
2
+ import { Model } from './model.mjs';
6
3
 
7
4
  const ID_STRING_MAX_SIZE = 15;
8
5
 
9
- class Migration extends Model {
6
+ export class Migration extends Model {
10
7
  static fields = {
11
8
  id: {
12
9
  type: Types.STRING(ID_STRING_MAX_SIZE),
@@ -28,8 +25,3 @@ class Migration extends Model {
28
25
  },
29
26
  };
30
27
  }
31
-
32
- const modelRegisterFactory = registerModel(Migration);
33
- modelRegisterFactory.MigrationModel = Migration;
34
-
35
- module.exports = modelRegisterFactory;
@@ -0,0 +1,10 @@
1
+ import { Application } from '../application';
2
+ import { Logger } from '../logger';
3
+ import { ConnectionBase, Model as _Model } from 'mythix-orm';
4
+
5
+ export declare class Model extends _Model {
6
+ declare public static _getConnection: (connection?: ConnectionBase) => ConnectionBase;
7
+
8
+ public getApplication(): Application;
9
+ public getLogger(): Logger;
10
+ }
@@ -0,0 +1,28 @@
1
+ import MythixORM from 'mythix-orm';
2
+
3
+ export class Model extends MythixORM.Model {
4
+ getApplication() {
5
+ return this.constructor.getApplication();
6
+ }
7
+
8
+ getLogger() {
9
+ let application = this.getApplication();
10
+ return application.getLogger();
11
+ }
12
+
13
+ static _getConnection(_connection) {
14
+ let connection = super._getConnection(_connection);
15
+ if (connection)
16
+ return connection;
17
+
18
+ return this.getApplication().getConnection();
19
+ }
20
+
21
+ static getModels() {
22
+ let connection = this._getConnection();
23
+ if (!connection)
24
+ return {};
25
+
26
+ return connection.getModels();
27
+ }
28
+ }
@@ -1,8 +1,8 @@
1
1
  import { ConnectionBase } from 'mythix-orm';
2
2
  import { GenericObject } from '../interfaces/common';
3
- import { BaseModule } from './base-module';
3
+ import { ModuleBase } from './module-base';
4
4
 
5
- export declare class DatabaseModule extends BaseModule {
5
+ export declare class DatabaseModule extends ModuleBase {
6
6
  public getDatabaseConfig(): GenericObject;
7
7
  public getConfig(): GenericObject;
8
8
  public getTablePrefix(): string | null;
@@ -1,12 +1,10 @@
1
- 'use strict';
1
+ import Nife from 'nife';
2
+ import { ConnectionBase } from 'mythix-orm';
3
+ import { ModuleBase } from './module-base.mjs';
2
4
 
3
- const Nife = require('nife');
4
- const { BaseModule } = require('../modules/base-module');
5
- const { ConnectionBase } = require('mythix-orm');
6
-
7
- class DatabaseModule extends BaseModule {
8
- static getModuleName() {
9
- return 'DatabaseModule';
5
+ export class DatabaseModule extends ModuleBase {
6
+ static getOptionsScopeName() {
7
+ return 'database';
10
8
  }
11
9
 
12
10
  static shouldUse(application, options) {
@@ -107,6 +105,17 @@ class DatabaseModule extends BaseModule {
107
105
  dbConnectionString = `${databaseConfig.dialect}://${databaseConfig.host}:${databaseConfig.port || '<default port>'}/${databaseConfig.database}`;
108
106
 
109
107
  try {
108
+ const registerModelsAndStartConnection = (connection) => {
109
+ let models = app.getAppModelClasses(connection);
110
+ if (models)
111
+ connection.registerModels(models, { bindModels: false });
112
+
113
+ if (connection.isStarted())
114
+ return;
115
+
116
+ return connection.start();
117
+ };
118
+
110
119
  let app = this.getApplication();
111
120
  if (typeof app.createDatabaseConnection !== 'function')
112
121
  throw new Error('DatabaseModule::connectToDatabase: You must define a "createDatabaseConnection" method on your Application class.');
@@ -118,12 +127,10 @@ class DatabaseModule extends BaseModule {
118
127
  if (!(connection instanceof ConnectionBase) && typeof connection === 'function' && connection.prototype instanceof ConnectionBase) {
119
128
  const ConnectionKlass = connection;
120
129
  connection = new ConnectionKlass(databaseConfig);
121
-
122
- await connection.start();
123
- } else if (!connection.isStarted()) {
124
- await connection.start();
125
130
  }
126
131
 
132
+ await registerModelsAndStartConnection(connection);
133
+
127
134
  this.getLogger().info(`Connection to ${dbConnectionString} has been established successfully!`);
128
135
 
129
136
  return connection;
@@ -155,5 +162,3 @@ class DatabaseModule extends BaseModule {
155
162
  this.getLogger().info('All database connections closed successfully!');
156
163
  }
157
164
  }
158
-
159
- module.exports = { DatabaseModule };
@@ -0,0 +1,2 @@
1
+ export * from './module-base';
2
+ export * from './database-module';
@@ -0,0 +1,2 @@
1
+ export * from './module-base.mjs';
2
+ export * from './database-module.mjs';
@@ -2,20 +2,13 @@ import { GenericObject } from '../interfaces/common';
2
2
  import { Application } from '../application';
3
3
  import { Logger } from '../logger';
4
4
 
5
- export declare type BaseModuleClass = typeof BaseModule;
6
-
7
- export declare type ModuleClasses = Array<BaseModuleClass>;
8
- export declare type Modules = Array<BaseModule>;
9
-
10
- export declare class BaseModule {
11
- public declare static fileWatcherQueueName: string;
5
+ export declare type Modules = Array<ModuleBase>;
12
6
 
7
+ export declare class ModuleBase {
13
8
  public static getModuleName(): string;
9
+ public static getOptionsScopeName(): string;
14
10
  public static shouldUse(application: Application, options: GenericObject): boolean;
15
11
 
16
- public fileWatcherGetMonitorPaths(options?: GenericObject): Array<string>;
17
- public fileWatcherHandler(options?: GenericObject): Promise<void>;
18
-
19
12
  public constructor(application: Application);
20
13
  public getApplication(): Application;
21
14
  public getLogger(): Logger;
@@ -1,8 +1,10 @@
1
- 'use strict';
2
-
3
- class BaseModule {
1
+ export class ModuleBase {
4
2
  static getModuleName() {
5
- throw new Error('BaseModule::getModuleName: Should not have been called. Child module must implement "static getModuleName(){}"');
3
+ return this.name;
4
+ }
5
+
6
+ static getOptionsScopeName() {
7
+ return this.getModuleName().toLowerCase().replace(/module$/i, '');
6
8
  }
7
9
 
8
10
  static shouldUse() {
@@ -17,9 +19,19 @@ class BaseModule {
17
19
  configurable: true,
18
20
  value: application,
19
21
  },
22
+ 'options': {
23
+ enumerable: false,
24
+ configurable: true,
25
+ get: () => (application.getOptions() || {})[this.constructor.getOptionsScopeName()] || {},
26
+ set: () => {},
27
+ },
20
28
  });
21
29
  }
22
30
 
31
+ getOptions() {
32
+ return this.options;
33
+ }
34
+
23
35
  getApplication() {
24
36
  return this.application;
25
37
  }
@@ -39,5 +51,3 @@ class BaseModule {
39
51
  async stop() {
40
52
  }
41
53
  }
42
-
43
- module.exports = { BaseModule };
@@ -0,0 +1,2 @@
1
+ export * from './task-base';
2
+ export * from './task-module';
@@ -0,0 +1,2 @@
1
+ export * from './task-base.mjs';
2
+ export * from './task-module.mjs';
@@ -2,6 +2,7 @@ import { GenericObject } from '../interfaces/common';
2
2
  import { Application } from '../application';
3
3
  import { Logger } from '../logger';
4
4
  import { ConnectionBase, ModelClass, Models } from 'mythix-orm';
5
+ import { DateTime } from 'luxon';
5
6
 
6
7
  export declare type Tasks = { [key: string]: TaskBase };
7
8
 
@@ -18,30 +19,19 @@ export declare interface TaskInfo {
18
19
  }
19
20
 
20
21
  export declare class TaskBase {
21
- public static onTaskClassCreate(Klass: TaskClass): TaskClass;
22
-
23
- public static getFrequency(taskIndex?: number): number;
24
- public static getStartDelay(taskIndex?: number): number;
25
- public static shouldRun(taskIndex: number, lastTime: number | null, currentTime: number, diff: number): boolean;
22
+ public static getWorkerCount(): number;
23
+ public static getTaskName(): string;
24
+ public static nextRun(taskInstance: TaskBase, lastTime: null | number, currentTime: number, diff: null | number): DateTime;
26
25
 
27
26
  declare public application: Application;
28
27
  declare public logger: Logger;
29
- declare public runID: string;
30
28
 
31
29
  public constructor(application: Application, logger: Logger, runID: string);
32
30
  public start(options?: GenericObject): Promise<any>;
33
31
  public stop(): Promise<any>;
34
32
  public getApplication(): Application;
35
33
  public getLogger(): Logger;
36
- public getRunID(): string;
37
- public getNumberOfWorkers(): number;
38
34
  public getModel(name: string): ModelClass;
39
35
  public getModels(): Models;
40
36
  public getConnection(connection?: ConnectionBase): ConnectionBase;
41
-
42
- // Deprecated
43
- public getDBConnection(connection?: ConnectionBase): ConnectionBase;
44
-
45
- public getFrequency(taskIndex?: number): number;
46
- public getStartDelay(taskIndex?: number): number;
47
37
  }
@@ -0,0 +1,73 @@
1
+ import { DateTime } from 'luxon';
2
+
3
+ export class TaskBase {
4
+ static getWorkerCount() {
5
+ return 1;
6
+ }
7
+
8
+ static getTaskName() {
9
+ return this.name.toLowerCase().replace(/task$/i, '');
10
+ }
11
+
12
+ // eslint-disable-next-line no-unused-vars
13
+ static nextRun(taskInstance, lastTime, currentTime, diff) {
14
+ return DateTime.now().plus({ seconds: 60 });
15
+ }
16
+
17
+ constructor(application, logger, workerIndex) {
18
+ Object.defineProperties(this, {
19
+ 'application': {
20
+ writable: false,
21
+ enumerable: false,
22
+ configurable: true,
23
+ value: application,
24
+ },
25
+ 'logger': {
26
+ writable: false,
27
+ enumerable: false,
28
+ configurable: true,
29
+ value: logger,
30
+ },
31
+ 'workerIndex': {
32
+ writable: false,
33
+ enumerable: false,
34
+ configurable: true,
35
+ value: workerIndex || 0,
36
+ },
37
+ });
38
+ }
39
+
40
+ async start() {
41
+ }
42
+
43
+ async stop() {
44
+
45
+ }
46
+
47
+ getApplication() {
48
+ return this.application;
49
+ }
50
+
51
+ getLogger() {
52
+ if (this.logger)
53
+ return this.logger;
54
+
55
+ let application = this.getApplication();
56
+ return application.getLogger();
57
+ }
58
+
59
+ getModel(name) {
60
+ let application = this.getApplication();
61
+ return application.getModel(name);
62
+ }
63
+
64
+ getModels() {
65
+ let application = this.getApplication();
66
+ return application.getModels();
67
+ }
68
+
69
+ getConnection(connection) {
70
+ let application = this.getApplication();
71
+ return application.getConnection(connection);
72
+ }
73
+ }
@@ -1,7 +1,7 @@
1
- import { BaseModule } from '../modules/base-module';
1
+ import { ModuleBase } from '../modules/module-base';
2
2
  import { Tasks, TaskInfo } from './task-base';
3
3
 
4
- export declare class TaskModule extends BaseModule {
4
+ export declare class TaskModule extends ModuleBase {
5
5
  public getTaskFilePaths(tasksPath: string): Array<string>;
6
6
  public loadTasks(tasksPath: string): Tasks;
7
7
  public runTasks(): Promise<void>;
@@ -13,5 +13,5 @@ export declare class TaskModule extends BaseModule {
13
13
  public waitForAllTasksToFinish(): Promise<void>;
14
14
 
15
15
  declare public tasks: Tasks;
16
- declare public taskInfo: { [ key: string ]: TaskInfo };
16
+ declare public taskInfo: { [key: string]: TaskInfo };
17
17
  }