@diia-inhouse/db 4.8.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/LICENCE.md +287 -0
- package/README.md +93 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/dbConfig.js +9 -0
- package/dist/interfaces/dbConfig.js.map +1 -0
- package/dist/interfaces/errors.js +46 -0
- package/dist/interfaces/errors.js.map +1 -0
- package/dist/interfaces/index.js +22 -0
- package/dist/interfaces/index.js.map +1 -0
- package/dist/interfaces/migrateMongoConfig.js +3 -0
- package/dist/interfaces/migrateMongoConfig.js.map +1 -0
- package/dist/interfaces/models/encryptedStorage.js +3 -0
- package/dist/interfaces/models/encryptedStorage.js.map +1 -0
- package/dist/interfaces/mongoStatus.js +12 -0
- package/dist/interfaces/mongoStatus.js.map +1 -0
- package/dist/models/encryptedStorage.js +13 -0
- package/dist/models/encryptedStorage.js.map +1 -0
- package/dist/mongo.js +68 -0
- package/dist/mongo.js.map +1 -0
- package/dist/services/database.js +211 -0
- package/dist/services/database.js.map +1 -0
- package/dist/services/encryptedStorage.js +90 -0
- package/dist/services/encryptedStorage.js.map +1 -0
- package/dist/services/index.js +19 -0
- package/dist/services/index.js.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/interfaces/dbConfig.d.ts +53 -0
- package/dist/types/interfaces/errors.d.ts +36 -0
- package/dist/types/interfaces/index.d.ts +30 -0
- package/dist/types/interfaces/migrateMongoConfig.d.ts +37 -0
- package/dist/types/interfaces/models/encryptedStorage.d.ts +32 -0
- package/dist/types/interfaces/mongoStatus.d.ts +12 -0
- package/dist/types/models/encryptedStorage.d.ts +30 -0
- package/dist/types/mongo.d.ts +9 -0
- package/dist/types/services/database.d.ts +43 -0
- package/dist/types/services/encryptedStorage.d.ts +43 -0
- package/dist/types/services/index.d.ts +2 -0
- package/package.json +102 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DatabaseService = void 0;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const lodash_1 = require("lodash");
|
|
10
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
11
|
+
const recursive_readdir_1 = __importDefault(require("recursive-readdir"));
|
|
12
|
+
const errors_1 = require("@diia-inhouse/errors");
|
|
13
|
+
const types_1 = require("@diia-inhouse/types");
|
|
14
|
+
const interfaces_1 = require("../interfaces");
|
|
15
|
+
class DatabaseService {
|
|
16
|
+
dbConfigs;
|
|
17
|
+
envService;
|
|
18
|
+
logger;
|
|
19
|
+
dbStateCodeToName = {
|
|
20
|
+
[mongoose_1.default.ConnectionStates.disconnected]: interfaces_1.DbConnectionStatus.Disconnected,
|
|
21
|
+
[mongoose_1.default.ConnectionStates.connected]: interfaces_1.DbConnectionStatus.Connected,
|
|
22
|
+
[mongoose_1.default.ConnectionStates.connecting]: interfaces_1.DbConnectionStatus.Connecting,
|
|
23
|
+
[mongoose_1.default.ConnectionStates.disconnecting]: interfaces_1.DbConnectionStatus.Disconnecting,
|
|
24
|
+
};
|
|
25
|
+
defaultModelsDir = 'models';
|
|
26
|
+
db = {};
|
|
27
|
+
constructor(dbConfigs, envService, logger) {
|
|
28
|
+
this.dbConfigs = dbConfigs;
|
|
29
|
+
this.envService = envService;
|
|
30
|
+
this.logger = logger;
|
|
31
|
+
}
|
|
32
|
+
async onInit() {
|
|
33
|
+
const tasks = Object.entries(this.dbConfigs).map(async ([type, config]) => {
|
|
34
|
+
const dbType = type;
|
|
35
|
+
const connection = await this.createDbConnection(dbType, config);
|
|
36
|
+
if (connection) {
|
|
37
|
+
this.db[dbType] = connection;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
await Promise.all(tasks);
|
|
41
|
+
const mainConfig = this.dbConfigs[interfaces_1.DbType.Main];
|
|
42
|
+
if (mainConfig?.indexes?.sync) {
|
|
43
|
+
await this.syncIndexes(mainConfig.indexes.exitAfterSync);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async onHealthCheck() {
|
|
47
|
+
const dbStatus = {};
|
|
48
|
+
for (const [type, db] of Object.entries(this.db)) {
|
|
49
|
+
const dbType = type;
|
|
50
|
+
try {
|
|
51
|
+
await db.connection.db
|
|
52
|
+
.listCollections({}, {
|
|
53
|
+
nameOnly: true,
|
|
54
|
+
authorizedCollections: true,
|
|
55
|
+
})
|
|
56
|
+
.toArray();
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
this.logger.error('Mongo list collections error', { err, dbType });
|
|
60
|
+
dbStatus[dbType] = interfaces_1.DbConnectionStatus.OpFailed;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
dbStatus[dbType] = this.dbStateCodeToName[db.connection.readyState];
|
|
64
|
+
}
|
|
65
|
+
const status = Object.values(dbStatus).some((s) => s !== interfaces_1.DbConnectionStatus.Connected)
|
|
66
|
+
? types_1.HttpStatusCode.SERVICE_UNAVAILABLE
|
|
67
|
+
: types_1.HttpStatusCode.OK;
|
|
68
|
+
return {
|
|
69
|
+
status,
|
|
70
|
+
details: { mongodb: dbStatus },
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
async createDbConnection(type, config) {
|
|
74
|
+
const { isEnabled } = config;
|
|
75
|
+
if (typeof isEnabled === 'boolean' && !isEnabled) {
|
|
76
|
+
this.logger.info(`Database is disabled: ${type}`);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
const { host, port, database, authSource, user, password, replicaSet, replicaSetNodes, readPreference } = config;
|
|
81
|
+
const connectionOptions = {};
|
|
82
|
+
let hosts = [];
|
|
83
|
+
if (host) {
|
|
84
|
+
if (port) {
|
|
85
|
+
hosts.push(`${host}:${port}`);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
hosts.push(`${host}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (user && password) {
|
|
92
|
+
connectionOptions.auth = { username: user, password };
|
|
93
|
+
}
|
|
94
|
+
if (replicaSet) {
|
|
95
|
+
connectionOptions.replicaSet = replicaSet;
|
|
96
|
+
}
|
|
97
|
+
if (replicaSetNodes) {
|
|
98
|
+
if (host) {
|
|
99
|
+
const errMsg = 'Must be only `host` and `port` or `replicaSetNodes` config';
|
|
100
|
+
this.logger.error('Wrong database configuration:', errMsg);
|
|
101
|
+
throw new Error(errMsg);
|
|
102
|
+
}
|
|
103
|
+
hosts = replicaSetNodes.map(({ replicaHost }) => `${replicaHost}:${port}`);
|
|
104
|
+
}
|
|
105
|
+
let connectionString = `mongodb://${hosts.join(',')}/`;
|
|
106
|
+
if (database) {
|
|
107
|
+
connectionOptions.dbName = database;
|
|
108
|
+
}
|
|
109
|
+
const query = [];
|
|
110
|
+
if (authSource) {
|
|
111
|
+
query.push(`authSource=${authSource}`);
|
|
112
|
+
}
|
|
113
|
+
if (readPreference) {
|
|
114
|
+
query.push(`readPreference=${readPreference}`);
|
|
115
|
+
}
|
|
116
|
+
if (query.length > 0) {
|
|
117
|
+
connectionString += `?${query.join('&')}`;
|
|
118
|
+
}
|
|
119
|
+
const logOptions = (0, lodash_1.cloneDeep)(connectionOptions);
|
|
120
|
+
if (logOptions.auth) {
|
|
121
|
+
logOptions.auth = {
|
|
122
|
+
username: '********',
|
|
123
|
+
password: '********',
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
this.logger.info(`Connecting to DB ${connectionString} ${type}`, logOptions);
|
|
127
|
+
if (this.envService.isLocal() || this.envService.isTest()) {
|
|
128
|
+
this.logger.debug('Mongoose set to Debug');
|
|
129
|
+
mongoose_1.default.set('debug', (coll, method, dbQuery, doc, options) => {
|
|
130
|
+
this.logger.debug('Mongo: ', { coll, method, query: dbQuery, doc, options });
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
let connection;
|
|
134
|
+
if (type === interfaces_1.DbType.Main) {
|
|
135
|
+
await mongoose_1.default.connect(connectionString, connectionOptions);
|
|
136
|
+
connection = mongoose_1.default.connection;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
connection = await mongoose_1.default.createConnection(connectionString, connectionOptions).asPromise();
|
|
140
|
+
}
|
|
141
|
+
connection.on('error', (err) => {
|
|
142
|
+
this.logger.error('Mongo connection error', { err, type });
|
|
143
|
+
});
|
|
144
|
+
return { connection, connectionString, connectionOptions };
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
this.logger.error('Failed to connect to Database', { type, err });
|
|
148
|
+
throw new errors_1.DatabaseError('Failed to connect to Database');
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async syncIndexes(exitAfterSync = false, modelsDir) {
|
|
152
|
+
const modelsPath = `./dist/${modelsDir || this.defaultModelsDir}`;
|
|
153
|
+
try {
|
|
154
|
+
if (!node_fs_1.default.existsSync(modelsPath)) {
|
|
155
|
+
this.logger.info('Models dir is absent, indexes sync skipped');
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const t0 = Date.now();
|
|
159
|
+
this.logger.info('Start syncing indexes');
|
|
160
|
+
const files = await (0, recursive_readdir_1.default)(modelsPath, ['*.map', 'index.js', 'schemas']);
|
|
161
|
+
const tasks = [];
|
|
162
|
+
for (const fileName of files) {
|
|
163
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
164
|
+
const modelModule = require(node_path_1.default.resolve(fileName));
|
|
165
|
+
if (modelModule.skipSyncIndexes) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const task = modelModule.default;
|
|
169
|
+
tasks.push(this.syncModel(task));
|
|
170
|
+
}
|
|
171
|
+
await Promise.all(tasks);
|
|
172
|
+
this.logger.info(`Ended syncing indexes in ${Date.now() - t0} ms`);
|
|
173
|
+
this.logger.info('Successfully synced indexes');
|
|
174
|
+
}
|
|
175
|
+
catch (err) {
|
|
176
|
+
this.logger.error('Failed to syncing indexes', { err });
|
|
177
|
+
throw err;
|
|
178
|
+
}
|
|
179
|
+
finally {
|
|
180
|
+
if (exitAfterSync) {
|
|
181
|
+
this.logger.info('Process exit after synced indexes');
|
|
182
|
+
// eslint-disable-next-line no-process-exit, unicorn/no-process-exit, n/no-process-exit
|
|
183
|
+
process.exit(0);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
async beginTransaction(dbType = interfaces_1.DbType.Main) {
|
|
188
|
+
const connection = this.db[dbType]?.connection;
|
|
189
|
+
if (!connection) {
|
|
190
|
+
throw new errors_1.DatabaseError('Connection is undefined');
|
|
191
|
+
}
|
|
192
|
+
const session = await connection.startSession();
|
|
193
|
+
try {
|
|
194
|
+
session.startTransaction();
|
|
195
|
+
return session;
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
await session.abortTransaction();
|
|
199
|
+
await session.endSession();
|
|
200
|
+
throw new errors_1.DatabaseError('Unable to begin transaction', { err });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
async syncModel(model) {
|
|
204
|
+
const t0 = Date.now();
|
|
205
|
+
this.logger.info(`Start syncing indexes for the ${model.modelName} collection`);
|
|
206
|
+
await model.syncIndexes();
|
|
207
|
+
this.logger.info(`Ended syncing indexes for the ${model.modelName} collection in ${Date.now() - t0} ms`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
exports.DatabaseService = DatabaseService;
|
|
211
|
+
//# sourceMappingURL=database.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.js","sourceRoot":"","sources":["../../src/services/database.ts"],"names":[],"mappings":";;;;;;AAAA,sDAAwB;AACxB,0DAA4B;AAE5B,mCAAkC;AAClC,wDAAkD;AAClD,0EAA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uFAAuF;gBACvF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;YACnB,CAAC;QACL,CAAC;IACL,CAAC;IAED,KAAK,CAAC,gBAAgB,CAAC,SAAiB,mBAAM,CAAC,IAAI;QAC/C,MAAM,UAAU,GAAG,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,UAAU,CAAA;QAE9C,IAAI,CAAC,UAAU,EAAE,CAAC;YACd,MAAM,IAAI,sBAAa,CAAC,yBAAyB,CAAC,CAAA;QACtD,CAAC;QAED,MAAM,OAAO,GAAG,MAAM,UAAU,CAAC,YAAY,EAAE,CAAA;QAE/C,IAAI,CAAC;YACD,OAAO,CAAC,gBAAgB,EAAE,CAAA;YAE1B,OAAO,OAAO,CAAA;QAClB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,MAAM,OAAO,CAAC,gBAAgB,EAAE,CAAA;YAChC,MAAM,OAAO,CAAC,UAAU,EAAE,CAAA;YAE1B,MAAM,IAAI,sBAAa,CAAC,6BAA6B,EAAE,EAAE,GAAG,EAAE,CAAC,CAAA;QACnE,CAAC;IACL,CAAC;IAEO,KAAK,CAAC,SAAS,CAAC,KAA8B;QAClD,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAErB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,iCAAiC,KAAK,CAAC,SAAS,aAAa,CAAC,CAAA;QAC/E,MAAM,KAAK,CAAC,WAAW,EAAE,CAAA;QACzB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,iCAAiC,KAAK,CAAC,SAAS,kBAAkB,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;IAC5G,CAAC;CACJ;AA3OD,0CA2OC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.EncryptedStorageService = void 0;
|
|
7
|
+
const errors_1 = require("@diia-inhouse/errors");
|
|
8
|
+
const utils_1 = require("@diia-inhouse/utils");
|
|
9
|
+
const encryptedStorage_1 = __importDefault(require("../models/encryptedStorage"));
|
|
10
|
+
class EncryptedStorageService {
|
|
11
|
+
logger;
|
|
12
|
+
auth;
|
|
13
|
+
envService;
|
|
14
|
+
constructor(logger, auth, envService) {
|
|
15
|
+
this.logger = logger;
|
|
16
|
+
this.auth = auth;
|
|
17
|
+
this.envService = envService;
|
|
18
|
+
}
|
|
19
|
+
async save(data, expiration) {
|
|
20
|
+
const encryptedData = await this.auth.encryptJWE(utils_1.utils.encodeObjectToBase64(data));
|
|
21
|
+
const storageItem = {
|
|
22
|
+
data: encryptedData,
|
|
23
|
+
expiresAt: new Date(Date.now() + expiration),
|
|
24
|
+
};
|
|
25
|
+
if (this.envService.isStage()) {
|
|
26
|
+
storageItem.source = data;
|
|
27
|
+
}
|
|
28
|
+
const result = await encryptedStorage_1.default.create(storageItem);
|
|
29
|
+
return result.id;
|
|
30
|
+
}
|
|
31
|
+
async get(id) {
|
|
32
|
+
const storageItem = await this.getStorageItemById(id);
|
|
33
|
+
const decryptedData = await this.auth.decryptJWE(storageItem.data);
|
|
34
|
+
return await utils_1.utils.decodeObjectFromBase64(decryptedData);
|
|
35
|
+
}
|
|
36
|
+
async getSafe(id) {
|
|
37
|
+
try {
|
|
38
|
+
return await this.get(id);
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
this.logger.log('Unable to retrieve data from encrypted storage', { err });
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async update(id, data) {
|
|
46
|
+
const storageItem = await this.getStorageItemById(id);
|
|
47
|
+
const encryptedData = await this.auth.encryptJWE(utils_1.utils.encodeObjectToBase64(data));
|
|
48
|
+
storageItem.data = encryptedData;
|
|
49
|
+
if (this.envService.isStage()) {
|
|
50
|
+
storageItem.source = data;
|
|
51
|
+
}
|
|
52
|
+
await storageItem.save();
|
|
53
|
+
}
|
|
54
|
+
async remove(id) {
|
|
55
|
+
const storageItem = await encryptedStorage_1.default.findOneAndDelete({ _id: id });
|
|
56
|
+
if (!storageItem) {
|
|
57
|
+
this.logger.info('Encrypted data is not removed from storage: data not found', { id });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
this.logger.info('Encrypted data removed from storage', { id });
|
|
61
|
+
}
|
|
62
|
+
async deleteMany(ids) {
|
|
63
|
+
const { deletedCount } = await encryptedStorage_1.default.deleteMany({ _id: { $in: ids } });
|
|
64
|
+
this.logger.info(`Encrypted data removed from storage: ${deletedCount}`);
|
|
65
|
+
}
|
|
66
|
+
async setExpiration(id, expiration) {
|
|
67
|
+
const storageItem = await this.getStorageItemById(id);
|
|
68
|
+
storageItem.expiresAt = new Date(Date.now() + expiration);
|
|
69
|
+
await storageItem.save();
|
|
70
|
+
this.logger.info('Updated encrypted data expiration date', { id });
|
|
71
|
+
}
|
|
72
|
+
async getExpiration(id) {
|
|
73
|
+
const storageItem = await encryptedStorage_1.default.findById(id, { expiresAt: 1 });
|
|
74
|
+
if (!storageItem) {
|
|
75
|
+
this.logger.error('Encrypted data is not found in storage', { id });
|
|
76
|
+
throw new errors_1.NotFoundError('Missing data');
|
|
77
|
+
}
|
|
78
|
+
return storageItem.expiresAt;
|
|
79
|
+
}
|
|
80
|
+
async getStorageItemById(id) {
|
|
81
|
+
const storageItem = await encryptedStorage_1.default.findById(id);
|
|
82
|
+
if (!storageItem) {
|
|
83
|
+
this.logger.error('Encrypted data is not found in storage', { id });
|
|
84
|
+
throw new errors_1.NotFoundError('Missing data');
|
|
85
|
+
}
|
|
86
|
+
return storageItem;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.EncryptedStorageService = EncryptedStorageService;
|
|
90
|
+
//# sourceMappingURL=encryptedStorage.js.map
|
|
@@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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("./database"), exports);
|
|
18
|
+
__exportStar(require("./encryptedStorage"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA0B;AAE1B,qDAAkC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
import mongoose from 'mongoose';
|
|
26
|
+
export interface ReplicaSetNodeConfig {
|
|
27
|
+
replicaHost: string;
|
|
28
|
+
}
|
|
29
|
+
export declare enum DbType {
|
|
30
|
+
Main = "main",
|
|
31
|
+
Cache = "cache"
|
|
32
|
+
}
|
|
33
|
+
export interface AppDbConfig {
|
|
34
|
+
isEnabled?: boolean;
|
|
35
|
+
user?: string;
|
|
36
|
+
password?: string;
|
|
37
|
+
database?: string;
|
|
38
|
+
authSource?: string;
|
|
39
|
+
host?: string;
|
|
40
|
+
port?: number;
|
|
41
|
+
replicaSet?: string;
|
|
42
|
+
replicaSetNodes?: ReplicaSetNodeConfig[];
|
|
43
|
+
readPreference?: string;
|
|
44
|
+
indexes?: {
|
|
45
|
+
sync: boolean;
|
|
46
|
+
exitAfterSync: boolean;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export interface AppDb {
|
|
50
|
+
connection: mongoose.Connection;
|
|
51
|
+
connectionString: string;
|
|
52
|
+
connectionOptions: mongoose.ConnectOptions;
|
|
53
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export declare enum MongoDBErrorCode {
|
|
2
|
+
AlreadyInitialized = 23,
|
|
3
|
+
AuthenticationFailed = 18,
|
|
4
|
+
BadValue = 2,
|
|
5
|
+
CannotMutateObject = 10,
|
|
6
|
+
CannotReuseObject = 19,
|
|
7
|
+
CollectionDoesNotExist = 26,
|
|
8
|
+
DuplicateKey = 11000,
|
|
9
|
+
EmptyArrayOperation = 21,
|
|
10
|
+
FailedToParse = 9,
|
|
11
|
+
GraphContainsCycle = 5,
|
|
12
|
+
HostNotFound = 7,
|
|
13
|
+
HostUnreachable = 6,
|
|
14
|
+
IllegalOperation = 20,
|
|
15
|
+
IndexNotFound = 27,
|
|
16
|
+
InternalError = 1,
|
|
17
|
+
InvalidBson = 22,
|
|
18
|
+
InvalidLength = 16,
|
|
19
|
+
InvalidPath = 30,
|
|
20
|
+
LockTimeout = 24,
|
|
21
|
+
MaxTimeMSExpired = 50,
|
|
22
|
+
NonExistentPath = 29,
|
|
23
|
+
NoSuchKey = 4,
|
|
24
|
+
Overflow = 15,
|
|
25
|
+
PathNotViable = 28,
|
|
26
|
+
ProtocolError = 17,
|
|
27
|
+
QueryCommandNotFound = 59,
|
|
28
|
+
QueryNotTailable = 13051,
|
|
29
|
+
RemoteValidationError = 25,
|
|
30
|
+
TypeMismatch = 14,
|
|
31
|
+
Unauthorized = 13,
|
|
32
|
+
UnknownError = 8,
|
|
33
|
+
UnsupportedFormat = 12,
|
|
34
|
+
UserNotFound = 11,
|
|
35
|
+
WriteConcernError = 64
|
|
36
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose" />
|
|
25
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
26
|
+
export * from 'mongoose';
|
|
27
|
+
export * from './dbConfig';
|
|
28
|
+
export * from './errors';
|
|
29
|
+
export * from './migrateMongoConfig';
|
|
30
|
+
export * from './mongoStatus';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
import { mongo } from 'mongoose';
|
|
26
|
+
export interface MigrateMongoConfig {
|
|
27
|
+
mongodb: {
|
|
28
|
+
url: Parameters<(typeof mongo.MongoClient)['connect']>[0];
|
|
29
|
+
databaseName?: mongo.Db['databaseName'];
|
|
30
|
+
options?: mongo.MongoClientOptions;
|
|
31
|
+
};
|
|
32
|
+
migrationsDir?: string;
|
|
33
|
+
changelogCollectionName: string;
|
|
34
|
+
migrationFileExtension?: string;
|
|
35
|
+
useFileHash?: boolean;
|
|
36
|
+
moduleSystem: string;
|
|
37
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
import { Document } from 'mongoose';
|
|
26
|
+
export interface EncryptedStorage {
|
|
27
|
+
data: string;
|
|
28
|
+
expiresAt: Date;
|
|
29
|
+
source?: unknown;
|
|
30
|
+
}
|
|
31
|
+
export interface EncryptedStorageModel extends EncryptedStorage, Document {
|
|
32
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DbType } from './dbConfig';
|
|
2
|
+
export declare enum DbConnectionStatus {
|
|
3
|
+
Disconnected = "disconnected",
|
|
4
|
+
Connected = "connected",
|
|
5
|
+
Connecting = "connecting",
|
|
6
|
+
Disconnecting = "disconnecting",
|
|
7
|
+
OpFailed = "op_failed"
|
|
8
|
+
}
|
|
9
|
+
export type DbStatusByType = Partial<Record<DbType, DbConnectionStatus>>;
|
|
10
|
+
export type MongoDbStatus = {
|
|
11
|
+
mongodb: DbStatusByType;
|
|
12
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
import { Model } from 'mongoose';
|
|
26
|
+
import { EncryptedStorage } from '../interfaces/models/encryptedStorage';
|
|
27
|
+
declare const _default: Model<EncryptedStorage, {}, {}, {}, import("mongoose").Document<unknown, {}, EncryptedStorage> & EncryptedStorage & {
|
|
28
|
+
_id: import("mongoose").Types.ObjectId;
|
|
29
|
+
}, any>;
|
|
30
|
+
export default _default;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { MigrateMongoConfig } from './interfaces';
|
|
2
|
+
export declare const MongoHelper: {
|
|
3
|
+
getMigrateMongoConfig(): Promise<MigrateMongoConfig>;
|
|
4
|
+
buildMongoUrl(user: string | null, password: string | null): string;
|
|
5
|
+
getMongoErrorDupField(msg: string, fields: string[]): string | undefined;
|
|
6
|
+
handleMongoUniqError(err: Error & {
|
|
7
|
+
code?: number;
|
|
8
|
+
}, params: Record<string, unknown>, uniqFieldNames: string[], modelName: string): void;
|
|
9
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
import { ClientSession } from 'mongoose';
|
|
26
|
+
import { EnvService } from '@diia-inhouse/env';
|
|
27
|
+
import { HealthCheckResult, Logger, OnHealthCheck, OnInit } from '@diia-inhouse/types';
|
|
28
|
+
import { AppDb, AppDbConfig, DbType, MongoDbStatus } from '../interfaces';
|
|
29
|
+
export declare class DatabaseService implements OnInit, OnHealthCheck {
|
|
30
|
+
private readonly dbConfigs;
|
|
31
|
+
private readonly envService;
|
|
32
|
+
private readonly logger;
|
|
33
|
+
private readonly dbStateCodeToName;
|
|
34
|
+
readonly defaultModelsDir = "models";
|
|
35
|
+
db: Partial<Record<DbType, AppDb>>;
|
|
36
|
+
constructor(dbConfigs: Record<DbType, AppDbConfig>, envService: EnvService, logger: Logger);
|
|
37
|
+
onInit(): Promise<void>;
|
|
38
|
+
onHealthCheck(): Promise<HealthCheckResult<MongoDbStatus>>;
|
|
39
|
+
createDbConnection(type: DbType, config: AppDbConfig): Promise<AppDb | undefined>;
|
|
40
|
+
syncIndexes(exitAfterSync?: boolean, modelsDir?: string): Promise<void>;
|
|
41
|
+
beginTransaction(dbType?: DbType): Promise<ClientSession>;
|
|
42
|
+
private syncModel;
|
|
43
|
+
}
|