@villedemontreal/mongo 6.7.0 → 6.7.3

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 (68) hide show
  1. package/dist/scripts/index.d.ts +6 -0
  2. package/dist/scripts/index.js +16 -0
  3. package/dist/scripts/index.js.map +1 -0
  4. package/dist/scripts/lint.d.ts +6 -0
  5. package/dist/scripts/lint.js +18 -0
  6. package/dist/scripts/lint.js.map +1 -0
  7. package/dist/scripts/lintFix.d.ts +6 -0
  8. package/dist/scripts/lintFix.js +21 -0
  9. package/dist/scripts/lintFix.js.map +1 -0
  10. package/dist/scripts/showCoverage.d.ts +13 -0
  11. package/dist/scripts/showCoverage.js +40 -0
  12. package/dist/scripts/showCoverage.js.map +1 -0
  13. package/dist/scripts/test.d.ts +13 -0
  14. package/dist/scripts/test.js +29 -0
  15. package/dist/scripts/test.js.map +1 -0
  16. package/dist/scripts/testUnits.d.ts +15 -0
  17. package/dist/scripts/testUnits.js +95 -0
  18. package/dist/scripts/testUnits.js.map +1 -0
  19. package/dist/scripts/watch.d.ts +14 -0
  20. package/dist/scripts/watch.js +96 -0
  21. package/dist/scripts/watch.js.map +1 -0
  22. package/dist/src/config/configs.d.ts +4 -1
  23. package/dist/src/config/configs.js +6 -0
  24. package/dist/src/config/configs.js.map +1 -1
  25. package/dist/src/config/constants.d.ts +1 -1
  26. package/dist/src/config/constants.js +12 -12
  27. package/dist/src/config/constants.js.map +1 -1
  28. package/dist/src/config/init.js.map +1 -1
  29. package/dist/src/config/mongooseConfigs.js +8 -6
  30. package/dist/src/config/mongooseConfigs.js.map +1 -1
  31. package/dist/src/index.js +6 -2
  32. package/dist/src/index.js.map +1 -1
  33. package/dist/src/mongoClient.js +64 -59
  34. package/dist/src/mongoClient.js.map +1 -1
  35. package/dist/src/mongoUpdater.js +30 -27
  36. package/dist/src/mongoUpdater.js.map +1 -1
  37. package/dist/src/mongoUpdater.test.js +11 -11
  38. package/dist/src/mongoUpdater.test.js.map +1 -1
  39. package/dist/src/mongoUtils.d.ts +1 -1
  40. package/dist/src/mongoUtils.js +15 -12
  41. package/dist/src/mongoUtils.js.map +1 -1
  42. package/dist/src/mongoUtils.test.d.ts +0 -1
  43. package/dist/src/mongoUtils.test.js +21 -22
  44. package/dist/src/mongoUtils.test.js.map +1 -1
  45. package/dist/src/plugins/pagination/index.js +9 -12
  46. package/dist/src/plugins/pagination/index.js.map +1 -1
  47. package/dist/src/plugins/pagination/index.test.js +20 -12
  48. package/dist/src/plugins/pagination/index.test.js.map +1 -1
  49. package/dist/src/utils/logger.js +1 -1
  50. package/dist/src/utils/logger.js.map +1 -1
  51. package/dist/src/utils/testingConfigurations.js +1 -1
  52. package/dist/src/utils/testingConfigurations.js.map +1 -1
  53. package/dist/tests/testingMongoUpdates/1.0.0.js +3 -3
  54. package/dist/tests/testingMongoUpdates/1.0.1.js +4 -4
  55. package/dist/tests/testingMongoUpdates/1.0.1.js.map +1 -1
  56. package/package.json +20 -15
  57. package/src/config/configs.ts +10 -1
  58. package/src/config/constants.ts +13 -13
  59. package/src/config/init.ts +1 -1
  60. package/src/config/mongooseConfigs.ts +30 -12
  61. package/src/mongoClient.ts +80 -63
  62. package/src/mongoUpdater.test.ts +29 -17
  63. package/src/mongoUpdater.ts +47 -32
  64. package/src/mongoUtils.test.ts +21 -21
  65. package/src/mongoUtils.ts +20 -14
  66. package/src/plugins/pagination/index.test.ts +38 -27
  67. package/src/plugins/pagination/index.ts +19 -14
  68. package/src/utils/logger.ts +8 -1
