@diia-inhouse/db 4.13.1 → 10.8.1
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/LICENCE.md +59 -59
- package/dist/cli/cleanupExpiredData.js +137 -0
- package/dist/cli/cleanupExpiredData.js.map +1 -0
- package/dist/cli/index.js +105 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/runMigration.js +106 -0
- package/dist/cli/runMigration.js.map +1 -0
- package/dist/cli/syncIndexes.js +78 -0
- package/dist/cli/syncIndexes.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/interfaces/database.js +3 -0
- package/dist/interfaces/database.js.map +1 -0
- package/dist/interfaces/errors.js +5 -1
- package/dist/interfaces/errors.js.map +1 -1
- package/dist/interfaces/index.js +3 -0
- package/dist/interfaces/index.js.map +1 -1
- package/dist/interfaces/metrics.js +20 -0
- package/dist/interfaces/metrics.js.map +1 -0
- package/dist/interfaces/models/index.js +19 -0
- package/dist/interfaces/models/index.js.map +1 -0
- package/dist/mongo.js +18 -3
- package/dist/mongo.js.map +1 -1
- package/dist/postgres.js +19 -0
- package/dist/postgres.js.map +1 -0
- package/dist/postgresUtils.js +20 -0
- package/dist/postgresUtils.js.map +1 -0
- package/dist/{models → schemas}/counter.js +2 -1
- package/dist/schemas/counter.js.map +1 -0
- package/dist/{models → schemas}/encryptedStorage.js +4 -2
- package/dist/schemas/encryptedStorage.js.map +1 -0
- package/dist/schemas/index.js +20 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/statusHistoryItem.js +12 -0
- package/dist/schemas/statusHistoryItem.js.map +1 -0
- package/dist/services/counter.js +60 -8
- package/dist/services/counter.js.map +1 -1
- package/dist/services/database.js +123 -55
- package/dist/services/database.js.map +1 -1
- package/dist/services/encryptedStorage.js +136 -15
- package/dist/services/encryptedStorage.js.map +1 -1
- package/dist/services/index.js +1 -0
- package/dist/services/index.js.map +1 -1
- package/dist/services/postgresDatabase.js +25 -0
- package/dist/services/postgresDatabase.js.map +1 -0
- package/dist/tables/counter.js +12 -0
- package/dist/tables/counter.js.map +1 -0
- package/dist/tables/encryptedStorage.js +14 -0
- package/dist/tables/encryptedStorage.js.map +1 -0
- package/dist/tables/index.js +19 -0
- package/dist/tables/index.js.map +1 -0
- package/dist/types/cli/cleanupExpiredData.d.ts +11 -0
- package/dist/types/cli/index.d.ts +2 -0
- package/dist/types/cli/runMigration.d.ts +11 -0
- package/dist/types/cli/syncIndexes.d.ts +7 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/interfaces/database.d.ts +8 -0
- package/dist/types/interfaces/dbConfig.d.ts +8 -29
- package/dist/types/interfaces/errors.d.ts +5 -1
- package/dist/types/interfaces/index.d.ts +3 -25
- package/dist/types/interfaces/metrics.d.ts +20 -0
- package/dist/types/interfaces/migrateMongoConfig.d.ts +0 -24
- package/dist/types/interfaces/models/counter.d.ts +0 -27
- package/dist/types/interfaces/models/encryptedStorage.d.ts +1 -27
- package/dist/types/interfaces/models/index.d.ts +2 -0
- package/dist/types/postgres.d.ts +2 -0
- package/dist/types/postgresUtils.d.ts +26 -0
- package/dist/types/schemas/counter.d.ts +12 -0
- package/dist/types/schemas/encryptedStorage.d.ts +12 -0
- package/dist/types/schemas/index.d.ts +3 -0
- package/dist/types/schemas/statusHistoryItem.d.ts +3 -0
- package/dist/types/services/counter.d.ts +11 -0
- package/dist/types/services/database.d.ts +33 -33
- package/dist/types/services/encryptedStorage.d.ts +60 -34
- package/dist/types/services/index.d.ts +1 -0
- package/dist/types/services/postgresDatabase.d.ts +8 -0
- package/dist/types/tables/counter.d.ts +94 -0
- package/dist/types/tables/encryptedStorage.d.ts +111 -0
- package/dist/types/tables/index.d.ts +2 -0
- package/package.json +62 -61
- package/dist/models/counter.js.map +0 -1
- package/dist/models/encryptedStorage.js.map +0 -1
- package/dist/types/models/counter.d.ts +0 -30
- package/dist/types/models/encryptedStorage.d.ts +0 -30
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
https://github.com/mongodb/mongo-c-driver/blob/master/src/libmongoc/src/mongoc/mongoc-error.h
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.MongoDBErrorCode = void 0;
|
|
8
|
+
exports.PostgresErrorCode = exports.MongoDBErrorCode = void 0;
|
|
9
9
|
var MongoDBErrorCode;
|
|
10
10
|
(function (MongoDBErrorCode) {
|
|
11
11
|
MongoDBErrorCode[MongoDBErrorCode["AlreadyInitialized"] = 23] = "AlreadyInitialized";
|
|
@@ -42,5 +42,9 @@ var MongoDBErrorCode;
|
|
|
42
42
|
MongoDBErrorCode[MongoDBErrorCode["UnsupportedFormat"] = 12] = "UnsupportedFormat";
|
|
43
43
|
MongoDBErrorCode[MongoDBErrorCode["UserNotFound"] = 11] = "UserNotFound";
|
|
44
44
|
MongoDBErrorCode[MongoDBErrorCode["WriteConcernError"] = 64] = "WriteConcernError";
|
|
45
|
+
MongoDBErrorCode[MongoDBErrorCode["WriteConflict"] = 112] = "WriteConflict";
|
|
45
46
|
})(MongoDBErrorCode || (exports.MongoDBErrorCode = MongoDBErrorCode = {}));
|
|
47
|
+
exports.PostgresErrorCode = {
|
|
48
|
+
InvalidCredentials: '28P01',
|
|
49
|
+
};
|
|
46
50
|
//# sourceMappingURL=errors.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/interfaces/errors.ts"],"names":[],"mappings":";AAAA;;;;EAIE;;;AAEF,IAAY,
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/interfaces/errors.ts"],"names":[],"mappings":";AAAA;;;;EAIE;;;AAEF,IAAY,gBAoCX;AApCD,WAAY,gBAAgB;IACxB,oFAAuB,CAAA;IACvB,wFAAyB,CAAA;IACzB,+DAAY,CAAA;IACZ,oFAAuB,CAAA;IACvB,kFAAsB,CAAA;IACtB,4FAA2B,CAAA;IAC3B,2EAAoB,CAAA;IACpB,sFAAwB,CAAA;IACxB,yEAAiB,CAAA;IACjB,mFAAsB,CAAA;IACtB,uEAAgB,CAAA;IAChB,6EAAmB,CAAA;IACnB,gFAAqB,CAAA;IACrB,0EAAkB,CAAA;IAClB,yEAAiB,CAAA;IACjB,sEAAgB,CAAA;IAChB,0EAAkB,CAAA;IAClB,sEAAgB,CAAA;IAChB,sEAAgB,CAAA;IAChB,gFAAqB,CAAA;IACrB,8EAAoB,CAAA;IACpB,iEAAa,CAAA;IACb,gEAAa,CAAA;IACb,0EAAkB,CAAA;IAClB,0EAAkB,CAAA;IAClB,wFAAyB,CAAA;IACzB,mFAAwB,CAAA;IACxB,0FAA0B,CAAA;IAC1B,wEAAiB,CAAA;IACjB,wEAAiB,CAAA;IACjB,uEAAgB,CAAA;IAChB,kFAAsB,CAAA;IACtB,wEAAiB,CAAA;IACjB,kFAAsB,CAAA;IACtB,2EAAmB,CAAA;AACvB,CAAC,EApCW,gBAAgB,gCAAhB,gBAAgB,QAoC3B;AAEY,QAAA,iBAAiB,GAAG;IAC7B,kBAAkB,EAAE,OAAO;CACrB,CAAA"}
|
package/dist/interfaces/index.js
CHANGED
|
@@ -15,8 +15,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("mongoose"), exports);
|
|
18
|
+
__exportStar(require("./database"), exports);
|
|
18
19
|
__exportStar(require("./dbConfig"), exports);
|
|
19
20
|
__exportStar(require("./errors"), exports);
|
|
20
21
|
__exportStar(require("./migrateMongoConfig"), exports);
|
|
21
22
|
__exportStar(require("./mongoStatus"), exports);
|
|
23
|
+
__exportStar(require("./metrics"), exports);
|
|
24
|
+
__exportStar(require("./models"), exports);
|
|
22
25
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AAExB,6CAA0B;AAE1B,2CAAwB;AAExB,uDAAoC;AAEpC,gDAA6B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AAExB,6CAA0B;AAE1B,6CAA0B;AAE1B,2CAAwB;AAExB,uDAAoC;AAEpC,gDAA6B;AAE7B,4CAAyB;AAEzB,2CAAwB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mongodbOperationsDefaultBuckets = exports.mongodbOperationsAllowedFields = exports.MongodbOperationResult = void 0;
|
|
4
|
+
var MongodbOperationResult;
|
|
5
|
+
(function (MongodbOperationResult) {
|
|
6
|
+
MongodbOperationResult["Successful"] = "successful";
|
|
7
|
+
MongodbOperationResult["Failed"] = "failed";
|
|
8
|
+
})(MongodbOperationResult || (exports.MongodbOperationResult = MongodbOperationResult = {}));
|
|
9
|
+
class MongodbOperationsLabelsMapConcrete {
|
|
10
|
+
operation = '';
|
|
11
|
+
status = MongodbOperationResult.Successful;
|
|
12
|
+
database = '';
|
|
13
|
+
collection = '';
|
|
14
|
+
}
|
|
15
|
+
exports.mongodbOperationsAllowedFields = Object.keys(new MongodbOperationsLabelsMapConcrete());
|
|
16
|
+
// From 1ms up to 30s
|
|
17
|
+
exports.mongodbOperationsDefaultBuckets = [
|
|
18
|
+
0.001, 0.003, 0.005, 0.01, 0.015, 0.03, 0.05, 0.07, 0.09, 0.1, 0.15, 0.3, 0.5, 0.7, 0.9, 1, 5, 10, 15, 20, 25, 30,
|
|
19
|
+
];
|
|
20
|
+
//# sourceMappingURL=metrics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metrics.js","sourceRoot":"","sources":["../../src/interfaces/metrics.ts"],"names":[],"mappings":";;;AAAA,IAAY,sBAGX;AAHD,WAAY,sBAAsB;IAC9B,mDAAyB,CAAA;IACzB,2CAAiB,CAAA;AACrB,CAAC,EAHW,sBAAsB,sCAAtB,sBAAsB,QAGjC;AAUD,MAAM,kCAAkC;IACpC,SAAS,GAAG,EAAE,CAAA;IAEd,MAAM,GAA2B,sBAAsB,CAAC,UAAU,CAAA;IAElE,QAAQ,GAAG,EAAE,CAAA;IAEb,UAAU,GAAG,EAAE,CAAA;CAClB;AAIY,QAAA,8BAA8B,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,kCAAkC,EAAE,CAAyC,CAAA;AAE3I,qBAAqB;AACR,QAAA,+BAA+B,GAAG;IAC3C,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE;CACpH,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./counter"), exports);
|
|
18
|
+
__exportStar(require("./encryptedStorage"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/models/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAAyB;AAEzB,qDAAkC"}
|
package/dist/mongo.js
CHANGED
|
@@ -10,8 +10,23 @@ const errors_1 = require("@diia-inhouse/errors");
|
|
|
10
10
|
const interfaces_1 = require("./interfaces");
|
|
11
11
|
exports.MongoHelper = {
|
|
12
12
|
async getMigrateMongoConfig() {
|
|
13
|
-
const
|
|
13
|
+
const logger = new diia_logger_1.default();
|
|
14
|
+
const envService = new env_1.EnvService(logger);
|
|
14
15
|
await envService.init();
|
|
16
|
+
const originalExit = process.exit;
|
|
17
|
+
process.exit = (code) => {
|
|
18
|
+
logger.info('Destroying the Env service on process exit...', { code });
|
|
19
|
+
return envService
|
|
20
|
+
.onDestroy()
|
|
21
|
+
.then(() => {
|
|
22
|
+
logger.info('Env service destroyed, exiting the process...', { code });
|
|
23
|
+
return originalExit(code);
|
|
24
|
+
})
|
|
25
|
+
.catch((err) => {
|
|
26
|
+
logger.error('Env service destroy error', { err });
|
|
27
|
+
originalExit(code);
|
|
28
|
+
});
|
|
29
|
+
};
|
|
15
30
|
const user = await envService.getSecret('MONGO_USER', { accessor: 'username', nullable: true });
|
|
16
31
|
const password = await envService.getSecret('MONGO_PASSWORD', { accessor: 'password', nullable: true });
|
|
17
32
|
return {
|
|
@@ -48,9 +63,9 @@ exports.MongoHelper = {
|
|
|
48
63
|
return mongoUrl;
|
|
49
64
|
},
|
|
50
65
|
getMongoErrorDupField(msg, fields) {
|
|
51
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
52
66
|
for (const field of fields) {
|
|
53
|
-
|
|
67
|
+
// eslint-disable-next-line security/detect-non-literal-regexp
|
|
68
|
+
const fieldRegExp = new RegExp(`${field}_[0-9] dup key`); // nosemgrep: eslint.detect-non-literal-regexp
|
|
54
69
|
if (fieldRegExp.test(msg)) {
|
|
55
70
|
return field;
|
|
56
71
|
}
|
package/dist/mongo.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongo.js","sourceRoot":"","sources":["../src/mongo.ts"],"names":[],"mappings":";;;;;;AAAA,4EAAkD;AAClD,2CAA8C;AAC9C,iDAA8E;AAE9E,6CAAmE;AAEtD,QAAA,WAAW,GAAG;IACvB,KAAK,CAAC,qBAAqB;QACvB,MAAM,UAAU,GAAG,IAAI,gBAAU,CAAC,IAAI,
|
|
1
|
+
{"version":3,"file":"mongo.js","sourceRoot":"","sources":["../src/mongo.ts"],"names":[],"mappings":";;;;;;AAAA,4EAAkD;AAClD,2CAA8C;AAC9C,iDAA8E;AAE9E,6CAAmE;AAEtD,QAAA,WAAW,GAAG;IACvB,KAAK,CAAC,qBAAqB;QACvB,MAAM,MAAM,GAAG,IAAI,qBAAU,EAAE,CAAA;QAC/B,MAAM,UAAU,GAAG,IAAI,gBAAU,CAAC,MAAM,CAAC,CAAA;QAEzC,MAAM,UAAU,CAAC,IAAI,EAAE,CAAA;QAEvB,MAAM,YAAY,GAAG,OAAO,CAAC,IAAI,CAAA;QACjC,OAAO,CAAC,IAAI,GAAG,CAAC,IAAI,EAAS,EAAE;YAC3B,MAAM,CAAC,IAAI,CAAC,+CAA+C,EAAE,EAAE,IAAI,EAAE,CAAC,CAAA;YAEtE,OAAO,UAAU;iBACZ,SAAS,EAAE;iBACX,IAAI,CAAC,GAAG,EAAE;gBACP,MAAM,CAAC,IAAI,CAAC,+CAA+C,EAAE,EAAE,IAAI,EAAE,CAAC,CAAA;gBAEtE,OAAO,YAAY,CAAC,IAAI,CAAC,CAAA;YAC7B,C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;oBACL,OAAO,EAAE,GAAG,KAAK,0BAA0B;oBAC3C,IAAI,EAAE,QAAQ;oBACd,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC;iBACvB,CAAC,CAAA;YACN,CAAC;YAED,MAAM,IAAI,KAAK,CAAC,6CAA6C,GAAG,CAAC,OAAO,EAAE,CAAC,CAAA;QAC/E,CAAC;QAED,MAAM,IAAI,sBAAa,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAA;IAC3C,CAAC;CACJ,CAAA"}
|
package/dist/postgres.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./tables"), exports);
|
|
18
|
+
__exportStar(require("./postgresUtils"), exports);
|
|
19
|
+
//# sourceMappingURL=postgres.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgres.js","sourceRoot":"","sources":["../src/postgres.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AAExB,kDAA+B"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.expiringTimestamp = exports.timestamps = void 0;
|
|
4
|
+
const pg_core_1 = require("drizzle-orm/pg-core");
|
|
5
|
+
exports.timestamps = {
|
|
6
|
+
createdAt: (0, pg_core_1.timestamp)('created_at').defaultNow().notNull(),
|
|
7
|
+
updatedAt: (0, pg_core_1.timestamp)('updated_at')
|
|
8
|
+
.defaultNow()
|
|
9
|
+
.$onUpdate(() => new Date())
|
|
10
|
+
.notNull(),
|
|
11
|
+
};
|
|
12
|
+
exports.expiringTimestamp = (0, pg_core_1.customType)({
|
|
13
|
+
dataType() {
|
|
14
|
+
return `timestamp`;
|
|
15
|
+
},
|
|
16
|
+
fromDriver(value) {
|
|
17
|
+
return new Date(value);
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
//# sourceMappingURL=postgresUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgresUtils.js","sourceRoot":"","sources":["../src/postgresUtils.ts"],"names":[],"mappings":";;;AAAA,iDAA2D;AAE9C,QAAA,UAAU,GAAG;IACtB,SAAS,EAAE,IAAA,mBAAS,EAAC,YAAY,CAAC,CAAC,UAAU,EAAE,CAAC,OAAO,EAAE;IACzD,SAAS,EAAE,IAAA,mBAAS,EAAC,YAAY,CAAC;SAC7B,UAAU,EAAE;SACZ,SAAS,CAAC,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC;SAC3B,OAAO,EAAE;CACjB,CAAA;AAEY,QAAA,iBAAiB,GAAG,IAAA,oBAAU,EAKxC;IACC,QAAQ;QACJ,OAAO,WAAW,CAAA;IACtB,CAAC;IACD,UAAU,CAAC,KAAa;QACpB,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC,CAAA;IAC1B,CAAC;CACJ,CAAC,CAAA"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.counterSchema = void 0;
|
|
3
4
|
const mongoose_1 = require("mongoose");
|
|
4
5
|
const counterSchema = new mongoose_1.Schema({
|
|
5
6
|
code: { type: String, required: true },
|
|
@@ -8,6 +9,6 @@ const counterSchema = new mongoose_1.Schema({
|
|
|
8
9
|
}, {
|
|
9
10
|
timestamps: true,
|
|
10
11
|
});
|
|
12
|
+
exports.counterSchema = counterSchema;
|
|
11
13
|
counterSchema.index({ code: 1, date: -1 }, { unique: true });
|
|
12
|
-
exports.default = mongoose_1.models.Counter || (0, mongoose_1.model)('Counter', counterSchema);
|
|
13
14
|
//# sourceMappingURL=counter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"counter.js","sourceRoot":"","sources":["../../src/schemas/counter.ts"],"names":[],"mappings":";;;AAAA,uCAAiC;AAIjC,MAAM,aAAa,GAAG,IAAI,iBAAM,CAC5B;IACI,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;IACtC,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;IACvC,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE;CACvB,EACD;IACI,UAAU,EAAE,IAAI;CACnB,CACJ,CAAA;AAIQ,sCAAa;AAFtB,aAAa,CAAC,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAA"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encryptedStorageSchema = void 0;
|
|
3
4
|
const mongoose_1 = require("mongoose");
|
|
5
|
+
const types_1 = require("@diia-inhouse/types");
|
|
4
6
|
const encryptedStorageSchema = new mongoose_1.Schema({
|
|
5
7
|
data: { type: String, required: true },
|
|
6
8
|
expiresAt: { type: Date, required: true },
|
|
@@ -8,6 +10,6 @@ const encryptedStorageSchema = new mongoose_1.Schema({
|
|
|
8
10
|
}, {
|
|
9
11
|
timestamps: true,
|
|
10
12
|
});
|
|
11
|
-
encryptedStorageSchema
|
|
12
|
-
|
|
13
|
+
exports.encryptedStorageSchema = encryptedStorageSchema;
|
|
14
|
+
encryptedStorageSchema.index({ expiresAt: 1 }, { expireAfterSeconds: types_1.DurationS.Day });
|
|
13
15
|
//# sourceMappingURL=encryptedStorage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encryptedStorage.js","sourceRoot":"","sources":["../../src/schemas/encryptedStorage.ts"],"names":[],"mappings":";;;AAAA,uCAAiC;AAEjC,+CAA+C;AAI/C,MAAM,sBAAsB,GAAG,IAAI,iBAAM,CACrC;IACI,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;IACtC,SAAS,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE;IACzC,MAAM,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE;CACvB,EACD;IACI,UAAU,EAAE,IAAI;CACnB,CACJ,CAAA;AAIQ,wDAAsB;AAF/B,sBAAsB,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,EAAE,EAAE,kBAAkB,EAAE,iBAAS,CAAC,GAAG,EAAE,CAAC,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./counter"), exports);
|
|
18
|
+
__exportStar(require("./encryptedStorage"), exports);
|
|
19
|
+
__exportStar(require("./statusHistoryItem"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/schemas/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAAyB;AAEzB,qDAAkC;AAElC,sDAAmC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createStatusHistoryItemSchema = createStatusHistoryItemSchema;
|
|
4
|
+
const mongoose_1 = require("mongoose");
|
|
5
|
+
function createStatusHistoryItemSchema(statuses) {
|
|
6
|
+
return new mongoose_1.Schema({
|
|
7
|
+
traceId: { type: String },
|
|
8
|
+
date: { type: Date, required: true },
|
|
9
|
+
status: { type: String, enum: statuses, required: true },
|
|
10
|
+
}, { _id: false });
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=statusHistoryItem.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"statusHistoryItem.js","sourceRoot":"","sources":["../../src/schemas/statusHistoryItem.ts"],"names":[],"mappings":";;AAIA,sEASC;AAbD,uCAAiC;AAIjC,SAAgB,6BAA6B,CAAC,QAAkB;IAC5D,OAAO,IAAI,iBAAM,CACb;QACI,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;QACzB,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE;QACpC,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE;KAC3D,EACD,EAAE,GAAG,EAAE,KAAK,EAAE,CACjB,CAAA;AACL,CAAC"}
|
package/dist/services/counter.js
CHANGED
|
@@ -1,19 +1,71 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.CounterService = void 0;
|
|
4
|
+
const drizzle_orm_1 = require("drizzle-orm");
|
|
7
5
|
const luxon_1 = require("luxon");
|
|
8
|
-
const
|
|
6
|
+
const tables_1 = require("../tables");
|
|
9
7
|
class CounterService {
|
|
8
|
+
databaseAdapter;
|
|
9
|
+
counterModel;
|
|
10
|
+
postgresDatabase;
|
|
11
|
+
getNextValueStrategy = {
|
|
12
|
+
postgres: async (code) => {
|
|
13
|
+
const result = await this.getPostgresDatabase()
|
|
14
|
+
.insert(tables_1.counter)
|
|
15
|
+
.values({ code, value: 1, date: luxon_1.DateTime.now().startOf('day').toJSDate() })
|
|
16
|
+
.onConflictDoUpdate({
|
|
17
|
+
target: [tables_1.counter.code, tables_1.counter.date],
|
|
18
|
+
set: { value: (0, drizzle_orm_1.sql) `${tables_1.counter.value} + 1` },
|
|
19
|
+
})
|
|
20
|
+
.returning()
|
|
21
|
+
.then((rows) => rows[0]);
|
|
22
|
+
return result.value;
|
|
23
|
+
},
|
|
24
|
+
mongo: async (code) => {
|
|
25
|
+
const result = await this.getModel().findOneAndUpdate({ code }, { $inc: { value: 1 } }, { new: true, upsert: true });
|
|
26
|
+
return result.value;
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
getNextDailyValueStrategy = {
|
|
30
|
+
postgres: async (code) => {
|
|
31
|
+
const result = await this.getPostgresDatabase()
|
|
32
|
+
.insert(tables_1.counter)
|
|
33
|
+
.values({ code, value: 1, date: luxon_1.DateTime.now().startOf('day').toJSDate() })
|
|
34
|
+
.onConflictDoUpdate({
|
|
35
|
+
target: [tables_1.counter.code, tables_1.counter.date],
|
|
36
|
+
set: { value: (0, drizzle_orm_1.sql) `${tables_1.counter.value} + 1` },
|
|
37
|
+
})
|
|
38
|
+
.returning()
|
|
39
|
+
.then((rows) => rows[0]);
|
|
40
|
+
return result.value;
|
|
41
|
+
},
|
|
42
|
+
mongo: async (code) => {
|
|
43
|
+
const result = await this.getModel().findOneAndUpdate({ code, date: luxon_1.DateTime.now().startOf('day').toJSDate() }, { $inc: { value: 1 } }, { new: true, upsert: true });
|
|
44
|
+
return result.value;
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
constructor(databaseAdapter, counterModel = null, postgresDatabase = null) {
|
|
48
|
+
this.databaseAdapter = databaseAdapter;
|
|
49
|
+
this.counterModel = counterModel;
|
|
50
|
+
this.postgresDatabase = postgresDatabase;
|
|
51
|
+
}
|
|
10
52
|
async getNextValue(code) {
|
|
11
|
-
|
|
12
|
-
return value;
|
|
53
|
+
return await this.getNextValueStrategy[this.databaseAdapter](code);
|
|
13
54
|
}
|
|
14
55
|
async getNextDailyValue(code) {
|
|
15
|
-
|
|
16
|
-
|
|
56
|
+
return await this.getNextDailyValueStrategy[this.databaseAdapter](code);
|
|
57
|
+
}
|
|
58
|
+
getModel() {
|
|
59
|
+
if (!this.counterModel) {
|
|
60
|
+
throw new Error('Counter model is not provided');
|
|
61
|
+
}
|
|
62
|
+
return this.counterModel;
|
|
63
|
+
}
|
|
64
|
+
getPostgresDatabase() {
|
|
65
|
+
if (!this.postgresDatabase) {
|
|
66
|
+
throw new Error('Postgres database is not provided');
|
|
67
|
+
}
|
|
68
|
+
return this.postgresDatabase;
|
|
17
69
|
}
|
|
18
70
|
}
|
|
19
71
|
exports.CounterService = CounterService;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"counter.js","sourceRoot":"","sources":["../../src/services/counter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"counter.js","sourceRoot":"","sources":["../../src/services/counter.ts"],"names":[],"mappings":";;;AAAA,6CAAiC;AACjC,iCAAgC;AAKhC,sCAAmC;AAEnC,MAAa,cAAc;IAgDF;IACA;IACA;IAjDJ,oBAAoB,GAAmE;QACpG,QAAQ,EAAE,KAAK,EAAE,IAAY,EAAmB,EAAE;YAC9C,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,mBAAmB,EAAE;iBAC1C,MAAM,CAAC,gBAAO,CAAC;iBACf,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,gBAAQ,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC;iBAC1E,kBAAkB,CAAC;gBAChB,MAAM,EAAE,CAAC,gBAAO,CAAC,IAAI,EAAE,gBAAO,CAAC,IAAI,CAAC;gBACpC,GAAG,EAAE,EAAE,KAAK,EAAE,IAAA,iBAAG,EAAA,GAAG,gBAAO,CAAC,KAAK,MAAM,EAAE;aAC5C,CAAC;iBACD,SAAS,EAAE;iBACX,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;YAE5B,OAAO,MAAM,CAAC,KAAK,CAAA;QACvB,CAAC;QACD,KAAK,EAAE,KAAK,EAAE,IAAY,EAAmB,EAAE;YAC3C,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAC,gBAAgB,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,EA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AAA;IAED,YACqB,eAAoC,EACpC,eAAsC,IAAI,EAC1C,mBAA4C,IAAI;QAFhD,oBAAe,GAAf,eAAe,CAAqB;QACpC,iBAAY,GAAZ,YAAY,CAA8B;QAC1C,qBAAgB,GAAhB,gBAAgB,CAAgC;IAClE,CAAC;IAEJ,KAAK,CAAC,YAAY,CAAC,IAAY;QAC3B,OAAO,MAAM,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,IAAI,CAAC,CAAA;IACtE,CAAC;IAED,KAAK,CAAC,iBAAiB,CAAC,IAAY;QAChC,OAAO,MAAM,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,IAAI,CAAC,CAAA;IAC3E,CAAC;IAEO,QAAQ;QACZ,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAA;QACpD,CAAC;QAED,OAAO,IAAI,CAAC,YAAY,CAAA;IAC5B,CAAC;IAEO,mBAAmB;QACvB,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAA;QACxD,CAAC;QAED,OAAO,IAAI,CAAC,gBAAgB,CAAA;IAChC,CAAC;CACJ;AA5ED,wCA4EC"}
|
|
@@ -4,32 +4,51 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.DatabaseService = void 0;
|
|
7
|
-
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
9
7
|
const lodash_1 = require("lodash");
|
|
10
8
|
const mongoose_1 = __importDefault(require("mongoose"));
|
|
11
|
-
const
|
|
9
|
+
const diia_metrics_1 = require("@diia-inhouse/diia-metrics");
|
|
12
10
|
const errors_1 = require("@diia-inhouse/errors");
|
|
13
11
|
const types_1 = require("@diia-inhouse/types");
|
|
14
12
|
const interfaces_1 = require("../interfaces");
|
|
15
13
|
class DatabaseService {
|
|
14
|
+
databaseAdapter;
|
|
16
15
|
dbConfigs;
|
|
17
16
|
envService;
|
|
18
17
|
logger;
|
|
18
|
+
defaultModelsDir = 'models';
|
|
19
|
+
db = {};
|
|
20
|
+
dbOperationsHistogram;
|
|
21
|
+
dbOplogObserver;
|
|
22
|
+
observerInterval;
|
|
23
|
+
dbOplog = {};
|
|
24
|
+
isMonitoringEnabled = false;
|
|
19
25
|
dbStateCodeToName = {
|
|
20
26
|
[mongoose_1.default.ConnectionStates.disconnected]: interfaces_1.DbConnectionStatus.Disconnected,
|
|
21
27
|
[mongoose_1.default.ConnectionStates.connected]: interfaces_1.DbConnectionStatus.Connected,
|
|
22
28
|
[mongoose_1.default.ConnectionStates.connecting]: interfaces_1.DbConnectionStatus.Connecting,
|
|
23
29
|
[mongoose_1.default.ConnectionStates.disconnecting]: interfaces_1.DbConnectionStatus.Disconnecting,
|
|
24
30
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
31
|
+
/**
|
|
32
|
+
* https://www.mongodb.com/docs/manual/core/transactions/#read-concern-write-concern-read-preference
|
|
33
|
+
*/
|
|
34
|
+
defaultTransactionOptions = {
|
|
35
|
+
readPreference: 'primary',
|
|
36
|
+
};
|
|
37
|
+
constructor(databaseAdapter, dbConfigs, envService, logger) {
|
|
38
|
+
this.databaseAdapter = databaseAdapter;
|
|
28
39
|
this.dbConfigs = dbConfigs;
|
|
29
40
|
this.envService = envService;
|
|
30
41
|
this.logger = logger;
|
|
42
|
+
this.isMonitoringEnabled = this.dbConfigs.main?.metrics.enabled || false;
|
|
43
|
+
if (this.isMonitoringEnabled) {
|
|
44
|
+
this.dbOperationsHistogram = new diia_metrics_1.Histogram('diia_mongodb_operation_seconds', interfaces_1.mongodbOperationsAllowedFields, 'Mongodb operation duration in seconds', this.dbConfigs.main?.metrics.buckets || interfaces_1.mongodbOperationsDefaultBuckets);
|
|
45
|
+
this.dbOplogObserver = new diia_metrics_1.Observer('diia_mongodb_oplog_cache_size', [], 'Amount of operations stored in in-memory cache for monitoring');
|
|
46
|
+
}
|
|
31
47
|
}
|
|
32
48
|
async onInit() {
|
|
49
|
+
if (this.databaseAdapter !== 'mongo') {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
33
52
|
const tasks = Object.entries(this.dbConfigs).map(async ([type, config]) => {
|
|
34
53
|
const dbType = type;
|
|
35
54
|
const connection = await this.createDbConnection(dbType, config);
|
|
@@ -38,9 +57,18 @@ class DatabaseService {
|
|
|
38
57
|
}
|
|
39
58
|
});
|
|
40
59
|
await Promise.all(tasks);
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
60
|
+
if (this.isMonitoringEnabled) {
|
|
61
|
+
this.observerInterval = setInterval(() => {
|
|
62
|
+
this.oplogObserver();
|
|
63
|
+
}, 5000);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async onDestroy() {
|
|
67
|
+
if (this.isMonitoringEnabled) {
|
|
68
|
+
clearInterval(this.observerInterval);
|
|
69
|
+
}
|
|
70
|
+
for (const db of Object.values(this.db)) {
|
|
71
|
+
await db.connection.close();
|
|
44
72
|
}
|
|
45
73
|
}
|
|
46
74
|
async onHealthCheck() {
|
|
@@ -49,7 +77,7 @@ class DatabaseService {
|
|
|
49
77
|
const dbType = type;
|
|
50
78
|
try {
|
|
51
79
|
await db.connection.db
|
|
52
|
-
|
|
80
|
+
?.listCollections({}, {
|
|
53
81
|
nameOnly: true,
|
|
54
82
|
authorizedCollections: true,
|
|
55
83
|
})
|
|
@@ -135,12 +163,15 @@ class DatabaseService {
|
|
|
135
163
|
}
|
|
136
164
|
let connection;
|
|
137
165
|
if (type === interfaces_1.DbType.Main) {
|
|
138
|
-
await mongoose_1.default.connect(connectionString, connectionOptions);
|
|
166
|
+
await mongoose_1.default.connect(connectionString, { ...connectionOptions, monitorCommands: this.isMonitoringEnabled });
|
|
139
167
|
connection = mongoose_1.default.connection;
|
|
140
168
|
}
|
|
141
169
|
else {
|
|
142
170
|
connection = await mongoose_1.default.createConnection(connectionString, connectionOptions).asPromise();
|
|
143
171
|
}
|
|
172
|
+
if (this.isMonitoringEnabled) {
|
|
173
|
+
this.connectMonitor(connection);
|
|
174
|
+
}
|
|
144
175
|
connection.on('error', (err) => {
|
|
145
176
|
this.logger.error('Mongo connection error', { err, type });
|
|
146
177
|
});
|
|
@@ -151,50 +182,30 @@ class DatabaseService {
|
|
|
151
182
|
throw new errors_1.DatabaseError('Failed to connect to Database');
|
|
152
183
|
}
|
|
153
184
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (modelModule.skipSyncIndexes) {
|
|
169
|
-
continue;
|
|
170
|
-
}
|
|
171
|
-
const task = modelModule.default;
|
|
172
|
-
tasks.push(this.syncModel(task));
|
|
173
|
-
}
|
|
174
|
-
await Promise.all(tasks);
|
|
175
|
-
this.logger.info(`Ended syncing indexes in ${Date.now() - t0} ms`);
|
|
176
|
-
this.logger.info('Successfully synced indexes');
|
|
177
|
-
}
|
|
178
|
-
catch (err) {
|
|
179
|
-
this.logger.error('Failed to syncing indexes', { err });
|
|
180
|
-
throw err;
|
|
181
|
-
}
|
|
182
|
-
finally {
|
|
183
|
-
if (exitAfterSync) {
|
|
184
|
-
this.logger.info('Process exit after synced indexes');
|
|
185
|
-
// eslint-disable-next-line no-process-exit, unicorn/no-process-exit, n/no-process-exit
|
|
186
|
-
process.exit(0);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
async beginTransaction(dbType = interfaces_1.DbType.Main) {
|
|
191
|
-
const connection = this.db[dbType]?.connection;
|
|
185
|
+
/**
|
|
186
|
+
* Begins a new MongoDB transaction by starting a session and transaction
|
|
187
|
+
*
|
|
188
|
+
* @param options - Optional transaction options to override the default transaction settings
|
|
189
|
+
* @returns A ClientSession with an active transaction
|
|
190
|
+
* @throws {DatabaseError} If the database connection is undefined or transaction fails to start
|
|
191
|
+
*
|
|
192
|
+
* @remarks
|
|
193
|
+
* - Uses the main database connection to start the session
|
|
194
|
+
* - Applies default transaction options (readPreference: primary)
|
|
195
|
+
* - Will abort transaction and end session if any errors occur during setup
|
|
196
|
+
*/
|
|
197
|
+
async beginTransaction(options) {
|
|
198
|
+
const connection = this.db[interfaces_1.DbType.Main]?.connection;
|
|
192
199
|
if (!connection) {
|
|
193
200
|
throw new errors_1.DatabaseError('Connection is undefined');
|
|
194
201
|
}
|
|
195
202
|
const session = await connection.startSession();
|
|
196
203
|
try {
|
|
197
|
-
|
|
204
|
+
const transactionOptions = {
|
|
205
|
+
...this.defaultTransactionOptions,
|
|
206
|
+
...options,
|
|
207
|
+
};
|
|
208
|
+
session.startTransaction(transactionOptions);
|
|
198
209
|
return session;
|
|
199
210
|
}
|
|
200
211
|
catch (err) {
|
|
@@ -203,11 +214,68 @@ class DatabaseService {
|
|
|
203
214
|
throw new errors_1.DatabaseError('Unable to begin transaction', { err });
|
|
204
215
|
}
|
|
205
216
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
217
|
+
connectMonitor(connection) {
|
|
218
|
+
try {
|
|
219
|
+
const client = connection.getClient();
|
|
220
|
+
client.on('commandStarted', (event) => {
|
|
221
|
+
const db = event.databaseName;
|
|
222
|
+
const opType = event.commandName;
|
|
223
|
+
const key = `${event.requestId}${event.connectionId}`;
|
|
224
|
+
const parsedCollection = Number.parseInt(event.command[opType] || '', 10);
|
|
225
|
+
const collection = Number.isNaN(parsedCollection) ? event.command[opType] : undefined;
|
|
226
|
+
this.dbOplog[key] = {
|
|
227
|
+
database: db,
|
|
228
|
+
collection,
|
|
229
|
+
opType,
|
|
230
|
+
};
|
|
231
|
+
this.logger.debug('Mongodb commandStarted operation', {
|
|
232
|
+
database: db,
|
|
233
|
+
collection,
|
|
234
|
+
operation: opType,
|
|
235
|
+
command: event.command,
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
client.on('commandSucceeded', (event) => {
|
|
239
|
+
const key = `${event.requestId}${event.connectionId}`;
|
|
240
|
+
const oplogEntry = this.dbOplog[key];
|
|
241
|
+
const durationMs = event.duration;
|
|
242
|
+
delete this.dbOplog[key];
|
|
243
|
+
this.logger.debug('Mongodb commandSucceeded operation', {
|
|
244
|
+
database: oplogEntry.database,
|
|
245
|
+
operation: oplogEntry.opType,
|
|
246
|
+
collection: oplogEntry.collection,
|
|
247
|
+
});
|
|
248
|
+
this.dbOperationsHistogram?.observe({
|
|
249
|
+
operation: oplogEntry.opType,
|
|
250
|
+
status: interfaces_1.MongodbOperationResult.Successful,
|
|
251
|
+
database: oplogEntry.database,
|
|
252
|
+
...(oplogEntry.collection && { collection: oplogEntry.collection }),
|
|
253
|
+
}, durationMs / 1000);
|
|
254
|
+
});
|
|
255
|
+
client.on('commandFailed', (event) => {
|
|
256
|
+
const key = `${event.requestId}${event.connectionId}`;
|
|
257
|
+
const oplogEntry = this.dbOplog[key];
|
|
258
|
+
const durationMs = event.duration;
|
|
259
|
+
delete this.dbOplog[key];
|
|
260
|
+
this.logger.debug('Mongodb commandFailed operation', {
|
|
261
|
+
database: oplogEntry.database,
|
|
262
|
+
operation: oplogEntry.opType,
|
|
263
|
+
collection: oplogEntry.collection,
|
|
264
|
+
});
|
|
265
|
+
this.dbOperationsHistogram?.observe({
|
|
266
|
+
operation: oplogEntry.opType,
|
|
267
|
+
status: interfaces_1.MongodbOperationResult.Failed,
|
|
268
|
+
database: oplogEntry.database,
|
|
269
|
+
...(oplogEntry.collection && { collection: oplogEntry.collection }),
|
|
270
|
+
}, durationMs / 1000);
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
catch (err) {
|
|
274
|
+
this.logger.error("Couldn't attach commands monitor", { err });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
oplogObserver() {
|
|
278
|
+
this.dbOplogObserver?.observe({}, Object.keys(this.dbOplog).length);
|
|
211
279
|
}
|
|
212
280
|
}
|
|
213
281
|
exports.DatabaseService = DatabaseService;
|