@elsikora/nestjs-crud-config 1.0.1 → 1.1.0-dev.2

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 (88) hide show
  1. package/README.md +644 -286
  2. package/dist/cjs/index.js +26 -10
  3. package/dist/cjs/index.js.map +1 -1
  4. package/dist/cjs/modules/config/config.module.js +67 -1
  5. package/dist/cjs/modules/config/config.module.js.map +1 -1
  6. package/dist/cjs/modules/config/config.service.js +55 -98
  7. package/dist/cjs/modules/config/config.service.js.map +1 -1
  8. package/dist/cjs/modules/config/data/entity/entity.js +1 -0
  9. package/dist/cjs/modules/config/data/entity/entity.js.map +1 -1
  10. package/dist/cjs/modules/config/index.d.ts +2 -0
  11. package/dist/cjs/modules/config/migration/entity/entity.d.ts +12 -0
  12. package/dist/cjs/modules/config/migration/entity/entity.js +139 -0
  13. package/dist/cjs/modules/config/migration/entity/entity.js.map +1 -0
  14. package/dist/cjs/modules/config/migration/entity/index.d.ts +1 -0
  15. package/dist/cjs/modules/config/migration/enum/index.d.ts +1 -0
  16. package/dist/cjs/modules/config/migration/enum/status.d.ts +7 -0
  17. package/dist/cjs/modules/config/migration/enum/status.js +11 -0
  18. package/dist/cjs/modules/config/migration/enum/status.js.map +1 -0
  19. package/dist/cjs/modules/config/migration/index.d.ts +5 -0
  20. package/dist/cjs/modules/config/migration/interface/index.d.ts +3 -0
  21. package/dist/cjs/modules/config/migration/interface/interface.d.ts +11 -0
  22. package/dist/cjs/modules/config/migration/interface/migration-definition.interface.d.ts +29 -0
  23. package/dist/cjs/modules/config/migration/interface/migration-options.interface.d.ts +34 -0
  24. package/dist/cjs/modules/config/migration/migration-runner.service.d.ts +22 -0
  25. package/dist/cjs/modules/config/migration/migration-runner.service.js +85 -0
  26. package/dist/cjs/modules/config/migration/migration-runner.service.js.map +1 -0
  27. package/dist/cjs/modules/config/migration/migration.service.d.ts +68 -0
  28. package/dist/cjs/modules/config/migration/migration.service.js +331 -0
  29. package/dist/cjs/modules/config/migration/migration.service.js.map +1 -0
  30. package/dist/cjs/modules/config/section/entity/entity.js +1 -0
  31. package/dist/cjs/modules/config/section/entity/entity.js.map +1 -1
  32. package/dist/cjs/package.json +2 -1
  33. package/dist/cjs/shared/constant/config/index.d.ts +1 -0
  34. package/dist/cjs/shared/constant/config/migration.constant.d.ts +6 -0
  35. package/dist/cjs/shared/constant/config/migration.constant.js +15 -0
  36. package/dist/cjs/shared/constant/config/migration.constant.js.map +1 -0
  37. package/dist/cjs/shared/constant/token.constant.d.ts +2 -0
  38. package/dist/cjs/shared/constant/token.constant.js +4 -0
  39. package/dist/cjs/shared/constant/token.constant.js.map +1 -1
  40. package/dist/cjs/shared/interface/config/options.interface.d.ts +3 -2
  41. package/dist/cjs/shared/utility/index.d.ts +1 -0
  42. package/dist/cjs/shared/utility/logger.utility.d.ts +21 -0
  43. package/dist/cjs/shared/utility/logger.utility.js +67 -0
  44. package/dist/cjs/shared/utility/logger.utility.js.map +1 -0
  45. package/dist/esm/index.js +10 -3
  46. package/dist/esm/index.js.map +1 -1
  47. package/dist/esm/modules/config/config.module.js +67 -1
  48. package/dist/esm/modules/config/config.module.js.map +1 -1
  49. package/dist/esm/modules/config/config.service.js +56 -99
  50. package/dist/esm/modules/config/config.service.js.map +1 -1
  51. package/dist/esm/modules/config/data/entity/entity.js +1 -0
  52. package/dist/esm/modules/config/data/entity/entity.js.map +1 -1
  53. package/dist/esm/modules/config/index.d.ts +2 -0
  54. package/dist/esm/modules/config/migration/entity/entity.d.ts +12 -0
  55. package/dist/esm/modules/config/migration/entity/entity.js +137 -0
  56. package/dist/esm/modules/config/migration/entity/entity.js.map +1 -0
  57. package/dist/esm/modules/config/migration/entity/index.d.ts +1 -0
  58. package/dist/esm/modules/config/migration/enum/index.d.ts +1 -0
  59. package/dist/esm/modules/config/migration/enum/status.d.ts +7 -0
  60. package/dist/esm/modules/config/migration/enum/status.js +11 -0
  61. package/dist/esm/modules/config/migration/enum/status.js.map +1 -0
  62. package/dist/esm/modules/config/migration/index.d.ts +5 -0
  63. package/dist/esm/modules/config/migration/interface/index.d.ts +3 -0
  64. package/dist/esm/modules/config/migration/interface/interface.d.ts +11 -0
  65. package/dist/esm/modules/config/migration/interface/migration-definition.interface.d.ts +29 -0
  66. package/dist/esm/modules/config/migration/interface/migration-options.interface.d.ts +34 -0
  67. package/dist/esm/modules/config/migration/migration-runner.service.d.ts +22 -0
  68. package/dist/esm/modules/config/migration/migration-runner.service.js +85 -0
  69. package/dist/esm/modules/config/migration/migration-runner.service.js.map +1 -0
  70. package/dist/esm/modules/config/migration/migration.service.d.ts +68 -0
  71. package/dist/esm/modules/config/migration/migration.service.js +331 -0
  72. package/dist/esm/modules/config/migration/migration.service.js.map +1 -0
  73. package/dist/esm/modules/config/section/entity/entity.js +1 -0
  74. package/dist/esm/modules/config/section/entity/entity.js.map +1 -1
  75. package/dist/esm/package.json +2 -1
  76. package/dist/esm/shared/constant/config/index.d.ts +1 -0
  77. package/dist/esm/shared/constant/config/migration.constant.d.ts +6 -0
  78. package/dist/esm/shared/constant/config/migration.constant.js +13 -0
  79. package/dist/esm/shared/constant/config/migration.constant.js.map +1 -0
  80. package/dist/esm/shared/constant/token.constant.d.ts +2 -0
  81. package/dist/esm/shared/constant/token.constant.js +4 -0
  82. package/dist/esm/shared/constant/token.constant.js.map +1 -1
  83. package/dist/esm/shared/interface/config/options.interface.d.ts +3 -2
  84. package/dist/esm/shared/utility/index.d.ts +1 -0
  85. package/dist/esm/shared/utility/logger.utility.d.ts +21 -0
  86. package/dist/esm/shared/utility/logger.utility.js +67 -0
  87. package/dist/esm/shared/utility/logger.utility.js.map +1 -0
  88. package/package.json +15 -14