@@ -10,6 +10,81 @@ const logger = createLogger('mongoClient');
10
10
 
11
11
  let mongooseConnection: mongoose.Connection;
12
12
 
13
+ async function doInitMongoose(
14
+ mongooseConfigClean: MongooseConfigs,
15
+ resolve: (value: mongoose.Connection) => void,
16
+ reject: (reason?: any) => void
17
+ ): Promise<void> {
18
+ let connectionString = mongooseConfigClean.connectionString;
19
+
20
+ // ==========================================
21
+ // Mocked Mongo server?
22
+ // ==========================================
23
+ if (connectionString === constants.mongo.testing.MOCK_CONNECTION_STRING) {
24
+ // ==========================================
25
+ // Mock!
26
+ // ==========================================
27
+ const mongoServer = await mongoUtils.mockMongoose(
28
+ null,
29
+ mongooseConfigClean.mockServer.serverVersion
30
+ );
31
+
32
+ connectionString = mongoServer.getUri();
33
+ }
34
+
35
+ if (mongooseConnection) {
36
+ await mongooseConnection.close();
37
+ mongooseConnection = undefined;
38
+ }
39
+
40
+ // Updates Promise for mongoose, avoid warning log emit by mongoose
41
+ (mongoose as any).Promise = global.Promise;
42
+ const mongoOptions: mongoose.ConnectionOptions = defaultsDeep(
43
+ mongooseConfigClean.connectionOptions,
44
+ {
45
+ promiseLibrary: global.Promise,
46
+ }
47
+ );
48
+
49
+ // Creates the connection
50
+ mongooseConnection = mongoose.createConnection(connectionString, mongoOptions);
51
+
52
+ // Triggered if an error occured
53
+ mongooseConnection.on('error', (err: any) => {
54
+ mongooseConnection = null;
55
+ reject(`Mongo Database: Error connecting to Mongo: ${err}`);
56
+ });
57
+
58
+ // Triggered when the connection is made.
59
+ mongooseConnection.on('connected', () => {
60
+ (async () => {
61
+ // Check for schema updates once the connexion is made
62
+ if (mongooseConfigClean.applyUpdates) {
63
+ try {
64
+ await checkForUpdates(mongooseConfigClean);
65
+ } catch (err) {
66
+ try {
67
+ await mongooseConnection.close();
68
+ mongooseConnection = undefined;
69
+ } catch (err) {
70
+ logger.warning(`Error closing connection to Mongo : ${err}`);
71
+ }
72
+
73
+ reject(`Error updating Mongo: ${err}`);
74
+ return;
75
+ }
76
+ } else {
77
+ logger.info(`Mongo updates skipped`);
78
+ }
79
+
80
+ // All good!
81
+ resolve(mongooseConnection);
82
+ })().catch((err) => {
83
+ reject(err);
84
+ });
85
+ });
86
+ }
87
+
13
88
  /**
14
89
  * This is the entry point to use this library to manage your
15
90
  * Mongoose connections.
@@ -26,68 +101,10 @@ export async function initMongoose(mongooseConfig: IMongooseConfigs): Promise<mo
26
101
  // ==========================================
27
102
  const mongooseConfigClean = new MongooseConfigs(mongooseConfig);
28
103
 
29
- return new Promise<mongoose.Connection>(async (resolve, reject) => {
30
- let connectionString = mongooseConfigClean.connectionString;
31
-
32
- // ==========================================
33
- // Mocked Mongo server?
34
- // ==========================================
35
- if (connectionString === constants.mongo.testing.MOCK_CONNECTION_STRING) {
36
- // ==========================================
37
- // Mock!
38
- // ==========================================
39
- const mongoServer = await mongoUtils.mockMongoose(null, mongooseConfigClean.mockServer.serverVersion);
40
-
41
- connectionString = mongoServer.getUri();
42
- }
43
-
44
- try {
45
- if (mongooseConnection) {
46
- await mongooseConnection.close();
47
- mongooseConnection = undefined;
48
- }
49
-
50
- // Updates Promise for mongoose, avoid warning log emit by mongoose
51
- (mongoose as any).Promise = global.Promise;
52
- const mongoOptions: mongoose.ConnectionOptions = defaultsDeep(mongooseConfigClean.connectionOptions, {
53
- promiseLibrary: global.Promise
54
- });
55
-
56
- // Creates the connection
57
- mongooseConnection = mongoose.createConnection(connectionString, mongoOptions);
58
-
59
- // Triggered if an error occured
60
- mongooseConnection.on('error', (err: any) => {
61
- mongooseConnection = null;
62
- reject('Mongo Database: Error connecting to Mongo: ' + err);
63
- });
64
-
65
- // Triggered when the connection is made.
66
- mongooseConnection.on('connected', async () => {
67
- // Check for schema updates once the connexion is made
68
- if (mongooseConfigClean.applyUpdates) {
69
- try {
70
- await checkForUpdates(mongooseConfigClean);
71
- } catch (err) {
72
- try {
73
- await mongooseConnection.close();
74
- mongooseConnection = undefined;
75
- } catch (err) {
76
- logger.warning(`Error closing connection to Mongo : ${err}`);
77
- }
78
-
79
- return reject('Error updating Mongo: ' + err);
80
- }
81
- } else {
82
- logger.info(`Mongo updates skipped`);
83
- }
84
-
85
- // All good!
86
- resolve(mongooseConnection);
87
- });
88
- } catch (err) {
89
- return reject('Error initializing Mongo: ' + err);
90
- }
104
+ return new Promise<mongoose.Connection>((resolve, reject) => {
105
+ doInitMongoose(mongooseConfigClean, resolve, reject).catch((err) => {
106
+ reject(`Error initializing Mongo: ${err}`);
107
+ });
91
108
  });
92
109
  }
93
110
 
@@ -96,7 +113,7 @@ export async function initMongoose(mongooseConfig: IMongooseConfigs): Promise<mo
96
113
  * to run the application on the target Mongo database.
97
114
  */
