@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.
Files changed (84) hide show
  1. package/LICENCE.md +59 -59
  2. package/dist/cli/cleanupExpiredData.js +137 -0
  3. package/dist/cli/cleanupExpiredData.js.map +1 -0
  4. package/dist/cli/index.js +105 -0
  5. package/dist/cli/index.js.map +1 -0
  6. package/dist/cli/runMigration.js +106 -0
  7. package/dist/cli/runMigration.js.map +1 -0
  8. package/dist/cli/syncIndexes.js +78 -0
  9. package/dist/cli/syncIndexes.js.map +1 -0
  10. package/dist/index.js +2 -0
  11. package/dist/index.js.map +1 -1
  12. package/dist/interfaces/database.js +3 -0
  13. package/dist/interfaces/database.js.map +1 -0
  14. package/dist/interfaces/errors.js +5 -1
  15. package/dist/interfaces/errors.js.map +1 -1
  16. package/dist/interfaces/index.js +3 -0
  17. package/dist/interfaces/index.js.map +1 -1
  18. package/dist/interfaces/metrics.js +20 -0
  19. package/dist/interfaces/metrics.js.map +1 -0
  20. package/dist/interfaces/models/index.js +19 -0
  21. package/dist/interfaces/models/index.js.map +1 -0
  22. package/dist/mongo.js +18 -3
  23. package/dist/mongo.js.map +1 -1
  24. package/dist/postgres.js +19 -0
  25. package/dist/postgres.js.map +1 -0
  26. package/dist/postgresUtils.js +20 -0
  27. package/dist/postgresUtils.js.map +1 -0
  28. package/dist/{models → schemas}/counter.js +2 -1
  29. package/dist/schemas/counter.js.map +1 -0
  30. package/dist/{models → schemas}/encryptedStorage.js +4 -2
  31. package/dist/schemas/encryptedStorage.js.map +1 -0
  32. package/dist/schemas/index.js +20 -0
  33. package/dist/schemas/index.js.map +1 -0
  34. package/dist/schemas/statusHistoryItem.js +12 -0
  35. package/dist/schemas/statusHistoryItem.js.map +1 -0
  36. package/dist/services/counter.js +60 -8
  37. package/dist/services/counter.js.map +1 -1
  38. package/dist/services/database.js +123 -55
  39. package/dist/services/database.js.map +1 -1
  40. package/dist/services/encryptedStorage.js +136 -15
  41. package/dist/services/encryptedStorage.js.map +1 -1
  42. package/dist/services/index.js +1 -0
  43. package/dist/services/index.js.map +1 -1
  44. package/dist/services/postgresDatabase.js +25 -0
  45. package/dist/services/postgresDatabase.js.map +1 -0
  46. package/dist/tables/counter.js +12 -0
  47. package/dist/tables/counter.js.map +1 -0
  48. package/dist/tables/encryptedStorage.js +14 -0
  49. package/dist/tables/encryptedStorage.js.map +1 -0
  50. package/dist/tables/index.js +19 -0
  51. package/dist/tables/index.js.map +1 -0
  52. package/dist/types/cli/cleanupExpiredData.d.ts +11 -0
  53. package/dist/types/cli/index.d.ts +2 -0
  54. package/dist/types/cli/runMigration.d.ts +11 -0
  55. package/dist/types/cli/syncIndexes.d.ts +7 -0
  56. package/dist/types/index.d.ts +2 -0
  57. package/dist/types/interfaces/database.d.ts +8 -0
  58. package/dist/types/interfaces/dbConfig.d.ts +8 -29
  59. package/dist/types/interfaces/errors.d.ts +5 -1
  60. package/dist/types/interfaces/index.d.ts +3 -25
  61. package/dist/types/interfaces/metrics.d.ts +20 -0
  62. package/dist/types/interfaces/migrateMongoConfig.d.ts +0 -24
  63. package/dist/types/interfaces/models/counter.d.ts +0 -27
  64. package/dist/types/interfaces/models/encryptedStorage.d.ts +1 -27
  65. package/dist/types/interfaces/models/index.d.ts +2 -0
  66. package/dist/types/postgres.d.ts +2 -0
  67. package/dist/types/postgresUtils.d.ts +26 -0
  68. package/dist/types/schemas/counter.d.ts +12 -0
  69. package/dist/types/schemas/encryptedStorage.d.ts +12 -0
  70. package/dist/types/schemas/index.d.ts +3 -0
  71. package/dist/types/schemas/statusHistoryItem.d.ts +3 -0
  72. package/dist/types/services/counter.d.ts +11 -0
  73. package/dist/types/services/database.d.ts +33 -33
  74. package/dist/types/services/encryptedStorage.d.ts +60 -34
  75. package/dist/types/services/index.d.ts +1 -0
  76. package/dist/types/services/postgresDatabase.d.ts +8 -0
  77. package/dist/types/tables/counter.d.ts +94 -0
  78. package/dist/types/tables/encryptedStorage.d.ts +111 -0
  79. package/dist/types/tables/index.d.ts +2 -0
  80. package/package.json +62 -61
  81. package/dist/models/counter.js.map +0 -1
  82. package/dist/models/encryptedStorage.js.map +0 -1
  83. package/dist/types/models/counter.d.ts +0 -30
  84. 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
