@villedemontreal/mongo 6.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/LICENSE +22 -0
- package/README.md +226 -0
- package/dist/src/config/configs.d.ts +16 -0
- package/dist/src/config/configs.js +26 -0
- package/dist/src/config/configs.js.map +1 -0
- package/dist/src/config/constants.d.ts +85 -0
- package/dist/src/config/constants.js +104 -0
- package/dist/src/config/constants.js.map +1 -0
- package/dist/src/config/init.d.ts +9 -0
- package/dist/src/config/init.js +24 -0
- package/dist/src/config/init.js.map +1 -0
- package/dist/src/config/mongooseConfigs.d.ts +73 -0
- package/dist/src/config/mongooseConfigs.js +107 -0
- package/dist/src/config/mongooseConfigs.js.map +1 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.js +24 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/mongoClient.d.ts +19 -0
- package/dist/src/mongoClient.js +111 -0
- package/dist/src/mongoClient.js.map +1 -0
- package/dist/src/mongoUpdater.d.ts +103 -0
- package/dist/src/mongoUpdater.js +297 -0
- package/dist/src/mongoUpdater.js.map +1 -0
- package/dist/src/mongoUpdater.test.d.ts +1 -0
- package/dist/src/mongoUpdater.test.js +232 -0
- package/dist/src/mongoUpdater.test.js.map +1 -0
- package/dist/src/mongoUtils.d.ts +68 -0
- package/dist/src/mongoUtils.js +280 -0
- package/dist/src/mongoUtils.js.map +1 -0
- package/dist/src/mongoUtils.test.d.ts +1 -0
- package/dist/src/mongoUtils.test.js +24 -0
- package/dist/src/mongoUtils.test.js.map +1 -0
- package/dist/src/plugins/pagination/index.d.ts +11 -0
- package/dist/src/plugins/pagination/index.js +79 -0
- package/dist/src/plugins/pagination/index.js.map +1 -0
- package/dist/src/plugins/pagination/index.test.d.ts +1 -0
- package/dist/src/plugins/pagination/index.test.js +129 -0
- package/dist/src/plugins/pagination/index.test.js.map +1 -0
- package/dist/src/plugins/pagination/specs/IPaginateOptions.d.ts +51 -0
- package/dist/src/plugins/pagination/specs/IPaginateOptions.js +3 -0
- package/dist/src/plugins/pagination/specs/IPaginateOptions.js.map +1 -0
- package/dist/src/utils/logger.d.ts +11 -0
- package/dist/src/utils/logger.js +54 -0
- package/dist/src/utils/logger.js.map +1 -0
- package/dist/src/utils/testingConfigurations.d.ts +8 -0
- package/dist/src/utils/testingConfigurations.js +17 -0
- package/dist/src/utils/testingConfigurations.js.map +1 -0
- package/dist/tests/testingMongoUpdates/1.0.0.d.ts +5 -0
- package/dist/tests/testingMongoUpdates/1.0.0.js +27 -0
- package/dist/tests/testingMongoUpdates/1.0.0.js.map +1 -0
- package/dist/tests/testingMongoUpdates/1.0.1.d.ts +5 -0
- package/dist/tests/testingMongoUpdates/1.0.1.js +22 -0
- package/dist/tests/testingMongoUpdates/1.0.1.js.map +1 -0
- package/package.json +63 -0
- package/src/config/configs.ts +27 -0
- package/src/config/constants.ts +122 -0
- package/src/config/init.ts +23 -0
- package/src/config/mongooseConfigs.ts +178 -0
- package/src/index.ts +13 -0
- package/src/mongoClient.ts +122 -0
- package/src/mongoUpdater.test.ts +286 -0
- package/src/mongoUpdater.ts +423 -0
- package/src/mongoUtils.test.ts +23 -0
- package/src/mongoUtils.ts +322 -0
- package/src/plugins/pagination/index.test.ts +140 -0
- package/src/plugins/pagination/index.ts +96 -0
- package/src/plugins/pagination/specs/IPaginateOptions.ts +51 -0
- package/src/utils/logger.ts +53 -0
- package/src/utils/testingConfigurations.ts +13 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MongoUpdater = void 0;
|
|
4
|
+
const general_utils_1 = require("@villedemontreal/general-utils");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const lodash_1 = require("lodash");
|
|
7
|
+
const semver = require("semver");
|
|
8
|
+
const constants_1 = require("./config/constants");
|
|
9
|
+
const logger_1 = require("./utils/logger");
|
|
10
|
+
const logger = logger_1.createLogger('MongoUpdater');
|
|
11
|
+
class MongoUpdater {
|
|
12
|
+
constructor(mongoDb,
|
|
13
|
+
/**
|
|
14
|
+
* The *relative* path to the directory where the
|
|
15
|
+
* update files are.
|
|
16
|
+
*/
|
|
17
|
+
mongoSchemaUpdatesDirPath, lockMaxAgeSeconds, appSchemaCollectionName) {
|
|
18
|
+
this.mongoDb = mongoDb;
|
|
19
|
+
this.mongoSchemaUpdatesDirPath = mongoSchemaUpdatesDirPath;
|
|
20
|
+
this.lockMaxAgeSeconds = lockMaxAgeSeconds;
|
|
21
|
+
this.appSchemaCollectionName = appSchemaCollectionName;
|
|
22
|
+
this.checkUpdates = async () => {
|
|
23
|
+
logger.info('Checking for db app schema updates:');
|
|
24
|
+
let lockAcquired = false;
|
|
25
|
+
const targetVersion = this.findMongoAppSchemaTargetVersion();
|
|
26
|
+
try {
|
|
27
|
+
while (true) {
|
|
28
|
+
// ==========================================
|
|
29
|
+
// Checks if the appSchema version has to be
|
|
30
|
+
// updated.
|
|
31
|
+
// ==========================================
|
|
32
|
+
const currentAppSchemaVersion = await this.getAppSchemaVersion();
|
|
33
|
+
if (semver.gte(currentAppSchemaVersion, targetVersion)) {
|
|
34
|
+
// tslint:disable-next-line: prefer-template
|
|
35
|
+
logger.info(' > Current database app schema is up to date : ' + currentAppSchemaVersion + ').');
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (lockAcquired) {
|
|
39
|
+
logger.info(` > Applying some required updates...`);
|
|
40
|
+
await this.applyAppSchemaUpdates(currentAppSchemaVersion, targetVersion);
|
|
41
|
+
await this.updateAppSchemaVersion(currentAppSchemaVersion, targetVersion);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
// ==========================================
|
|
45
|
+
// Tries to get the lock. Will do this as long
|
|
46
|
+
// as the lock can't be acquired (ie : it is
|
|
47
|
+
// released or is too old) or as long as the appSchema
|
|
48
|
+
// version still is not up to date.
|
|
49
|
+
// ==========================================
|
|
50
|
+
lockAcquired = await this.lockAppSchemaDocument();
|
|
51
|
+
if (!lockAcquired) {
|
|
52
|
+
const wait = 1000;
|
|
53
|
+
logger.warning(`The lock can't be acquired. The maximum age it can be before being considered ` +
|
|
54
|
+
`to be too old is ${this.lockMaxAgeSeconds} seconds. Waiting for ${wait} milliseconds...`);
|
|
55
|
+
await general_utils_1.utils.sleep(wait);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
logger.info(` > Lock acquired.`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
if (lockAcquired) {
|
|
64
|
+
await this.unlockAppSchemaDocument();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
async installAppSchemaCollection() {
|
|
70
|
+
try {
|
|
71
|
+
// Installing the "appSchema" collection.
|
|
72
|
+
// tslint:disable-next-line: prefer-template
|
|
73
|
+
logger.info(' > Installing the "' + this.appSchemaCollectionName + '" collection.');
|
|
74
|
+
const collection = await this.mongoDb.createCollection(this.appSchemaCollectionName);
|
|
75
|
+
// ==========================================
|
|
76
|
+
// Makes sure only one appSchema document exists.
|
|
77
|
+
// ==========================================
|
|
78
|
+
await collection.createIndexes([
|
|
79
|
+
{
|
|
80
|
+
key: {
|
|
81
|
+
name: 1
|
|
82
|
+
},
|
|
83
|
+
name: 'name_1',
|
|
84
|
+
unique: true
|
|
85
|
+
}
|
|
86
|
+
]);
|
|
87
|
+
// ==========================================
|
|
88
|
+
// Inserts the first version of the AppSchema document
|
|
89
|
+
// ==========================================
|
|
90
|
+
await collection.insertOne({
|
|
91
|
+
name: 'singleton',
|
|
92
|
+
version: '0.0.0',
|
|
93
|
+
lock: false,
|
|
94
|
+
lockTimestamp: 0
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
// ==========================================
|
|
99
|
+
// Maybe the error occured because another app
|
|
100
|
+
// was also trying to create the collection?
|
|
101
|
+
// ==========================================
|
|
102
|
+
const maxWaitMilliseconds = 10 * 1000;
|
|
103
|
+
const start = new general_utils_1.Timer();
|
|
104
|
+
while (start.getMillisecondsElapsed() < maxWaitMilliseconds) {
|
|
105
|
+
await general_utils_1.utils.sleep(1000);
|
|
106
|
+
const appSchemaCollection = await this.getAppSchemaCollection();
|
|
107
|
+
if (!appSchemaCollection) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const document = await appSchemaCollection.findOne({});
|
|
111
|
+
if (!document) {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
// ==========================================
|
|
115
|
+
// We ignore the error!
|
|
116
|
+
// ==========================================
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
// ==========================================
|
|
120
|
+
// Throws the error...
|
|
121
|
+
// ==========================================
|
|
122
|
+
throw err;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async updateAppSchemaVersion(currentVersion, newVersion) {
|
|
126
|
+
const appSchemaCollection = await this.getAppSchemaCollection();
|
|
127
|
+
await appSchemaCollection.updateOne({}, { $set: { version: newVersion } });
|
|
128
|
+
// tslint:disable-next-line: prefer-template
|
|
129
|
+
logger.info(' > MongoDB App Schema updagred from version ' + currentVersion + ' to version ' + newVersion);
|
|
130
|
+
}
|
|
131
|
+
async getAppSchemaUpdateFiles(currentAppSchemaVersion, targetAppSchemaVersion) {
|
|
132
|
+
return new Promise((resolve, reject) => {
|
|
133
|
+
fs.readdir(this.getAppSchemaFilesDirPath(), (err, files) => {
|
|
134
|
+
if (err) {
|
|
135
|
+
return reject(err);
|
|
136
|
+
}
|
|
137
|
+
let filesClean = files;
|
|
138
|
+
try {
|
|
139
|
+
filesClean = filesClean
|
|
140
|
+
.filter(name => {
|
|
141
|
+
return name.match(/\.js$/) !== null;
|
|
142
|
+
})
|
|
143
|
+
.map(name => {
|
|
144
|
+
return name.split('.js')[0];
|
|
145
|
+
})
|
|
146
|
+
.filter(updateFileVersion => {
|
|
147
|
+
if (semver.gt(updateFileVersion, currentAppSchemaVersion) &&
|
|
148
|
+
semver.lte(updateFileVersion, targetAppSchemaVersion)) {
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
return false;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
catch (err2) {
|
|
155
|
+
return reject(err2);
|
|
156
|
+
}
|
|
157
|
+
return resolve(filesClean.sort(semver.compare));
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
async applyAppSchemaUpdates(currentVersion, newVersion) {
|
|
162
|
+
const updateFileNames = await this.getAppSchemaUpdateFiles(currentVersion, newVersion);
|
|
163
|
+
if (updateFileNames.length > 0) {
|
|
164
|
+
for (const updateFileName of updateFileNames) {
|
|
165
|
+
logger.info(' > Pending app schema update: ' + updateFileName);
|
|
166
|
+
// tslint:disable-next-line: prefer-template
|
|
167
|
+
const updateFilePath = this.getAppSchemaFilesDirPath() + '/' + updateFileName;
|
|
168
|
+
let updateFunction;
|
|
169
|
+
try {
|
|
170
|
+
updateFunction = require(updateFilePath).default;
|
|
171
|
+
}
|
|
172
|
+
catch (e) {
|
|
173
|
+
return Promise.reject(e);
|
|
174
|
+
}
|
|
175
|
+
if (!lodash_1.isFunction(updateFunction)) {
|
|
176
|
+
return Promise.reject('The default export for an app schema update file must be a function! Was not for file : ' + updateFilePath);
|
|
177
|
+
}
|
|
178
|
+
await updateFunction(this.mongoDb);
|
|
179
|
+
}
|
|
180
|
+
logger.info('All app schema updates done');
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async getAppSchemaCollection() {
|
|
184
|
+
return await this.mongoDb.collection(this.appSchemaCollectionName);
|
|
185
|
+
}
|
|
186
|
+
async getAppSchemaVersion() {
|
|
187
|
+
const appSchemaCollection = await this.getAppSchemaCollection();
|
|
188
|
+
const documents = await appSchemaCollection.find().toArray();
|
|
189
|
+
if (documents.length > 0) {
|
|
190
|
+
return documents[0].version;
|
|
191
|
+
}
|
|
192
|
+
return '0.0.0';
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Tries to get the lock to modify Mongo's schemas.
|
|
196
|
+
*
|
|
197
|
+
* If a lock already exists, checks if it is too old.
|
|
198
|
+
* If too old, will create a new one... This is to prevents
|
|
199
|
+
* situations where a lock would have been taken by an app
|
|
200
|
+
* but that app *crashed* while the lock was on. We don't want
|
|
201
|
+
* suck lock to be active forever...
|
|
202
|
+
*
|
|
203
|
+
*/
|
|
204
|
+
async lockAppSchemaDocument() {
|
|
205
|
+
const appSchemaCollection = await this.getAppSchemaCollection();
|
|
206
|
+
let document = await appSchemaCollection.findOneAndUpdate({ lock: false }, {
|
|
207
|
+
$set: {
|
|
208
|
+
lock: true,
|
|
209
|
+
lockTimestamp: new Date().getTime()
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
if (document.value !== null) {
|
|
213
|
+
logger.info(` > Succesfully locked the ${this.appSchemaCollectionName} document`);
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
document = await appSchemaCollection.findOne({ lock: true });
|
|
217
|
+
if (document === null) {
|
|
218
|
+
// try again!
|
|
219
|
+
return this.lockAppSchemaDocument();
|
|
220
|
+
}
|
|
221
|
+
// ==========================================
|
|
222
|
+
// Checks the existing lock's timestamp
|
|
223
|
+
// ==========================================
|
|
224
|
+
const lockTimestamp = document.lockTimestamp;
|
|
225
|
+
const nowTimestamp = new Date().getTime();
|
|
226
|
+
const lockAgeMilliSeconds = nowTimestamp - lockTimestamp;
|
|
227
|
+
// ==========================================
|
|
228
|
+
// Lock is too old! We overwrite it....
|
|
229
|
+
// ==========================================
|
|
230
|
+
if (lockAgeMilliSeconds > this.lockMaxAgeSeconds * 1000) {
|
|
231
|
+
document = await appSchemaCollection.findOneAndUpdate({ lockTimestamp }, {
|
|
232
|
+
$set: {
|
|
233
|
+
lock: true,
|
|
234
|
+
lockTimestamp: new Date().getTime()
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
// ==========================================
|
|
238
|
+
// *Just* taken by another app!
|
|
239
|
+
// ==========================================
|
|
240
|
+
if (document.value === null) {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
// ==========================================
|
|
246
|
+
// The existing lock is still valid...
|
|
247
|
+
// We can't get it.
|
|
248
|
+
// ==========================================
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
async unlockAppSchemaDocument() {
|
|
252
|
+
const appSchemaCollection = await this.getAppSchemaCollection();
|
|
253
|
+
const document = await appSchemaCollection.findOneAndUpdate({ lock: true }, {
|
|
254
|
+
$set: {
|
|
255
|
+
lock: false,
|
|
256
|
+
lockTimestamp: 0
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
if (document.value !== null) {
|
|
260
|
+
logger.info(` > Succesfully unlocked the ${this.appSchemaCollectionName} document`);
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
logger.info(`> The ${this.appSchemaCollectionName} document was not locked`);
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
async checkInstallation() {
|
|
267
|
+
logger.info(`Validating that the "${this.appSchemaCollectionName}" collection required by the application has been installed.`);
|
|
268
|
+
const collections = await this.mongoDb.listCollections({ name: this.appSchemaCollectionName }).toArray();
|
|
269
|
+
if (collections.length === 0) {
|
|
270
|
+
logger.info(` > The "${this.appSchemaCollectionName}" collection was not found... Starting a new installation.`);
|
|
271
|
+
await this.installAppSchemaCollection();
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
logger.info(` > The "${this.appSchemaCollectionName}" collection was found. No installation required.`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
getAppSchemaFilesDirPath() {
|
|
278
|
+
return constants_1.constants.appRoot + this.mongoSchemaUpdatesDirPath;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Finds the latest Mongo update file version.
|
|
282
|
+
*/
|
|
283
|
+
findMongoAppSchemaTargetVersion() {
|
|
284
|
+
let targetVersion = '0.0.0';
|
|
285
|
+
fs.readdirSync(this.getAppSchemaFilesDirPath()).forEach(fileName => {
|
|
286
|
+
if (fileName.endsWith('.js')) {
|
|
287
|
+
const version = fileName.split('.js')[0];
|
|
288
|
+
if (semver.gt(version, targetVersion)) {
|
|
289
|
+
targetVersion = version;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
return targetVersion;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
exports.MongoUpdater = MongoUpdater;
|
|
297
|
+
//# sourceMappingURL=mongoUpdater.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongoUpdater.js","sourceRoot":"","sources":["../../src/mongoUpdater.ts"],"names":[],"mappings":";;;AAAA,kEAA8D;AAC9D,yBAAyB;AACzB,mCAAoC;AAEpC,iCAAiC;AACjC,kDAAmE;AACnE,2CAA8C;AAE9C,MAAM,MAAM,GAAG,qBAAY,CAAC,cAAc,CAAC,CAAC;AA0E5C,MAAa,YAAY;IACvB,YACU,OAAmB;IAC3B;;;OAGG;IACK,yBAAiC,EACjC,iBAAyB,EACzB,uBAA+B;QAP/B,YAAO,GAAP,OAAO,CAAY;QAKnB,8BAAyB,GAAzB,yBAAyB,CAAQ;QACjC,sBAAiB,GAAjB,iBAAiB,CAAQ;QACzB,4BAAuB,GAAvB,uBAAuB,CAAQ;QAoQlC,iBAAY,GAAG,KAAK,IAAmB,EAAE;YAC9C,MAAM,CAAC,IAAI,CAAC,qCAAqC,CAAC,CAAC;YAEnD,IAAI,YAAY,GAAG,KAAK,CAAC;YACzB,MAAM,aAAa,GAAW,IAAI,CAAC,+BAA+B,EAAE,CAAC;YACrE,IAAI;gBACF,OAAO,IAAI,EAAE;oBACX,6CAA6C;oBAC7C,4CAA4C;oBAC5C,WAAW;oBACX,6CAA6C;oBAC7C,MAAM,uBAAuB,GAAW,MAAM,IAAI,CAAC,mBAAmB,EAAE,CAAC;o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,CAAC,MAAM,KAAK,CAAC,EAAE;YAC5B,MAAM,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,uBAAuB,4DAA4D,CAAC,CAAC;YACjH,MAAM,IAAI,CAAC,0BAA0B,EAAE,CAAC;SACzC;aAAM;YACL,MAAM,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,uBAAuB,mDAAmD,CAAC,CAAC;SACzG;IACH,CAAC;IAoDS,wBAAwB;QAChC,OAAO,qBAAgB,CAAC,OAAO,GAAG,IAAI,CAAC,yBAAyB,CAAC;IACnE,CAAC;IAED;;OAEG;IACO,+BAA+B;QACvC,IAAI,aAAa,GAAG,OAAO,CAAC;QAE5B,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,wBAAwB,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;YACjE,IAAI,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE;gBAC5B,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;gBACzC,IAAI,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,aAAa,CAAC,EAAE;oBACrC,aAAa,GAAG,OAAO,CAAC;iBACzB;aACF;QACH,CAAC,CAAC,CAAC;QAEH,OAAO,aAAa,CAAC;IACvB,CAAC;CACF;AApVD,oCAoVC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Ok for test files :
|
|
3
|
+
// tslint:disable:max-func-body-length
|
|
4
|
+
// tslint:disable:no-string-literal
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const general_utils_1 = require("@villedemontreal/general-utils");
|
|
7
|
+
const chai_1 = require("chai");
|
|
8
|
+
const sinon = require("sinon");
|
|
9
|
+
const constants_1 = require("./config/constants");
|
|
10
|
+
const mongoClient_1 = require("./mongoClient");
|
|
11
|
+
const mongoUpdater_1 = require("./mongoUpdater");
|
|
12
|
+
const mongoUtils_1 = require("./mongoUtils");
|
|
13
|
+
const testingConfigurations_1 = require("./utils/testingConfigurations");
|
|
14
|
+
let mongoDb;
|
|
15
|
+
let mongoUpdater;
|
|
16
|
+
testingConfigurations_1.setTestingConfigurations();
|
|
17
|
+
// ==========================================
|
|
18
|
+
// Mongo Updater
|
|
19
|
+
// ==========================================
|
|
20
|
+
describe('Mongo Updater', () => {
|
|
21
|
+
// A regular function is *required* to get the proper
|
|
22
|
+
// "this" value for "MongoUtils.mockMongoose(...)"
|
|
23
|
+
const testconfig = constants_1.constants.testsConfig;
|
|
24
|
+
before(async function () {
|
|
25
|
+
// Makes sure Mongoose is mocked, but not in Jenkins as we will start a dedicated mongodb container.
|
|
26
|
+
await mongoUtils_1.mongoUtils.mockMongoose(this, testconfig.mockServer.serverVersion);
|
|
27
|
+
const connection = await mongoClient_1.initMongoose(testconfig);
|
|
28
|
+
chai_1.assert.isOk(connection);
|
|
29
|
+
mongoDb = connection.db;
|
|
30
|
+
// Creates the Updater directly.
|
|
31
|
+
mongoUpdater = new mongoUpdater_1.MongoUpdater(mongoDb, testconfig.updater.mongoSchemaUpdatesDirPath, testconfig.updater.lockMaxAgeSeconds, testconfig.updater.appSchemaCollectionName);
|
|
32
|
+
});
|
|
33
|
+
after(async () => {
|
|
34
|
+
// ==========================================
|
|
35
|
+
// Drops the mocked databases
|
|
36
|
+
// ==========================================
|
|
37
|
+
await mongoDb.dropDatabase();
|
|
38
|
+
});
|
|
39
|
+
describe('getSchemaVersion', async () => {
|
|
40
|
+
it('should contain schema version 0.0.0', async () => {
|
|
41
|
+
const version = await mongoUpdater.getAppSchemaVersion();
|
|
42
|
+
chai_1.assert.strictEqual(version, '0.0.0');
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
describe('checkInstall', async () => {
|
|
46
|
+
let installAppSchemaCollectionSpy;
|
|
47
|
+
beforeEach(async () => {
|
|
48
|
+
installAppSchemaCollectionSpy = sinon.spy(mongoUpdater, 'installAppSchemaCollection');
|
|
49
|
+
});
|
|
50
|
+
afterEach(async () => {
|
|
51
|
+
installAppSchemaCollectionSpy.restore();
|
|
52
|
+
});
|
|
53
|
+
it('should not contains app schema collection', async () => {
|
|
54
|
+
const collections = await mongoDb
|
|
55
|
+
.listCollections({ name: testconfig.updater.appSchemaCollectionName })
|
|
56
|
+
.toArray();
|
|
57
|
+
chai_1.assert.strictEqual(collections.length, 0);
|
|
58
|
+
});
|
|
59
|
+
it('should call installAppSchemaCollection to create schema collection', async () => {
|
|
60
|
+
await mongoUpdater.checkInstallation();
|
|
61
|
+
chai_1.assert.strictEqual(installAppSchemaCollectionSpy.callCount, 1);
|
|
62
|
+
const collections = await mongoDb
|
|
63
|
+
.listCollections({ name: testconfig.updater.appSchemaCollectionName })
|
|
64
|
+
.toArray();
|
|
65
|
+
chai_1.assert.strictEqual(collections.length, 1);
|
|
66
|
+
});
|
|
67
|
+
it('should not call installAppSchemaCollection again', async () => {
|
|
68
|
+
await mongoUpdater.checkInstallation();
|
|
69
|
+
chai_1.assert.strictEqual(installAppSchemaCollectionSpy.callCount, 0);
|
|
70
|
+
});
|
|
71
|
+
it('should contain schema version 0.0.0', async () => {
|
|
72
|
+
await mongoUpdater.checkInstallation();
|
|
73
|
+
const collections = await mongoDb
|
|
74
|
+
.listCollections({ name: testconfig.updater.appSchemaCollectionName })
|
|
75
|
+
.toArray();
|
|
76
|
+
chai_1.assert.strictEqual(collections.length, 1);
|
|
77
|
+
chai_1.assert.strictEqual(collections[0].name, testconfig.updater.appSchemaCollectionName);
|
|
78
|
+
const schema = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
|
|
79
|
+
const schemaDb = await schema.find().toArray();
|
|
80
|
+
chai_1.assert.strictEqual(schemaDb[0].version, '0.0.0');
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
describe('getSchemaVersion', async () => {
|
|
84
|
+
it('should contain schema version 0.0.0', async () => {
|
|
85
|
+
const version = await mongoUpdater.getAppSchemaVersion();
|
|
86
|
+
chai_1.assert.strictEqual(version, '0.0.0');
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
describe('lock', async () => {
|
|
90
|
+
it('lock should be equal to false', async () => {
|
|
91
|
+
const schema = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
|
|
92
|
+
const schemaDb = await schema.find().toArray();
|
|
93
|
+
chai_1.assert.strictEqual(schemaDb[0].lock, false);
|
|
94
|
+
});
|
|
95
|
+
it('should success to lock', async () => {
|
|
96
|
+
const isLocked = await mongoUpdater.lockAppSchemaDocument();
|
|
97
|
+
chai_1.assert.strictEqual(isLocked, true);
|
|
98
|
+
});
|
|
99
|
+
it('lock should be equal to true', async () => {
|
|
100
|
+
const schema = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
|
|
101
|
+
const schemaDb = await schema.find().toArray();
|
|
102
|
+
chai_1.assert.strictEqual(schemaDb[0].lock, true);
|
|
103
|
+
});
|
|
104
|
+
it('should fail to lock again', async () => {
|
|
105
|
+
const isLocked = await mongoUpdater.lockAppSchemaDocument();
|
|
106
|
+
chai_1.assert.strictEqual(isLocked, false);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
describe('unlock', async () => {
|
|
110
|
+
it('lock should be equal to true', async () => {
|
|
111
|
+
const schema = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
|
|
112
|
+
const schemaDb = await schema.find().toArray();
|
|
113
|
+
chai_1.assert.strictEqual(schemaDb[0].lock, true);
|
|
114
|
+
});
|
|
115
|
+
it('should success to unlock', async () => {
|
|
116
|
+
const isUnlocked = await mongoUpdater.unlockAppSchemaDocument();
|
|
117
|
+
chai_1.assert.strictEqual(isUnlocked, true);
|
|
118
|
+
});
|
|
119
|
+
it('lock should be equal to false', async () => {
|
|
120
|
+
const schema = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
|
|
121
|
+
const schemaDb = await schema.find().toArray();
|
|
122
|
+
chai_1.assert.strictEqual(schemaDb[0].lock, false);
|
|
123
|
+
});
|
|
124
|
+
it('should fail to unlock again', async () => {
|
|
125
|
+
const isUnlocked = await mongoUpdater.unlockAppSchemaDocument();
|
|
126
|
+
chai_1.assert.strictEqual(isUnlocked, false);
|
|
127
|
+
const schema = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
|
|
128
|
+
const schemaDb = await schema.find().toArray();
|
|
129
|
+
chai_1.assert.strictEqual(schemaDb[0].lock, false);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
describe('updateSchemaVersion', async () => {
|
|
133
|
+
it('should contain schema version 0.0.0', async () => {
|
|
134
|
+
const version = await mongoUpdater.getAppSchemaVersion();
|
|
135
|
+
chai_1.assert.strictEqual(version, '0.0.0');
|
|
136
|
+
});
|
|
137
|
+
it('should success to update the schema version to 6.5.4', async () => {
|
|
138
|
+
await mongoUpdater.updateAppSchemaVersion('0.0.0', '6.5.4');
|
|
139
|
+
const version = await mongoUpdater.getAppSchemaVersion();
|
|
140
|
+
chai_1.assert.strictEqual(version, '6.5.4');
|
|
141
|
+
});
|
|
142
|
+
it('should success to update the schema version to 0.0.0', async () => {
|
|
143
|
+
await mongoUpdater.updateAppSchemaVersion('6.5.4', '0.0.0');
|
|
144
|
+
const version = await mongoUpdater.getAppSchemaVersion();
|
|
145
|
+
chai_1.assert.strictEqual(version, '0.0.0');
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
describe('checkUpdate', async () => {
|
|
149
|
+
let lockSpy;
|
|
150
|
+
let applyUpdateSchemasSpy;
|
|
151
|
+
let updateSchemaVersionSpy;
|
|
152
|
+
let unlockSpy;
|
|
153
|
+
beforeEach(async () => {
|
|
154
|
+
lockSpy = sinon.spy(mongoUpdater, 'lockAppSchemaDocument');
|
|
155
|
+
applyUpdateSchemasSpy = sinon.spy(mongoUpdater, 'applyAppSchemaUpdates');
|
|
156
|
+
updateSchemaVersionSpy = sinon.spy(mongoUpdater, 'updateAppSchemaVersion');
|
|
157
|
+
unlockSpy = sinon.spy(mongoUpdater, 'unlockAppSchemaDocument');
|
|
158
|
+
});
|
|
159
|
+
afterEach(async () => {
|
|
160
|
+
await mongoDb.dropCollection('test');
|
|
161
|
+
lockSpy.restore();
|
|
162
|
+
applyUpdateSchemasSpy.restore();
|
|
163
|
+
updateSchemaVersionSpy.restore();
|
|
164
|
+
unlockSpy.restore();
|
|
165
|
+
});
|
|
166
|
+
it('should work when not already locked', async () => {
|
|
167
|
+
const timer = new general_utils_1.Timer();
|
|
168
|
+
await mongoUpdater.checkUpdates();
|
|
169
|
+
const elapsed = timer.getMillisecondsElapsed();
|
|
170
|
+
chai_1.assert.isTrue(elapsed < 2000);
|
|
171
|
+
chai_1.assert.strictEqual(lockSpy.callCount, 1);
|
|
172
|
+
chai_1.assert.strictEqual(applyUpdateSchemasSpy.callCount, 1);
|
|
173
|
+
chai_1.assert.strictEqual(updateSchemaVersionSpy.callCount, 1);
|
|
174
|
+
chai_1.assert.strictEqual(unlockSpy.callCount, 1);
|
|
175
|
+
});
|
|
176
|
+
// ==========================================
|
|
177
|
+
// A regular function is *required* to get the proper
|
|
178
|
+
// "this" value to call ".timeout(...)"
|
|
179
|
+
// ==========================================
|
|
180
|
+
it('should wait when is already locked and should delete a lock that is too old', async function () {
|
|
181
|
+
this.timeout(5000);
|
|
182
|
+
// Resets version to 0.0.0
|
|
183
|
+
await mongoUpdater.updateAppSchemaVersion('X.X.X', '0.0.0');
|
|
184
|
+
// ==========================================
|
|
185
|
+
// Temporarly changes the lock max age.
|
|
186
|
+
// ==========================================
|
|
187
|
+
const lockMaxAgeSecondsBackup = testconfig.updater.lockMaxAgeSeconds;
|
|
188
|
+
mongoUpdater['lockMaxAgeSeconds'] = 3;
|
|
189
|
+
chai_1.assert.strictEqual(mongoUpdater['lockMaxAgeSeconds'], 3);
|
|
190
|
+
try {
|
|
191
|
+
const isLocked = await mongoUpdater.lockAppSchemaDocument();
|
|
192
|
+
chai_1.assert.strictEqual(isLocked, true);
|
|
193
|
+
const timer = new general_utils_1.Timer();
|
|
194
|
+
await mongoUpdater.checkUpdates();
|
|
195
|
+
const elapsed = timer.getMillisecondsElapsed();
|
|
196
|
+
chai_1.assert.isTrue(elapsed > 3000);
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
mongoUpdater['lockMaxAgeSeconds'] = lockMaxAgeSecondsBackup;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
describe('getUpdateFiles', async () => {
|
|
204
|
+
it('should not contain files for version between 0.0.0 and 0.0.0', async () => {
|
|
205
|
+
const files = await mongoUpdater.getAppSchemaUpdateFiles('0.0.0', '0.0.0');
|
|
206
|
+
chai_1.assert.strictEqual(files.length, 0);
|
|
207
|
+
});
|
|
208
|
+
it('should not contain files for version between 1.0.0 and 1.0.0', async () => {
|
|
209
|
+
const files = await mongoUpdater.getAppSchemaUpdateFiles('1.0.0', '1.0.0');
|
|
210
|
+
chai_1.assert.strictEqual(files.length, 0);
|
|
211
|
+
});
|
|
212
|
+
it('should contain one file for version between 0.0.0 and 1.0.0', async () => {
|
|
213
|
+
const files = await mongoUpdater.getAppSchemaUpdateFiles('0.0.0', '1.0.0');
|
|
214
|
+
chai_1.assert.strictEqual(files.length, 1);
|
|
215
|
+
chai_1.assert.strictEqual(files[0], '1.0.0');
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
describe('applyUpdateSchemas', async () => {
|
|
219
|
+
let getUpdateFilesSpy;
|
|
220
|
+
beforeEach(async () => {
|
|
221
|
+
getUpdateFilesSpy = sinon.spy(mongoUpdater, 'getAppSchemaUpdateFiles');
|
|
222
|
+
});
|
|
223
|
+
afterEach(async () => {
|
|
224
|
+
getUpdateFilesSpy.restore();
|
|
225
|
+
});
|
|
226
|
+
it('should call getUpdateFilesSpy one time', async () => {
|
|
227
|
+
await mongoUpdater.applyAppSchemaUpdates('0.0.0', '1.0.0');
|
|
228
|
+
chai_1.assert.strictEqual(getUpdateFilesSpy.callCount, 1);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
//# sourceMappingURL=mongoUpdater.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongoUpdater.test.js","sourceRoot":"","sources":["../../src/mongoUpdater.test.ts"],"names":[],"mappings":";AAAA,sBAAsB;AACtB,sCAAsC;AACtC,mCAAmC;;AAEnC,kEAAuD;AACvD,+BAA8B;AAE9B,+BAA+B;AAC/B,kDAAmE;AAEnE,+CAA6C;AAC7C,iDAA6D;AAC7D,6CAA0C;AAC1C,yEAAyE;AAEzE,IAAI,OAAmB,CAAC;AACxB,IAAI,YAA2B,CAAC;AAEhC,gDAAwB,EAAE,CAAC;AAE3B,6CAA6C;AAC7C,gBAAgB;AAChB,6CAA6C;AAC7C,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC7B,qDAAqD;IACrD,kDAAkD;IAClD,MAAM,UAAU,GAAqB,qBAAgB,CAAC,WAAW,CAAC;IAElE,MAAM,CAAC,KAAK;QACV,oGAAoG;QACpG,MAAM,uBAAU,CAAC,YAAY,CAAC,IAAI,EAAE,UAAU,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;QACzE,MAAM,UAAU,GAAG,MAAM,0BAAY,CAAC,UAAU,CAAC,CAAC;QAElD,aAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACxB,OAAO,GAAG,UAAU,CAAC,EAAE,CAAC;QAExB,gCAAgC;QAChC,YAAY,GAAG,IAAI,2BAAY,CAC7B,OAAO,EACP,UAAU,CAAC,OAAO,CAAC,yBAAyB,EAC5C,UAAU,CAAC,OAAO,CAAC,iBAAiB,EACpC,UAAU,CAAC,OAAO,CAAC,uBAAuB,CAC3C,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,KAAK,CAAC,KAAK,IAAI,EAAE;QACf,6CAA6C;QAC7C,6BAA6B;QAC7B,6CAA6C;QAC7C,MAAM,OAAO,CAAC,YAAY,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C,OAAO,EAAE,OAAO,CAAC,CAAC;YACrF,aAAM,CAAC,WAAW,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;QACtC,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,6DAA6D,EAAE,KAAK,IAAI,EAAE;YAC3E,MAAM,KAAK,GAAa,MAAM,YAAY,CAAC,uBAAuB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAErF,aAAM,CAAC,WAAW,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;YACpC,aAAM,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;QACxC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,oBAAoB,EAAE,KAAK,IAAI,EAAE;QACxC,IAAI,iBAAiC,CAAC;QACtC,UAAU,CAAC,KAAK,IAAI,EAAE;YACpB,iBAAiB,GAAG,KAAK,CAAC,GAAG,CAAC,YAAY,EAAE,yBAAyB,CAAC,CAAC;QACzE,CAAC,CAAC,CAAC;QACH,SAAS,CAAC,KAAK,IAAI,EAAE;YACnB,iBAAiB,CAAC,OAAO,EAAE,CAAC;QAC9B,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,wCAAwC,EAAE,KAAK,IAAI,EAAE;YACtD,MAAM,YAAY,CAAC,qBAAqB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC3D,aAAM,CAAC,WAAW,CAAC,iBAAiB,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;QACrD,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ApiErrorAndInfo } from '@villedemontreal/general-utils';
|
|
2
|
+
import * as mocha from 'mocha';
|
|
3
|
+
import { MongoMemoryReplSet, MongoMemoryServer } from 'mongodb-memory-server-core';
|
|
4
|
+
import * as mongoose from 'mongoose';
|
|
5
|
+
/**
|
|
6
|
+
* Mongo utilities
|
|
7
|
+
*/
|
|
8
|
+
export declare class MongoUtils {
|
|
9
|
+
private mongoMemServer;
|
|
10
|
+
private useReplSet;
|
|
11
|
+
private mockgosseMockedFlag;
|
|
12
|
+
/**
|
|
13
|
+
* Mocks the Mongo databases created through Mongoose.
|
|
14
|
+
*
|
|
15
|
+
* IMPORTANT!!
|
|
16
|
+
* For the "mochaInstance" parameter to be the proper
|
|
17
|
+
* one, this function must be called from a *regular function* in a
|
|
18
|
+
* test file, not from an *arrow function*! For more informations,
|
|
19
|
+
* see : https://github.com/mochajs/mocha/issues/2018
|
|
20
|
+
*
|
|
21
|
+
* Note that, currently, once this mocking is in place,
|
|
22
|
+
* it can't be removed. You should only call this function
|
|
23
|
+
* during testing!
|
|
24
|
+
*
|
|
25
|
+
* If Mongoose is already mocked, the function does nothing.
|
|
26
|
+
*
|
|
27
|
+
* @param mocha from a Mocha test file, pass "this" as the value
|
|
28
|
+
* for this parameter or "null" from elsewhere.
|
|
29
|
+
*
|
|
30
|
+
* @param mongoServerVersion the Mongo version to use.
|
|
31
|
+
* Pass null (or undefined) to use the default version
|
|
32
|
+
* downloaded by mongodb-memory-server.
|
|
33
|
+
*/
|
|
34
|
+
mockMongoose(mochaInstance: mocha.Context, mongoServerVersion: string, useReplSet?: boolean): Promise<MongoMemoryServer | MongoMemoryReplSet>;
|
|
35
|
+
/**
|
|
36
|
+
* Drop all mocked Mongo databases.
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
39
|
+
dropMockedDatabases(): Promise<void>;
|
|
40
|
+
getMockedServerPort(): Promise<number>;
|
|
41
|
+
/**
|
|
42
|
+
* Validates if an object is a Mongoose
|
|
43
|
+
* error.
|
|
44
|
+
*/
|
|
45
|
+
isMongooseError(obj: any): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Validates if an object is a plain
|
|
48
|
+
* Mongo error.
|
|
49
|
+
*/
|
|
50
|
+
isPlainMongoError(errorObject: any): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Creates an Api eror from an error thrown
|
|
53
|
+
* by Mongoose.
|
|
54
|
+
*
|
|
55
|
+
* If the specified error is not a Mongo/Mongoose error, it
|
|
56
|
+
* will be returned as is.
|
|
57
|
+
*
|
|
58
|
+
* @param error the Mongo/Mongoose error object
|
|
59
|
+
* @param publicMessage a public message to be used in the
|
|
60
|
+
* generated error. Fopr example : "The user is invalid".
|
|
61
|
+
*/
|
|
62
|
+
convertMongoOrMongooseErrorToApiError(err: any, publicMessage: string): ApiErrorAndInfo | any;
|
|
63
|
+
/**
|
|
64
|
+
* Converts a Mongoose Document to a plain Pojo.
|
|
65
|
+
*/
|
|
66
|
+
convertMongooseDocumentToPlainObject<T>(document: T & mongoose.Document): T;
|
|
67
|
+
}
|
|
68
|
+
export declare let mongoUtils: MongoUtils;
|