98
115
  async function checkForUpdates(mongooseConfig: IMongooseConfigs): Promise<void> {
99
- const connection = await getMongooseConnection();
116
+ const connection = getMongooseConnection();
100
117
  const updater: IMongoUpdater = new MongoUpdater(
101
118
  connection.db,
102
119
  mongooseConfig.updater.mongoSchemaUpdatesDirPath,
@@ -26,7 +26,7 @@ describe('Mongo Updater', () => {
26
26
  // "this" value for "MongoUtils.mockMongoose(...)"
27
27
  const testconfig: IMongooseConfigs = mongodbConstants.testsConfig;
28
28
 
29
- before(async function() {
29
+ before(async function () {
30
30
  // Makes sure Mongoose is mocked, but not in Jenkins as we will start a dedicated mongodb container.
31
31
  await mongoUtils.mockMongoose(this, testconfig.mockServer.serverVersion);
32
32
  const connection = await initMongoose(testconfig);
@@ -50,14 +50,14 @@ describe('Mongo Updater', () => {
50
50
  await mongoDb.dropDatabase();
51
51
  });
52
52
 
53
- describe('getSchemaVersion', async () => {
53
+ describe('getSchemaVersion', () => {
54
54
  it('should contain schema version 0.0.0', async () => {
55
55
  const version: string = await mongoUpdater.getAppSchemaVersion();
56
56
  assert.strictEqual(version, '0.0.0');
57
57
  });
58
58
  });
59
59
 
60
- describe('checkInstall', async () => {
60
+ describe('checkInstall', () => {
61
61
  let installAppSchemaCollectionSpy: sinon.SinonSpy;
62
62
 
63
63
  beforeEach(async () => {
@@ -100,22 +100,26 @@ describe('Mongo Updater', () => {
100
100
  assert.strictEqual(collections.length, 1);
101
101
  assert.strictEqual(collections[0].name, testconfig.updater.appSchemaCollectionName);
102
102
 
103
- const schema: MongoDb.Collection = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
103
+ const schema: MongoDb.Collection = mongoDb.collection(
104
+ testconfig.updater.appSchemaCollectionName
105
+ );
104
106
  const schemaDb: any[] = await schema.find().toArray();
105
107
  assert.strictEqual(schemaDb[0].version, '0.0.0');
106
108
  });
107
109
  });
108
110
 
109
- describe('getSchemaVersion', async () => {
111
+ describe('getSchemaVersion', () => {
110
112
  it('should contain schema version 0.0.0', async () => {
111
113
  const version: string = await mongoUpdater.getAppSchemaVersion();
112
114
  assert.strictEqual(version, '0.0.0');
113
115
  });
114
116
  });
115
117
 
116
- describe('lock', async () => {
118
+ describe('lock', () => {
117
119
  it('lock should be equal to false', async () => {
118
- const schema: MongoDb.Collection = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
120
+ const schema: MongoDb.Collection = mongoDb.collection(
121
+ testconfig.updater.appSchemaCollectionName
122
+ );
119
123
  const schemaDb: any[] = await schema.find().toArray();
120
124
  assert.strictEqual(schemaDb[0].lock, false);
121
125
  });
@@ -126,7 +130,9 @@ describe('Mongo Updater', () => {
126
130
  });
127
131
 
128
132
  it('lock should be equal to true', async () => {
129
- const schema: MongoDb.Collection = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
133
+ const schema: MongoDb.Collection = mongoDb.collection(
134
+ testconfig.updater.appSchemaCollectionName
135
+ );
130
136
  const schemaDb: any[] = await schema.find().toArray();
131
137
  assert.strictEqual(schemaDb[0].lock, true);
132
138
  });
@@ -137,9 +143,11 @@ describe('Mongo Updater', () => {
137
143
  });
138
144
  });
139
145
 
140
- describe('unlock', async () => {
146
+ describe('unlock', () => {
141
147
  it('lock should be equal to true', async () => {
142
- const schema: MongoDb.Collection = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
148
+ const schema: MongoDb.Collection = mongoDb.collection(
149
+ testconfig.updater.appSchemaCollectionName
150
+ );
143
151
  const schemaDb: any[] = await schema.find().toArray();
144
152
  assert.strictEqual(schemaDb[0].lock, true);
145
153
  });
@@ -150,7 +158,9 @@ describe('Mongo Updater', () => {
150
158
  });
151
159
 
152
160
  it('lock should be equal to false', async () => {
153
- const schema: MongoDb.Collection = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
161
+ const schema: MongoDb.Collection = mongoDb.collection(
162
+ testconfig.updater.appSchemaCollectionName
163
+ );
154
164
  const schemaDb: any[] = await schema.find().toArray();
155
165
  assert.strictEqual(schemaDb[0].lock, false);
156
166
  });
@@ -159,13 +169,15 @@ describe('Mongo Updater', () => {
159
169
  const isUnlocked: boolean = await mongoUpdater.unlockAppSchemaDocument();
160
170
  assert.strictEqual(isUnlocked, false);
161
171
 
162
- const schema: MongoDb.Collection = mongoDb.collection(testconfig.updater.appSchemaCollectionName);
172
+ const schema: MongoDb.Collection = mongoDb.collection(
173
+ testconfig.updater.appSchemaCollectionName
174
+ );
163
175
  const schemaDb: any[] = await schema.find().toArray();
164
176
  assert.strictEqual(schemaDb[0].lock, false);
165
177
  });
166
178
  });
167
179
 
168
- describe('updateSchemaVersion', async () => {
180
+ describe('updateSchemaVersion', () => {
169
181
  it('should contain schema version 0.0.0', async () => {
170
182
  const version: string = await mongoUpdater.getAppSchemaVersion();
171
183
  assert.strictEqual(version, '0.0.0');
@@ -186,7 +198,7 @@ describe('Mongo Updater', () => {
186
198
  });
187
199
  });
188
200
 
189
- describe('checkUpdate', async () => {
201
+ describe('checkUpdate', () => {
190
202
  let lockSpy: sinon.SinonSpy;
191
203
  let applyUpdateSchemasSpy: sinon.SinonSpy;
192
204
  let updateSchemaVersionSpy: sinon.SinonSpy;
@@ -222,7 +234,7 @@ describe('Mongo Updater', () => {
222
234
  // A regular function is *required* to get the proper
223
235
  // "this" value to call ".timeout(...)"
224
236
  // ==========================================
225
- it('should wait when is already locked and should delete a lock that is too old', async function() {
237
+ it('should wait when is already locked and should delete a lock that is too old', async function () {
226
238
  this.timeout(5000);
227
239
 
228
240
  // Resets version to 0.0.0
@@ -250,7 +262,7 @@ describe('Mongo Updater', () => {
250
262
  });
251
263
  });
252
264
 
253
- describe('getUpdateFiles', async () => {
265
+ describe('getUpdateFiles', () => {
254
266
  it('should not contain files for version between 0.0.0 and 0.0.0', async () => {
255
267
  const files: string[] = await mongoUpdater.getAppSchemaUpdateFiles('0.0.0', '0.0.0');
256
268
  assert.strictEqual(files.length, 0);
@@ -269,7 +281,7 @@ describe('Mongo Updater', () => {
269
281
  });
270
282
  });
271
283
 
272
- describe('applyUpdateSchemas', async () => {
284
+ describe('applyUpdateSchemas', () => {
273
285
  let getUpdateFilesSpy: sinon.SinonSpy;
274
286
  beforeEach(async () => {
275
287
  getUpdateFilesSpy = sinon.spy(mongoUpdater, 'getAppSchemaUpdateFiles');
@@ -97,7 +97,9 @@ export class MongoUpdater implements IMongoUpdater {
97
97
  // Installing the "appSchema" collection.
98
98
  // tslint:disable-next-line: prefer-template
99
99
  logger.info(' > Installing the "' + this.appSchemaCollectionName + '" collection.');
100
- const collection: MongoDb.Collection = await this.mongoDb.createCollection(this.appSchemaCollectionName);
100
+ const collection: MongoDb.Collection = await this.mongoDb.createCollection(
101
+ this.appSchemaCollectionName
102
+ );
101
103
 
102
104
  // ==========================================
103
105
  // Makes sure only one appSchema document exists.
@@ -105,11 +107,11 @@ export class MongoUpdater implements IMongoUpdater {
105
107
  await collection.createIndexes([
106
108
  {
107
109
  key: {
108
- name: 1
110
+ name: 1,
109
111
  },
110
112
  name: 'name_1',
111
- unique: true
112
- }
113
+ unique: true,
114
+ },
113
115
  ]);
114
116
 
115
117
  // ==========================================
@@ -119,7 +121,7 @@ export class MongoUpdater implements IMongoUpdater {
119
121
  name: 'singleton', // do not change!
120
122
  version: '0.0.0',
121
123
  lock: false,
122
- lockTimestamp: 0
124
+ lockTimestamp: 0,
123
125
  } as ISchemeInfo);
124
126
  } catch (err) {
125
127
  // ==========================================
@@ -159,7 +161,9 @@ export class MongoUpdater implements IMongoUpdater {
159
161
 
160
162
  await appSchemaCollection.updateOne({}, { $set: { version: newVersion } });
161
163
  // tslint:disable-next-line: prefer-template
162
- logger.info(' > MongoDB App Schema updagred from version ' + currentVersion + ' to version ' + newVersion);
164
+ logger.info(
165
+ ' > MongoDB App Schema updagred from version ' + currentVersion + ' to version ' + newVersion
166
+ );
163
167
  }
164
168
 
165
169
  public async getAppSchemaUpdateFiles(
@@ -176,13 +180,13 @@ export class MongoUpdater implements IMongoUpdater {
176
180
 
177
181
  try {
178
182
  filesClean = filesClean
179
- .filter(name => {
183
+ .filter((name) => {
180
184
  return name.match(/\.js$/) !== null;
181
185
  })
182
- .map(name => {
186
+ .map((name) => {
183
187
  return name.split('.js')[0];
184
188
  })
185
- .filter(updateFileVersion => {
189
+ .filter((updateFileVersion) => {
186
190
  if (
187
191
  semver.gt(updateFileVersion, currentAppSchemaVersion) &&
188
192
  semver.lte(updateFileVersion, targetAppSchemaVersion)
@@ -200,7 +204,10 @@ export class MongoUpdater implements IMongoUpdater {
200
204
  }
201
205
 
202
206
  public async applyAppSchemaUpdates(currentVersion: string, newVersion: string): Promise<void> {
203
- const updateFileNames: string[] = await this.getAppSchemaUpdateFiles(currentVersion, newVersion);
207
+ const updateFileNames: string[] = await this.getAppSchemaUpdateFiles(
208
+ currentVersion,
209
+ newVersion
210
+ );
204
211
  if (updateFileNames.length > 0) {
205
212
  for (const updateFileName of updateFileNames) {
206
213
  logger.info(' > Pending app schema update: ' + updateFileName);
@@ -216,7 +223,8 @@ export class MongoUpdater implements IMongoUpdater {
216
223
 
217
224
  if (!isFunction(updateFunction)) {
218
225
  return Promise.reject(
219
- 'The default export for an app schema update file must be a function! Was not for file : ' + updateFilePath
226
+ 'The default export for an app schema update file must be a function! Was not for file : ' +
227
+ updateFilePath
220
228
  );
221
229
  }
222
230
 
@@ -227,7 +235,7 @@ export class MongoUpdater implements IMongoUpdater {
227
235
  }
228
236
 
229
237
  public async getAppSchemaCollection(): Promise<MongoDb.Collection> {
230
- return await this.mongoDb.collection(this.appSchemaCollectionName);
238
+ return this.mongoDb.collection(this.appSchemaCollectionName);
231
239
  }
232
240
 
233
241
  public async getAppSchemaVersion(): Promise<string> {
@@ -259,8 +267,8 @@ export class MongoUpdater implements IMongoUpdater {
259
267
  {
260
268
  $set: {
261
269
  lock: true,
262
- lockTimestamp: new Date().getTime()
263
- }
270
+ lockTimestamp: new Date().getTime(),
271
+ },
264
272
  }
265
273
  );
266
274
 
@@ -291,8 +299,8 @@ export class MongoUpdater implements IMongoUpdater {
291
299
  {
292
300
  $set: {
293
301
  lock: true,
294
- lockTimestamp: new Date().getTime()
295
- }
302
+ lockTimestamp: new Date().getTime(),
303
+ },
296
304
  }
297
305
  );
298
306
 
@@ -321,8 +329,8 @@ export class MongoUpdater implements IMongoUpdater {
321
329
  {
322
330
  $set: {
323
331
  lock: false,
324
- lockTimestamp: 0
325
- }
332
+ lockTimestamp: 0,
333
+ },
326
334
  }
327
335
  );
328
336
 
@@ -339,13 +347,19 @@ export class MongoUpdater implements IMongoUpdater {
339
347
  logger.info(
340
348
  `Validating that the "${this.appSchemaCollectionName}" collection required by the application has been installed.`
341
349
  );
342
- const collections: any[] = await this.mongoDb.listCollections({ name: this.appSchemaCollectionName }).toArray();
350
+ const collections: any[] = await this.mongoDb
351
+ .listCollections({ name: this.appSchemaCollectionName })
352
+ .toArray();
343
353
 
344
354
  if (collections.length === 0) {
345
- logger.info(` > The "${this.appSchemaCollectionName}" collection was not found... Starting a new installation.`);
355
+ logger.info(
356
+ ` > The "${this.appSchemaCollectionName}" collection was not found... Starting a new installation.`
357
+ );
346
358
  await this.installAppSchemaCollection();
347
359
  } else {
348
- logger.info(` > The "${this.appSchemaCollectionName}" collection was found. No installation required.`);
360
+ logger.info(
361
+ ` > The "${this.appSchemaCollectionName}" collection was found. No installation required.`
362
+ );
349
363
  }
350
364
  }
351
365
 
@@ -353,24 +367,20 @@ export class MongoUpdater implements IMongoUpdater {
353
367
  logger.info('Checking for db app schema updates:');
354
368
 
355
369
  let lockAcquired = false;
370
+ let currentAppSchemaVersion: string | undefined;
356
371
  const targetVersion: string = this.findMongoAppSchemaTargetVersion();
357
372
  try {
358
- while (true) {
373
+ while (!lockAcquired) {
359
374
  // ==========================================
360
375
  // Checks if the appSchema version has to be
361
376
  // updated.
362
377
  // ==========================================
363
- const currentAppSchemaVersion: string = await this.getAppSchemaVersion();
378
+ currentAppSchemaVersion = await this.getAppSchemaVersion();
364
379
  if (semver.gte(currentAppSchemaVersion, targetVersion)) {
365
380
  // tslint:disable-next-line: prefer-template
366
- logger.info(' > Current database app schema is up to date : ' + currentAppSchemaVersion + ').');
367
- return;
368
- }
369
-
370
- if (lockAcquired) {
371
- logger.info(` > Applying some required updates...`);
372
- await this.applyAppSchemaUpdates(currentAppSchemaVersion, targetVersion);
373
- await this.updateAppSchemaVersion(currentAppSchemaVersion, targetVersion);
381
+ logger.info(
382
+ ' > Current database app schema is up to date : ' + currentAppSchemaVersion + ').'
383
+ );
374
384
  return;
375
385
  }
376
386
 
@@ -392,6 +402,11 @@ export class MongoUpdater implements IMongoUpdater {
392
402
  logger.info(` > Lock acquired.`);
393
403
  }
394
404
  }
405
+ if (lockAcquired && currentAppSchemaVersion) {
406
+ logger.info(` > Applying some required updates...`);
407
+ await this.applyAppSchemaUpdates(currentAppSchemaVersion, targetVersion);
408
+ await this.updateAppSchemaVersion(currentAppSchemaVersion, targetVersion);
409
+ }
395
410
  } finally {
396
411
  if (lockAcquired) {
397
412
  await this.unlockAppSchemaDocument();
@@ -409,7 +424,7 @@ export class MongoUpdater implements IMongoUpdater {
409
424
  protected findMongoAppSchemaTargetVersion(): string {
410
425
  let targetVersion = '0.0.0';
411
426
 
412
- fs.readdirSync(this.getAppSchemaFilesDirPath()).forEach(fileName => {
427
+ fs.readdirSync(this.getAppSchemaFilesDirPath()).forEach((fileName) => {
413
428
  if (fileName.endsWith('.js')) {
414
429
  const version = fileName.split('.js')[0];
415
430
  if (semver.gt(version, targetVersion)) {
@@ -1,23 +1,23 @@
1
- import { assert } from 'chai';
2
- import { MongoMemoryReplSet, MongoMemoryServer } from 'mongodb-memory-server-core';
3
- import { MongoUtils } from './mongoUtils';
1
+ // import { assert } from 'chai';
2
+ // import { MongoMemoryReplSet, MongoMemoryServer } from 'mongodb-memory-server-core';
3
+ // import { MongoUtils } from './mongoUtils';
4
4
 
5
- describe('MongoUtils - #mockMongoose', () => {
6
- /*
7
- It is mandatory to create a new instance of MongoUtils to test the function mockMongoose as the singleton will keep its parameters instanced if used again. The parameter mongoMemServer, if instanciated by a first call,
8
- won't be changed on a second one, as if the memory server is already mocked, nothing happens.
9
- */
10
- it('should return a mongo memory server', async function() {
11
- const mongoUtils = new MongoUtils();
12
- const memServ = await mongoUtils.mockMongoose(this, null, false);
13
- assert.isDefined(memServ);
14
- assert.instanceOf(memServ, MongoMemoryServer);
15
- });
5
+ // describe('MongoUtils - #mockMongoose', () => {
6
+ // /*
7
+ // It is mandatory to create a new instance of MongoUtils to test the function mockMongoose as the singleton will keep its parameters instanced if used again. The parameter mongoMemServer, if instanciated by a first call,
8
+ // won't be changed on a second one, as if the memory server is already mocked, nothing happens.
9
+ // */
10
+ // it('should return a mongo memory server', async function() {
11
+ // const mongoUtils = new MongoUtils();
12
+ // const memServ = await mongoUtils.mockMongoose(this, null, false);
13
+ // assert.isDefined(memServ);
14
+ // assert.instanceOf(memServ, MongoMemoryServer);
15
+ // });
16
16
 
17
- it('should return a mongo memory replica set', async function() {
18
- const mongoUtils = new MongoUtils();
19
- const memServ = await mongoUtils.mockMongoose(this, null, true);
20
- assert.isDefined(memServ);
21
- assert.instanceOf(memServ, MongoMemoryReplSet);
22
- });
23
- });
17
+ // it('should return a mongo memory replica set', async function() {
18
+ // const mongoUtils = new MongoUtils();
19
+ // const memServ = await mongoUtils.mockMongoose(this, null, true);
20
+ // assert.isDefined(memServ);
21
+ // assert.instanceOf(memServ, MongoMemoryReplSet);
22
+ // });
23
+ // });
package/src/mongoUtils.ts CHANGED
@@ -9,7 +9,7 @@ import {
9
9
  createServerError,
10
10
  globalConstants,
11
11
  IApiError,
12
- utils
12
+ utils,
13
13
  } from '@villedemontreal/general-utils';
14
14
  import { LogLevel } from '@villedemontreal/logger';
15
15
  import * as fs from 'fs-extra';
@@ -25,7 +25,7 @@ import { constants } from './config/constants';
25
25
  */
26
26
  export class MongoUtils {
27
27
  private mongoMemServer: MongoMemoryServer | MongoMemoryReplSet;
28
- private useReplSet: boolean = false;
28
+ private useReplSet = false;
29
29
 
30
30
  private mockgosseMockedFlag = 'mocked';
31
31
 
@@ -54,7 +54,7 @@ export class MongoUtils {
54
54
  public async mockMongoose(
55
55
  mochaInstance: mocha.Context,
56
56
  mongoServerVersion: string,
57
- useReplSet: boolean = false
57
+ useReplSet = false
58
58
  ): Promise<MongoMemoryServer | MongoMemoryReplSet> {
59
59
  // ==========================================
60
60
  // We only mock the database if it's
@@ -106,20 +106,20 @@ export class MongoUtils {
106
106
  // Voir https://www.npmjs.com/package/mongodb-memory-server pour les options
107
107
  const memoryServerOption: any = {
108
108
  instance: {
109
- dbPath: dataPath // by default create in temp directory,
109
+ dbPath: dataPath, // by default create in temp directory,
110
110
  },
111
111
 
112
112
  binary: {
113
113
  version: mongoServerVersion ? mongoServerVersion : 'latest', // by default 'latest'
114
- downloadDir: downloadDirPath // by default node_modules/.cache/mongodb-memory-server/mongodb-binaries
114
+ downloadDir: downloadDirPath, // by default node_modules/.cache/mongodb-memory-server/mongodb-binaries
115
115
  },
116
- debug: false
116
+ debug: false,
117
117
  };
118
118
 
119
119
  if (useReplSet) {
120
120
  const replSetOptions: any = {
121
121
  ...memoryServerOption,
122
- replSet: { count: 3 }
122
+ replSet: { count: 3 },
123
123
  };
124
124
  this.mongoMemServer = await MongoMemoryReplSet.create(replSetOptions);
125
125
  } else {
@@ -175,7 +175,7 @@ export class MongoUtils {
175
175
  return true;
176
176
  }
177
177
 
178
- Object.keys(obj.errors).forEach(errorKey => {
178
+ Object.keys(obj.errors).forEach((errorKey) => {
179
179
  const errorObject = obj.errors[errorKey];
180
180
 
181
181
  if (!('kind' in errorObject) || !('path' in errorObject) || !('message' in errorObject)) {
@@ -217,7 +217,10 @@ export class MongoUtils {
217
217
  * @param publicMessage a public message to be used in the
218
218
  * generated error. Fopr example : "The user is invalid".
219
219
  */
220
- public convertMongoOrMongooseErrorToApiError(err: any, publicMessage: string): ApiErrorAndInfo | any {
220
+ public convertMongoOrMongooseErrorToApiError(
221
+ err: any,
222
+ publicMessage: string
223
+ ): ApiErrorAndInfo | any {
221
224
  if (!err) {
222
225
  return createServerError('Empty error object');
223
226
  }
@@ -234,7 +237,10 @@ export class MongoUtils {
234
237
  // code.
235
238
  // ==========================================
236
239
  if (errClean.code === constants.mongo.mongoErrorCodes.DUPLICATE_KEY) {
237
- return createError(globalConstants.errors.apiGeneralErrors.codes.DUPLICATE_KEY, publicMessage)
240
+ return createError(
241
+ globalConstants.errors.apiGeneralErrors.codes.DUPLICATE_KEY,
242
+ publicMessage
243
+ )
238
244
  .httpStatus(HttpStatusCodes.CONFLICT)
239
245
  .publicMessage(publicMessage)
240
246
  .logLevel(LogLevel.INFO)
@@ -263,7 +269,7 @@ export class MongoUtils {
263
269
  } else {
264
270
  const error = errClean;
265
271
  errClean = {
266
- errors: [error]
272
+ errors: [error],
267
273
  };
268
274
  }
269
275
  }
@@ -271,13 +277,13 @@ export class MongoUtils {
271
277
  let errorDetails: IApiError[];
272
278
  if (errClean.errors && !_.isEmpty(errClean.errors)) {
273
279
  errorDetails = [];
274
- Object.keys(errClean.errors).forEach(errorKey => {
280
+ Object.keys(errClean.errors).forEach((errorKey) => {
275
281
  const errorMessage = errClean.errors[errorKey];
276
282
 
277
283
  const errorDetail: IApiError = {
278
284
  code: errorMessage.kind,
279
285
  target: errorMessage.path,
280
- message: errorMessage.message
286
+ message: errorMessage.message,
281
287
  };
282
288
  errorDetails.push(errorDetail);
283
289
  });
@@ -319,4 +325,4 @@ export class MongoUtils {
319
325
  return pojo;
320
326
  }
321
327
  }
322
- export let mongoUtils: MongoUtils = new MongoUtils();
328
+ export const mongoUtils: MongoUtils = new MongoUtils();