@mimik/mongooser 3.0.2 → 4.0.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/.claude/settings.local.json +9 -0
- package/.husky/pre-commit +0 -2
- package/.husky/pre-push +0 -2
- package/README.md +60 -41
- package/eslint.config.js +18 -11
- package/index.js +79 -59
- package/package.json +14 -19
- package/.nycrc +0 -4
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"permissions": {
|
|
3
|
+
"allow": [
|
|
4
|
+
"Bash(npx eslint index.js --no-error-on-unmatched-pattern)",
|
|
5
|
+
"Bash(npx eslint index.js)",
|
|
6
|
+
"Bash(node -e \"const p = JSON.parse\\(require\\(''fs''\\).readFileSync\\(''node_modules/globals/package.json'',''utf8''\\)\\); console.log\\(p.version\\)\")"
|
|
7
|
+
]
|
|
8
|
+
}
|
|
9
|
+
}
|
package/.husky/pre-commit
CHANGED
package/.husky/pre-push
CHANGED
package/README.md
CHANGED
|
@@ -7,72 +7,91 @@ import mongodb from '@mimik/mongooser';
|
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
* [mongooser](#module_mongooser)
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
*
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
* [module.exports(config)](#exp_module_mongooser--module.exports) ⇒ <code>object</code> ⏏
|
|
11
|
+
* _async_
|
|
12
|
+
* [~validate()](#module_mongooser--module.exports..validate) ⇒ <code>Promise</code>
|
|
13
|
+
* _sync_
|
|
14
|
+
* [~getKMSProviders()](#module_mongooser--module.exports..getKMSProviders) ⇒ <code>object</code>
|
|
15
|
+
* [~initializeSync()](#module_mongooser--module.exports..initializeSync) ⇒ <code>object</code>
|
|
16
|
+
* [~replicaSet()](#module_mongooser--module.exports..replicaSet) ⇒ <code>boolean</code>
|
|
16
17
|
|
|
17
|
-
<a name="
|
|
18
|
+
<a name="exp_module_mongooser--module.exports"></a>
|
|
18
19
|
|
|
19
|
-
###
|
|
20
|
+
### module.exports(config) ⇒ <code>object</code> ⏏
|
|
21
|
+
Creates a MongoDB connection manager for mimik microservices.
|
|
22
|
+
|
|
23
|
+
**Kind**: Exported function
|
|
24
|
+
**Returns**: <code>object</code> - Object with methods: initializeSync, validate, replicaSet, getKMSProviders.
|
|
25
|
+
|
|
26
|
+
| Param | Type | Description |
|
|
27
|
+
| --- | --- | --- |
|
|
28
|
+
| config | <code>object</code> | Service configuration object. |
|
|
29
|
+
| config.mongoSettings | <code>object</code> | MongoDB connection settings. |
|
|
30
|
+
| config.mongoSettings.url | <code>string</code> | MongoDB connection URL. |
|
|
31
|
+
| config.mongoSettings.options | <code>object</code> | Mongoose connection options. Must include `heartbeatFrequencyMS`. |
|
|
32
|
+
| [config.mongoSettings.password] | <code>string</code> | MongoDB password (masked in production logs). |
|
|
33
|
+
| config.mongoSettings.reconnectOffset | <code>number</code> | Reconnect offset in seconds. |
|
|
34
|
+
| config.mongoSettings.connectTimeout | <code>number</code> | Connection timeout in seconds. |
|
|
35
|
+
| config.mongoSettings.validationCheck | <code>number</code> | Validation check delay in milliseconds. |
|
|
36
|
+
| config.nodeEnvironment | <code>string</code> | Current node environment (e.g. 'production'). |
|
|
37
|
+
| [config.encryption] | <code>object</code> | Encryption settings for client-side field level encryption. |
|
|
38
|
+
| [config.encryption.set] | <code>string</code> | Set to 'on' to enable auto-encryption. |
|
|
39
|
+
| [config.encryption.kmsProvider] | <code>string</code> | KMS provider type ('local' or 'aws'). |
|
|
40
|
+
| [config.encryption.localMasterKey] | <code>string</code> | Base64-encoded local master key. |
|
|
41
|
+
| [config.encryption.aws] | <code>object</code> | AWS KMS provider settings. |
|
|
42
|
+
| [config.encryption.database] | <code>string</code> | Database name for key vault. |
|
|
43
|
+
| [config.encryption.keyVaultTable] | <code>string</code> | Collection name for key vault. |
|
|
44
|
+
|
|
45
|
+
<a name="module_mongooser--module.exports..validate"></a>
|
|
46
|
+
|
|
47
|
+
#### module.exports~validate() ⇒ <code>Promise</code>
|
|
20
48
|
Database connection validation.
|
|
21
49
|
|
|
22
|
-
**Kind**: inner method of [<code>
|
|
23
|
-
**Returns**: <code>Promise</code> - .
|
|
50
|
+
**Kind**: inner method of [<code>module.exports</code>](#exp_module_mongooser--module.exports)
|
|
51
|
+
**Returns**: <code>Promise</code> - Resolves to null on success.
|
|
24
52
|
**Category**: async
|
|
25
53
|
**Throws**:
|
|
26
54
|
|
|
27
|
-
- <code>
|
|
55
|
+
- <code>Error</code> Will throw an error if the database connection is not `connected`.
|
|
28
56
|
|
|
29
57
|
Will exit 1 if the connection request times out and the connection state is not `connected`.
|
|
30
58
|
|
|
31
59
|
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>
|
|
32
|
-
|
|
33
|
-
<a name="module_mongooser..getKMSProviders"></a>
|
|
60
|
+
<a name="module_mongooser--module.exports..getKMSProviders"></a>
|
|
34
61
|
|
|
35
|
-
|
|
62
|
+
#### module.exports~getKMSProviders() ⇒ <code>object</code>
|
|
36
63
|
AutoEncryption KMSProvider.
|
|
37
64
|
|
|
38
|
-
**Kind**: inner method of [<code>
|
|
65
|
+
**Kind**: inner method of [<code>module.exports</code>](#exp_module_mongooser--module.exports)
|
|
39
66
|
**Returns**: <code>object</code> - The KMSProviders object.
|
|
40
67
|
|
|
41
|
-
Will return the kmsProvider settings for the service.
|
|
68
|
+
Will return the kmsProvider settings for the service.
|
|
69
|
+
Requires `config.encryption` to be provided to the factory.
|
|
42
70
|
**Category**: sync
|
|
43
|
-
|
|
71
|
+
**Throws**:
|
|
72
|
+
|
|
73
|
+
- <code>TypeError</code> If encryption config was not provided to the factory.
|
|
74
|
+
|
|
75
|
+
<a name="module_mongooser--module.exports..initializeSync"></a>
|
|
44
76
|
|
|
45
|
-
|
|
77
|
+
#### module.exports~initializeSync() ⇒ <code>object</code>
|
|
46
78
|
Database initialization.
|
|
47
79
|
|
|
48
|
-
**Kind**: inner method of [<code>
|
|
49
|
-
**Returns**: <code>object</code> - The database object.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
"keyVaultClient" : <object>,
|
|
55
|
-
"keyVaultNamespace": <string>, // namespace for the keyvault collection
|
|
56
|
-
"kmsProviders": <object>, // KeyManager service settings
|
|
57
|
-
"schemaMap": <object>, // json object for defining encryption schema
|
|
58
|
-
"bypassAutoEncryption": <boolean>
|
|
59
|
-
}
|
|
60
|
-
```
|
|
61
|
-
Check MongoDB documentation for more details. https://docs.mongodb.com/manual/reference/method/Mongo/#clientsidefieldlevelencryptionoptions
|
|
80
|
+
**Kind**: inner method of [<code>module.exports</code>](#exp_module_mongooser--module.exports)
|
|
81
|
+
**Returns**: <code>object</code> - The mongoose database object. Connection is established asynchronously in the background.
|
|
82
|
+
|
|
83
|
+
When encryption is enabled, configures auto-encryption by setting `keyVaultNamespace` and `kmsProviders`
|
|
84
|
+
on the mongoose connection options.
|
|
85
|
+
Check MongoDB documentation for the full autoEncryption specification: https://docs.mongodb.com/manual/reference/method/Mongo/#clientsidefieldlevelencryptionoptions
|
|
62
86
|
|
|
63
87
|
Will exit 1 if the connection request generates an error or the connection state is `disconnected` or `disconnecting`.
|
|
64
88
|
**Category**: sync
|
|
65
|
-
**Requires**: <code>module
|
|
66
|
-
|
|
67
|
-
| Param | Type | Description |
|
|
68
|
-
| --- | --- | --- |
|
|
69
|
-
| autoEncryption | <code>object</code> | Encryption settings for mongo connection. |
|
|
70
|
-
|
|
71
|
-
<a name="module_mongooser..replicat"></a>
|
|
89
|
+
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>
|
|
90
|
+
<a name="module_mongooser--module.exports..replicaSet"></a>
|
|
72
91
|
|
|
73
|
-
|
|
92
|
+
#### module.exports~replicaSet() ⇒ <code>boolean</code>
|
|
74
93
|
Indicates if the replica is set.
|
|
75
94
|
|
|
76
|
-
**Kind**: inner method of [<code>
|
|
95
|
+
**Kind**: inner method of [<code>module.exports</code>](#exp_module_mongooser--module.exports)
|
|
77
96
|
**Returns**: <code>boolean</code> - `True` if replica is set, `False` if not.
|
|
78
97
|
**Category**: sync
|
package/eslint.config.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import globals from 'globals';
|
|
1
2
|
import importPlugin from 'eslint-plugin-import';
|
|
2
3
|
import js from '@eslint/js';
|
|
3
4
|
import processDoc from '@mimik/eslint-plugin-document-env';
|
|
@@ -10,6 +11,9 @@ const MAX_LINES_IN_FUNCTION = 150;
|
|
|
10
11
|
const MAX_STATEMENTS_IN_FUNCTION = 45;
|
|
11
12
|
const MIN_KEYS_IN_OBJECT = 10;
|
|
12
13
|
const MAX_COMPLEXITY = 30;
|
|
14
|
+
const ECMA_VERSION = 'latest';
|
|
15
|
+
const MAX_DEPTH = 6;
|
|
16
|
+
const ALLOWED_CONSTANTS = [0, 1, -1];
|
|
13
17
|
|
|
14
18
|
export default [
|
|
15
19
|
{
|
|
@@ -23,18 +27,18 @@ export default [
|
|
|
23
27
|
processDoc,
|
|
24
28
|
},
|
|
25
29
|
languageOptions: {
|
|
26
|
-
ecmaVersion:
|
|
30
|
+
ecmaVersion: ECMA_VERSION,
|
|
27
31
|
globals: {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
it: 'readonly',
|
|
31
|
-
require: 'readonly',
|
|
32
|
+
...globals.mocha,
|
|
33
|
+
...globals.nodeBuiltin,
|
|
32
34
|
},
|
|
33
35
|
sourceType: 'module',
|
|
34
36
|
},
|
|
35
37
|
rules: {
|
|
36
38
|
'@stylistic/brace-style': ['warn', 'stroustrup', { allowSingleLine: true }],
|
|
37
39
|
'@stylistic/line-comment-position': ['off'],
|
|
40
|
+
'@stylistic/max-len': ['warn', MAX_LENGTH_LINE, { ignoreComments: true, ignoreStrings: true, ignoreRegExpLiterals: true }],
|
|
41
|
+
'@stylistic/quotes': ['warn', 'single'],
|
|
38
42
|
'@stylistic/semi': ['error', 'always'],
|
|
39
43
|
'capitalized-comments': ['off'],
|
|
40
44
|
'complexity': ['error', MAX_COMPLEXITY],
|
|
@@ -44,20 +48,23 @@ export default [
|
|
|
44
48
|
'import/no-unresolved': ['error', { amd: true, caseSensitiveStrict: true, commonjs: true }],
|
|
45
49
|
'init-declarations': ['off'],
|
|
46
50
|
'linebreak-style': ['off'],
|
|
47
|
-
'max-
|
|
48
|
-
'max-
|
|
49
|
-
'max-lines
|
|
51
|
+
'max-depth': ['error', MAX_DEPTH],
|
|
52
|
+
'max-len': ['off'],
|
|
53
|
+
'max-lines': ['warn', { max: MAX_LINES_IN_FILES, skipComments: true, skipBlankLines: true }],
|
|
54
|
+
'max-lines-per-function': ['warn', { max: MAX_LINES_IN_FUNCTION, skipComments: true, skipBlankLines: true }],
|
|
50
55
|
'max-params': ['error', MAX_FUNCTION_PARAMETERS],
|
|
51
56
|
'max-statements': ['warn', MAX_STATEMENTS_IN_FUNCTION],
|
|
52
|
-
'no-confusing-arrow': ['off'],
|
|
57
|
+
'no-confusing-arrow': ['off'],
|
|
53
58
|
'no-inline-comments': ['off'],
|
|
59
|
+
'no-magic-numbers': ['error', { ignore: ALLOWED_CONSTANTS, enforceConst: true, detectObjects: true }],
|
|
54
60
|
'no-process-env': ['error'],
|
|
55
61
|
'no-ternary': ['off'],
|
|
56
62
|
'no-undefined': ['off'],
|
|
57
63
|
'one-var': ['error', 'never'],
|
|
58
64
|
'processDoc/validate-document-env': ['error'],
|
|
59
|
-
'quotes': ['
|
|
60
|
-
'sort-
|
|
65
|
+
'quotes': ['off'],
|
|
66
|
+
'sort-imports': ['error', { allowSeparatedGroups: true }],
|
|
67
|
+
'sort-keys': ['error', 'asc', { caseSensitive: true, minKeys: MIN_KEYS_IN_OBJECT, natural: false, allowLineSeparatedGroups: true }],
|
|
61
68
|
},
|
|
62
69
|
},
|
|
63
70
|
];
|
package/index.js
CHANGED
|
@@ -7,11 +7,10 @@ import {
|
|
|
7
7
|
setInterval,
|
|
8
8
|
} from 'timers';
|
|
9
9
|
import { Buffer } from 'buffer';
|
|
10
|
-
import Promise from 'bluebird';
|
|
11
10
|
import { getCorrelationId } from '@mimik/request-helper';
|
|
12
11
|
import logger from '@mimik/sumologic-winston-logger';
|
|
13
12
|
import mongoose from 'mongoose';
|
|
14
|
-
import
|
|
13
|
+
import { setTimeout } from 'node:timers/promises';
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* @module mongooser
|
|
@@ -20,7 +19,6 @@ import replace from 'lodash.replace';
|
|
|
20
19
|
*/
|
|
21
20
|
|
|
22
21
|
const type = 'mongo';
|
|
23
|
-
const correlationId = getCorrelationId(`${type}-database-start`);
|
|
24
22
|
|
|
25
23
|
const DISCONNECTED = 0;
|
|
26
24
|
const CONNECTED = 1;
|
|
@@ -29,21 +27,45 @@ const DISCONNECTING = 3;
|
|
|
29
27
|
const ISSUE_EXIT = 1;
|
|
30
28
|
const MILLI_SEC = 1000;
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Creates a MongoDB connection manager for mimik microservices.
|
|
32
|
+
*
|
|
33
|
+
* @param {object} config - Service configuration object.
|
|
34
|
+
* @param {object} config.mongoSettings - MongoDB connection settings.
|
|
35
|
+
* @param {string} config.mongoSettings.url - MongoDB connection URL.
|
|
36
|
+
* @param {object} config.mongoSettings.options - Mongoose connection options. Must include `heartbeatFrequencyMS`.
|
|
37
|
+
* @param {string} [config.mongoSettings.password] - MongoDB password (masked in production logs).
|
|
38
|
+
* @param {number} config.mongoSettings.reconnectOffset - Reconnect offset in seconds.
|
|
39
|
+
* @param {number} config.mongoSettings.connectTimeout - Connection timeout in seconds.
|
|
40
|
+
* @param {number} config.mongoSettings.validationCheck - Validation check delay in milliseconds.
|
|
41
|
+
* @param {string} config.nodeEnvironment - Current node environment (e.g. 'production').
|
|
42
|
+
* @param {object} [config.encryption] - Encryption settings for client-side field level encryption.
|
|
43
|
+
* @param {string} [config.encryption.set] - Set to 'on' to enable auto-encryption.
|
|
44
|
+
* @param {string} [config.encryption.kmsProvider] - KMS provider type ('local' or 'aws').
|
|
45
|
+
* @param {string} [config.encryption.localMasterKey] - Base64-encoded local master key.
|
|
46
|
+
* @param {object} [config.encryption.aws] - AWS KMS provider settings.
|
|
47
|
+
* @param {string} [config.encryption.database] - Database name for key vault.
|
|
48
|
+
* @param {string} [config.encryption.keyVaultTable] - Collection name for key vault.
|
|
49
|
+
* @return {object} Object with methods: initializeSync, validate, replicaSet, getKMSProviders.
|
|
50
|
+
*/
|
|
34
51
|
export default (config) => {
|
|
35
52
|
const { mongoSettings, encryption } = config;
|
|
36
53
|
const { options } = mongoSettings;
|
|
54
|
+
const correlationId = getCorrelationId(`${type}-database-start`);
|
|
55
|
+
let fatal = false;
|
|
56
|
+
let listenersRegistered = false;
|
|
37
57
|
|
|
38
58
|
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
59
|
+
* AutoEncryption KMSProvider.
|
|
60
|
+
*
|
|
61
|
+
* @function getKMSProviders
|
|
62
|
+
* @category sync
|
|
63
|
+
* @return {object} The KMSProviders object.
|
|
64
|
+
*
|
|
65
|
+
* Will return the kmsProvider settings for the service.
|
|
66
|
+
* Requires `config.encryption` to be provided to the factory.
|
|
67
|
+
* @throws {TypeError} If encryption config was not provided to the factory.
|
|
68
|
+
*/
|
|
47
69
|
const getKMSProviders = () => {
|
|
48
70
|
const kmsProviders = {};
|
|
49
71
|
const provider = encryption.kmsProvider.toLowerCase();
|
|
@@ -60,45 +82,39 @@ export default (config) => {
|
|
|
60
82
|
/**
|
|
61
83
|
* Database initialization.
|
|
62
84
|
*
|
|
63
|
-
* @requires sumologic-winston-logger
|
|
85
|
+
* @requires @mimik/sumologic-winston-logger
|
|
64
86
|
*
|
|
65
|
-
* @
|
|
87
|
+
* @function initializeSync
|
|
66
88
|
* @category sync
|
|
67
|
-
* @return {object} The database object.
|
|
89
|
+
* @return {object} The mongoose database object. Connection is established asynchronously in the background.
|
|
68
90
|
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* "keyVaultClient" : <object>,
|
|
73
|
-
* "keyVaultNamespace": <string>, // namespace for the keyvault collection
|
|
74
|
-
* "kmsProviders": <object>, // KeyManager service settings
|
|
75
|
-
* "schemaMap": <object>, // json object for defining encryption schema
|
|
76
|
-
* "bypassAutoEncryption": <boolean>
|
|
77
|
-
* }
|
|
78
|
-
*```
|
|
79
|
-
* Check MongoDB documentation for more details. https://docs.mongodb.com/manual/reference/method/Mongo/#clientsidefieldlevelencryptionoptions
|
|
91
|
+
* When encryption is enabled, configures auto-encryption by setting `keyVaultNamespace` and `kmsProviders`
|
|
92
|
+
* on the mongoose connection options.
|
|
93
|
+
* Check MongoDB documentation for the full autoEncryption specification: https://docs.mongodb.com/manual/reference/method/Mongo/#clientsidefieldlevelencryptionoptions
|
|
80
94
|
*
|
|
81
95
|
* Will exit 1 if the connection request generates an error or the connection state is `disconnected` or `disconnecting`.
|
|
82
96
|
*/
|
|
83
97
|
const initializeSync = () => {
|
|
84
98
|
let interval;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
99
|
+
const connectOptions = encryption?.set === 'on'
|
|
100
|
+
? {
|
|
101
|
+
...options,
|
|
102
|
+
autoEncryption: {
|
|
103
|
+
keyVaultNamespace: `${encryption.database}.${encryption.keyVaultTable}`,
|
|
104
|
+
kmsProviders: getKMSProviders(),
|
|
105
|
+
},
|
|
106
|
+
}
|
|
107
|
+
: options;
|
|
92
108
|
|
|
93
109
|
if (mongoose.connection.readyState === DISCONNECTED && !fatal) {
|
|
94
110
|
if (isProd(config.nodeEnvironment) && mongoSettings.password) {
|
|
95
111
|
const mongoSettingsClone = { ...mongoSettings };
|
|
96
112
|
|
|
97
|
-
mongoSettingsClone.url =
|
|
113
|
+
mongoSettingsClone.url = mongoSettingsClone.url.replace(mongoSettings.password, MASK);
|
|
98
114
|
logger.info('creating a database connection', { type, settings: mongoSettingsClone }, correlationId);
|
|
99
115
|
}
|
|
100
116
|
else logger.info('creating a database connection', { type, settings: mongoSettings }, correlationId);
|
|
101
|
-
mongoose.connect(mongoSettings.url,
|
|
117
|
+
mongoose.connect(mongoSettings.url, connectOptions)
|
|
102
118
|
.then(() => mongoose)
|
|
103
119
|
.catch((error) => {
|
|
104
120
|
fatal = true;
|
|
@@ -108,21 +124,24 @@ export default (config) => {
|
|
|
108
124
|
});
|
|
109
125
|
}
|
|
110
126
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
127
|
+
if (!listenersRegistered) {
|
|
128
|
+
listenersRegistered = true;
|
|
129
|
+
mongoose.connection.on('disconnected', () => {
|
|
130
|
+
interval = setInterval(() => {
|
|
131
|
+
const state = mongoose.connection.readyState;
|
|
132
|
+
|
|
133
|
+
if ((state === DISCONNECTED || state === DISCONNECTING) && !fatal) {
|
|
134
|
+
fatal = true;
|
|
135
|
+
logger.error('Fatal error: Could not connect to database', { type, error: state }, correlationId);
|
|
136
|
+
clearInterval(interval);
|
|
137
|
+
logger.flushAndExit(ISSUE_EXIT);
|
|
138
|
+
}
|
|
139
|
+
}, (connectOptions.heartbeatFrequencyMS + (mongoSettings.reconnectOffset * MILLI_SEC)));
|
|
140
|
+
});
|
|
141
|
+
mongoose.connection.on('connected', () => {
|
|
142
|
+
if (interval) clearInterval(interval);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
126
145
|
return mongoose;
|
|
127
146
|
};
|
|
128
147
|
|
|
@@ -134,9 +153,8 @@ export default (config) => {
|
|
|
134
153
|
*
|
|
135
154
|
* @function validate
|
|
136
155
|
* @category async
|
|
137
|
-
* @returns {Promise}.
|
|
138
|
-
* @
|
|
139
|
-
* @throws {Promise} Will throw an error if the database connection is not `connected`.
|
|
156
|
+
* @returns {Promise} Resolves to null on success.
|
|
157
|
+
* @throws {Error} Will throw an error if the database connection is not `connected`.
|
|
140
158
|
*
|
|
141
159
|
* Will exit 1 if the connection request times out and the connection state is not `connected`.
|
|
142
160
|
*/
|
|
@@ -152,18 +170,20 @@ export default (config) => {
|
|
|
152
170
|
clearInterval(interval);
|
|
153
171
|
logger.flushAndExit(ISSUE_EXIT);
|
|
154
172
|
}
|
|
155
|
-
}, mongoSettings.connectTimeout * MILLI_SEC); // convert
|
|
173
|
+
}, mongoSettings.connectTimeout * MILLI_SEC); // convert to milliseconds
|
|
156
174
|
|
|
157
|
-
return
|
|
175
|
+
return setTimeout(mongoSettings.validationCheck).then(() => {
|
|
158
176
|
const state = mongoose.connection.readyState;
|
|
159
177
|
|
|
160
178
|
if (state === DISCONNECTED || state === DISCONNECTING) {
|
|
179
|
+
clearInterval(interval);
|
|
161
180
|
const error = new Error(`connection not established: ${mongoose.connection.readyState}`);
|
|
162
181
|
|
|
163
182
|
logger.error('database connection error', { type, error }, correlationId);
|
|
164
183
|
throw error;
|
|
165
184
|
}
|
|
166
185
|
if (state === CONNECTING) {
|
|
186
|
+
clearInterval(interval);
|
|
167
187
|
return validate();
|
|
168
188
|
}
|
|
169
189
|
clearInterval(interval);
|
|
@@ -176,11 +196,11 @@ export default (config) => {
|
|
|
176
196
|
*
|
|
177
197
|
* Indicates if the replica is set.
|
|
178
198
|
*
|
|
179
|
-
* @function
|
|
199
|
+
* @function replicaSet
|
|
180
200
|
* @category sync
|
|
181
201
|
* @return {boolean} `True` if replica is set, `False` if not.
|
|
182
202
|
*/
|
|
183
|
-
const
|
|
203
|
+
const replicaSet = () => {
|
|
184
204
|
const isReplicaSet = Boolean(mongoose.connection.client.s.options.replicaSet);
|
|
185
205
|
return isReplicaSet;
|
|
186
206
|
};
|
|
@@ -188,7 +208,7 @@ export default (config) => {
|
|
|
188
208
|
return {
|
|
189
209
|
initializeSync,
|
|
190
210
|
validate,
|
|
191
|
-
|
|
211
|
+
replicaSet,
|
|
192
212
|
getKMSProviders,
|
|
193
213
|
};
|
|
194
214
|
};
|
package/package.json
CHANGED
|
@@ -1,26 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mimik/mongooser",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Helper for setting up mongodb using mongoose for mimik microservices",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"exports": "./index.js",
|
|
7
8
|
"engines": {
|
|
8
|
-
"node": ">=
|
|
9
|
+
"node": ">=24.0.0"
|
|
9
10
|
},
|
|
10
11
|
"scripts": {
|
|
11
12
|
"lint": "eslint . --no-error-on-unmatched-pattern",
|
|
12
13
|
"docs": "jsdoc2md index.js > README.md",
|
|
13
14
|
"test": "echo \"Error: no test specified\" && exit 0",
|
|
14
|
-
"test-ci": "
|
|
15
|
+
"test-ci": "c8 --reporter=lcov --reporter=text npm test",
|
|
15
16
|
"prepublishOnly": "npm run docs && npm run lint && npm run test-ci",
|
|
16
17
|
"commit-ready": "npm run docs && npm run lint && npm run test-ci"
|
|
17
18
|
},
|
|
18
|
-
"husky": {
|
|
19
|
-
"hooks": {
|
|
20
|
-
"pre-commit": "npm run commit-ready",
|
|
21
|
-
"pre-push": "npm run test"
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
19
|
"keywords": [
|
|
25
20
|
"mimik",
|
|
26
21
|
"microservice"
|
|
@@ -32,20 +27,20 @@
|
|
|
32
27
|
"url": "https://bitbucket.org/mimiktech/mongooser"
|
|
33
28
|
},
|
|
34
29
|
"dependencies": {
|
|
35
|
-
"@mimik/lib-filters": "^2.0.
|
|
36
|
-
"@mimik/request-helper": "^2.0.
|
|
37
|
-
"@mimik/sumologic-winston-logger": "^2.
|
|
38
|
-
"
|
|
39
|
-
"lodash.replace": "4.1.4",
|
|
40
|
-
"mongoose": "8.16.4"
|
|
30
|
+
"@mimik/lib-filters": "^2.0.7",
|
|
31
|
+
"@mimik/request-helper": "^2.0.5",
|
|
32
|
+
"@mimik/sumologic-winston-logger": "^2.1.14",
|
|
33
|
+
"mongoose": "9.2.2"
|
|
41
34
|
},
|
|
42
35
|
"devDependencies": {
|
|
43
|
-
"@eslint/js": "9.
|
|
36
|
+
"@eslint/js": "9.39.3",
|
|
44
37
|
"@mimik/eslint-plugin-document-env": "^2.0.8",
|
|
45
|
-
"@stylistic/eslint-plugin": "5.
|
|
46
|
-
"
|
|
38
|
+
"@stylistic/eslint-plugin": "5.9.0",
|
|
39
|
+
"c8": "11.0.0",
|
|
40
|
+
"eslint": "9.39.3",
|
|
47
41
|
"eslint-plugin-import": "2.32.0",
|
|
42
|
+
"globals": "17.3.0",
|
|
48
43
|
"husky": "9.1.7",
|
|
49
|
-
"jsdoc-to-markdown": "9.1.
|
|
44
|
+
"jsdoc-to-markdown": "9.1.3"
|
|
50
45
|
}
|
|
51
46
|
}
|
package/.nycrc
DELETED