@@ -0,0 +1,137 @@
1
+ import { ApiPropertyDescribe, EApiPropertyDescribeType, EApiPropertyDateIdentifier, EApiPropertyDateType, EApiPropertyStringType } from '@elsikora/nestjs-crud-automator';
2
+ import { API_PROPERTY_CONSTANT } from '../../../../shared/constant/api-property.constant.js';
3
+ import { CONFIG_MIGRATION_CONSTANT } from '../../../../shared/constant/config/migration.constant.js';
4
+ import { createDynamicEntityClass } from '../../../../shared/utility/create-dynamic/entity.utility.js';
5
+ import '../../../../external/tslib/tslib.es6.js';
6
+ import '@nestjs/common';
7
+ import '@nestjs/typeorm';
8
+ import { UpdateDateColumn, CreateDateColumn } from 'typeorm';
9
+ import '../../../../shared/utility/crypto.utility.js';
10
+ import '../../../../shared/utility/logger.utility.js';
11
+
12
+ /**
13
+ * Creates a dynamic ConfigMigration entity with TypeORM decorators
14
+ * @param {object} options - Configuration options for the entity
15
+ * @param {number} options.maxNameLength - Maximum length for migration name field
16
+ * @param {string} options.tableName - Name of the database table
17
+ * @returns {TDynamicEntity} Dynamically created ConfigMigration entity class
18
+ */
19
+ function createConfigMigrationEntity(options) {
20
+ return createDynamicEntityClass({
21
+ columns: {
22
+ createdAt: {
23
+ default: () => "CURRENT_TIMESTAMP",
24
+ nullable: false,
25
+ type: "timestamp",
26
+ },
27
+ executedAt: {
28
+ nullable: true,
29
+ type: "timestamp",
30
+ },
31
+ failedAt: {
32
+ nullable: true,
33
+ type: "timestamp",
34
+ },
35
+ name: {
36
+ length: options.maxNameLength,
37
+ nullable: false,
38
+ type: "varchar",
39
+ },
40
+ startedAt: {
41
+ nullable: true,
42
+ type: "timestamp",
43
+ },
44
+ status: {
45
+ default: "PENDING",
46
+ enum: ["PENDING", "RUNNING", "COMPLETED", "FAILED", "STUCK"],
47
+ nullable: false,
48
+ type: "enum",
49
+ },
50
+ updatedAt: {
51
+ default: () => "CURRENT_TIMESTAMP",
52
+ nullable: false,
53
+ type: "timestamp",
54
+ },
55
+ },
56
+ decorators: {
57
+ createdAt: [
58
+ CreateDateColumn(),
59
+ ApiPropertyDescribe({
60
+ format: EApiPropertyDateType.DATE_TIME,
61
+ identifier: EApiPropertyDateIdentifier.CREATED_AT,
62
+ type: EApiPropertyDescribeType.DATE,
63
+ }),
64
+ ],
65
+ executedAt: [
66
+ ApiPropertyDescribe({
67
+ format: EApiPropertyDateType.DATE_TIME,
68
+ identifier: EApiPropertyDateIdentifier.CREATED_AT,
69
+ isNullable: true,
70
+ type: EApiPropertyDescribeType.DATE,
71
+ }),
72
+ ],
73
+ failedAt: [
74
+ ApiPropertyDescribe({
75
+ format: EApiPropertyDateType.DATE_TIME,
76
+ identifier: EApiPropertyDateIdentifier.CREATED_AT,
77
+ isNullable: true,
78
+ type: EApiPropertyDescribeType.DATE,
79
+ }),
80
+ ],
81
+ id: [
82
+ ApiPropertyDescribe({
83
+ type: EApiPropertyDescribeType.UUID,
84
+ }),
85
+ ],
86
+ name: [
87
+ ApiPropertyDescribe({
88
+ description: "Migration name",
89
+ exampleValue: "001_initial_config_setup",
90
+ format: EApiPropertyStringType.STRING,
91
+ maxLength: options.maxNameLength,
92
+ minLength: API_PROPERTY_CONSTANT.MIN_NAME_LENGTH,
93
+ pattern: `/^[A-Za-z0-9_-]{1,${options.maxNameLength}}/`,
94
+ type: EApiPropertyDescribeType.STRING,
95
+ }),
96
+ ],
97
+ startedAt: [
98
+ ApiPropertyDescribe({
99
+ format: EApiPropertyDateType.DATE_TIME,
100
+ identifier: EApiPropertyDateIdentifier.CREATED_AT,
101
+ isNullable: true,
102
+ type: EApiPropertyDescribeType.DATE,
103
+ }),
104
+ ],
105
+ status: [
106
+ ApiPropertyDescribe({
107
+ description: "Migration execution status",
108
+ exampleValue: "COMPLETED",
109
+ format: EApiPropertyStringType.STRING,
110
+ maxLength: CONFIG_MIGRATION_CONSTANT.MAX_STATUS_LENGTH,
111
+ minLength: API_PROPERTY_CONSTANT.MIN_NAME_LENGTH,
112
+ pattern: `/^[A-Z_]{1,${CONFIG_MIGRATION_CONSTANT.MAX_STATUS_LENGTH}}/`,
113
+ type: EApiPropertyDescribeType.STRING,
114
+ }),
115
+ ],
116
+ updatedAt: [
117
+ UpdateDateColumn(),
118
+ ApiPropertyDescribe({
119
+ format: EApiPropertyDateType.DATE_TIME,
120
+ identifier: EApiPropertyDateIdentifier.UPDATED_AT,
121
+ type: EApiPropertyDescribeType.DATE,
122
+ }),
123
+ ],
124
+ },
125
+ indexes: [
126
+ { columns: ["status"], name: "idx_config_migration_status" },
127
+ { columns: ["startedAt"], name: "idx_config_migration_started_at" },
128
+ { columns: ["createdAt"], name: "idx_config_migration_created_at" },
129
+ ],
130
+ name: "ConfigMigration",
131
+ tableName: options.tableName,
132
+ uniques: [["name"]],
133
+ });
134
+ }
135
+
136
+ export { createConfigMigrationEntity };
137
+ //# sourceMappingURL=entity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity.js","sources":["../../../../../../../src/modules/config/migration/entity/entity.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;;;;;AAcA;;;;;;AAMG;AACG,SAAU,2BAA2B,CAAC,OAG3C,EAAA;AACA,IAAA,OAAO,wBAAwB,CAAC;AAC/B,QAAA,OAAO,EAAE;AACR,YAAA,SAAS,EAAE;AACV,gBAAA,OAAO,EAAE,MAAM,mBAAmB;AAClC,gBAAA,QAAQ,EAAE,KAAK;AACf,gBAAA,IAAI,EAAE,WAAW;AACjB,aAAA;AACD,YAAA,UAAU,EAAE;AACX,gBAAA,QAAQ,EAAE,IAAI;AACd,gBAAA,IAAI,EAAE,WAAW;AACjB,aAAA;AACD,YAAA,QAAQ,EAAE;AACT,gBAAA,QAAQ,EAAE,IAAI;AACd,gBAAA,IAAI,EAAE,WAAW;AACjB,aAAA;AACD,YAAA,IAAI,EAAE;gBACL,MAAM,EAAE,OAAO,CAAC,aAAa;AAC7B,gBAAA,QAAQ,EAAE,KAAK;AACf,gBAAA,IAAI,EAAE,SAAS;AACf,aAAA;AACD,YAAA,SAAS,EAAE;AACV,gBAAA,QAAQ,EAAE,IAAI;AACd,gBAAA,IAAI,EAAE,WAAW;AACjB,aAAA;AACD,YAAA,MAAM,EAAE;AACP,gBAAA,OAAO,EAAE,SAAS;gBAClB,IAAI,EAAE,CAAC,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,O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AO,EAAE,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,iCAAiC,EAAE;AACnE,SAAA;AACD,QAAA,IAAI,EAAE,iBAAiB;QACvB,SAAS,EAAE,OAAO,CAAC,SAAS;AAC5B,QAAA,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;AACnB,KAAA,CAAC;AACH;;;;"}
@@ -0,0 +1 @@
1
+ export { createConfigMigrationEntity } from './entity';
@@ -0,0 +1 @@
1
+ export { EConfigMigrationStatus } from './status';
@@ -0,0 +1,7 @@
1
+ export declare enum EConfigMigrationStatus {
2
+ COMPLETED = "COMPLETED",
3
+ FAILED = "FAILED",
4
+ PENDING = "PENDING",
5
+ RUNNING = "RUNNING",
6
+ STUCK = "STUCK"
7
+ }
@@ -0,0 +1,11 @@
1
+ var EConfigMigrationStatus;
2
+ (function (EConfigMigrationStatus) {
3
+ EConfigMigrationStatus["COMPLETED"] = "COMPLETED";
4
+ EConfigMigrationStatus["FAILED"] = "FAILED";
5
+ EConfigMigrationStatus["PENDING"] = "PENDING";
6
+ EConfigMigrationStatus["RUNNING"] = "RUNNING";
7
+ EConfigMigrationStatus["STUCK"] = "STUCK";
8
+ })(EConfigMigrationStatus || (EConfigMigrationStatus = {}));
9
+
10
+ export { EConfigMigrationStatus };
11
+ //# sourceMappingURL=status.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"status.js","sources":["../../../../../../../src/modules/config/migration/enum/status.ts"],"sourcesContent":[null],"names":[],"mappings":"IAAY;AAAZ,CAAA,UAAY,sBAAsB,EAAA;AACjC,IAAA,sBAAA,CAAA,WAAA,CAAA,GAAA,WAAuB;AACvB,IAAA,sBAAA,CAAA,QAAA,CAAA,GAAA,QAAiB;AACjB,IAAA,sBAAA,CAAA,SAAA,CAAA,GAAA,SAAmB;AACnB,IAAA,sBAAA,CAAA,SAAA,CAAA,GAAA,SAAmB;AACnB,IAAA,sBAAA,CAAA,OAAA,CAAA,GAAA,OAAe;AAChB,CAAC,EANW,sBAAsB,KAAtB,sBAAsB,GAAA,EAAA,CAAA,CAAA;;;;"}
@@ -0,0 +1,5 @@
1
+ export * from './entity/index';
2
+ export * from './enum/index';
3
+ export type * from './interface/index';
4
+ export { ConfigMigrationRunnerService } from './migration-runner.service';
5
+ export { ConfigMigrationService } from './migration.service';
@@ -0,0 +1,3 @@
1
+ export type { IConfigMigration } from './interface';
2
+ export type { IConfigMigrationDefinition } from './migration-definition.interface';
3
+ export type { IConfigMigrationOptions } from './migration-options.interface';
@@ -0,0 +1,11 @@
1
+ import type { EConfigMigrationStatus } from '../enum/index';
2
+ export interface IConfigMigration {
3
+ createdAt: Date;
4
+ executedAt?: Date;
5
+ failedAt?: Date;
6
+ id: string;
7
+ name: string;
8
+ startedAt?: Date;
9
+ status: EConfigMigrationStatus;
10
+ updatedAt: Date;
11
+ }
@@ -0,0 +1,29 @@
1
+ import type { CrudConfigService } from '../../config.service';
2
+ import type { EntityManager } from "typeorm";
3
+ /**
4
+ * Interface for defining a migration
5
+ */
6
+ export interface IConfigMigrationDefinition {
7
+ /**
8
+ * Description of what this migration does
9
+ */
10
+ description?: string;
11
+ /**
12
+ * Optional rollback function (for future use)
13
+ * @param {CrudConfigService} configService - The config service for managing configurations
14
+ * @param {EntityManager} [entityManager] - Optional entity manager for transactions
15
+ * @returns {Promise<void>} Promise that resolves when rollback is complete
16
+ */
17
+ down?: (configService: CrudConfigService, entityManager?: EntityManager) => Promise<void>;
18
+ /**
19
+ * Unique name of the migration (should be sortable, e.g., "001_initial_config")
20
+ */
21
+ name: string;
22
+ /**
23
+ * The migration function that will be executed
24
+ * @param {CrudConfigService} configService - The config service for managing configurations
25
+ * @param {EntityManager} [entityManager] - Optional entity manager for transactions
26
+ * @returns {Promise<void>} Promise that resolves when migration is complete
27
+ */
28
+ up: (configService: CrudConfigService, entityManager?: EntityManager) => Promise<void>;
29
+ }
@@ -0,0 +1,34 @@
1
+ import type { IConfigMigrationDefinition } from './migration-definition.interface';
2
+ /**
3
+ * Interface for migration configuration options
4
+ */
5
+ export interface IConfigMigrationOptions {
6
+ /**
7
+ * Whether migrations are enabled
8
+ */
9
+ isEnabled?: boolean;
10
+ /**
11
+ * Maximum length for migration name field
12
+ */
13
+ maxNameLength?: number;
14
+ /**
15
+ * Array of migration definitions to execute
16
+ */
17
+ migrations?: Array<IConfigMigrationDefinition>;
18
+ /**
19
+ * Whether to run migrations automatically on application startup
20
+ */
21
+ shouldRunOnStartup?: boolean;
22
+ /**
23
+ * Timeout in minutes for stuck migration cleanup (default: 30)
24
+ */
25
+ stuckMigrationTimeoutMinutes?: number;
26
+ /**
27
+ * Table name for storing migration history
28
+ */
29
+ tableName?: string;
30
+ /**
31
+ * Whether to run migrations in transaction
32
+ */
33
+ useTransaction?: boolean;
34
+ }
@@ -0,0 +1,22 @@
1
+ import type { IConfigOptions } from '../../../shared/interface/config/index';
2
+ import { OnApplicationBootstrap } from "@nestjs/common";
3
+ import { ConfigMigrationService } from './migration.service';
4
+ /**
5
+ * Service that runs migrations on application startup
6
+ */
7
+ export declare class ConfigMigrationRunnerService implements OnApplicationBootstrap {
8
+ private readonly migrationService;
9
+ private readonly options;
10
+ private readonly LOGGER;
11
+ constructor(migrationService: ConfigMigrationService, options: IConfigOptions);
12
+ /**
13
+ * Called when the application is fully bootstrapped
14
+ * Runs migrations if enabled and configured to run on startup
15
+ */
16
+ onApplicationBootstrap(): Promise<void>;
17
+ /**
18
+ * Manually run migrations (for testing or manual execution)
19
+ * @returns {Promise<void>} Promise that resolves when migrations are complete
20
+ */
21
+ runMigrations(): Promise<void>;
22
+ }
@@ -0,0 +1,85 @@
1
+ import { __decorate, __param, __metadata } from '../../../external/tslib/tslib.es6.js';
2
+ import { Injectable, Inject } from '@nestjs/common';
3
+ import { CONFIG_MIGRATION_CONSTANT } from '../../../shared/constant/config/migration.constant.js';
4
+ import { TOKEN_CONSTANT } from '../../../shared/constant/token.constant.js';
5
+ import 'typeorm';
6
+ import '@elsikora/nestjs-crud-automator';
7
+ import '@nestjs/typeorm';
8
+ import '../../../shared/utility/crypto.utility.js';
9
+ import { LoggerUtility } from '../../../shared/utility/logger.utility.js';
10
+ import { EConfigMigrationStatus } from './enum/status.js';
11
+ import { ConfigMigrationService } from './migration.service.js';
12
+
13
+ /**
14
+ * Service that runs migrations on application startup
15
+ */
16
+ let ConfigMigrationRunnerService = class ConfigMigrationRunnerService {
17
+ migrationService;
18
+ options;
19
+ LOGGER = LoggerUtility.getLogger("ConfigMigrationRunnerService");
20
+ constructor(migrationService, options) {
21
+ this.migrationService = migrationService;
22
+ this.options = options;
23
+ }
24
+ /**
25
+ * Called when the application is fully bootstrapped
26
+ * Runs migrations if enabled and configured to run on startup
27
+ */
28
+ async onApplicationBootstrap() {
29
+ const migrationOptions = this.options.migrationOptions;
30
+ if (!migrationOptions?.isEnabled) {
31
+ this.LOGGER.verbose("Migrations are disabled");
32
+ return;
33
+ }
34
+ if (!migrationOptions.shouldRunOnStartup) {
35
+ this.LOGGER.verbose("Migrations configured to not run on startup");
36
+ return;
37
+ }
38
+ if (!migrationOptions.migrations || migrationOptions.migrations.length === 0) {
39
+ this.LOGGER.verbose("No migrations configured");
40
+ return;
41
+ }
42
+ try {
43
+ this.LOGGER.verbose("Starting migration runner");
44
+ // Check for failed and stuck migrations from previous runs
45
+ const allMigrations = await this.migrationService.getExecutedMigrations();
46
+ const failedMigrations = allMigrations.filter((m) => m.status === EConfigMigrationStatus.FAILED);
47
+ if (failedMigrations.length > 0) {
48
+ this.LOGGER.warn(`Found ${failedMigrations.length} failed migration(s) from previous runs: ${failedMigrations.map((m) => m.name).join(", ")}`);
49
+ this.LOGGER.warn("Consider reviewing and cleaning up failed migrations before proceeding");
50
+ }
51
+ // Clean up stuck migrations with configured timeout
52
+ const timeoutMinutes = migrationOptions.stuckMigrationTimeoutMinutes ??
53
+ CONFIG_MIGRATION_CONSTANT.DEFAULT_STUCK_MIGRATION_TIMEOUT_MINUTES;
54
+ await this.migrationService.cleanupStuckMigrations(timeoutMinutes);
55
+ await this.migrationService.executeMigrations(migrationOptions.migrations, migrationOptions.useTransaction ?? true);
56
+ this.LOGGER.verbose("Migration runner completed successfully");
57
+ }
58
+ catch (error) {
59
+ this.LOGGER.error("Migration runner failed:", error);
60
+ throw error;
61
+ }
62
+ }
63
+ /**
64
+ * Manually run migrations (for testing or manual execution)
65
+ * @returns {Promise<void>} Promise that resolves when migrations are complete
66
+ */
67
+ async runMigrations() {
68
+ const migrationOptions = this.options.migrationOptions;
69
+ if (!migrationOptions?.isEnabled) {
70
+ throw new Error("Migrations are not enabled");
71
+ }
72
+ if (!migrationOptions.migrations || migrationOptions.migrations.length === 0) {
73
+ throw new Error("No migrations configured");
74
+ }
75
+ await this.migrationService.executeMigrations(migrationOptions.migrations, migrationOptions.useTransaction ?? true);
76
+ }
77
+ };
78
+ ConfigMigrationRunnerService = __decorate([
79
+ Injectable(),
80
+ __param(1, Inject(TOKEN_CONSTANT.CONFIG_OPTIONS)),
81
+ __metadata("design:paramtypes", [ConfigMigrationService, Object])
82
+ ], ConfigMigrationRunnerService);
83
+
84
+ export { ConfigMigrationRunnerService };
85
+ //# sourceMappingURL=migration-runner.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migration-runner.service.js","sources":["../../../../../../src/modules/config/migration/migration-runner.service.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;;;;;;AAYA;;AAEG;AAEI,IAAM,4BAA4B,GAAlC,MAAM,4BAA4B,CAAA;AAItB,IAAA,gBAAA;AAEA,IAAA,OAAA;AALD,IAAA,MAAM,GAAkB,aAAa,CAAC,SAAS,CAAC,8BAA8B,CAAC;IAEhG,WAAA,CACkB,gBAAwC,EAExC,OAAuB,EAAA;QAFvB,IAAA,CAAA,gBAAgB,GAAhB,gBAAgB;QAEhB,IAAA,CAAA,OAAO,GAAP,OAAO;IACtB;AAEH;;;AAGG;AACH,IAAA,MAAM,sBAAsB,GAAA;AAC3B,QAAA,MAAM,gBAAgB,GAAwC,IAAI,CAAC,OAAO,CA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AAA,MAAM,IAAI,CAAC,gBAAgB,CAAC,iBAAiB,CAC5C,gBAAgB,CAAC,UAAU,EAC3B,gBAAgB,CAAC,cAAc,IAAI,IAAI,CACvC;AAED,YAAA,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,yCAAyC,CAAC;QAC/D;QAAE,OAAO,KAAK,EAAE;YACf,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,0BAA0B,EAAE,KAAK,CAAC;AAEpD,YAAA,MAAM,KAAK;QACZ;IACD;AAEA;;;AAGG;AACH,IAAA,MAAM,aAAa,GAAA;AAClB,QAAA,MAAM,gBAAgB,GAAwC,IAAI,CAAC,OAAO,CAAC,gBAAgB;AAE3F,QAAA,IAAI,CAAC,gBAAgB,EAAE,SAAS,EAAE;AACjC,YAAA,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC;QAC9C;AAEA,QAAA,IAAI,CAAC,gBAAgB,CAAC,UAAU,IAAI,gBAAgB,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE;AAC7E,YAAA,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC;QAC5C;AAEA,QAAA,MAAM,IAAI,CAAC,gBAAgB,CAAC,iBAAiB,CAC5C,gBAAgB,CAAC,UAAU,EAC3B,gBAAgB,CAAC,cAAc,IAAI,IAAI,CACvC;IACF;;AA1FY,4BAA4B,GAAA,UAAA,CAAA;AADxC,IAAA,UAAU,EAAE;AAMV,IAAA,OAAA,CAAA,CAAA,EAAA,MAAM,CAAC,cAAc,CAAC,cAAc,CAAC,CAAA;qCADH,sBAAsB,EAAA,MAAA,CAAA;AAJ9C,CAAA,EAAA,4BAA4B,CA2FxC;;;;"}
@@ -0,0 +1,68 @@
1
+ import type { IConfigOptions } from '../../../shared/interface/config/index';
2
+ import type { IConfigMigration, IConfigMigrationDefinition } from './interface/index';
3
+ import { CrudConfigService } from '../config.service';
4
+ import { type OnModuleInit } from "@nestjs/common";
5
+ import { DataSource } from "typeorm";
6
+ export declare class ConfigMigrationService implements OnModuleInit {
7
+ private readonly dataSource;
8
+ private readonly options;
9
+ private readonly configService;
10
+ private readonly LOGGER;
11
+ private migrationService;
12
+ constructor(dataSource: DataSource, options: IConfigOptions, configService: CrudConfigService);
13
+ /**
14
+ * Cleans up failed migrations
15
+ * @param {string} [migrationName] - Optional migration name to clean up
16
+ * @returns {Promise<void>} Promise that resolves when cleanup is complete
17
+ */
18
+ cleanupFailedMigrations(migrationName?: string): Promise<void>;
19
+ /**
20
+ * Cleans up stuck migrations (running for too long)
21
+ * @param {number} [timeoutMinutes] - Minutes after which a RUNNING migration is considered stuck
22
+ * @returns {Promise<void>} Promise that resolves when cleanup is complete
23
+ */
24
+ cleanupStuckMigrations(timeoutMinutes?: number): Promise<void>;
25
+ /**
26
+ * Executes pending migrations
27
+ * @param {Array<IConfigMigrationDefinition>} migrations - Array of migration definitions
28
+ * @param {boolean} [useTransaction] - Whether to run migrations in a transaction
29
+ * @returns {Promise<void>} Promise that resolves when all migrations are executed
30
+ */
31
+ executeMigrations(migrations: Array<IConfigMigrationDefinition>, useTransaction?: boolean): Promise<void>;
32
+ /**
33
+ * Gets list of executed migration names
34
+ * @returns {Promise<string[]>} Promise that resolves to list of executed migration names
35
+ */
36
+ getExecutedMigrationList(): Promise<Array<string>>;
37
+ /**
38
+ * Gets list of executed migrations
39
+ * @returns {Promise<IConfigMigration[]>} Promise that resolves to list of executed migrations
40
+ */
41
+ getExecutedMigrations(): Promise<Array<IConfigMigration>>;
42
+ /**
43
+ * Checks if a migration has been executed
44
+ * @param {string} migrationName - The name of the migration to check
45
+ * @returns {Promise<boolean>} Promise that resolves to true if migration is executed
46
+ */
47
+ isMigrationExecuted(migrationName: string): Promise<boolean>;
48
+ onModuleInit(): void;
49
+ /**
50
+ * Rolls back a migration
51
+ * @param {string} migrationName - The name of the migration to roll back
52
+ * @param {Array<IConfigMigrationDefinition>} migrations - Array of migration definitions
53
+ * @returns {Promise<void>} Promise that resolves when rollback is complete
54
+ */
55
+ rollbackMigration(migrationName: string, migrations: Array<IConfigMigrationDefinition>): Promise<void>;
56
+ /**
57
+ * Executes a single migration
58
+ * @param {IConfigMigrationDefinition} migration - Migration definition
59
+ * @param {EntityManager} [transactionalEntityManager] - Optional transaction manager
60
+ * @returns {Promise<void>} Promise that resolves when migration is executed
61
+ */
62
+ private executeSingleMigration;
63
+ /**
64
+ * Validates migrations for duplicates and naming issues
65
+ * @param {Array<IConfigMigrationDefinition>} migrations - Array of migration definitions
66
+ */
67
+ private validateMigrations;
68
+ }