- - ‘The Licence’: this Licence.
21
+ - ‘The Licence’: this Licence.
22
22
 
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.
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
- - ‘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.
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
- - ‘The Work’: the Original Work or its Derivative Works.
33
+ - ‘The Work’: the Original Work or its Derivative Works.
34
34
 
35
- - ‘The Source Code’: the human-readable form of the Work which is the most
36
- convenient for people to study and modify.
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
- - ‘The Executable Code’: any code which has generally been compiled and which is
39
- meant to be interpreted by a computer as a program.
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
- - ‘The Licensor’: the natural or legal person that distributes or communicates
42
- the Work under the Licence.
41
+ - ‘The Licensor’: the natural or legal person that distributes or communicates
42
+ the Work under the Licence.
43
43
 
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.
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
- - ‘The Licensee’ or ‘You’: any natural or legal person who makes any usage of
48
- the Work under the terms of the Licence.
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
- - ‘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.
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
- - 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.
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
- - 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,
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
- - 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.
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
- - 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,
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
- - 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.
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
- - 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+).
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,137 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.CleanupExpiredDataCommand = void 0;
40
+ const node_fs_1 = require("node:fs");
41
+ const node_path_1 = __importDefault(require("node:path"));
42
+ const drizzle_orm_1 = require("drizzle-orm");
43
+ const node_postgres_1 = require("drizzle-orm/node-postgres");
44
+ const pg_core_1 = require("drizzle-orm/pg-core");
45
+ const ts_node_1 = require("ts-node");
46
+ (0, ts_node_1.register)();
47
+ class CleanupExpiredDataCommand {
48
+ logger;
49
+ constructor(logger) {
50
+ this.logger = logger;
51
+ }
52
+ async run(filePath) {
53
+ const schema = await this.loadSchema(filePath);
54
+ if (!schema) {
55
+ this.logger.info('Pg schemas not found under provided path', { schemasPath: filePath });
56
+ return;
57
+ }
58
+ const db = this.createDbConnection(schema);
59
+ const expiringTables = this.findExpiringTables(schema);
60
+ if (expiringTables.length === 0) {
61
+ this.logger.info('No tables with expiring data found');
62
+ return;
63
+ }
64
+ await this.cleanupTables(db, schema, expiringTables);
65
+ await db.$client.end();
66
+ }
67
+ async loadSchema(filePath) {
68
+ const schemaPath = `./dist/${filePath}`;
69
+ const fullSchemaPath = node_path_1.default.resolve(schemaPath);
70
+ // eslint-disable-next-line security/detect-non-literal-fs-filename
71
+ const isExistsSync = (0, node_fs_1.existsSync)(fullSchemaPath); // nosemgrep: eslint.detect-non-literal-fs-filename
72
+ if (!isExistsSync) {
73
+ return;
74
+ }
75
+ return await Promise.resolve(`${fullSchemaPath}`).then(s => __importStar(require(s)));
76
+ }
77
+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
78
+ createDbConnection(schema) {
79
+ return (0, node_postgres_1.drizzle)(process.env.POSTGRES_DATABASE_URL, {
80
+ schema,
81
+ casing: 'snake_case',
82
+ logger: true,
83
+ });
84
+ }
85
+ async cleanupTables(db, schema, expiringTables) {
86
+ this.logger.info(`Found ${expiringTables.length} tables with expiring data: ${expiringTables.map(({ name }) => name).join(', ')}`);
87
+ const startTime = Date.now();
88
+ try {
89
+ for (const { name, tableName, expiringColumns } of expiringTables) {
90
+ this.logger.info(`Starting cleanup expired data from table: ${name}`);
91
+ const table = schema[tableName];
92
+ for (const { column, expireAfterSeconds } of expiringColumns) {
93
+ const result = await db
94
+ .delete(table)
95
+ .where((0, drizzle_orm_1.lt)(drizzle_orm_1.sql.raw(`"${name}"."${column.name}" + interval '${expireAfterSeconds} seconds'`), new Date()));
96
+ this.logger.info(`Deleted ${result.rowCount} rows from table ${name} by column ${column.name} with expireAfterSeconds ${expireAfterSeconds}`);
97
+ }
98
+ }
99
+ this.logger.info(`Cleanup expired data finished! It took ${((Date.now() - startTime) / 1000).toFixed(1)} seconds`);
100
+ }
101
+ catch (err) {
102
+ this.logger.error('Failed to cleanup expired data', { err });
103
+ throw err;
104
+ }
105
+ }
106
+ findExpiringTables(schemas) {
107
+ const tables = Object.entries(schemas)
108
+ .filter(([, table]) => table?.constructor?.name === 'PgTable')
109
+ .map(([tableName, table]) => ({
110
+ ...(0, pg_core_1.getTableConfig)(table),
111
+ tableName,
112
+ }));
113
+ const expiringTables = [];
114
+ for (const table of tables) {
115
+ const expiringColumns = table.columns
116
+ .filter((column) => this.isExpiringColumn(column))
117
+ .map((column) => ({
118
+ column: column,
119
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
120
+ expireAfterSeconds: column['config'].fieldConfig.expireAfterSeconds,
121
+ }));
122
+ if (expiringColumns.length > 0) {
123
+ expiringTables.push({ ...table, expiringColumns });
124
+ }
125
+ }
126
+ return expiringTables;
127
+ }
128
+ isExpiringColumn(column) {
129
+ if (column.columnType !== 'PgCustomColumn') {
130
+ return false;
131
+ }
132
+ const config = column['config'];
133
+ return typeof config?.fieldConfig?.expireAfterSeconds === 'number';
134
+ }
135
+ }
136
+ exports.CleanupExpiredDataCommand = CleanupExpiredDataCommand;
137
+ //# 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;QAAd,WAAM,GAAN,MAAM,CAAQ;IAAG,CAAC;IAE/C,KAAK,CAAC,GAAG,CAAC,QAAgB;QACtB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAA;QAC9C,IAAI,CAAC,MAAM,EAAE,CAAC;YACV,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,0CAA0C,EAAE,EAAE,WAAW,EAAE,QAAQ,EAAE,CAAC,CAAA;YAEvF,OAAM;QACV,CAAC;QAED,MAAM,EAAE,GAAG,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAA;QAC1C,MAAM,cAAc,GAAG,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAA;QAEtD,IAAI,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,oCAAoC,CAAC,CAAA;YAEtD,OAAM;QACV,CAAC;QAED,MAAM,IAAI,CAAC,a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C,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YACzB,MAAM,eAAe,GAAG,KAAK,CAAC,OAAO;iBAChC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,gBAAgB,CAAC,MAA6B,CAAC,CAAC;iBACxE,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;gBACd,MAAM,EAAE,MAA6B;gBACrC,8DAA8D;gBAC9D,kBAAkB,EAAG,MAAM,CAAC,QAAQ,CAAS,CAAC,WAAW,CAAC,kBAAkB;aAC/E,CAAC,CAAC,CAAA;YAEP,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC7B,cAAc,CAAC,IAAI,CAAC,EAAE,GAAG,KAAK,EAAE,eAAe,EAAE,CAAC,CAAA;YACtD,CAAC;QACL,CAAC;QAED,OAAO,cAAc,CAAA;IACzB,CAAC;IAEO,gBAAgB,CAAC,MAAgB;QACrC,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,OAAO,EACP;QACI,CAAC,mBAAM,CAAC,IAAI,CAAC,EAAE;YACX,QAAQ,EAAE,gBAAU,CAAC,MAAM,CAAC,gBAAgB,CAAC;YAC7C,UAAU,EAAE,gBAAU,CAAC,MAAM,CAAC,mBAAmB,CAAC;YAClD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EAAE,IAAI,CAAC,MAAM,EAAE,CAAC;gBACjB,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAA;YAC3D,CAAC;YAED,MAAM,OAAO,GAAG,IAAI,kCAAmB,CAAC,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,CAAA;YAE3D,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;YACvC,MAAM,eAAe,CAAC,SAAS,EAAE,CAAA;QACrC,CAAC;gBAAS,CAAC;YACP,MAAM,UAAU,CAAC,SAAS,EAAE,CAAA;QAChC,CAAC;IACL,CAAC,CACJ;SACA,OAAO,CACJ,yBAAyB,EACzB,wEAAwE,EACxE,CAAC,IAAI,EAAE,EAAE,CACL,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;QAChB,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,YAAY;QACrB,QAAQ,EAAE,2BAA2B;KACxC,CAAC,EACN,KAAK,EAAE,IAAI,EAAE,EAAE;QACX,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;QACvB,IAAI,CAAC;YACD,MAAM,OAAO,GAAG,IAAI,8CAAyB,CAAC,MAAM,CAAC,CAAA;YAErD,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAChC,CAAC;gBAAS,CAAC;YACP,MAAM,UAAU,CAAC,SAAS,EAAE,CAAA;QAChC,CAAC;IACL,CAAC,CACJ;SACA,IAAI,EAAE,CAAC,IAAI,CAAA;AACpB,CAAC;AAED,KAAK,IAAI,EAAE,CAAA"}
@@ -0,0 +1,106 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.RunMigrationCommand = void 0;
40
+ const node_fs_1 = require("node:fs");
41
+ const node_path_1 = __importDefault(require("node:path"));
42
+ const ts_node_1 = require("ts-node");
43
+ (0, ts_node_1.register)();
44
+ class RunMigrationCommand {
45
+ logger;
46
+ db;
47
+ client;
48
+ changelogCollectionName = 'migrations-history';
49
+ constructor(logger, db, client) {
50
+ this.logger = logger;
51
+ this.db = db;
52
+ this.client = client;
53
+ }
54
+ async run(filePath, execDown = false) {
55
+ const scriptPath = `./${filePath}`;
56
+ const fileName = node_path_1.default.parse(scriptPath).name;
57
+ // eslint-disable-next-line security/detect-non-literal-fs-filename
58
+ const isExistsSync = (0, node_fs_1.existsSync)(scriptPath); // nosemgrep: eslint.detect-non-literal-fs-filename
59
+ if (!isExistsSync) {
60
+ this.logger.info('Script not found under provided path', { scriptPath });
61
+ return;
62
+ }
63
+ const startTime = Date.now();
64
+ const execScriptName = execDown ? 'down' : 'up';
65
+ const modelModule = await Promise.resolve(`${node_path_1.default.resolve(scriptPath)}`).then(s => __importStar(require(s)));
66
+ const { up, down } = modelModule;
67
+ this.logger.info(`Starting migration ${execScriptName}: ${fileName}`);
68
+ try {
69
+ if (execDown) {
70
+ if (!down) {
71
+ throw new Error(`Migration script is missing 'down' function.`);
72
+ }
73
+ await down(this.db, this.client);
74
+ }
75
+ else {
76
+ if (!up) {
77
+ throw new Error(`Migration script is missing 'up' function.`);
78
+ }
79
+ await up(this.db, this.client);
80
+ }
81
+ this.logger.info(`Migration script finished! It took ${((Date.now() - startTime) / 1000).toFixed(1)} seconds`);
82
+ }
83
+ catch (err) {
84
+ this.logger.error(`Couldn't migrate ${execScriptName}: ${fileName}`);
85
+ await this.updateChangeLog(startTime, fileName, execScriptName, true);
86
+ throw err;
87
+ }
88
+ await this.updateChangeLog(startTime, fileName, execScriptName);
89
+ }
90
+ async updateChangeLog(startTime, fileName, type, isFailed) {
91
+ const changelogCollection = this.db.collection(this.changelogCollectionName);
92
+ const startedAt = new Date(startTime);
93
+ const endedAt = new Date();
94
+ const duration = Math.ceil((Date.now() - startTime) / 1000);
95
+ const status = isFailed ? 'FAILED' : 'DONE';
96
+ try {
97
+ await changelogCollection.insertOne({ fileName, type, duration, startedAt, endedAt, status });
98
+ }
99
+ catch (err) {
100
+ this.logger.error(`Couldn't update changelog: ${err.message}`);
101
+ throw err;
102
+ }
103
+ }
104
+ }
105
+ exports.RunMigrationCommand = RunMigrationCommand;
106
+ //# sourceMappingURL=runMigration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runMigration.js","sourceRoot":"","sources":["../../src/cli/runMigration.ts"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I,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,GAAG,IAAI,CAAC,CAAA;QAC3D,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAA;QAE3C,IAAI,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,78 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.SyncIndexesCommand = void 0;
40
+ const node_fs_1 = require("node:fs");
41
+ const node_path_1 = __importDefault(require("node:path"));
42
+ const recursive_readdir_1 = __importDefault(require("recursive-readdir"));
43
+ class SyncIndexesCommand {
44
+ logger;
45
+ constructor(logger) {
46
+ this.logger = logger;
47
+ }
48
+ async run(modelsDir) {
49
+ const modelsPath = `./dist/${modelsDir}`;
50
+ // eslint-disable-next-line security/detect-non-literal-fs-filename
51
+ const isExistsSync = (0, node_fs_1.existsSync)(modelsPath); // nosemgrep: eslint.detect-non-literal-fs-filename
52
+ if (!isExistsSync) {
53
+ throw new Error(`Models dir is absent: ${modelsPath}`);
54
+ }
55
+ const t0 = Date.now();
56
+ this.logger.info('Start syncing indexes');
57
+ const files = await (0, recursive_readdir_1.default)(modelsPath, ['*.map', 'index.js', 'schemas', '*.types.js']);
58
+ const tasks = [];
59
+ for (const fileName of files) {
60
+ const modelModule = await Promise.resolve(`${node_path_1.default.resolve(fileName)}`).then(s => __importStar(require(s)));
61
+ if (modelModule.skipSyncIndexes) {
62
+ continue;
63
+ }
64
+ const task = modelModule.default;
65
+ tasks.push(this.syncModel(task));
66
+ }
67
+ await Promise.all(tasks);
68
+ this.logger.info(`Ended syncing indexes in ${Date.now() - t0} ms`);
69
+ }
70
+ async syncModel(model) {
71
+ const t0 = Date.now();
72
+ this.logger.info(`Start syncing indexes for the ${model.modelName} collection`);
73
+ await model.syncIndexes();
74
+ this.logger.info(`Ended syncing indexes for the ${model.modelName} collection in ${Date.now() - t0} ms`);
75
+ }
76
+ }
77
+ exports.SyncIndexesCommand = SyncIndexesCommand;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AAO,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,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=database.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.js","sourceRoot":"","sources":["../../src/interfaces/database.ts"],"names":[],"mappings":""}