@villedemontreal/mongo 6.7.1 → 7.0.0-beta
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/dist/src/config/configs.d.ts +1 -1
- package/dist/src/config/configs.js.map +1 -1
- package/dist/src/config/constants.d.ts +1 -1
- package/dist/src/config/constants.js +12 -12
- package/dist/src/config/constants.js.map +1 -1
- package/dist/src/config/init.js.map +1 -1
- package/dist/src/config/mongooseConfigs.js +8 -6
- package/dist/src/config/mongooseConfigs.js.map +1 -1
- package/dist/src/index.js +6 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/mongoClient.js +3 -3
- package/dist/src/mongoClient.js.map +1 -1
- package/dist/src/mongoUpdater.js +24 -21
- package/dist/src/mongoUpdater.js.map +1 -1
- package/dist/src/mongoUpdater.test.js +2 -2
- package/dist/src/mongoUpdater.test.js.map +1 -1
- package/dist/src/mongoUtils.d.ts +1 -1
- package/dist/src/mongoUtils.js +11 -11
- package/dist/src/mongoUtils.js.map +1 -1
- package/dist/src/mongoUtils.test.js +0 -1
- package/dist/src/mongoUtils.test.js.map +1 -1
- package/dist/src/plugins/pagination/index.js +9 -12
- package/dist/src/plugins/pagination/index.js.map +1 -1
- package/dist/src/plugins/pagination/index.test.js +20 -12
- package/dist/src/plugins/pagination/index.test.js.map +1 -1
- package/dist/src/utils/logger.js +1 -1
- package/dist/src/utils/logger.js.map +1 -1
- package/dist/src/utils/testingConfigurations.js +1 -1
- package/dist/src/utils/testingConfigurations.js.map +1 -1
- package/dist/tests/testingMongoUpdates/1.0.0.js +3 -3
- package/dist/tests/testingMongoUpdates/1.0.1.js +3 -3
- package/package.json +16 -13
- package/src/config/configs.ts +1 -1
- package/src/config/constants.ts +13 -13
- package/src/config/init.ts +1 -1
- package/src/config/mongooseConfigs.ts +30 -12
- package/src/mongoClient.ts +10 -4
- package/src/mongoUpdater.test.ts +20 -8
- package/src/mongoUpdater.ts +41 -25
- package/src/mongoUtils.ts +20 -14
- package/src/plugins/pagination/index.test.ts +38 -27
- package/src/plugins/pagination/index.ts +19 -14
- package/src/utils/logger.ts +8 -1
package/src/mongoUpdater.ts
CHANGED
|
@@ -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(
|
|
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(
|
|
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(
|
|
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 : ' +
|
|
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
|
|
|
@@ -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
|
|
|
@@ -269,8 +277,8 @@ export class MongoUpdater implements IMongoUpdater {
|
|
|
269
277
|
return true;
|
|
270
278
|
}
|
|
271
279
|
|
|
272
|
-
|
|
273
|
-
if (
|
|
280
|
+
const existingLock = await appSchemaCollection.findOne({ lock: true });
|
|
281
|
+
if (existingLock === null) {
|
|
274
282
|
// try again!
|
|
275
283
|
return this.lockAppSchemaDocument();
|
|
276
284
|
}
|
|
@@ -278,7 +286,7 @@ export class MongoUpdater implements IMongoUpdater {
|
|
|
278
286
|
// ==========================================
|
|
279
287
|
// Checks the existing lock's timestamp
|
|
280
288
|
// ==========================================
|
|
281
|
-
const lockTimestamp = (
|
|
289
|
+
const lockTimestamp = (existingLock as any).lockTimestamp;
|
|
282
290
|
const nowTimestamp = new Date().getTime();
|
|
283
291
|
const lockAgeMilliSeconds = nowTimestamp - lockTimestamp;
|
|
284
292
|
|
|
@@ -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
|
|
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(
|
|
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(
|
|
360
|
+
logger.info(
|
|
361
|
+
` > The "${this.appSchemaCollectionName}" collection was found. No installation required.`
|
|
362
|
+
);
|
|
349
363
|
}
|
|
350
364
|
}
|
|
351
365
|
|
|
@@ -363,7 +377,9 @@ export class MongoUpdater implements IMongoUpdater {
|
|
|
363
377
|
const currentAppSchemaVersion: string = await this.getAppSchemaVersion();
|
|
364
378
|
if (semver.gte(currentAppSchemaVersion, targetVersion)) {
|
|
365
379
|
// tslint:disable-next-line: prefer-template
|
|
366
|
-
logger.info(
|
|
380
|
+
logger.info(
|
|
381
|
+
' > Current database app schema is up to date : ' + currentAppSchemaVersion + ').'
|
|
382
|
+
);
|
|
367
383
|
return;
|
|
368
384
|
}
|
|
369
385
|
|
|
@@ -409,7 +425,7 @@ export class MongoUpdater implements IMongoUpdater {
|
|
|
409
425
|
protected findMongoAppSchemaTargetVersion(): string {
|
|
410
426
|
let targetVersion = '0.0.0';
|
|
411
427
|
|
|
412
|
-
fs.readdirSync(this.getAppSchemaFilesDirPath()).forEach(fileName => {
|
|
428
|
+
fs.readdirSync(this.getAppSchemaFilesDirPath()).forEach((fileName) => {
|
|
413
429
|
if (fileName.endsWith('.js')) {
|
|
414
430
|
const version = fileName.split('.js')[0];
|
|
415
431
|
if (semver.gt(version, targetVersion)) {
|
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
|
|
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
|
|
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(
|
|
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(
|
|
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
|
|
328
|
+
export const mongoUtils: MongoUtils = new MongoUtils();
|
|
@@ -18,34 +18,37 @@ const bookSchema = new mongoose.Schema({
|
|
|
18
18
|
date: Date,
|
|
19
19
|
author: {
|
|
20
20
|
type: String,
|
|
21
|
-
ref: 'Author'
|
|
22
|
-
}
|
|
21
|
+
ref: 'Author',
|
|
22
|
+
},
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
bookSchema.plugin(mongoosePaginate);
|
|
26
26
|
const bookModel: any = mongoose.model('Book', bookSchema);
|
|
27
27
|
|
|
28
28
|
describe('plugin pagination', () => {
|
|
29
|
-
before(async function() {
|
|
29
|
+
before(async function () {
|
|
30
30
|
this.timeout(120000);
|
|
31
31
|
|
|
32
32
|
// Makes sure Mongoose is mocked, but not in Jenkins as we will start a dedicated mongodb container.
|
|
33
|
-
const mockedDb = await mongoUtils.mockMongoose(
|
|
33
|
+
const mockedDb = await mongoUtils.mockMongoose(
|
|
34
|
+
this,
|
|
35
|
+
constants.testsConfig.mockServer.serverVersion
|
|
36
|
+
);
|
|
34
37
|
const connString = mockedDb.getUri();
|
|
35
|
-
await mongoose.connect(connString
|
|
38
|
+
await mongoose.connect(connString);
|
|
36
39
|
});
|
|
37
40
|
|
|
38
|
-
before(async function() {
|
|
41
|
+
before(async function () {
|
|
39
42
|
this.timeout(10000);
|
|
40
43
|
let book;
|
|
41
44
|
const books: any = [];
|
|
42
45
|
const date = new Date();
|
|
43
|
-
return authorModel.create({ name: 'Arthur Conan Doyle' }).then(author => {
|
|
46
|
+
return authorModel.create({ name: 'Arthur Conan Doyle' }).then((author) => {
|
|
44
47
|
for (let i = 1; i <= 100; i++) {
|
|
45
48
|
book = new bookModel({
|
|
46
49
|
title: 'Book #' + i,
|
|
47
50
|
date: new Date(date.getTime() + i),
|
|
48
|
-
author: author._id
|
|
51
|
+
author: author._id,
|
|
49
52
|
});
|
|
50
53
|
books.push(book);
|
|
51
54
|
}
|
|
@@ -58,7 +61,7 @@ describe('plugin pagination', () => {
|
|
|
58
61
|
expect(promise.then).to.be.an.instanceof(Function);
|
|
59
62
|
});
|
|
60
63
|
|
|
61
|
-
it('calls callback', done => {
|
|
64
|
+
it('calls callback', (done) => {
|
|
62
65
|
bookModel.paginate({}, {}, (err: Error, result: IPaginatedResult<any>) => {
|
|
63
66
|
expect(err).to.be.null;
|
|
64
67
|
expect(result).to.be.an.instanceOf(Object);
|
|
@@ -83,12 +86,14 @@ describe('plugin pagination', () => {
|
|
|
83
86
|
});
|
|
84
87
|
});
|
|
85
88
|
it('with offset and limit', () => {
|
|
86
|
-
return bookModel
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
return bookModel
|
|
90
|
+
.paginate({}, { offset: 30, limit: 20 })
|
|
91
|
+
.then((result: IPaginatedResult<any>) => {
|
|
92
|
+
expect(result.items).to.have.length(20);
|
|
93
|
+
expect(result.paging.totalCount).to.equal(100);
|
|
94
|
+
expect(result.paging.limit).to.equal(20);
|
|
95
|
+
expect(result.paging.offset).to.equal(30);
|
|
96
|
+
});
|
|
92
97
|
});
|
|
93
98
|
it('with zero limit', () => {
|
|
94
99
|
return bookModel.paginate({}, { page: 1, limit: 0 }).then((result: IPaginatedResult<any>) => {
|
|
@@ -104,14 +109,18 @@ describe('plugin pagination', () => {
|
|
|
104
109
|
});
|
|
105
110
|
});
|
|
106
111
|
it('with sort', () => {
|
|
107
|
-
return bookModel
|
|
108
|
-
|
|
109
|
-
|
|
112
|
+
return bookModel
|
|
113
|
+
.paginate({}, { sort: { date: -1 } })
|
|
114
|
+
.then((result: IPaginatedResult<any>) => {
|
|
115
|
+
expect(result.items[0].title).to.equal('Book #100');
|
|
116
|
+
});
|
|
110
117
|
});
|
|
111
118
|
it('with populate', () => {
|
|
112
|
-
return bookModel
|
|
113
|
-
|
|
114
|
-
|
|
119
|
+
return bookModel
|
|
120
|
+
.paginate({}, { populate: 'author' })
|
|
121
|
+
.then((result: IPaginatedResult<any>) => {
|
|
122
|
+
expect(result.items[0].author.name).to.equal('Arthur Conan Doyle');
|
|
123
|
+
});
|
|
115
124
|
});
|
|
116
125
|
describe('with lean', () => {
|
|
117
126
|
it('with default leanWithId=true', () => {
|
|
@@ -122,19 +131,21 @@ describe('plugin pagination', () => {
|
|
|
122
131
|
});
|
|
123
132
|
});
|
|
124
133
|
it('with leanWithId=false', () => {
|
|
125
|
-
return bookModel
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
134
|
+
return bookModel
|
|
135
|
+
.paginate({}, { lean: true, leanWithId: false })
|
|
136
|
+
.then((result: IPaginatedResult<any>) => {
|
|
137
|
+
expect(result.items[0]).to.not.be.an.instanceof((mongoose as any).Document);
|
|
138
|
+
expect(result.items[0]).to.not.have.property('id');
|
|
139
|
+
});
|
|
129
140
|
});
|
|
130
141
|
});
|
|
131
142
|
});
|
|
132
143
|
|
|
133
|
-
after(done => {
|
|
144
|
+
after((done) => {
|
|
134
145
|
mongoose.connection.db.dropDatabase(done);
|
|
135
146
|
});
|
|
136
147
|
|
|
137
|
-
after(done => {
|
|
148
|
+
after((done) => {
|
|
138
149
|
mongoose.disconnect(done);
|
|
139
150
|
});
|
|
140
151
|
});
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { IPaginateOptions } from './specs/IPaginateOptions';
|
|
2
2
|
|
|
3
|
-
function paginate(
|
|
3
|
+
function paginate(
|
|
4
|
+
q: any,
|
|
5
|
+
options: IPaginateOptions,
|
|
6
|
+
callback: (error: Error, result: any) => void
|
|
7
|
+
) {
|
|
4
8
|
const optionsClean = PaginateBuilder.getOptions((paginate as any).options, options);
|
|
5
9
|
|
|
6
10
|
const query = q || {};
|
|
@@ -22,14 +26,18 @@ export class PaginateBuilder {
|
|
|
22
26
|
lean: false,
|
|
23
27
|
leanWithId: true,
|
|
24
28
|
limit: 10,
|
|
25
|
-
offset: 0
|
|
29
|
+
offset: 0,
|
|
26
30
|
});
|
|
27
31
|
|
|
28
32
|
public static getOptions(...options: IPaginateOptions[]): IPaginateOptions {
|
|
29
33
|
return Object.assign({}, PaginateBuilder.defaultOptions, ...options);
|
|
30
34
|
}
|
|
31
35
|
|
|
32
|
-
public static executeQueries(
|
|
36
|
+
public static executeQueries(
|
|
37
|
+
model: any,
|
|
38
|
+
query: any,
|
|
39
|
+
options: IPaginateOptions
|
|
40
|
+
): [Promise<any[]>, Promise<number>] {
|
|
33
41
|
const { select, sort, populate, lean, leanWithId, limit, offset } = options;
|
|
34
42
|
let itemsQuery: any;
|
|
35
43
|
|
|
@@ -38,20 +46,17 @@ export class PaginateBuilder {
|
|
|
38
46
|
}
|
|
39
47
|
|
|
40
48
|
if (limit > 0) {
|
|
41
|
-
itemsQuery = model
|
|
42
|
-
.find(query)
|
|
43
|
-
.select(select)
|
|
44
|
-
.sort(sort)
|
|
45
|
-
.skip(offset)
|
|
46
|
-
.limit(limit)
|
|
47
|
-
.lean(lean);
|
|
49
|
+
itemsQuery = model.find(query).select(select).sort(sort).skip(offset).limit(limit).lean(lean);
|
|
48
50
|
|
|
49
51
|
if (populate) {
|
|
50
|
-
[].concat(populate).forEach(item => itemsQuery.populate(item));
|
|
52
|
+
[].concat(populate).forEach((item) => itemsQuery.populate(item));
|
|
51
53
|
}
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
return [
|
|
56
|
+
return [
|
|
57
|
+
itemsQuery && limit > 0 ? itemsQuery.exec() : Promise.resolve([]),
|
|
58
|
+
model.countDocuments(query).exec(),
|
|
59
|
+
];
|
|
55
60
|
}
|
|
56
61
|
|
|
57
62
|
public static processResult(
|
|
@@ -62,7 +67,7 @@ export class PaginateBuilder {
|
|
|
62
67
|
const { lean, leanWithId, limit, offset } = options;
|
|
63
68
|
return new Promise((resolve, reject) => {
|
|
64
69
|
Promise.all(promises).then(
|
|
65
|
-
data => {
|
|
70
|
+
(data) => {
|
|
66
71
|
const items = data[0] as any[];
|
|
67
72
|
const count = data[1] as number;
|
|
68
73
|
const result: any = { paging: { limit, offset, totalCount: count } };
|
|
@@ -84,7 +89,7 @@ export class PaginateBuilder {
|
|
|
84
89
|
|
|
85
90
|
resolve(result);
|
|
86
91
|
},
|
|
87
|
-
error => {
|
|
92
|
+
(error) => {
|
|
88
93
|
if (typeof callback === 'function') {
|
|
89
94
|
return callback(error, null);
|
|
90
95
|
}
|
package/src/utils/logger.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ILogger,
|
|
3
|
+
initLogger,
|
|
4
|
+
LazyLogger,
|
|
5
|
+
Logger,
|
|
6
|
+
LoggerConfigs,
|
|
7
|
+
LogLevel,
|
|
8
|
+
} from '@villedemontreal/logger';
|
|
2
9
|
import { configs } from '../config/configs';
|
|
3
10
|
|
|
4
11
|
let testingLoggerLibInitialised = false;
|