@adaptivestone/framework 5.0.0-beta.1 → 5.0.0-beta.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.
- package/CHANGELOG.md +6 -0
- package/Cli.js +1 -1
- package/commands/GenerateRandomBytes.js +2 -0
- package/modules/AbstractCommand.js +15 -0
- package/modules/AbstractModel.js +15 -9
- package/modules/BaseCli.js +17 -4
- package/package.json +1 -1
- package/server.js +28 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
### 5.0.0-beta.2
|
|
2
|
+
|
|
3
|
+
[UPDATE] update deps
|
|
4
|
+
[NEW] add ability to skip mongo model init in CLI env
|
|
5
|
+
[NEW] now each mongo connection on CLI have own name and inslude command name there too (getMongoConnectionName in command)
|
|
6
|
+
|
|
1
7
|
### 5.0.0-beta.1
|
|
2
8
|
|
|
3
9
|
[UPDATE] update deps
|
package/Cli.js
CHANGED
|
@@ -12,7 +12,7 @@ class Cli extends BaseCli {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
async run() {
|
|
15
|
-
await this.server.init();
|
|
15
|
+
await this.server.init({ isSkipModelInit: true, isSkipModelLoading: true });
|
|
16
16
|
const command = process.argv[2]?.toLowerCase();
|
|
17
17
|
await super.run(command, this.args);
|
|
18
18
|
this.app.events.emit('shutdown');
|
|
@@ -11,6 +11,21 @@ class AbstractCommand extends Base {
|
|
|
11
11
|
return 'Command description. PLEASE PROVIDE IT';
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* If true, then this command will load models and init mongo connection
|
|
16
|
+
*/
|
|
17
|
+
static isShouldInitModels = true;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Get mongo connection name
|
|
21
|
+
* @param {String} commandName
|
|
22
|
+
* @param {array} args
|
|
23
|
+
* @returns string
|
|
24
|
+
*/
|
|
25
|
+
static getMongoConnectionName(commandName, args) {
|
|
26
|
+
return `CLI: ${commandName} ${args.join(' ')}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
14
29
|
/**
|
|
15
30
|
* Entry point to every command. This method should be overridden
|
|
16
31
|
* @return {Promise<boolean>} resut
|
package/modules/AbstractModel.js
CHANGED
|
@@ -37,17 +37,23 @@ class AbstractModel extends Base {
|
|
|
37
37
|
}
|
|
38
38
|
// await mongoose.disconnect(); // TODO it have problems with replica-set
|
|
39
39
|
});
|
|
40
|
+
const connectionParams = {};
|
|
41
|
+
if (process.env.MONGO_APP_NAME) {
|
|
42
|
+
connectionParams.appName = process.env.MONGO_APP_NAME;
|
|
43
|
+
}
|
|
40
44
|
// do not connect on test
|
|
41
|
-
mongoose
|
|
42
|
-
()
|
|
43
|
-
|
|
45
|
+
mongoose
|
|
46
|
+
.connect(this.app.getConfig('mongo').connectionString, connectionParams)
|
|
47
|
+
.then(
|
|
48
|
+
() => {
|
|
49
|
+
this.logger.info('Mongo connection success');
|
|
44
50
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
callback();
|
|
52
|
+
},
|
|
53
|
+
(error) => {
|
|
54
|
+
this.logger.error("Can't install mongodb connection", error);
|
|
55
|
+
},
|
|
56
|
+
);
|
|
51
57
|
} else {
|
|
52
58
|
callback();
|
|
53
59
|
}
|
package/modules/BaseCli.js
CHANGED
|
@@ -29,8 +29,8 @@ class Cli extends Base {
|
|
|
29
29
|
return true;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
async
|
|
33
|
-
const commands = Object.keys(this.commands);
|
|
32
|
+
async printCommandTable() {
|
|
33
|
+
const commands = Object.keys(this.commands).sort();
|
|
34
34
|
const maxLength = commands.reduce((max, c) => Math.max(max, c.length), 0);
|
|
35
35
|
console.log('Available commands:');
|
|
36
36
|
let commandsClasses = [];
|
|
@@ -55,17 +55,30 @@ class Cli extends Base {
|
|
|
55
55
|
|
|
56
56
|
if (!command) {
|
|
57
57
|
console.log('Please provide command name');
|
|
58
|
-
await this.
|
|
58
|
+
await this.printCommandTable();
|
|
59
59
|
return false;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
if (!this.commands[command]) {
|
|
63
63
|
console.log(`Command ${command} not found `);
|
|
64
|
-
await this.
|
|
64
|
+
await this.printCommandTable();
|
|
65
65
|
return false;
|
|
66
66
|
}
|
|
67
67
|
const { default: Command } = await import(this.commands[command]);
|
|
68
68
|
|
|
69
|
+
if (Command.isShouldInitModels) {
|
|
70
|
+
this.logger.debug(
|
|
71
|
+
`Command ${command} isShouldInitModels called. If you want to skip loading and init models, please set isShouldInitModels to false in tyou command`,
|
|
72
|
+
);
|
|
73
|
+
process.env.MONGO_APP_NAME = Command.getMongoConnectionName(
|
|
74
|
+
command,
|
|
75
|
+
args,
|
|
76
|
+
);
|
|
77
|
+
await this.server.initAllModels();
|
|
78
|
+
} else {
|
|
79
|
+
this.logger.debug(`Command ${command} NOT need to isShouldInitModels`);
|
|
80
|
+
}
|
|
81
|
+
|
|
69
82
|
const c = new Command(this.app, this.commands, args);
|
|
70
83
|
let result = false;
|
|
71
84
|
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -24,6 +24,10 @@ class Server {
|
|
|
24
24
|
|
|
25
25
|
#isInited = false;
|
|
26
26
|
|
|
27
|
+
#isModelsInited = false;
|
|
28
|
+
|
|
29
|
+
#isModelsLoaded = false;
|
|
30
|
+
|
|
27
31
|
cli = null;
|
|
28
32
|
|
|
29
33
|
/**
|
|
@@ -93,14 +97,18 @@ class Server {
|
|
|
93
97
|
* Do an initialization (config reading, etc)
|
|
94
98
|
* @returns {Promise}
|
|
95
99
|
*/
|
|
96
|
-
async init({ isSkipModelInit = false } = {}) {
|
|
100
|
+
async init({ isSkipModelInit = false, isSkipModelLoading = false } = {}) {
|
|
97
101
|
if (this.#isInited) {
|
|
98
102
|
return true;
|
|
99
103
|
}
|
|
100
104
|
|
|
101
105
|
console.time('Server init. Done');
|
|
102
106
|
console.time('Loading config and model files. Time');
|
|
103
|
-
|
|
107
|
+
const prom = [this.#initConfigFiles()];
|
|
108
|
+
if (!isSkipModelLoading) {
|
|
109
|
+
prom.push(this.#loadModelFiles());
|
|
110
|
+
}
|
|
111
|
+
await Promise.all(prom);
|
|
104
112
|
console.timeEnd('Loading config and model files. Time');
|
|
105
113
|
|
|
106
114
|
if (!isSkipModelInit) {
|
|
@@ -119,7 +127,15 @@ class Server {
|
|
|
119
127
|
* @returns {Promise}
|
|
120
128
|
*/
|
|
121
129
|
async initAllModels() {
|
|
122
|
-
|
|
130
|
+
if (this.#isModelsInited) {
|
|
131
|
+
// already inited
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const now = performance.now();
|
|
135
|
+
|
|
136
|
+
if (!this.#isModelsLoaded) {
|
|
137
|
+
await this.#loadModelFiles();
|
|
138
|
+
}
|
|
123
139
|
|
|
124
140
|
if (this.app.getConfig('mongo').connectionString) {
|
|
125
141
|
for (const [modelName, ModelConstructor] of this.cache
|
|
@@ -140,7 +156,10 @@ class Server {
|
|
|
140
156
|
);
|
|
141
157
|
}
|
|
142
158
|
|
|
143
|
-
|
|
159
|
+
this.app.logger.debug(
|
|
160
|
+
`Inited models in ${(performance.now() - now).toFixed(2)}ms`,
|
|
161
|
+
);
|
|
162
|
+
this.#isModelsInited = true;
|
|
144
163
|
}
|
|
145
164
|
|
|
146
165
|
async #initConfigFiles() {
|
|
@@ -198,6 +217,10 @@ class Server {
|
|
|
198
217
|
}
|
|
199
218
|
|
|
200
219
|
async #loadModelFiles() {
|
|
220
|
+
if (this.#isModelsLoaded) {
|
|
221
|
+
// already inited
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
201
224
|
const dirname = url.fileURLToPath(new URL('.', import.meta.url));
|
|
202
225
|
const files = await getFilesPathWithInheritance({
|
|
203
226
|
internalFolder: path.join(dirname, '/models'),
|
|
@@ -223,6 +246,7 @@ class Server {
|
|
|
223
246
|
for (const model of loadedModels) {
|
|
224
247
|
this.cache.modelConstructors.set(model.name, model.file.default);
|
|
225
248
|
}
|
|
249
|
+
this.#isModelsLoaded = true;
|
|
226
250
|
return true;
|
|
227
251
|
}
|
|
228
252
|
|