@diia-inhouse/db 4.12.0 → 10.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 +59 -59
- package/dist/cli/cleanupExpiredData.js +104 -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 +73 -0
- package/dist/cli/runMigration.js.map +1 -0
- package/dist/cli/syncIndexes.js +45 -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/counter.js +3 -0
- package/dist/interfaces/models/counter.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/schemas/counter.js +14 -0
- 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 +72 -0
- package/dist/services/counter.js.map +1 -0
- 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 +2 -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 +5 -0
- 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 +15 -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 +2 -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 -60
- package/dist/models/encryptedStorage.js.map +0 -1
- package/dist/types/models/encryptedStorage.d.ts +0 -30
package/LICENCE.md
CHANGED
|
@@ -18,40 +18,40 @@ or has expressed by any other means his willingness to license under the EUPL.
|
|
|
18
18
|
|
|
19
19
|
In this Licence, the following terms have the following meaning:
|
|
20
20
|
|
|
21
|
-
-
|
|
21
|
+
- ‘The Licence’: this Licence.
|
|
22
22
|
|
|
23
|
-
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
- ‘The Original Work’: the work or software distributed or communicated by the
|
|
24
|
+
Licensor under this Licence, available as Source Code and also as Executable
|
|
25
|
+
Code as the case may be.
|
|
26
26
|
|
|
27
|
-
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
- ‘Derivative Works’: the works or software that could be created by the
|
|
28
|
+
Licensee, based upon the Original Work or modifications thereof. This Licence
|
|
29
|
+
does not define the extent of modification or dependence on the Original Work
|
|
30
|
+
required in order to classify a work as a Derivative Work; this extent is
|
|
31
|
+
determined by copyright law applicable in the country mentioned in Article 15.
|
|
32
32
|
|
|
33
|
-
-
|
|
33
|
+
- ‘The Work’: the Original Work or its Derivative Works.
|
|
34
34
|
|
|
35
|
-
-
|
|
36
|
-
|
|
35
|
+
- ‘The Source Code’: the human-readable form of the Work which is the most
|
|
36
|
+
convenient for people to study and modify.
|
|
37
37
|
|
|
38
|
-
-
|
|
39
|
-
|
|
38
|
+
- ‘The Executable Code’: any code which has generally been compiled and which is
|
|
39
|
+
meant to be interpreted by a computer as a program.
|
|
40
40
|
|
|
41
|
-
-
|
|
42
|
-
|
|
41
|
+
- ‘The Licensor’: the natural or legal person that distributes or communicates
|
|
42
|
+
the Work under the Licence.
|
|
43
43
|
|
|
44
|
-
-
|
|
45
|
-
|
|
44
|
+
- ‘Contributor(s)’: any natural or legal person who modifies the Work under the
|
|
45
|
+
Licence, or otherwise contributes to the creation of a Derivative Work.
|
|
46
46
|
|
|
47
|
-
-
|
|
48
|
-
|
|
47
|
+
- ‘The Licensee’ or ‘You’: any natural or legal person who makes any usage of
|
|
48
|
+
the Work under the terms of the Licence.
|
|
49
49
|
|
|
50
|
-
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
- ‘Distribution’ or ‘Communication’: any act of selling, giving, lending,
|
|
51
|
+
renting, distributing, communicating, transmitting, or otherwise making
|
|
52
|
+
available, online or offline, copies of the Work or providing access to its
|
|
53
|
+
essential functionalities at the disposal of any other natural or legal
|
|
54
|
+
person.
|
|
55
55
|
|
|
56
56
|
2. Scope of the rights granted by the Licence
|
|
57
57
|
|
|
@@ -59,15 +59,15 @@ The Licensor hereby grants You a worldwide, royalty-free, non-exclusive,
|
|
|
59
59
|
sublicensable licence to do the following, for the duration of copyright vested
|
|
60
60
|
in the Original Work:
|
|
61
61
|
|
|
62
|
-
-
|
|
63
|
-
-
|
|
64
|
-
-
|
|
65
|
-
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
-
|
|
69
|
-
-
|
|
70
|
-
-
|
|
62
|
+
- use the Work in any circumstance and for all usage,
|
|
63
|
+
- reproduce the Work,
|
|
64
|
+
- modify the Work, and make Derivative Works based upon the Work,
|
|
65
|
+
- communicate to the public, including the right to make available or display
|
|
66
|
+
the Work or copies thereof to the public and perform publicly, as the case may
|
|
67
|
+
be, the Work,
|
|
68
|
+
- distribute the Work or copies thereof,
|
|
69
|
+
- lend and rent the Work or copies thereof,
|
|
70
|
+
- sublicense rights in the Work or copies thereof.
|
|
71
71
|
|
|
72
72
|
Those rights can be exercised on any media, supports and formats, whether now
|
|
73
73
|
known or later invented, as far as the applicable law permits so.
|
|
@@ -240,43 +240,43 @@ their choice.
|
|
|
240
240
|
|
|
241
241
|
Without prejudice to specific agreement between parties,
|
|
242
242
|
|
|
243
|
-
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
243
|
+
- any litigation resulting from the interpretation of this License, arising
|
|
244
|
+
between the European Union institutions, bodies, offices or agencies, as a
|
|
245
|
+
Licensor, and any Licensee, will be subject to the jurisdiction of the Court
|
|
246
|
+
of Justice of the European Union, as laid down in article 272 of the Treaty on
|
|
247
|
+
the Functioning of the European Union,
|
|
248
248
|
|
|
249
|
-
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
249
|
+
- any litigation arising between other parties and resulting from the
|
|
250
|
+
interpretation of this License, will be subject to the exclusive jurisdiction
|
|
251
|
+
of the competent court where the Licensor resides or conducts its primary
|
|
252
|
+
business.
|
|
253
253
|
|
|
254
254
|
15. Applicable Law
|
|
255
255
|
|
|
256
256
|
Without prejudice to specific agreement between parties,
|
|
257
257
|
|
|
258
|
-
-
|
|
259
|
-
|
|
258
|
+
- this Licence shall be governed by the law of the European Union Member State
|
|
259
|
+
where the Licensor has his seat, resides or has his registered office,
|
|
260
260
|
|
|
261
|
-
-
|
|
262
|
-
|
|
261
|
+
- this licence shall be governed by Belgian law if the Licensor has no seat,
|
|
262
|
+
residence or registered office inside a European Union Member State.
|
|
263
263
|
|
|
264
264
|
Appendix
|
|
265
265
|
|
|
266
266
|
‘Compatible Licences’ according to Article 5 EUPL are:
|
|
267
267
|
|
|
268
|
-
-
|
|
269
|
-
-
|
|
270
|
-
-
|
|
271
|
-
-
|
|
272
|
-
-
|
|
273
|
-
-
|
|
274
|
-
-
|
|
275
|
-
-
|
|
276
|
-
|
|
277
|
-
-
|
|
278
|
-
-
|
|
279
|
-
|
|
268
|
+
- GNU General Public License (GPL) v. 2, v. 3
|
|
269
|
+
- GNU Affero General Public License (AGPL) v. 3
|
|
270
|
+
- Open Software License (OSL) v. 2.1, v. 3.0
|
|
271
|
+
- Eclipse Public License (EPL) v. 1.0
|
|
272
|
+
- CeCILL v. 2.0, v. 2.1
|
|
273
|
+
- Mozilla Public Licence (MPL) v. 2
|
|
274
|
+
- GNU Lesser General Public Licence (LGPL) v. 2.1, v. 3
|
|
275
|
+
- Creative Commons Attribution-ShareAlike v. 3.0 Unported (CC BY-SA 3.0) for
|
|
276
|
+
works other than software
|
|
277
|
+
- European Union Public Licence (EUPL) v. 1.1, v. 1.2
|
|
278
|
+
- Québec Free and Open-Source Licence — Reciprocity (LiLiQ-R) or Strong
|
|
279
|
+
Reciprocity (LiLiQ-R+).
|
|
280
280
|
|
|
281
281
|
The European Commission may update this Appendix to later versions of the above
|
|
282
282
|
licences without producing a new version of the EUPL, as long as they provide
|
|
@@ -0,0 +1,104 @@
|
|
|
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.CleanupExpiredDataCommand = void 0;
|
|
7
|
+
const node_fs_1 = require("node:fs");
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const drizzle_orm_1 = require("drizzle-orm");
|
|
10
|
+
const node_postgres_1 = require("drizzle-orm/node-postgres");
|
|
11
|
+
const pg_core_1 = require("drizzle-orm/pg-core");
|
|
12
|
+
const ts_node_1 = require("ts-node");
|
|
13
|
+
(0, ts_node_1.register)();
|
|
14
|
+
class CleanupExpiredDataCommand {
|
|
15
|
+
logger;
|
|
16
|
+
constructor(logger) {
|
|
17
|
+
this.logger = logger;
|
|
18
|
+
}
|
|
19
|
+
async run(filePath) {
|
|
20
|
+
const schema = await this.loadSchema(filePath);
|
|
21
|
+
if (!schema) {
|
|
22
|
+
this.logger.info('Pg schemas not found under provided path', { schemasPath: filePath });
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const db = this.createDbConnection(schema);
|
|
26
|
+
const expiringTables = this.findExpiringTables(schema);
|
|
27
|
+
if (expiringTables.length === 0) {
|
|
28
|
+
this.logger.info('No tables with expiring data found');
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
await this.cleanupTables(db, schema, expiringTables);
|
|
32
|
+
await db.$client.end();
|
|
33
|
+
}
|
|
34
|
+
async loadSchema(filePath) {
|
|
35
|
+
const schemaPath = `./dist/${filePath}`;
|
|
36
|
+
const fullSchemaPath = node_path_1.default.resolve(schemaPath);
|
|
37
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
38
|
+
const isExistsSync = (0, node_fs_1.existsSync)(fullSchemaPath); // nosemgrep: eslint.detect-non-literal-fs-filename
|
|
39
|
+
if (!isExistsSync) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
return await import(fullSchemaPath);
|
|
43
|
+
}
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
45
|
+
createDbConnection(schema) {
|
|
46
|
+
return (0, node_postgres_1.drizzle)(process.env.POSTGRES_DATABASE_URL, {
|
|
47
|
+
schema,
|
|
48
|
+
casing: 'snake_case',
|
|
49
|
+
logger: true,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async cleanupTables(db, schema, expiringTables) {
|
|
53
|
+
this.logger.info(`Found ${expiringTables.length} tables with expiring data: ${expiringTables.map(({ name }) => name).join(', ')}`);
|
|
54
|
+
const startTime = Date.now();
|
|
55
|
+
try {
|
|
56
|
+
for (const { name, tableName, expiringColumns } of expiringTables) {
|
|
57
|
+
this.logger.info(`Starting cleanup expired data from table: ${name}`);
|
|
58
|
+
const table = schema[tableName];
|
|
59
|
+
for (const { column, expireAfterSeconds } of expiringColumns) {
|
|
60
|
+
const result = await db
|
|
61
|
+
.delete(table)
|
|
62
|
+
.where((0, drizzle_orm_1.lt)(drizzle_orm_1.sql.raw(`"${name}"."${column.name}" + interval '${expireAfterSeconds} seconds'`), new Date()));
|
|
63
|
+
this.logger.info(`Deleted ${result.rowCount} rows from table ${name} by column ${column.name} with expireAfterSeconds ${expireAfterSeconds}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
this.logger.info(`Cleanup expired data finished! It took ${((Date.now() - startTime) / 1000).toFixed(1)} seconds`);
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
this.logger.error('Failed to cleanup expired data', { err });
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
findExpiringTables(schemas) {
|
|
74
|
+
const tables = Object.entries(schemas)
|
|
75
|
+
.filter(([, table]) => table?.constructor?.name === 'PgTable')
|
|
76
|
+
.map(([tableName, table]) => ({
|
|
77
|
+
...(0, pg_core_1.getTableConfig)(table),
|
|
78
|
+
tableName,
|
|
79
|
+
}));
|
|
80
|
+
const expiringTables = [];
|
|
81
|
+
for (const table of tables) {
|
|
82
|
+
const expiringColumns = table.columns
|
|
83
|
+
.filter((column) => this.isExpiringColumn(column))
|
|
84
|
+
.map((column) => ({
|
|
85
|
+
column: column,
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
87
|
+
expireAfterSeconds: column['config'].fieldConfig.expireAfterSeconds,
|
|
88
|
+
}));
|
|
89
|
+
if (expiringColumns.length > 0) {
|
|
90
|
+
expiringTables.push({ ...table, expiringColumns });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return expiringTables;
|
|
94
|
+
}
|
|
95
|
+
isExpiringColumn(column) {
|
|
96
|
+
if (column.columnType !== 'PgCustomColumn') {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
const config = column['config'];
|
|
100
|
+
return typeof config?.fieldConfig?.expireAfterSeconds === 'number';
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.CleanupExpiredDataCommand = CleanupExpiredDataCommand;
|
|
104
|
+
//# sourceMappingURL=cleanupExpiredData.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cleanupExpiredData.js","sourceRoot":"","sources":["../../src/cli/cleanupExpiredData.ts"],"names":[],"mappings":";;;;;;AAAA,qCAAoC;AACpC,0DAA4B;AAE5B,6CAAqC;AACrC,6DAAiF;AACjF,iDAAuE;AAEvE,qCAAkC;AAIlC,IAAA,kBAAQ,GAAE,CAAA;AAaV,MAAa,yBAAyB;IACL;IAA7B,YAA6B,MAAc;Q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IAAI,MAAM,CAAC,UAAU,KAAK,gBAAgB,EAAE,CAAC;YACzC,OAAO,KAAK,CAAA;QAChB,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAA+D,CAAA;QAE7F,OAAO,OAAO,MAAM,EAAE,WAAW,EAAE,kBAAkB,KAAK,QAAQ,CAAA;IACtE,CAAC;CACJ;AA9GD,8DA8GC"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const yargs_1 = __importDefault(require("yargs"));
|
|
8
|
+
const helpers_1 = require("yargs/helpers");
|
|
9
|
+
const diia_logger_1 = __importDefault(require("@diia-inhouse/diia-logger"));
|
|
10
|
+
const env_1 = require("@diia-inhouse/env");
|
|
11
|
+
const interfaces_1 = require("../interfaces");
|
|
12
|
+
const database_1 = require("../services/database");
|
|
13
|
+
const cleanupExpiredData_1 = require("./cleanupExpiredData");
|
|
14
|
+
const runMigration_1 = require("./runMigration");
|
|
15
|
+
const syncIndexes_1 = require("./syncIndexes");
|
|
16
|
+
async function createDatabaseService(envService, logger) {
|
|
17
|
+
return new database_1.DatabaseService('mongo', {
|
|
18
|
+
[interfaces_1.DbType.Main]: {
|
|
19
|
+
database: env_1.EnvService.getVar('MONGO_DATABASE'),
|
|
20
|
+
replicaSet: env_1.EnvService.getVar('MONGO_REPLICA_SET'),
|
|
21
|
+
user: await envService.getSecret('MONGO_USER', { accessor: 'username', nullable: true }),
|
|
22
|
+
password: await envService.getSecret('MONGO_PASSWORD', { accessor: 'password', nullable: true }),
|
|
23
|
+
authSource: env_1.EnvService.getVar('MONGO_AUTH_SOURCE', 'string', null),
|
|
24
|
+
port: env_1.EnvService.getVar('MONGO_PORT', 'number'),
|
|
25
|
+
replicaSetNodes: env_1.EnvService.getVar('MONGO_HOSTS', 'string')
|
|
26
|
+
.split(',')
|
|
27
|
+
.map((replicaHost) => ({ replicaHost })),
|
|
28
|
+
readPreference: env_1.EnvService.getVar('MONGO_READ_PREFERENCE'),
|
|
29
|
+
metrics: {
|
|
30
|
+
enabled: false,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
}, envService, logger);
|
|
34
|
+
}
|
|
35
|
+
async function main() {
|
|
36
|
+
await (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
|
|
37
|
+
.command('sync-indexes', 'Sync indexes for MongoDB models', (args) => args.option('modelsDir', {
|
|
38
|
+
type: 'string',
|
|
39
|
+
default: 'models',
|
|
40
|
+
describe: 'Directory for models',
|
|
41
|
+
}), async (argv) => {
|
|
42
|
+
const logger = new diia_logger_1.default();
|
|
43
|
+
const envService = new env_1.EnvService(logger);
|
|
44
|
+
await envService.init();
|
|
45
|
+
const command = new syncIndexes_1.SyncIndexesCommand(logger);
|
|
46
|
+
try {
|
|
47
|
+
const databaseService = await createDatabaseService(envService, logger);
|
|
48
|
+
await databaseService.onInit();
|
|
49
|
+
await command.run(argv.modelsDir);
|
|
50
|
+
await databaseService.onDestroy();
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
await envService.onDestroy();
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
.command('migrate', 'Run MongoDB migration script', (args) => args
|
|
57
|
+
.option('path', {
|
|
58
|
+
type: 'string',
|
|
59
|
+
demandOption: true,
|
|
60
|
+
describe: 'Path to migration script',
|
|
61
|
+
})
|
|
62
|
+
.option('down', {
|
|
63
|
+
type: 'boolean',
|
|
64
|
+
default: false,
|
|
65
|
+
describe: 'Run migration down script',
|
|
66
|
+
}), async (argv) => {
|
|
67
|
+
const logger = new diia_logger_1.default();
|
|
68
|
+
const envService = new env_1.EnvService(logger);
|
|
69
|
+
await envService.init();
|
|
70
|
+
try {
|
|
71
|
+
const databaseService = await createDatabaseService(envService, logger);
|
|
72
|
+
await databaseService.onInit();
|
|
73
|
+
const db = databaseService.db.main?.connection.db;
|
|
74
|
+
const client = databaseService.db.main?.connection.getClient();
|
|
75
|
+
if (!db || !client) {
|
|
76
|
+
throw new Error('Expected db and client to be defined');
|
|
77
|
+
}
|
|
78
|
+
const command = new runMigration_1.RunMigrationCommand(logger, db, client);
|
|
79
|
+
await command.run(argv.path, argv.down);
|
|
80
|
+
await databaseService.onDestroy();
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
await envService.onDestroy();
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
.command('pg-cleanup-expired-data', 'Cleanup expired data for PostgreSQL tables based on expiring timestamp', (args) => args.option('path', {
|
|
87
|
+
type: 'string',
|
|
88
|
+
default: 'db/schemas',
|
|
89
|
+
describe: 'Path to schemas directory',
|
|
90
|
+
}), async (argv) => {
|
|
91
|
+
const logger = new diia_logger_1.default();
|
|
92
|
+
const envService = new env_1.EnvService(logger);
|
|
93
|
+
await envService.init();
|
|
94
|
+
try {
|
|
95
|
+
const command = new cleanupExpiredData_1.CleanupExpiredDataCommand(logger);
|
|
96
|
+
await command.run(argv.path);
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
await envService.onDestroy();
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
.help().argv;
|
|
103
|
+
}
|
|
104
|
+
void main();
|
|
105
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":";;;;;;AACA,kDAAyB;AACzB,2CAAuC;AAEvC,4EAAkD;AAClD,2CAA8C;AAG9C,8CAAsC;AACtC,mDAAsD;AACtD,6DAAgE;AAChE,iDAAoD;AACpD,+CAAkD;AAElD,KAAK,UAAU,qBAAqB,CAAC,UAAsB,EAAE,MAAc;IACvE,OAAO,IAAI,0BAAe,CACtB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AE,CAAC,IAAI,CAAA;AACpB,CAAC;AAED,KAAK,IAAI,EAAE,CAAA"}
|
|
@@ -0,0 +1,73 @@
|
|
|
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.RunMigrationCommand = void 0;
|
|
7
|
+
const node_fs_1 = require("node:fs");
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const ts_node_1 = require("ts-node");
|
|
10
|
+
(0, ts_node_1.register)();
|
|
11
|
+
class RunMigrationCommand {
|
|
12
|
+
logger;
|
|
13
|
+
db;
|
|
14
|
+
client;
|
|
15
|
+
changelogCollectionName = 'migrations-history';
|
|
16
|
+
constructor(logger, db, client) {
|
|
17
|
+
this.logger = logger;
|
|
18
|
+
this.db = db;
|
|
19
|
+
this.client = client;
|
|
20
|
+
}
|
|
21
|
+
async run(filePath, execDown = false) {
|
|
22
|
+
const scriptPath = `./${filePath}`;
|
|
23
|
+
const fileName = node_path_1.default.parse(scriptPath).name;
|
|
24
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
25
|
+
const isExistsSync = (0, node_fs_1.existsSync)(scriptPath); // nosemgrep: eslint.detect-non-literal-fs-filename
|
|
26
|
+
if (!isExistsSync) {
|
|
27
|
+
this.logger.info('Script not found under provided path', { scriptPath });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const startTime = Date.now();
|
|
31
|
+
const execScriptName = execDown ? 'down' : 'up';
|
|
32
|
+
const modelModule = await import(node_path_1.default.resolve(scriptPath));
|
|
33
|
+
const { up, down } = modelModule;
|
|
34
|
+
this.logger.info(`Starting migration ${execScriptName}: ${fileName}`);
|
|
35
|
+
try {
|
|
36
|
+
if (execDown) {
|
|
37
|
+
if (!down) {
|
|
38
|
+
throw new Error(`Migration script is missing 'down' function.`);
|
|
39
|
+
}
|
|
40
|
+
await down(this.db, this.client);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
if (!up) {
|
|
44
|
+
throw new Error(`Migration script is missing 'up' function.`);
|
|
45
|
+
}
|
|
46
|
+
await up(this.db, this.client);
|
|
47
|
+
}
|
|
48
|
+
this.logger.info(`Migration script finished! It took ${((Date.now() - startTime) / 1000).toFixed(1)} seconds`);
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
this.logger.error(`Couldn't migrate ${execScriptName}: ${fileName}`);
|
|
52
|
+
await this.updateChangeLog(startTime, fileName, execScriptName, true);
|
|
53
|
+
throw err;
|
|
54
|
+
}
|
|
55
|
+
await this.updateChangeLog(startTime, fileName, execScriptName);
|
|
56
|
+
}
|
|
57
|
+
async updateChangeLog(startTime, fileName, type, isFailed) {
|
|
58
|
+
const changelogCollection = this.db.collection(this.changelogCollectionName);
|
|
59
|
+
const startedAt = new Date(startTime);
|
|
60
|
+
const endedAt = new Date();
|
|
61
|
+
const duration = Math.ceil((Date.now() - startTime) / 1000);
|
|
62
|
+
const status = isFailed ? 'FAILED' : 'DONE';
|
|
63
|
+
try {
|
|
64
|
+
await changelogCollection.insertOne({ fileName, type, duration, startedAt, endedAt, status });
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
this.logger.error(`Couldn't update changelog: ${err.message}`);
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.RunMigrationCommand = RunMigrationCommand;
|
|
73
|
+
//# sourceMappingURL=runMigration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runMigration.js","sourceRoot":"","sources":["../../src/cli/runMigration.ts"],"names":[],"mappings":";;;;;;AAAA,qCAAoC;AACpC,0DAA4B;AAE5B,qCAAkC;AAMlC,IAAA,kBAAQ,GAAE,CAAA;AAEV,MAAa,mBAAmB;IAIP;IACA;IAC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,CAAC;YACD,MAAM,mBAAmB,CAAC,SAAS,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAA;QACjG,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,8BAA+B,GAAa,CAAC,OAAO,EAAE,CAAC,CAAA;YAEzE,MAAM,GAAG,CAAA;QACb,CAAC;IACL,CAAC;CACJ;AAxED,kDAwEC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
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.SyncIndexesCommand = void 0;
|
|
7
|
+
const node_fs_1 = require("node:fs");
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const recursive_readdir_1 = __importDefault(require("recursive-readdir"));
|
|
10
|
+
class SyncIndexesCommand {
|
|
11
|
+
logger;
|
|
12
|
+
constructor(logger) {
|
|
13
|
+
this.logger = logger;
|
|
14
|
+
}
|
|
15
|
+
async run(modelsDir) {
|
|
16
|
+
const modelsPath = `./dist/${modelsDir}`;
|
|
17
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
18
|
+
const isExistsSync = (0, node_fs_1.existsSync)(modelsPath); // nosemgrep: eslint.detect-non-literal-fs-filename
|
|
19
|
+
if (!isExistsSync) {
|
|
20
|
+
throw new Error(`Models dir is absent: ${modelsPath}`);
|
|
21
|
+
}
|
|
22
|
+
const t0 = Date.now();
|
|
23
|
+
this.logger.info('Start syncing indexes');
|
|
24
|
+
const files = await (0, recursive_readdir_1.default)(modelsPath, ['*.map', 'index.js', 'schemas', '*.types.js']);
|
|
25
|
+
const tasks = [];
|
|
26
|
+
for (const fileName of files) {
|
|
27
|
+
const modelModule = await import(node_path_1.default.resolve(fileName));
|
|
28
|
+
if (modelModule.skipSyncIndexes) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const task = modelModule.default;
|
|
32
|
+
tasks.push(this.syncModel(task));
|
|
33
|
+
}
|
|
34
|
+
await Promise.all(tasks);
|
|
35
|
+
this.logger.info(`Ended syncing indexes in ${Date.now() - t0} ms`);
|
|
36
|
+
}
|
|
37
|
+
async syncModel(model) {
|
|
38
|
+
const t0 = Date.now();
|
|
39
|
+
this.logger.info(`Start syncing indexes for the ${model.modelName} collection`);
|
|
40
|
+
await model.syncIndexes();
|
|
41
|
+
this.logger.info(`Ended syncing indexes for the ${model.modelName} collection in ${Date.now() - t0} ms`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.SyncIndexesCommand = SyncIndexesCommand;
|
|
45
|
+
//# sourceMappingURL=syncIndexes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"syncIndexes.js","sourceRoot":"","sources":["../../src/cli/syncIndexes.ts"],"names":[],"mappings":";;;;;;AAAA,qCAAoC;AACpC,0DAA4B;AAG5B,0EAA6C;AAI7C,MAAa,kBAAkB;IACE;IAA7B,YAA6B,MAAc;QAAd,WAAM,GAAN,MAAM,CAAQ;IAAG,CAAC;IAE/C,KAAK,CAAC,GAAG,CAAC,SAAiB;QACvB,MAAM,UAAU,GAAG,UAAU,SAAS,EAAE,CAAA;QACxC,mEAAmE;QACnE,MAAM,YAAY,GAAG,IAAA,oBAAU,EAAC,UAAU,CAAC,CAAA,CAAC,mDAAmD;QAE/F,IAAI,CAAC,YAAY,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,yBAAyB,UAAU,EAAE,CAAC,CAAA;QAC1D,CAAC;QAED,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAErB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAA;QACzC,MAAM,KAAK,GAAG,MAAM,IAAA,2BAAa,EAAC,UAAU,EAAE,CAAC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC,CAAA;QAC7F,MAAM,KAAK,GAAG,EAAE,CAAA;QAChB,KAAK,MAAM,QAAQ,IAAI,KAAK,EAAE,CAAC;YAC3B,MAAM,WAAW,GAAG,MAAM,MAAM,CAAC,mBAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAA;YACxD,IAAI,WAAW,CAAC,eAAe,EAAE,CAAC;gBAC9B,SAAQ;YACZ,CAAC;YAED,MAAM,IAAI,GAAG,WAAW,CAAC,OAAO,CAAA;YAEhC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAA;QACpC,CAAC;QAED,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QACxB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,4BAA4B,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;IACtE,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;AAvCD,gDAuCC"}
|
package/dist/index.js
CHANGED
|
@@ -17,4 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./interfaces"), exports);
|
|
18
18
|
__exportStar(require("./services"), exports);
|
|
19
19
|
__exportStar(require("./mongo"), exports);
|
|
20
|
+
__exportStar(require("./schemas"), exports);
|
|
21
|
+
__exportStar(require("./postgres"), exports);
|
|
20
22
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA4B;AAE5B,6CAA0B;AAE1B,0CAAuB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA4B;AAE5B,6CAA0B;AAE1B,0CAAuB;AAEvB,4CAAyB;AAEzB,6CAA0B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.js","sourceRoot":"","sources":["../../src/interfaces/database.ts"],"names":[],"mappings":""}
|
|
@@ -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 @@
|
|
|
1
|
+
{"version":3,"file":"counter.js","sourceRoot":"","sources":["../../../src/interfaces/models/counter.ts"],"names":[],"mappings":""}
|