@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,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTestingLoggerCreator = exports.createLogger = void 0;
|
|
4
|
+
const logger_1 = require("@villedemontreal/logger");
|
|
5
|
+
const configs_1 = require("../config/configs");
|
|
6
|
+
let testingLoggerLibInitialised = false;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a Logger.
|
|
9
|
+
*/
|
|
10
|
+
function createLogger(name) {
|
|
11
|
+
// ==========================================
|
|
12
|
+
// We use a LazyLogger so the real Logger
|
|
13
|
+
// is only created when the first
|
|
14
|
+
// log is actually performed... At that point,
|
|
15
|
+
// our "configs.loggerCreator" configuration
|
|
16
|
+
// must have been set by the code using our library!
|
|
17
|
+
//
|
|
18
|
+
// This pattern allows calling code to import
|
|
19
|
+
// modules from us in which a logger is
|
|
20
|
+
// created in the global scope :
|
|
21
|
+
//
|
|
22
|
+
// let logger = createLogger('someName');
|
|
23
|
+
//
|
|
24
|
+
// Without a Lazy Logger, the library configurations
|
|
25
|
+
// would at that moment *not* have been set yet
|
|
26
|
+
// (by the calling code) and an Error would be thrown
|
|
27
|
+
// because the "configs.loggerCreator" is required.
|
|
28
|
+
// ==========================================
|
|
29
|
+
return new logger_1.LazyLogger(name, (nameArg) => {
|
|
30
|
+
return configs_1.configs.loggerCreator(nameArg);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
exports.createLogger = createLogger;
|
|
34
|
+
function initTestingLoggerConfigs() {
|
|
35
|
+
const loggerConfig = new logger_1.LoggerConfigs(() => 'test-cid');
|
|
36
|
+
loggerConfig.setLogLevel(logger_1.LogLevel.DEBUG);
|
|
37
|
+
logger_1.initLogger(loggerConfig);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A Logger that uses a dummy cid provider.
|
|
41
|
+
*
|
|
42
|
+
* Only use this when running the tests!
|
|
43
|
+
*/
|
|
44
|
+
function getTestingLoggerCreator() {
|
|
45
|
+
return (name) => {
|
|
46
|
+
if (!testingLoggerLibInitialised) {
|
|
47
|
+
initTestingLoggerConfigs();
|
|
48
|
+
testingLoggerLibInitialised = true;
|
|
49
|
+
}
|
|
50
|
+
return new logger_1.Logger(name);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.getTestingLoggerCreator = getTestingLoggerCreator;
|
|
54
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/utils/logger.ts"],"names":[],"mappings":";;;AAAA,oDAA2G;AAC3G,+CAA4C;AAE5C,IAAI,2BAA2B,GAAG,KAAK,CAAC;AAExC;;GAEG;AACH,SAAgB,YAAY,CAAC,IAAY;IACvC,6CAA6C;IAC7C,yCAAyC;IACzC,iCAAiC;IACjC,8CAA8C;IAC9C,4CAA4C;IAC5C,oDAAoD;IACpD,EAAE;IACF,6CAA6C;IAC7C,uCAAuC;IACvC,gCAAgC;IAChC,EAAE;IACF,yCAAyC;IACzC,EAAE;IACF,oDAAoD;IACpD,+CAA+C;IAC/C,qDAAqD;IACrD,mDAAmD;IACnD,6CAA6C;IAC7C,OAAO,IAAI,mBAAU,CAAC,IAAI,EAAE,CAAC,OAAe,EAAE,EAAE;QAC9C,OAAO,iBAAO,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;AACL,CAAC;AAtBD,oCAsBC;AAED,SAAS,wBAAwB;IAC/B,MAAM,YAAY,GAAkB,IAAI,sBAAa,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,CAAC;IACxE,YAAY,CAAC,WAAW,CAAC,iBAAQ,CAAC,KAAK,CAAC,CAAC;IACzC,mBAAU,CAAC,YAAY,CAAC,CAAC;AAC3B,CAAC;AAED;;;;GAIG;AACH,SAAgB,uBAAuB;IACrC,OAAO,CAAC,IAAY,EAAW,EAAE;QAC/B,IAAI,CAAC,2BAA2B,EAAE;YAChC,wBAAwB,EAAE,CAAC;YAC3B,2BAA2B,GAAG,IAAI,CAAC;SACpC;QAED,OAAO,IAAI,eAAM,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC,CAAC;AACJ,CAAC;AATD,0DASC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setTestingConfigurations = void 0;
|
|
4
|
+
const configs_1 = require("../config/configs");
|
|
5
|
+
const logger_1 = require("../utils/logger");
|
|
6
|
+
/**
|
|
7
|
+
* Call this when your need to set
|
|
8
|
+
* *Testing* configurations to the current
|
|
9
|
+
* library, without the need for a calling code
|
|
10
|
+
* to do so.
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
function setTestingConfigurations() {
|
|
14
|
+
configs_1.configs.setLoggerCreator(logger_1.getTestingLoggerCreator());
|
|
15
|
+
}
|
|
16
|
+
exports.setTestingConfigurations = setTestingConfigurations;
|
|
17
|
+
//# sourceMappingURL=testingConfigurations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testingConfigurations.js","sourceRoot":"","sources":["../../../src/utils/testingConfigurations.ts"],"names":[],"mappings":";;;AAAA,+CAA4C;AAC5C,4CAA0D;AAE1D;;;;;;GAMG;AACH,SAAgB,wBAAwB;IACtC,iBAAO,CAAC,gBAAgB,CAAC,gCAAuB,EAAE,CAAC,CAAC;AACtD,CAAC;AAFD,4DAEC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* TEST update - version 1.0.0
|
|
5
|
+
*/
|
|
6
|
+
async function update(db) {
|
|
7
|
+
// ==========================================
|
|
8
|
+
// Creating the "test" collection.
|
|
9
|
+
// ==========================================
|
|
10
|
+
const testCollection = await db.createCollection('test');
|
|
11
|
+
// ==========================================
|
|
12
|
+
// Creating indexes for the "test" collection.
|
|
13
|
+
//
|
|
14
|
+
// @see https://docs.mongodb.com/manual/reference/command/createIndexes/
|
|
15
|
+
// ==========================================
|
|
16
|
+
await testCollection.createIndexes([
|
|
17
|
+
{
|
|
18
|
+
key: {
|
|
19
|
+
email: 1
|
|
20
|
+
},
|
|
21
|
+
name: 'email_1',
|
|
22
|
+
unique: true
|
|
23
|
+
}
|
|
24
|
+
]);
|
|
25
|
+
}
|
|
26
|
+
exports.default = update;
|
|
27
|
+
//# sourceMappingURL=1.0.0.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"1.0.0.js","sourceRoot":"","sources":["../../../tests/testingMongoUpdates/1.0.0.ts"],"names":[],"mappings":";;AAEA;;GAEG;AACY,KAAK,UAAU,MAAM,CAAC,EAAc;IACjD,6CAA6C;IAC7C,kCAAkC;IAClC,6CAA6C;IAC7C,MAAM,cAAc,GAAuB,MAAM,EAAE,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAE7E,6CAA6C;IAC7C,8CAA8C;IAC9C,EAAE;IACF,wEAAwE;IACxE,6CAA6C;IAC7C,MAAM,cAAc,CAAC,aAAa,CAAC;QACjC;YACE,GAAG,EAAE;gBACH,KAAK,EAAE,CAAC;aACT;YACD,IAAI,EAAE,SAAS;YACf,MAAM,EAAE,IAAI;SACb;KACF,CAAC,CAAC;AACL,CAAC;AApBD,yBAoBC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* TEST update - version 1.0.1
|
|
5
|
+
*/
|
|
6
|
+
async function update(db) {
|
|
7
|
+
// ==========================================
|
|
8
|
+
// Adding extra indexes on the "test" collection.
|
|
9
|
+
// ==========================================
|
|
10
|
+
const testUserCollection = await db.collection('test');
|
|
11
|
+
await testUserCollection.createIndexes([
|
|
12
|
+
{
|
|
13
|
+
key: {
|
|
14
|
+
firstName: 1,
|
|
15
|
+
lastName: 1
|
|
16
|
+
},
|
|
17
|
+
name: 'firstName_lastName'
|
|
18
|
+
}
|
|
19
|
+
]);
|
|
20
|
+
}
|
|
21
|
+
exports.default = update;
|
|
22
|
+
//# sourceMappingURL=1.0.1.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"1.0.1.js","sourceRoot":"","sources":["../../../tests/testingMongoUpdates/1.0.1.ts"],"names":[],"mappings":";;AAEA;;GAEG;AACY,KAAK,UAAU,MAAM,CAAC,EAAc;IACjD,6CAA6C;IAC7C,iDAAiD;IACjD,6CAA6C;IAC7C,MAAM,kBAAkB,GAAuB,MAAM,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IAE3E,MAAM,kBAAkB,CAAC,aAAa,CAAC;QACrC;YACE,GAAG,EAAE;gBACH,SAAS,EAAE,CAAC;gBACZ,QAAQ,EAAE,CAAC;aACZ;YACD,IAAI,EAAE,oBAAoB;SAC3B;KACF,CAAC,CAAC;AACL,CAAC;AAfD,yBAeC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@villedemontreal/mongo",
|
|
3
|
+
"version": "6.7.0",
|
|
4
|
+
"description": "Utilities for Mongo / Mongoose",
|
|
5
|
+
"main": "dist/src/index.js",
|
|
6
|
+
"typings": "dist/src",
|
|
7
|
+
"files": [
|
|
8
|
+
"src",
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"start": "node run test",
|
|
13
|
+
"test": "node run test",
|
|
14
|
+
"compile": "node run compile",
|
|
15
|
+
"lint": "node run lint",
|
|
16
|
+
"lint-fix": "node run lint-fix",
|
|
17
|
+
"tslint": "node run tslint",
|
|
18
|
+
"tslint-fix": "node run tslint-fix",
|
|
19
|
+
"prettier": "node run prettier",
|
|
20
|
+
"prettier-fix": "node run prettier-fix",
|
|
21
|
+
"watch": "node run watch"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/VilledeMontreal/node-mongo"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/VilledeMontreal/node-mongo",
|
|
28
|
+
"keywords": [
|
|
29
|
+
"mongo",
|
|
30
|
+
"mongoose",
|
|
31
|
+
"utilities"
|
|
32
|
+
],
|
|
33
|
+
"author": "Ville de Montréal",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@types/http-status-codes": "1.2.0",
|
|
37
|
+
"@types/lodash": "4.14.181",
|
|
38
|
+
"@types/semver": "7.3.9",
|
|
39
|
+
"@villedemontreal/general-utils": "5.16.3",
|
|
40
|
+
"@villedemontreal/logger": "6.5.3",
|
|
41
|
+
"@villedemontreal/scripting": "1.5.0",
|
|
42
|
+
"fs-extra": "10.0.1",
|
|
43
|
+
"http-status-codes": "2.2.0",
|
|
44
|
+
"lodash": "4.17.21",
|
|
45
|
+
"mongodb": "3.6.12",
|
|
46
|
+
"mongodb-memory-server-core": "7.2.1",
|
|
47
|
+
"mongoose": "5.13.13",
|
|
48
|
+
"semver": "7.3.5"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/chai": "4.3.0",
|
|
52
|
+
"@types/fs-extra": "9.0.13",
|
|
53
|
+
"@types/mocha": "9.1.0",
|
|
54
|
+
"@types/sinon": "10.0.11",
|
|
55
|
+
"@villedemontreal/lint-config": "1.7.7",
|
|
56
|
+
"chai": "4.3.6",
|
|
57
|
+
"mocha": "9.2.2",
|
|
58
|
+
"mocha-jenkins-reporter": "0.4.7",
|
|
59
|
+
"sinon": "13.0.1",
|
|
60
|
+
"tslint": "6.1.3",
|
|
61
|
+
"typescript": "3.9.5"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ILogger } from '@villedemontreal/logger';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Lib configs
|
|
5
|
+
*/
|
|
6
|
+
export class Configs {
|
|
7
|
+
private _loggerCreator: (name: string) => ILogger;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The Logger creator
|
|
11
|
+
*/
|
|
12
|
+
get loggerCreator(): (name: string) => ILogger {
|
|
13
|
+
if (!this._loggerCreator) {
|
|
14
|
+
throw new Error(`The Logger Creator HAS to be set as a configuration`);
|
|
15
|
+
}
|
|
16
|
+
return this._loggerCreator;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sets the Logger creator.
|
|
21
|
+
*/
|
|
22
|
+
public setLoggerCreator(loggerCreator: (name: string) => ILogger) {
|
|
23
|
+
this._loggerCreator = loggerCreator;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export let configs: Configs = new Configs();
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// ==========================================
|
|
2
|
+
// Application constants
|
|
3
|
+
// ==========================================
|
|
4
|
+
import { path as appRoot } from 'app-root-path';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import { IMongooseConfigs } from './mongooseConfigs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Library constants
|
|
10
|
+
*/
|
|
11
|
+
export class Constants {
|
|
12
|
+
/**
|
|
13
|
+
* The library root. When this library is used
|
|
14
|
+
* as a dependency in a project, the "libRoot"
|
|
15
|
+
* will be the path to the dependency folder,
|
|
16
|
+
* inside the "node_modules".
|
|
17
|
+
*/
|
|
18
|
+
public libRoot: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The app root. When this library is used
|
|
22
|
+
* as a dependency in a project, the "appRoot"
|
|
23
|
+
* will be the path to the root project!
|
|
24
|
+
*/
|
|
25
|
+
public appRoot: string;
|
|
26
|
+
|
|
27
|
+
constructor() {
|
|
28
|
+
// From the "dist/src/config" folder
|
|
29
|
+
this.libRoot = path.normalize(__dirname + '/../../..');
|
|
30
|
+
this.appRoot = appRoot;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Base config to 'mock' a mongo server
|
|
35
|
+
*/
|
|
36
|
+
get testsConfig(): IMongooseConfigs {
|
|
37
|
+
return {
|
|
38
|
+
applyUpdates: false,
|
|
39
|
+
connectionString: 'mock',
|
|
40
|
+
connectionOptions: {
|
|
41
|
+
useNewUrlParser: true,
|
|
42
|
+
useUnifiedTopology: true
|
|
43
|
+
},
|
|
44
|
+
updater: {
|
|
45
|
+
lockMaxAgeSeconds: 30,
|
|
46
|
+
mongoSchemaUpdatesDirPath: '/dist/tests/testingMongoUpdates',
|
|
47
|
+
appSchemaCollectionName: 'testAppSchema'
|
|
48
|
+
},
|
|
49
|
+
mockServer: {
|
|
50
|
+
serverVersion: '4.0.16'
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Mongo constants
|
|
57
|
+
*/
|
|
58
|
+
get mongo() {
|
|
59
|
+
return {
|
|
60
|
+
testing: {
|
|
61
|
+
/**
|
|
62
|
+
* The "connectionString" to use for a mock
|
|
63
|
+
* Mongo server to be used instead of a real one.
|
|
64
|
+
* This option is only available on the "development"
|
|
65
|
+
* environment, or when tests are ran.
|
|
66
|
+
*/
|
|
67
|
+
MOCK_CONNECTION_STRING: 'mock'
|
|
68
|
+
},
|
|
69
|
+
/**
|
|
70
|
+
* The names of the Mongo collections used in
|
|
71
|
+
* this application.
|
|
72
|
+
*/
|
|
73
|
+
collectionNames: {
|
|
74
|
+
/**
|
|
75
|
+
* Special collection that stores informations about the
|
|
76
|
+
* schema currently installed for the application.
|
|
77
|
+
*/
|
|
78
|
+
APP_SCHEMA: 'appSchema'
|
|
79
|
+
},
|
|
80
|
+
/**
|
|
81
|
+
* Mongo error codes
|
|
82
|
+
*/
|
|
83
|
+
mongoErrorCodes: {
|
|
84
|
+
/**
|
|
85
|
+
* The code for a Mongo "duplicate key" error.
|
|
86
|
+
*/
|
|
87
|
+
DUPLICATE_KEY: 11000
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Mongoose constants
|
|
92
|
+
*/
|
|
93
|
+
mongoose: {
|
|
94
|
+
/**
|
|
95
|
+
* Mongoose error codes
|
|
96
|
+
*/
|
|
97
|
+
errorCodes: {
|
|
98
|
+
/**
|
|
99
|
+
* The code for a Mongoose "required" error.
|
|
100
|
+
*/
|
|
101
|
+
REQUIRED_FIELD: 'required'
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Mongoose error kinds
|
|
106
|
+
*/
|
|
107
|
+
errorKinds: {
|
|
108
|
+
OBJECT_ID: 'ObjectId'
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Mongoose error names
|
|
113
|
+
*/
|
|
114
|
+
errorNames: {
|
|
115
|
+
CAST_ERROR: 'CastError'
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export let constants: Constants = new Constants();
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ILogger } from '@villedemontreal/logger';
|
|
2
|
+
import { configs } from './configs';
|
|
3
|
+
|
|
4
|
+
let libIsInitialized: boolean = false;
|
|
5
|
+
/**
|
|
6
|
+
* Inits the library.
|
|
7
|
+
*/
|
|
8
|
+
export function init(loggerCreator: (name: string) => ILogger): void {
|
|
9
|
+
if (!loggerCreator) {
|
|
10
|
+
throw new Error(`The Logger Creator is required.`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
configs.setLoggerCreator(loggerCreator);
|
|
14
|
+
|
|
15
|
+
libIsInitialized = true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* checks if the library has been initialized.
|
|
20
|
+
*/
|
|
21
|
+
export function isInited(): boolean {
|
|
22
|
+
return libIsInitialized;
|
|
23
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { utils } from '@villedemontreal/general-utils';
|
|
2
|
+
import * as _ from 'lodash';
|
|
3
|
+
import { createLogger } from '../utils/logger';
|
|
4
|
+
import { constants } from './constants';
|
|
5
|
+
|
|
6
|
+
const logger = createLogger('mongooseConfigs');
|
|
7
|
+
|
|
8
|
+
export interface IMongooseConfigs {
|
|
9
|
+
/**
|
|
10
|
+
* The updater.mongoSchemaUpdatesDirPath
|
|
11
|
+
* is a required config.
|
|
12
|
+
*/
|
|
13
|
+
updater?: {
|
|
14
|
+
/**
|
|
15
|
+
* Name of the app schema collection name to use.
|
|
16
|
+
* Useful when multiple components use the same database.
|
|
17
|
+
*/
|
|
18
|
+
appSchemaCollectionName?: string;
|
|
19
|
+
/**
|
|
20
|
+
* The path where to find update files.
|
|
21
|
+
*/
|
|
22
|
+
mongoSchemaUpdatesDirPath: string;
|
|
23
|
+
|
|
24
|
+
lockMaxAgeSeconds?: number;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param applyUpdates Should the database be checked for missing
|
|
29
|
+
* updates and have them applied if required?
|
|
30
|
+
* Defaults to true.
|
|
31
|
+
*/
|
|
32
|
+
applyUpdates?: boolean;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* If no connectionString is provided, "mock" will be
|
|
36
|
+
* used by default and a temporary Mongo server will
|
|
37
|
+
* be used.
|
|
38
|
+
*/
|
|
39
|
+
connectionString?: string;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The Mongoose connection options.
|
|
43
|
+
*/
|
|
44
|
+
connectionOptions?: any;
|
|
45
|
+
|
|
46
|
+
mockServer?: {
|
|
47
|
+
/**
|
|
48
|
+
* @param mongoServerVersion the Mongo version to use.
|
|
49
|
+
*
|
|
50
|
+
* Pass null (or undefined) to use the default version
|
|
51
|
+
* downloaded by mockServer.
|
|
52
|
+
*/
|
|
53
|
+
serverVersion?: string;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Mongoose configs with default values.
|
|
59
|
+
*/
|
|
60
|
+
export class MongooseConfigs implements IMongooseConfigs {
|
|
61
|
+
/**
|
|
62
|
+
* @param applyUpdates Should the database be checked for missing
|
|
63
|
+
* updates and have them applied if required?
|
|
64
|
+
*/
|
|
65
|
+
public applyUpdates: boolean = true;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* If no connectionString is provided, "mock" will be
|
|
69
|
+
* used by default and a temporary Mongo server will
|
|
70
|
+
* be used.
|
|
71
|
+
*/
|
|
72
|
+
public connectionString: string = 'mock';
|
|
73
|
+
|
|
74
|
+
public connectionOptions: any = {
|
|
75
|
+
w: 1,
|
|
76
|
+
wtimeout: 5000,
|
|
77
|
+
auto_reconnect: true,
|
|
78
|
+
reconnectTries: 604800,
|
|
79
|
+
reconnectInterval: 1000,
|
|
80
|
+
useNewUrlParser: true
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
public updater: { lockMaxAgeSeconds: number; mongoSchemaUpdatesDirPath: string; appSchemaCollectionName: string } = {
|
|
84
|
+
appSchemaCollectionName: constants.mongo.collectionNames.APP_SCHEMA,
|
|
85
|
+
lockMaxAgeSeconds: 60,
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The path *relative* to the app root, of the directory
|
|
89
|
+
* where the update files are.
|
|
90
|
+
* Required!
|
|
91
|
+
*/
|
|
92
|
+
mongoSchemaUpdatesDirPath: null
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
public mockServer: { serverVersion: string } = {
|
|
96
|
+
serverVersion: '3.2.1'
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Overrides default configurations using the ones passed
|
|
101
|
+
* as parameters.
|
|
102
|
+
*/
|
|
103
|
+
// tslint:disable-next-line:cyclomatic-complexity
|
|
104
|
+
constructor(overridingConfigs: IMongooseConfigs) {
|
|
105
|
+
// ==========================================
|
|
106
|
+
// Required configs
|
|
107
|
+
// ==========================================
|
|
108
|
+
|
|
109
|
+
if (
|
|
110
|
+
_.isNil(overridingConfigs) ||
|
|
111
|
+
_.isNil(overridingConfigs.updater) ||
|
|
112
|
+
utils.isBlank(overridingConfigs.updater.mongoSchemaUpdatesDirPath)
|
|
113
|
+
) {
|
|
114
|
+
throw new Error(`The updater.mongoSchemaUpdatesDirPath config is required!`);
|
|
115
|
+
}
|
|
116
|
+
this.updater.mongoSchemaUpdatesDirPath = overridingConfigs.updater.mongoSchemaUpdatesDirPath;
|
|
117
|
+
|
|
118
|
+
// ==========================================
|
|
119
|
+
// Not required...
|
|
120
|
+
// ==========================================
|
|
121
|
+
|
|
122
|
+
if (!_.isNil(overridingConfigs.updater.lockMaxAgeSeconds)) {
|
|
123
|
+
if (!utils.isIntegerValue(overridingConfigs.updater.lockMaxAgeSeconds, true, false)) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
`The updater.lockMaxAgeSeconds config is not valid : ${overridingConfigs.updater.lockMaxAgeSeconds}`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
this.updater.lockMaxAgeSeconds = Number(overridingConfigs.updater.lockMaxAgeSeconds);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!_.isNil(overridingConfigs.updater.appSchemaCollectionName)) {
|
|
132
|
+
if (
|
|
133
|
+
!_.isString(overridingConfigs.updater.appSchemaCollectionName) ||
|
|
134
|
+
utils.isBlank(overridingConfigs.updater.appSchemaCollectionName)
|
|
135
|
+
) {
|
|
136
|
+
throw new Error(
|
|
137
|
+
`The appSchemaCollectionName config is not valid : ${overridingConfigs.updater.appSchemaCollectionName}`
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
this.updater.appSchemaCollectionName = overridingConfigs.updater.appSchemaCollectionName;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!_.isNil(overridingConfigs.applyUpdates)) {
|
|
144
|
+
if (!_.isBoolean(overridingConfigs.applyUpdates)) {
|
|
145
|
+
throw new Error(`The applyUpdates config must be a boolean: ${overridingConfigs.applyUpdates}`);
|
|
146
|
+
}
|
|
147
|
+
this.applyUpdates = overridingConfigs.applyUpdates;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!_.isNil(overridingConfigs.connectionString)) {
|
|
151
|
+
if (!_.isString(overridingConfigs.connectionString) || utils.isBlank(overridingConfigs.connectionString)) {
|
|
152
|
+
throw new Error(`The connectionString config is not valid : ${overridingConfigs.connectionString}`);
|
|
153
|
+
}
|
|
154
|
+
this.connectionString = overridingConfigs.connectionString;
|
|
155
|
+
} else {
|
|
156
|
+
logger.warning(`No "connectionString" config was provided: a *mocked* Mongo server will be used!`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (!_.isNil(overridingConfigs.connectionOptions)) {
|
|
160
|
+
if (!_.isObject(overridingConfigs.connectionOptions)) {
|
|
161
|
+
throw new Error(`The connectionOptions config is not valid : ${overridingConfigs.connectionString}`);
|
|
162
|
+
}
|
|
163
|
+
this.connectionOptions = overridingConfigs.connectionOptions;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (!_.isNil(overridingConfigs.mockServer) && !_.isNil(overridingConfigs.mockServer.serverVersion)) {
|
|
167
|
+
if (
|
|
168
|
+
!_.isString(overridingConfigs.mockServer.serverVersion) ||
|
|
169
|
+
utils.isBlank(overridingConfigs.mockServer.serverVersion)
|
|
170
|
+
) {
|
|
171
|
+
throw new Error(
|
|
172
|
+
`The mockServer.serverVersion config is not valid : ${overridingConfigs.mockServer.serverVersion}`
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
this.mockServer.serverVersion = overridingConfigs.mockServer.serverVersion;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './mongoUtils';
|
|
2
|
+
export * from './config/constants';
|
|
3
|
+
export { IMongooseConfigs } from './config/mongooseConfigs';
|
|
4
|
+
export * from './mongoClient';
|
|
5
|
+
|
|
6
|
+
// ==========================================
|
|
7
|
+
// We do not export the configs instance itself,
|
|
8
|
+
// only the "init()" method, so we can define
|
|
9
|
+
// which are the required parameters.
|
|
10
|
+
// ==========================================
|
|
11
|
+
export * from './config/init';
|
|
12
|
+
export * from './plugins/pagination';
|
|
13
|
+
// import { IPaginateOptions } from './plugins/pagination/specs/IPaginateOptions';
|