taon 18.0.24 → 18.0.26

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 (204) hide show
  1. package/README.md +175 -114
  2. package/assets/shared/shared_folder_info.txt +1 -1
  3. package/bin/start.js +279 -279
  4. package/bin/taon +6 -6
  5. package/bin/taon-debug +5 -5
  6. package/bin/taon-debug-brk +5 -5
  7. package/browser/README.md +24 -24
  8. package/browser/esm2022/lib/endpoint-context.mjs +4 -1
  9. package/browser/esm2022/lib/index.mjs +2 -2
  10. package/browser/esm2022/lib/models.mjs +1 -1
  11. package/browser/esm2022/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.mjs +11 -11
  12. package/browser/fesm2022/taon.mjs +3 -0
  13. package/browser/fesm2022/taon.mjs.map +1 -1
  14. package/browser/lib/models.d.ts +5 -0
  15. package/browser/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.d.ts +4 -4
  16. package/cli.js +11 -17
  17. package/cli.js.map +1 -1
  18. package/client/README.md +24 -24
  19. package/client/esm2022/lib/endpoint-context.mjs +4 -1
  20. package/client/esm2022/lib/index.mjs +2 -2
  21. package/client/esm2022/lib/models.mjs +1 -1
  22. package/client/esm2022/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.mjs +11 -11
  23. package/client/fesm2022/taon.mjs +3 -0
  24. package/client/fesm2022/taon.mjs.map +1 -1
  25. package/client/lib/models.d.ts +5 -0
  26. package/client/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.d.ts +4 -4
  27. package/index.js +15 -2
  28. package/index.js.map +1 -1
  29. package/lib/base-classes/base-abstract-entity.js +30 -28
  30. package/lib/base-classes/base-abstract-entity.js.map +1 -1
  31. package/lib/base-classes/base-class.js +9 -17
  32. package/lib/base-classes/base-class.js.map +1 -1
  33. package/lib/base-classes/base-context.js +4 -4
  34. package/lib/base-classes/base-context.js.map +1 -1
  35. package/lib/base-classes/base-controller.js +15 -15
  36. package/lib/base-classes/base-controller.js.map +1 -1
  37. package/lib/base-classes/base-crud-controller.js +211 -329
  38. package/lib/base-classes/base-crud-controller.js.map +1 -1
  39. package/lib/base-classes/base-entity.js +15 -15
  40. package/lib/base-classes/base-entity.js.map +1 -1
  41. package/lib/base-classes/base-injector.js +99 -124
  42. package/lib/base-classes/base-injector.js.map +1 -1
  43. package/lib/base-classes/base-migration.js +15 -31
  44. package/lib/base-classes/base-migration.js.map +1 -1
  45. package/lib/base-classes/base-provider.js +3 -9
  46. package/lib/base-classes/base-provider.js.map +1 -1
  47. package/lib/base-classes/base-repository.js +310 -444
  48. package/lib/base-classes/base-repository.js.map +1 -1
  49. package/lib/base-classes/base-subscriber-for-entity.js +73 -73
  50. package/lib/base-classes/base-subscriber-for-entity.js.map +1 -1
  51. package/lib/base-classes/base.js +10 -10
  52. package/lib/base-classes/base.js.map +1 -1
  53. package/lib/build-info._auto-generated_.js.map +1 -1
  54. package/lib/constants.js.map +1 -1
  55. package/lib/context-db-migrations.js +325 -581
  56. package/lib/context-db-migrations.js.map +1 -1
  57. package/lib/create-context.js +80 -109
  58. package/lib/create-context.js.map +1 -1
  59. package/lib/decorators/classes/controller-decorator.js +15 -13
  60. package/lib/decorators/classes/controller-decorator.js.map +1 -1
  61. package/lib/decorators/classes/entity-decorator.js +23 -20
  62. package/lib/decorators/classes/entity-decorator.js.map +1 -1
  63. package/lib/decorators/classes/migration-decorator.js +7 -13
  64. package/lib/decorators/classes/migration-decorator.js.map +1 -1
  65. package/lib/decorators/classes/provider-decorator.js +7 -13
  66. package/lib/decorators/classes/provider-decorator.js.map +1 -1
  67. package/lib/decorators/classes/repository-decorator.js +7 -13
  68. package/lib/decorators/classes/repository-decorator.js.map +1 -1
  69. package/lib/decorators/classes/subscriber-decorator.js +8 -13
  70. package/lib/decorators/classes/subscriber-decorator.js.map +1 -1
  71. package/lib/decorators/http/http-decorators.js +4 -4
  72. package/lib/decorators/http/http-decorators.js.map +1 -1
  73. package/lib/decorators/http/http-methods-decorators.js +19 -25
  74. package/lib/decorators/http/http-methods-decorators.js.map +1 -1
  75. package/lib/decorators/http/http-params-decorators.js +7 -9
  76. package/lib/decorators/http/http-params-decorators.js.map +1 -1
  77. package/lib/dependency-injection/di-container.js +13 -18
  78. package/lib/dependency-injection/di-container.js.map +1 -1
  79. package/lib/endpoint-context.js +1196 -1543
  80. package/lib/endpoint-context.js.map +1 -1
  81. package/lib/entity-process.js +102 -112
  82. package/lib/entity-process.js.map +1 -1
  83. package/lib/env.js +2 -2
  84. package/lib/env.js.map +1 -1
  85. package/lib/formly/formly-group-wrapper-component.js +45 -23
  86. package/lib/formly/formly-group-wrapper-component.js.map +1 -1
  87. package/lib/formly/formly-repeat-component.js +74 -27
  88. package/lib/formly/formly-repeat-component.js.map +1 -1
  89. package/lib/formly/formly.models.js.map +1 -1
  90. package/lib/formly/fromly.js +57 -61
  91. package/lib/formly/fromly.js.map +1 -1
  92. package/lib/formly/type-from-entity.js +11 -16
  93. package/lib/formly/type-from-entity.js.map +1 -1
  94. package/lib/get-response-value.js +22 -33
  95. package/lib/get-response-value.js.map +1 -1
  96. package/lib/helpers/class-helpers.js +62 -73
  97. package/lib/helpers/class-helpers.js.map +1 -1
  98. package/lib/helpers/taon-helpers.js +33 -31
  99. package/lib/helpers/taon-helpers.js.map +1 -1
  100. package/lib/index._auto-generated_.d.ts +1 -0
  101. package/lib/index._auto-generated_.js.map +1 -1
  102. package/lib/index.d.ts +1 -25
  103. package/lib/index.js +44 -41
  104. package/lib/index.js.map +1 -1
  105. package/lib/inject.js +15 -15
  106. package/lib/inject.js.map +1 -1
  107. package/lib/models.d.ts +5 -0
  108. package/lib/models.js +124 -100
  109. package/lib/models.js.map +1 -1
  110. package/lib/orm.js +13 -27
  111. package/lib/orm.js.map +1 -1
  112. package/lib/realtime/realtime-client.js +70 -65
  113. package/lib/realtime/realtime-client.js.map +1 -1
  114. package/lib/realtime/realtime-core.js +48 -29
  115. package/lib/realtime/realtime-core.js.map +1 -1
  116. package/lib/realtime/realtime-server.js +81 -80
  117. package/lib/realtime/realtime-server.js.map +1 -1
  118. package/lib/realtime/realtime-strategy/index.js +18 -5
  119. package/lib/realtime/realtime-strategy/index.js.map +1 -1
  120. package/lib/realtime/realtime-strategy/realtime-strategy-ipc.js +132 -172
  121. package/lib/realtime/realtime-strategy/realtime-strategy-ipc.js.map +1 -1
  122. package/lib/realtime/realtime-strategy/realtime-strategy-mock.js +158 -241
  123. package/lib/realtime/realtime-strategy/realtime-strategy-mock.js.map +1 -1
  124. package/lib/realtime/realtime-strategy/realtime-strategy-socket-io.js +18 -29
  125. package/lib/realtime/realtime-strategy/realtime-strategy-socket-io.js.map +1 -1
  126. package/lib/realtime/realtime-strategy/realtime-strategy.js +9 -13
  127. package/lib/realtime/realtime-strategy/realtime-strategy.js.map +1 -1
  128. package/lib/realtime/realtime-subs-manager.js +29 -30
  129. package/lib/realtime/realtime-subs-manager.js.map +1 -1
  130. package/lib/realtime/realtime.models.js.map +1 -1
  131. package/lib/storage.js +1 -1
  132. package/lib/storage.js.map +1 -1
  133. package/lib/symbols.js +62 -66
  134. package/lib/symbols.js.map +1 -1
  135. package/lib/ui/directives/index.js +2 -2
  136. package/lib/ui/directives/index.js.map +1 -1
  137. package/lib/ui/directives/view-mode.js.map +1 -1
  138. package/lib/ui/index.js +2 -2
  139. package/lib/ui/index.js.map +1 -1
  140. package/lib/ui/taon-admin-mode-configuration/components/taon-admin-edit-mode/index.d.ts +2 -0
  141. package/lib/ui/taon-admin-mode-configuration/components/taon-admin-edit-mode/index.js +5 -0
  142. package/lib/ui/taon-admin-mode-configuration/components/taon-admin-edit-mode/index.js.map +1 -0
  143. package/lib/ui/taon-admin-mode-configuration/components/taon-admin-edit-mode/taon-admin-edit-mode.component.d.ts +29 -0
  144. package/lib/ui/taon-admin-mode-configuration/components/taon-admin-edit-mode/taon-admin-edit-mode.module.d.ts +2 -0
  145. package/lib/ui/taon-admin-mode-configuration/components/taon-db-admin/index.d.ts +1 -0
  146. package/lib/ui/taon-admin-mode-configuration/components/taon-db-admin/index.js +5 -0
  147. package/lib/ui/taon-admin-mode-configuration/components/taon-db-admin/index.js.map +1 -0
  148. package/lib/ui/taon-admin-mode-configuration/components/taon-db-admin/taon-db-admin.component.d.ts +8 -0
  149. package/lib/ui/taon-admin-mode-configuration/components/taon-file-general-opt/index.d.ts +2 -0
  150. package/lib/ui/taon-admin-mode-configuration/components/taon-file-general-opt/index.js +5 -0
  151. package/lib/ui/taon-admin-mode-configuration/components/taon-file-general-opt/index.js.map +1 -0
  152. package/lib/ui/taon-admin-mode-configuration/components/taon-file-general-opt/taon-file-general-opt.component.d.ts +13 -0
  153. package/lib/ui/taon-admin-mode-configuration/components/taon-file-general-opt/taon-file-general-opt.module.d.ts +2 -0
  154. package/lib/ui/taon-admin-mode-configuration/index.js +2 -2
  155. package/lib/ui/taon-admin-mode-configuration/index.js.map +1 -1
  156. package/lib/ui/taon-admin-mode-configuration/models/taon-admin-mode-tabs.d.ts +5 -0
  157. package/lib/ui/taon-admin-mode-configuration/models/taon-admin-mode-tabs.js +3 -0
  158. package/lib/ui/taon-admin-mode-configuration/models/taon-admin-mode-tabs.js.map +1 -0
  159. package/lib/ui/taon-admin-mode-configuration/taon-admin-control.service.d.ts +14 -0
  160. package/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.d.ts +4 -4
  161. package/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.module.d.ts +2 -0
  162. package/lib/ui/taon-admin-mode-configuration/taon-admin.models.js.map +1 -1
  163. package/lib/ui/taon-github-fork-me-corner/index.js +2 -2
  164. package/lib/ui/taon-github-fork-me-corner/index.js.map +1 -1
  165. package/lib/ui/taon-github-fork-me-ribbon/index.js +2 -2
  166. package/lib/ui/taon-github-fork-me-ribbon/index.js.map +1 -1
  167. package/lib/ui/taon-notifications/index.js +15 -2
  168. package/lib/ui/taon-notifications/index.js.map +1 -1
  169. package/lib/ui/taon-notifications/taon-notifications.models.js +2 -2
  170. package/lib/ui/taon-notifications/taon-notifications.models.js.map +1 -1
  171. package/lib/ui/taon-progress-bar/index.js +2 -2
  172. package/lib/ui/taon-progress-bar/index.js.map +1 -1
  173. package/lib/ui/taon-progress-bar/taon-progress-bar.component.d.ts +2 -1
  174. package/lib/ui/taon-session-passcode/index.js +2 -2
  175. package/lib/ui/taon-session-passcode/index.js.map +1 -1
  176. package/lib/ui/taon-table/index.js +2 -2
  177. package/lib/ui/taon-table/index.js.map +1 -1
  178. package/lib/ui/taon.models.js.map +1 -1
  179. package/lib/validators.js +48 -16
  180. package/lib/validators.js.map +1 -1
  181. package/migrations/index.js +15 -2
  182. package/migrations/index.js.map +1 -1
  183. package/migrations/migrations_index._auto-generated_.d.ts +1 -0
  184. package/migrations/migrations_index._auto-generated_.js.map +1 -1
  185. package/old-app .d.ts +1 -0
  186. package/old-app .js +67 -78
  187. package/old-app .js.map +1 -1
  188. package/package.json +18 -65
  189. package/playground.d.ts +1 -0
  190. package/playground.js.map +1 -1
  191. package/src.d.ts +1 -1
  192. package/taon.jsonc +44 -47
  193. package/tmp-environment.json +8 -401
  194. package/websql/README.md +24 -24
  195. package/websql/esm2022/lib/endpoint-context.mjs +4 -1
  196. package/websql/esm2022/lib/index.mjs +2 -2
  197. package/websql/esm2022/lib/models.mjs +1 -1
  198. package/websql/esm2022/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.mjs +11 -11
  199. package/websql/fesm2022/taon.mjs +3 -0
  200. package/websql/fesm2022/taon.mjs.map +1 -1
  201. package/websql/lib/models.d.ts +5 -0
  202. package/websql/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.d.ts +4 -4
  203. package/browser/package.json +0 -25
  204. package/websql/package.json +0 -25
@@ -1,605 +1,349 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ContextDbMigrations = void 0;
4
- var tslib_1 = require("tslib");
5
4
  //#region imports
6
- var tnp_core_1 = require("tnp-core");
7
- var models_1 = require("./models");
8
- var class_helpers_1 = require("./helpers/class-helpers");
9
- var taon_typeorm_1 = require("taon-typeorm");
10
- var ContextDbMigrations = /** @class */ (function () {
5
+ const tnp_core_1 = require("tnp-core");
6
+ const models_1 = require("./models");
7
+ const class_helpers_1 = require("./helpers/class-helpers");
8
+ const taon_typeorm_1 = require("taon-typeorm");
9
+ class ContextDbMigrations {
10
+ ctx;
11
+ //#region fields
12
+ //#region fields / migration table name
13
+ DEFAULT_MIGRATION_TABLE_NAME = 'TAON_MIGRATION_META';
14
+ //#endregion
15
+ //#region fields / migration statuses
16
+ MIGRATION_STATUS_COMPLETED = 'completed';
17
+ MIGRATION_STATUS_PENDING = 'pending';
18
+ //#endregion
19
+ //#region fields / migration table schema
20
+ table = new taon_typeorm_1.Table({
21
+ name: this.DEFAULT_MIGRATION_TABLE_NAME,
22
+ columns: [
23
+ {
24
+ name: 'id',
25
+ type: 'integer',
26
+ isPrimary: true, // Mark it as the primary key
27
+ isGenerated: true, // Enable auto-generation
28
+ generationStrategy: 'increment', // Use auto-increment strategy
29
+ },
30
+ {
31
+ name: 'name',
32
+ type: 'varchar',
33
+ length: '255',
34
+ isUnique: true, // Ensure the name is unique
35
+ isNullable: false, // Ensure this field is required
36
+ },
37
+ {
38
+ // context is a part of name
39
+ name: 'context',
40
+ type: 'varchar',
41
+ length: '255',
42
+ isNullable: false, // Optional context for migrations (e.g., tenant or module name)
43
+ },
44
+ {
45
+ name: 'applied_at',
46
+ type: 'timestamp',
47
+ default: 'CURRENT_TIMESTAMP', // Automatically set the timestamp
48
+ isNullable: true,
49
+ },
50
+ {
51
+ name: 'status',
52
+ type: 'varchar',
53
+ length: '50',
54
+ default: `'${this.MIGRATION_STATUS_COMPLETED}'`,
55
+ isNullable: false,
56
+ },
57
+ // { // TODO not needed for now
58
+ // name: 'checksum',
59
+ // type: 'varchar',
60
+ // length: '64',
61
+ // isNullable: true, // Optional field to store a hash/checksum of migration file
62
+ // },
63
+ ],
64
+ });
11
65
  //#endregion
12
66
  //#endregion
13
67
  //#region constructor
14
- function ContextDbMigrations(ctx) {
68
+ constructor(ctx) {
15
69
  this.ctx = ctx;
16
- //#region fields
17
- //#region fields / migration table name
18
- this.DEFAULT_MIGRATION_TABLE_NAME = 'TAON_MIGRATION_META';
19
- //#endregion
20
- //#region fields / migration statuses
21
- this.MIGRATION_STATUS_COMPLETED = 'completed';
22
- this.MIGRATION_STATUS_PENDING = 'pending';
23
- //#endregion
24
- //#region fields / migration table schema
25
- this.table = new taon_typeorm_1.Table({
26
- name: this.DEFAULT_MIGRATION_TABLE_NAME,
27
- columns: [
28
- {
29
- name: 'id',
30
- type: 'integer',
31
- isPrimary: true, // Mark it as the primary key
32
- isGenerated: true, // Enable auto-generation
33
- generationStrategy: 'increment', // Use auto-increment strategy
34
- },
35
- {
36
- name: 'name',
37
- type: 'varchar',
38
- length: '255',
39
- isUnique: true, // Ensure the name is unique
40
- isNullable: false, // Ensure this field is required
41
- },
42
- {
43
- // context is a part of name
44
- name: 'context',
45
- type: 'varchar',
46
- length: '255',
47
- isNullable: false, // Optional context for migrations (e.g., tenant or module name)
48
- },
49
- {
50
- name: 'applied_at',
51
- type: 'timestamp',
52
- default: 'CURRENT_TIMESTAMP', // Automatically set the timestamp
53
- isNullable: true,
54
- },
55
- {
56
- name: 'status',
57
- type: 'varchar',
58
- length: '50',
59
- default: "'".concat(this.MIGRATION_STATUS_COMPLETED, "'"),
60
- isNullable: false,
61
- },
62
- // { // TODO not needed for now
63
- // name: 'checksum',
64
- // type: 'varchar',
65
- // length: '64',
66
- // isNullable: true, // Optional field to store a hash/checksum of migration file
67
- // },
68
- ],
69
- });
70
70
  }
71
71
  //#endregion
72
72
  //#region methods & getters / make sure migration table exists
73
- ContextDbMigrations.prototype.ensureMigrationTableExists = function () {
74
- return tslib_1.__awaiter(this, void 0, void 0, function () {
75
- var queryRunner, hasTable, error_1;
76
- return tslib_1.__generator(this, function (_a) {
77
- switch (_a.label) {
78
- case 0:
79
- //#region @websqlFunc
80
- if (this.ctx.remoteHost) {
81
- return [2 /*return*/];
82
- }
83
- queryRunner = this.ctx.connection.createQueryRunner();
84
- return [4 /*yield*/, queryRunner.connect()];
85
- case 1:
86
- _a.sent();
87
- return [4 /*yield*/, queryRunner.startTransaction()];
88
- case 2:
89
- _a.sent();
90
- return [4 /*yield*/, queryRunner.hasTable(this.DEFAULT_MIGRATION_TABLE_NAME)];
91
- case 3:
92
- hasTable = _a.sent();
93
- if (!hasTable) return [3 /*break*/, 6];
94
- this.ctx.logMigrations &&
95
- console.log("Table ".concat(this.DEFAULT_MIGRATION_TABLE_NAME, " already exists."));
96
- return [4 /*yield*/, queryRunner.commitTransaction()];
97
- case 4:
98
- _a.sent();
99
- return [4 /*yield*/, queryRunner.release()];
100
- case 5:
101
- _a.sent();
102
- return [2 /*return*/]; // Exit early if the table exists
103
- case 6:
104
- _a.trys.push([6, 10, 12, 14]);
105
- return [4 /*yield*/, queryRunner.createTable(this.table)];
106
- case 7:
107
- _a.sent();
108
- return [4 /*yield*/, queryRunner.createIndex(this.DEFAULT_MIGRATION_TABLE_NAME, new taon_typeorm_1.TableIndex({
109
- name: 'IDX_NAME',
110
- columnNames: ['name'],
111
- }))];
112
- case 8:
113
- _a.sent();
114
- return [4 /*yield*/, queryRunner.commitTransaction()];
115
- case 9:
116
- _a.sent();
117
- return [3 /*break*/, 14];
118
- case 10:
119
- error_1 = _a.sent();
120
- this.ctx.logMigrations &&
121
- console.error("Transaction failed [ensureMigrationTableExists]" + ", rolling back:", error_1);
122
- return [4 /*yield*/, queryRunner.rollbackTransaction()];
123
- case 11:
124
- _a.sent();
125
- return [3 /*break*/, 14];
126
- case 12: return [4 /*yield*/, queryRunner.release()];
127
- case 13:
128
- _a.sent();
129
- return [7 /*endfinally*/];
130
- case 14: return [2 /*return*/];
131
- }
132
- });
133
- });
134
- };
73
+ async ensureMigrationTableExists() {
74
+ //#region @websqlFunc
75
+ if (this.ctx.remoteHost) {
76
+ return;
77
+ }
78
+ const queryRunner = this.ctx.connection.createQueryRunner();
79
+ await queryRunner.connect();
80
+ await queryRunner.startTransaction();
81
+ // Check if the table already exists
82
+ const hasTable = await queryRunner.hasTable(this.DEFAULT_MIGRATION_TABLE_NAME);
83
+ if (hasTable) {
84
+ this.ctx.logMigrations &&
85
+ console.log(`Table ${this.DEFAULT_MIGRATION_TABLE_NAME} already exists.`);
86
+ await queryRunner.commitTransaction();
87
+ await queryRunner.release();
88
+ return; // Exit early if the table exists
89
+ }
90
+ try {
91
+ await queryRunner.createTable(this.table);
92
+ await queryRunner.createIndex(this.DEFAULT_MIGRATION_TABLE_NAME, new taon_typeorm_1.TableIndex({
93
+ name: 'IDX_NAME',
94
+ columnNames: ['name'],
95
+ }));
96
+ await queryRunner.commitTransaction();
97
+ }
98
+ catch (error) {
99
+ this.ctx.logMigrations &&
100
+ console.error(`Transaction failed [ensureMigrationTableExists]` + `, rolling back:`, error);
101
+ await queryRunner.rollbackTransaction();
102
+ }
103
+ finally {
104
+ await queryRunner.release();
105
+ }
106
+ //#endregion
107
+ }
135
108
  //#endregion
136
109
  //#region methods & getters / revert migration to timestamp
137
- ContextDbMigrations.prototype.logSelectALl = function (name, queryRunner) {
138
- return tslib_1.__awaiter(this, void 0, void 0, function () {
139
- var _a, _b, _c;
140
- return tslib_1.__generator(this, function (_d) {
141
- switch (_d.label) {
142
- case 0:
143
- _b = (_a = console).log;
144
- _c = [name];
145
- return [4 /*yield*/, queryRunner.query("SELECT * FROM ".concat(this.DEFAULT_MIGRATION_TABLE_NAME, " WHERE context = $1"), [this.ctx.contextName])];
146
- case 1:
147
- _b.apply(_a, _c.concat([(_d.sent()).map(function (m) { return m.name; })]));
148
- return [2 /*return*/];
149
- }
150
- });
151
- });
152
- };
153
- ContextDbMigrations.prototype.revertMigrationToTimestamp = function (timestamp) {
154
- return tslib_1.__awaiter(this, void 0, void 0, function () {
155
- var migrationsClassFns, migrationClassesInstancesToRevert, queryRunner, appliedMigrationsForContext, appliedMigrationsForContextNames, migrationClassesInstancesToRevert_1, migrationClassesInstancesToRevert_1_1, migrationClassInstance, migrationName, e_1_1, error_2;
156
- var e_1, _a;
157
- var _this = this;
158
- return tslib_1.__generator(this, function (_b) {
159
- switch (_b.label) {
160
- case 0:
161
- //#region @websqlFunc
162
- if (this.ctx.remoteHost) {
163
- return [2 /*return*/];
164
- }
165
- if (!tnp_core_1.UtilsMigrations.isValidTimestamp(timestamp)) {
166
- tnp_core_1.Helpers.throw("Invalid timestamp provided for migration revert: ".concat(timestamp));
167
- }
168
- migrationsClassFns = this.ctx
169
- .getClassFunByArr(models_1.Models.ClassType.MIGRATION)
170
- .reverse();
171
- migrationClassesInstancesToRevert = migrationsClassFns
172
- .map(function (classFn) {
173
- var timestampFromClassName = Number(tnp_core_1.UtilsMigrations.getTimestampFromClassName(class_helpers_1.ClassHelpers.getName(classFn)));
174
- if (timestampFromClassName <= timestamp) {
175
- // this.ctx.logMigrations &&
176
- // console.log(
177
- // `Stopping migration filter at: ${ClassHelpers.getName(classFn)} ` +
178
- // `with timestamp ${timestampFromClassName}`,
179
- // );
180
- return null;
181
- }
182
- return _this.ctx.getInstanceBy(classFn);
183
- })
184
- .filter(function (f) { return !!f; })
185
- .map(function (f) { return f; })
186
- .filter(function (migrationInstance) { return migrationInstance.isReadyToRun(); });
187
- queryRunner = this.ctx.connection.createQueryRunner();
188
- return [4 /*yield*/, queryRunner.connect()];
189
- case 1:
190
- _b.sent();
191
- _b.label = 2;
192
- case 2:
193
- _b.trys.push([2, 15, 17, 19]);
194
- return [4 /*yield*/, queryRunner.startTransaction()];
195
- case 3:
196
- _b.sent();
197
- return [4 /*yield*/, queryRunner.query("SELECT name FROM ".concat(this.DEFAULT_MIGRATION_TABLE_NAME, "\n WHERE status = $1 AND context = $2"), [this.MIGRATION_STATUS_COMPLETED, this.ctx.contextName])];
198
- case 4:
199
- appliedMigrationsForContext = _b.sent();
200
- appliedMigrationsForContextNames = appliedMigrationsForContext.map(function (m) { return m.name; });
201
- _b.label = 5;
202
- case 5:
203
- _b.trys.push([5, 11, 12, 13]);
204
- migrationClassesInstancesToRevert_1 = tslib_1.__values(migrationClassesInstancesToRevert), migrationClassesInstancesToRevert_1_1 = migrationClassesInstancesToRevert_1.next();
205
- _b.label = 6;
206
- case 6:
207
- if (!!migrationClassesInstancesToRevert_1_1.done) return [3 /*break*/, 10];
208
- migrationClassInstance = migrationClassesInstancesToRevert_1_1.value;
209
- migrationName = class_helpers_1.ClassHelpers.getName(migrationClassInstance);
210
- if (!appliedMigrationsForContextNames.includes(migrationName)) {
211
- this.ctx.logMigrations &&
212
- console.warn("Skipping migration not marked as applied: ".concat(migrationName));
213
- return [3 /*break*/, 9];
214
- }
215
- this.ctx.logMigrations &&
216
- console.log("Reverting migration: ".concat(migrationName, " , context: ").concat(this.ctx.contextName));
217
- return [4 /*yield*/, migrationClassInstance.down(queryRunner)];
218
- case 7:
219
- _b.sent();
220
- // Remove the reverted migration from the tracking table
221
- return [4 /*yield*/, queryRunner.query("DELETE FROM ".concat(this.DEFAULT_MIGRATION_TABLE_NAME, " WHERE name = $1"), [migrationName])];
222
- case 8:
223
- // Remove the reverted migration from the tracking table
224
- _b.sent();
225
- _b.label = 9;
226
- case 9:
227
- migrationClassesInstancesToRevert_1_1 = migrationClassesInstancesToRevert_1.next();
228
- return [3 /*break*/, 6];
229
- case 10: return [3 /*break*/, 13];
230
- case 11:
231
- e_1_1 = _b.sent();
232
- e_1 = { error: e_1_1 };
233
- return [3 /*break*/, 13];
234
- case 12:
235
- try {
236
- if (migrationClassesInstancesToRevert_1_1 && !migrationClassesInstancesToRevert_1_1.done && (_a = migrationClassesInstancesToRevert_1.return)) _a.call(migrationClassesInstancesToRevert_1);
237
- }
238
- finally { if (e_1) throw e_1.error; }
239
- return [7 /*endfinally*/];
240
- case 13: return [4 /*yield*/, queryRunner.commitTransaction()];
241
- case 14:
242
- _b.sent();
243
- this.ctx.logMigrations &&
244
- console.log("Migrations successfully reverted " +
245
- "to the specified timestamp ".concat(timestamp, " ."));
246
- return [3 /*break*/, 19];
247
- case 15:
248
- error_2 = _b.sent();
249
- this.ctx.logMigrations &&
250
- console.error('Transaction failed, rolling back:', error_2);
251
- return [4 /*yield*/, queryRunner.rollbackTransaction()];
252
- case 16:
253
- _b.sent();
254
- return [3 /*break*/, 19];
255
- case 17: return [4 /*yield*/, queryRunner.release()];
256
- case 18:
257
- _b.sent();
258
- return [7 /*endfinally*/];
259
- case 19: return [2 /*return*/];
110
+ async logSelectALl(name, queryRunner) {
111
+ console.log(name, (await queryRunner.query(`SELECT * FROM ${this.DEFAULT_MIGRATION_TABLE_NAME} WHERE context = $1`, [this.ctx.contextName])).map(m => m.name));
112
+ }
113
+ async revertMigrationToTimestamp(timestamp) {
114
+ //#region @websqlFunc
115
+ if (this.ctx.remoteHost) {
116
+ return;
117
+ }
118
+ if (!tnp_core_1.UtilsMigrations.isValidTimestamp(timestamp)) {
119
+ tnp_core_1.Helpers.throw(`Invalid timestamp provided for migration revert: ${timestamp}`);
120
+ }
121
+ // Get all migration class functions and reverse the order
122
+ const migrationsClassFns = this.ctx
123
+ .getClassFunByArr(models_1.Models.ClassType.MIGRATION)
124
+ .reverse();
125
+ // Filter migrations that need to be reverted
126
+ const migrationClassesInstancesToRevert = migrationsClassFns
127
+ .map(classFn => {
128
+ const timestampFromClassName = Number(tnp_core_1.UtilsMigrations.getTimestampFromClassName(class_helpers_1.ClassHelpers.getName(classFn)));
129
+ if (timestampFromClassName <= timestamp) {
130
+ // this.ctx.logMigrations &&
131
+ // console.log(
132
+ // `Stopping migration filter at: ${ClassHelpers.getName(classFn)} ` +
133
+ // `with timestamp ${timestampFromClassName}`,
134
+ // );
135
+ return null;
136
+ }
137
+ return this.ctx.getInstanceBy(classFn);
138
+ })
139
+ .filter(f => !!f)
140
+ .map(f => f)
141
+ .filter(migrationInstance => migrationInstance.isReadyToRun());
142
+ const queryRunner = this.ctx.connection.createQueryRunner();
143
+ await queryRunner.connect();
144
+ try {
145
+ await queryRunner.startTransaction();
146
+ // Fetch applied migrations from the database
147
+ const appliedMigrationsForContext = await queryRunner.query(`SELECT name FROM ${this.DEFAULT_MIGRATION_TABLE_NAME}
148
+ WHERE status = $1 AND context = $2`, [this.MIGRATION_STATUS_COMPLETED, this.ctx.contextName]);
149
+ const appliedMigrationsForContextNames = appliedMigrationsForContext.map(m => m.name);
150
+ // console.log({ appliedMigrationsForContextNames });
151
+ for (const migrationClassInstance of migrationClassesInstancesToRevert) {
152
+ const migrationName = class_helpers_1.ClassHelpers.getName(migrationClassInstance);
153
+ if (!appliedMigrationsForContextNames.includes(migrationName)) {
154
+ this.ctx.logMigrations &&
155
+ console.warn(`Skipping migration not marked as applied: ${migrationName}`);
156
+ continue;
260
157
  }
261
- });
262
- });
263
- };
158
+ this.ctx.logMigrations &&
159
+ console.log(`Reverting migration: ${migrationName} , context: ${this.ctx.contextName}`);
160
+ await migrationClassInstance.down(queryRunner);
161
+ // Remove the reverted migration from the tracking table
162
+ await queryRunner.query(`DELETE FROM ${this.DEFAULT_MIGRATION_TABLE_NAME} WHERE name = $1`, [migrationName]);
163
+ }
164
+ await queryRunner.commitTransaction();
165
+ this.ctx.logMigrations &&
166
+ console.log(`Migrations successfully reverted ` +
167
+ `to the specified timestamp ${timestamp} .`);
168
+ }
169
+ catch (error) {
170
+ this.ctx.logMigrations &&
171
+ console.error('Transaction failed, rolling back:', error);
172
+ await queryRunner.rollbackTransaction();
173
+ }
174
+ finally {
175
+ await queryRunner.release();
176
+ }
177
+ //#endregion
178
+ }
264
179
  //#endregion
265
180
  //#region methods & getters / clear migration table
266
- ContextDbMigrations.prototype.clearMigrationTable = function () {
267
- return tslib_1.__awaiter(this, void 0, void 0, function () {
268
- var queryRunner, error_3;
269
- return tslib_1.__generator(this, function (_a) {
270
- switch (_a.label) {
271
- case 0:
272
- //#region @websqlFunc
273
- if (this.ctx.remoteHost) {
274
- return [2 /*return*/];
275
- }
276
- queryRunner = this.ctx.connection.createQueryRunner();
277
- return [4 /*yield*/, queryRunner.connect()];
278
- case 1:
279
- _a.sent();
280
- return [4 /*yield*/, queryRunner.startTransaction()];
281
- case 2:
282
- _a.sent();
283
- _a.label = 3;
284
- case 3:
285
- _a.trys.push([3, 6, 8, 10]);
286
- return [4 /*yield*/, queryRunner.clearTable(this.DEFAULT_MIGRATION_TABLE_NAME)];
287
- case 4:
288
- _a.sent();
289
- return [4 /*yield*/, queryRunner.commitTransaction()];
290
- case 5:
291
- _a.sent();
292
- return [3 /*break*/, 10];
293
- case 6:
294
- error_3 = _a.sent();
295
- this.ctx.logMigrations &&
296
- console.error('Transaction failed, rolling back:', error_3);
297
- return [4 /*yield*/, queryRunner.rollbackTransaction()];
298
- case 7:
299
- _a.sent();
300
- return [3 /*break*/, 10];
301
- case 8: return [4 /*yield*/, queryRunner.release()];
302
- case 9:
303
- _a.sent();
304
- return [7 /*endfinally*/];
305
- case 10: return [2 /*return*/];
306
- }
307
- });
308
- });
309
- };
181
+ async clearMigrationTable() {
182
+ //#region @websqlFunc
183
+ if (this.ctx.remoteHost) {
184
+ return;
185
+ }
186
+ const queryRunner = this.ctx.connection.createQueryRunner();
187
+ await queryRunner.connect();
188
+ await queryRunner.startTransaction();
189
+ try {
190
+ await queryRunner.clearTable(this.DEFAULT_MIGRATION_TABLE_NAME);
191
+ await queryRunner.commitTransaction();
192
+ }
193
+ catch (error) {
194
+ this.ctx.logMigrations &&
195
+ console.error('Transaction failed, rolling back:', error);
196
+ await queryRunner.rollbackTransaction();
197
+ }
198
+ finally {
199
+ await queryRunner.release();
200
+ }
201
+ //#endregion
202
+ }
310
203
  //#endregion
311
204
  //#region methods & getters / mark all migrations as applied
312
- ContextDbMigrations.prototype.markAllMigrationsAsApplied = function () {
313
- return tslib_1.__awaiter(this, void 0, void 0, function () {
314
- var migrationsClassFns, migrationClassesInstances, queryRunner, allMigrationsInDb, allMigrationInDBNames, migrationClassesInstances_1, migrationClassesInstances_1_1, instance, migrationName, e_2_1, error_4;
315
- var e_2, _a;
316
- var _this = this;
317
- return tslib_1.__generator(this, function (_b) {
318
- switch (_b.label) {
319
- case 0:
320
- //#region @websqlFunc
321
- if (this.ctx.remoteHost) {
322
- return [2 /*return*/];
323
- }
324
- migrationsClassFns = this.ctx.getClassFunByArr(models_1.Models.ClassType.MIGRATION);
325
- migrationClassesInstances = migrationsClassFns
326
- .map(function (classFn) { return _this.ctx.getInstanceBy(classFn); })
327
- .map(function (f) { return f; })
328
- .filter(function (migrationInstance) { return migrationInstance.isReadyToRun(); });
329
- queryRunner = this.ctx.connection.createQueryRunner();
330
- return [4 /*yield*/, queryRunner.connect()];
331
- case 1:
332
- _b.sent();
333
- _b.label = 2;
334
- case 2:
335
- _b.trys.push([2, 15, 17, 19]);
336
- return [4 /*yield*/, queryRunner.startTransaction()];
337
- case 3:
338
- _b.sent();
339
- return [4 /*yield*/, queryRunner.query("SELECT name FROM ".concat(this.DEFAULT_MIGRATION_TABLE_NAME))];
340
- case 4:
341
- allMigrationsInDb = _b.sent();
342
- allMigrationInDBNames = allMigrationsInDb.map(function (m) { return m.name; });
343
- _b.label = 5;
344
- case 5:
345
- _b.trys.push([5, 10, 11, 12]);
346
- migrationClassesInstances_1 = tslib_1.__values(migrationClassesInstances), migrationClassesInstances_1_1 = migrationClassesInstances_1.next();
347
- _b.label = 6;
348
- case 6:
349
- if (!!migrationClassesInstances_1_1.done) return [3 /*break*/, 9];
350
- instance = migrationClassesInstances_1_1.value;
351
- migrationName = class_helpers_1.ClassHelpers.getName(instance);
352
- if (allMigrationInDBNames.includes(migrationName)) {
353
- this.ctx.logMigrations &&
354
- console.log("Skipping already applied migration: ".concat(migrationName));
355
- return [3 /*break*/, 8];
356
- }
357
- this.ctx.logMigrations &&
358
- console.log("Marking migration as applied: ".concat(migrationName));
359
- // Insert migration as 'complete' without running
360
- return [4 /*yield*/, queryRunner.query("INSERT INTO ".concat(this.DEFAULT_MIGRATION_TABLE_NAME, " (name, status, context, applied_at) ") +
361
- "VALUES ($1, $2, $3, CURRENT_TIMESTAMP)", [
362
- migrationName,
363
- this.MIGRATION_STATUS_COMPLETED,
364
- instance.ctx.contextName,
365
- ])];
366
- case 7:
367
- // Insert migration as 'complete' without running
368
- _b.sent();
369
- _b.label = 8;
370
- case 8:
371
- migrationClassesInstances_1_1 = migrationClassesInstances_1.next();
372
- return [3 /*break*/, 6];
373
- case 9: return [3 /*break*/, 12];
374
- case 10:
375
- e_2_1 = _b.sent();
376
- e_2 = { error: e_2_1 };
377
- return [3 /*break*/, 12];
378
- case 11:
379
- try {
380
- if (migrationClassesInstances_1_1 && !migrationClassesInstances_1_1.done && (_a = migrationClassesInstances_1.return)) _a.call(migrationClassesInstances_1);
381
- }
382
- finally { if (e_2) throw e_2.error; }
383
- return [7 /*endfinally*/];
384
- case 12:
385
- // update all pending migrations to completed
386
- return [4 /*yield*/, queryRunner.query("UPDATE ".concat(this.DEFAULT_MIGRATION_TABLE_NAME, "\n SET status = $1, applied_at = CURRENT_TIMESTAMP\n WHERE status = $2"), [this.MIGRATION_STATUS_COMPLETED, this.MIGRATION_STATUS_PENDING])];
387
- case 13:
388
- // update all pending migrations to completed
389
- _b.sent();
390
- return [4 /*yield*/, queryRunner.commitTransaction()];
391
- case 14:
392
- _b.sent();
393
- this.ctx.logMigrations &&
394
- console.log('All migrations marked as applied.');
395
- return [3 /*break*/, 19];
396
- case 15:
397
- error_4 = _b.sent();
398
- this.ctx.logMigrations &&
399
- console.error('Failed to mark all migrations as applied, rolling back:', error_4);
400
- return [4 /*yield*/, queryRunner.rollbackTransaction()];
401
- case 16:
402
- _b.sent();
403
- return [3 /*break*/, 19];
404
- case 17: return [4 /*yield*/, queryRunner.release()];
405
- case 18:
406
- _b.sent();
407
- return [7 /*endfinally*/];
408
- case 19: return [2 /*return*/];
205
+ async markAllMigrationsAsApplied() {
206
+ //#region @websqlFunc
207
+ if (this.ctx.remoteHost) {
208
+ return;
209
+ }
210
+ const migrationsClassFns = this.ctx.getClassFunByArr(models_1.Models.ClassType.MIGRATION);
211
+ const migrationClassesInstances = migrationsClassFns
212
+ .map(classFn => this.ctx.getInstanceBy(classFn))
213
+ .map(f => f)
214
+ .filter(migrationInstance => migrationInstance.isReadyToRun());
215
+ const queryRunner = this.ctx.connection.createQueryRunner();
216
+ await queryRunner.connect();
217
+ try {
218
+ await queryRunner.startTransaction();
219
+ // Fetch already applied migrations from the database
220
+ const allMigrationsInDb = await queryRunner.query(`SELECT name FROM ${this.DEFAULT_MIGRATION_TABLE_NAME}`);
221
+ const allMigrationInDBNames = allMigrationsInDb.map(m => m.name);
222
+ for (const instance of migrationClassesInstances) {
223
+ const migrationName = class_helpers_1.ClassHelpers.getName(instance);
224
+ if (allMigrationInDBNames.includes(migrationName)) {
225
+ this.ctx.logMigrations &&
226
+ console.log(`Skipping already applied migration: ${migrationName}`);
227
+ continue;
409
228
  }
410
- });
411
- });
412
- };
229
+ this.ctx.logMigrations &&
230
+ console.log(`Marking migration as applied: ${migrationName}`);
231
+ // Insert migration as 'complete' without running
232
+ await queryRunner.query(`INSERT INTO ${this.DEFAULT_MIGRATION_TABLE_NAME} (name, status, context, applied_at) ` +
233
+ `VALUES ($1, $2, $3, CURRENT_TIMESTAMP)`, [
234
+ migrationName,
235
+ this.MIGRATION_STATUS_COMPLETED,
236
+ instance.ctx.contextName,
237
+ ]);
238
+ }
239
+ // update all pending migrations to completed
240
+ await queryRunner.query(`UPDATE ${this.DEFAULT_MIGRATION_TABLE_NAME}
241
+ SET status = $1, applied_at = CURRENT_TIMESTAMP
242
+ WHERE status = $2`, [this.MIGRATION_STATUS_COMPLETED, this.MIGRATION_STATUS_PENDING]);
243
+ await queryRunner.commitTransaction();
244
+ this.ctx.logMigrations &&
245
+ console.log('All migrations marked as applied.');
246
+ }
247
+ catch (error) {
248
+ this.ctx.logMigrations &&
249
+ console.error('Failed to mark all migrations as applied, rolling back:', error);
250
+ await queryRunner.rollbackTransaction();
251
+ }
252
+ finally {
253
+ await queryRunner.release();
254
+ }
255
+ //#endregion
256
+ }
413
257
  //#endregion
414
258
  //#region methods & getters / run all migrations
415
- ContextDbMigrations.prototype.runAllNotCompletedMigrations = function () {
416
- return tslib_1.__awaiter(this, void 0, void 0, function () {
417
- var migrationsClassFns, migrationClassesInstances, queryRunner, appliedMigrationsForContext, pendingMigrationsForContext, _loop_1, this_1, pendingMigrationsForContext_1, pendingMigrationsForContext_1_1, pendingContextMigration, e_3_1, _loop_2, this_2, migrationClassesInstances_2, migrationClassesInstances_2_1, instance, e_4_1, error_5;
418
- var e_3, _a, e_4, _b;
419
- var _this = this;
420
- return tslib_1.__generator(this, function (_c) {
421
- switch (_c.label) {
422
- case 0:
423
- //#region @websqlFunc
424
- if (this.ctx.remoteHost) {
425
- return [2 /*return*/];
426
- }
427
- migrationsClassFns = this.ctx.getClassFunByArr(models_1.Models.ClassType.MIGRATION);
428
- migrationClassesInstances = migrationsClassFns
429
- .map(function (classFn) { return _this.ctx.getInstanceBy(classFn); })
430
- .map(function (f) { return f; })
431
- .filter(function (migrationInstance) { return migrationInstance.isReadyToRun(); });
432
- queryRunner = this.ctx.connection.createQueryRunner();
433
- return [4 /*yield*/, queryRunner.connect()];
434
- case 1:
435
- _c.sent();
436
- _c.label = 2;
437
- case 2:
438
- _c.trys.push([2, 22, 24, 26]);
439
- return [4 /*yield*/, queryRunner.startTransaction()];
440
- case 3:
441
- _c.sent();
442
- return [4 /*yield*/, queryRunner.query("SELECT name, status FROM ".concat(this.DEFAULT_MIGRATION_TABLE_NAME, " ") +
443
- "WHERE context = $1", [this.ctx.contextName])];
444
- case 4:
445
- appliedMigrationsForContext = _c.sent();
446
- pendingMigrationsForContext = appliedMigrationsForContext.filter(function (m) { return m.status === _this.MIGRATION_STATUS_PENDING; });
447
- _loop_1 = function (pendingContextMigration) {
448
- var migrationInstance;
449
- return tslib_1.__generator(this, function (_d) {
450
- switch (_d.label) {
451
- case 0:
452
- migrationInstance = migrationClassesInstances.find(function (instance) {
453
- return class_helpers_1.ClassHelpers.getName(instance) === pendingContextMigration.name;
454
- });
455
- if (!migrationInstance) {
456
- this_1.ctx.logMigrations &&
457
- console.warn("Pending migration ".concat(pendingContextMigration.name, " not found in loaded migrations."));
458
- return [2 /*return*/, "continue"];
459
- }
460
- this_1.ctx.logMigrations &&
461
- console.log("Completing pending migration: ".concat(pendingContextMigration.name));
462
- return [4 /*yield*/, migrationInstance.up(queryRunner)];
463
- case 1:
464
- _d.sent();
465
- // Update migration status to 'complete'
466
- return [4 /*yield*/, queryRunner.query("UPDATE ".concat(this_1.DEFAULT_MIGRATION_TABLE_NAME, "\n SET status = $1, applied_at = CURRENT_TIMESTAMP\n WHERE name = $2"), [this_1.MIGRATION_STATUS_COMPLETED, pendingContextMigration.name])];
467
- case 2:
468
- // Update migration status to 'complete'
469
- _d.sent();
470
- return [2 /*return*/];
471
- }
472
- });
473
- };
474
- this_1 = this;
475
- _c.label = 5;
476
- case 5:
477
- _c.trys.push([5, 10, 11, 12]);
478
- pendingMigrationsForContext_1 = tslib_1.__values(pendingMigrationsForContext), pendingMigrationsForContext_1_1 = pendingMigrationsForContext_1.next();
479
- _c.label = 6;
480
- case 6:
481
- if (!!pendingMigrationsForContext_1_1.done) return [3 /*break*/, 9];
482
- pendingContextMigration = pendingMigrationsForContext_1_1.value;
483
- return [5 /*yield**/, _loop_1(pendingContextMigration)];
484
- case 7:
485
- _c.sent();
486
- _c.label = 8;
487
- case 8:
488
- pendingMigrationsForContext_1_1 = pendingMigrationsForContext_1.next();
489
- return [3 /*break*/, 6];
490
- case 9: return [3 /*break*/, 12];
491
- case 10:
492
- e_3_1 = _c.sent();
493
- e_3 = { error: e_3_1 };
494
- return [3 /*break*/, 12];
495
- case 11:
496
- try {
497
- if (pendingMigrationsForContext_1_1 && !pendingMigrationsForContext_1_1.done && (_a = pendingMigrationsForContext_1.return)) _a.call(pendingMigrationsForContext_1);
498
- }
499
- finally { if (e_3) throw e_3.error; }
500
- return [7 /*endfinally*/];
501
- case 12:
502
- _loop_2 = function (instance) {
503
- var migrationName, error_6;
504
- return tslib_1.__generator(this, function (_e) {
505
- switch (_e.label) {
506
- case 0:
507
- migrationName = class_helpers_1.ClassHelpers.getName(instance);
508
- if (appliedMigrationsForContext.some(function (m) { return m.name === migrationName; })) {
509
- this_2.ctx.logMigrations &&
510
- console.log("Skipping already applied migration: ".concat(migrationName));
511
- return [2 /*return*/, "continue"];
512
- }
513
- this_2.ctx.logMigrations &&
514
- console.log("Applying new migration: ".concat(migrationName));
515
- // Insert migration as 'pending' before execution
516
- return [4 /*yield*/, queryRunner.query("INSERT INTO ".concat(this_2.DEFAULT_MIGRATION_TABLE_NAME, " (name, status, context, applied_at) ") +
517
- "VALUES ($1, $2, $3, NULL)", [migrationName, this_2.MIGRATION_STATUS_PENDING, this_2.ctx.contextName])];
518
- case 1:
519
- // Insert migration as 'pending' before execution
520
- _e.sent();
521
- _e.label = 2;
522
- case 2:
523
- _e.trys.push([2, 5, , 7]);
524
- // Apply migration
525
- return [4 /*yield*/, instance.up(queryRunner)];
526
- case 3:
527
- // Apply migration
528
- _e.sent();
529
- // Update migration to 'complete' after successful execution
530
- return [4 /*yield*/, queryRunner.query("UPDATE ".concat(this_2.DEFAULT_MIGRATION_TABLE_NAME, " ") +
531
- "SET status = '".concat(this_2.MIGRATION_STATUS_COMPLETED, "', applied_at = CURRENT_TIMESTAMP ") +
532
- "WHERE name = $1", [migrationName])];
533
- case 4:
534
- // Update migration to 'complete' after successful execution
535
- _e.sent();
536
- return [3 /*break*/, 7];
537
- case 5:
538
- error_6 = _e.sent();
539
- this_2.ctx.logMigrations &&
540
- console.error("Failed to apply migration: ".concat(migrationName), error_6);
541
- // Rollback pending migration entry
542
- return [4 /*yield*/, queryRunner.query("DELETE FROM ".concat(this_2.DEFAULT_MIGRATION_TABLE_NAME, " WHERE name = $1"), [migrationName])];
543
- case 6:
544
- // Rollback pending migration entry
545
- _e.sent();
546
- throw error_6; // Rethrow to ensure the transaction is rolled back
547
- case 7: return [2 /*return*/];
548
- }
549
- });
550
- };
551
- this_2 = this;
552
- _c.label = 13;
553
- case 13:
554
- _c.trys.push([13, 18, 19, 20]);
555
- migrationClassesInstances_2 = tslib_1.__values(migrationClassesInstances), migrationClassesInstances_2_1 = migrationClassesInstances_2.next();
556
- _c.label = 14;
557
- case 14:
558
- if (!!migrationClassesInstances_2_1.done) return [3 /*break*/, 17];
559
- instance = migrationClassesInstances_2_1.value;
560
- return [5 /*yield**/, _loop_2(instance)];
561
- case 15:
562
- _c.sent();
563
- _c.label = 16;
564
- case 16:
565
- migrationClassesInstances_2_1 = migrationClassesInstances_2.next();
566
- return [3 /*break*/, 14];
567
- case 17: return [3 /*break*/, 20];
568
- case 18:
569
- e_4_1 = _c.sent();
570
- e_4 = { error: e_4_1 };
571
- return [3 /*break*/, 20];
572
- case 19:
573
- try {
574
- if (migrationClassesInstances_2_1 && !migrationClassesInstances_2_1.done && (_b = migrationClassesInstances_2.return)) _b.call(migrationClassesInstances_2);
575
- }
576
- finally { if (e_4) throw e_4.error; }
577
- return [7 /*endfinally*/];
578
- case 20:
579
- //#endregion
580
- return [4 /*yield*/, queryRunner.commitTransaction()];
581
- case 21:
582
- //#endregion
583
- _c.sent();
584
- return [3 /*break*/, 26];
585
- case 22:
586
- error_5 = _c.sent();
587
- this.ctx.logMigrations &&
588
- console.error('Transaction failed, rolling back:', error_5);
589
- return [4 /*yield*/, queryRunner.rollbackTransaction()];
590
- case 23:
591
- _c.sent();
592
- return [3 /*break*/, 26];
593
- case 24: return [4 /*yield*/, queryRunner.release()];
594
- case 25:
595
- _c.sent();
596
- return [7 /*endfinally*/];
597
- case 26: return [2 /*return*/];
259
+ async runAllNotCompletedMigrations() {
260
+ //#region @websqlFunc
261
+ if (this.ctx.remoteHost) {
262
+ return;
263
+ }
264
+ const migrationsClassFns = this.ctx.getClassFunByArr(models_1.Models.ClassType.MIGRATION);
265
+ // console.log({
266
+ // migrationClassesALl: migrationsClassFns.map(f => ClassHelpers.getName(f)),
267
+ // });
268
+ const migrationClassesInstances = migrationsClassFns
269
+ .map(classFn => this.ctx.getInstanceBy(classFn))
270
+ .map(f => f)
271
+ .filter(migrationInstance => migrationInstance.isReadyToRun());
272
+ // console.log({
273
+ // migrationClassesInstances: migrationsClassFns.map(f =>
274
+ // ClassHelpers.getName(f),
275
+ // ),
276
+ // });
277
+ const queryRunner = this.ctx.connection.createQueryRunner();
278
+ await queryRunner.connect();
279
+ try {
280
+ await queryRunner.startTransaction();
281
+ // Check if the migrations table exists
282
+ // TODO: Implement check for migrations table existence here
283
+ // Fetch applied migrations from the database
284
+ const appliedMigrationsForContext = await queryRunner.query(`SELECT name, status FROM ${this.DEFAULT_MIGRATION_TABLE_NAME} ` +
285
+ `WHERE context = $1`, [this.ctx.contextName]);
286
+ //#region check and update pending migrations
287
+ const pendingMigrationsForContext = appliedMigrationsForContext.filter(m => m.status === this.MIGRATION_STATUS_PENDING);
288
+ // Run pending migrations first
289
+ for (const pendingContextMigration of pendingMigrationsForContext) {
290
+ const migrationInstance = migrationClassesInstances.find(instance => class_helpers_1.ClassHelpers.getName(instance) === pendingContextMigration.name);
291
+ if (!migrationInstance) {
292
+ this.ctx.logMigrations &&
293
+ console.warn(`Pending migration ${pendingContextMigration.name} not found in loaded migrations.`);
294
+ continue;
598
295
  }
599
- });
600
- });
601
- };
602
- return ContextDbMigrations;
603
- }());
296
+ this.ctx.logMigrations &&
297
+ console.log(`Completing pending migration: ${pendingContextMigration.name}`);
298
+ await migrationInstance.up(queryRunner);
299
+ // Update migration status to 'complete'
300
+ await queryRunner.query(`UPDATE ${this.DEFAULT_MIGRATION_TABLE_NAME}
301
+ SET status = $1, applied_at = CURRENT_TIMESTAMP
302
+ WHERE name = $2`, [this.MIGRATION_STATUS_COMPLETED, pendingContextMigration.name]);
303
+ }
304
+ //#endregion
305
+ //#region run new migrations
306
+ for (const instance of migrationClassesInstances) {
307
+ const migrationName = class_helpers_1.ClassHelpers.getName(instance);
308
+ if (appliedMigrationsForContext.some(m => m.name === migrationName)) {
309
+ this.ctx.logMigrations &&
310
+ console.log(`Skipping already applied migration: ${migrationName}`);
311
+ continue;
312
+ }
313
+ this.ctx.logMigrations &&
314
+ console.log(`Applying new migration: ${migrationName}`);
315
+ // Insert migration as 'pending' before execution
316
+ await queryRunner.query(`INSERT INTO ${this.DEFAULT_MIGRATION_TABLE_NAME} (name, status, context, applied_at) ` +
317
+ `VALUES ($1, $2, $3, NULL)`, [migrationName, this.MIGRATION_STATUS_PENDING, this.ctx.contextName]);
318
+ try {
319
+ // Apply migration
320
+ await instance.up(queryRunner);
321
+ // Update migration to 'complete' after successful execution
322
+ await queryRunner.query(`UPDATE ${this.DEFAULT_MIGRATION_TABLE_NAME} ` +
323
+ `SET status = '${this.MIGRATION_STATUS_COMPLETED}', applied_at = CURRENT_TIMESTAMP ` +
324
+ `WHERE name = $1`, [migrationName]);
325
+ }
326
+ catch (error) {
327
+ this.ctx.logMigrations &&
328
+ console.error(`Failed to apply migration: ${migrationName}`, error);
329
+ // Rollback pending migration entry
330
+ await queryRunner.query(`DELETE FROM ${this.DEFAULT_MIGRATION_TABLE_NAME} WHERE name = $1`, [migrationName]);
331
+ throw error; // Rethrow to ensure the transaction is rolled back
332
+ }
333
+ }
334
+ //#endregion
335
+ await queryRunner.commitTransaction();
336
+ }
337
+ catch (error) {
338
+ this.ctx.logMigrations &&
339
+ console.error('Transaction failed, rolling back:', error);
340
+ await queryRunner.rollbackTransaction();
341
+ }
342
+ finally {
343
+ await queryRunner.release();
344
+ }
345
+ //#endregion
346
+ }
347
+ }
604
348
  exports.ContextDbMigrations = ContextDbMigrations;
605
349
  //# sourceMappingURL=context-db-migrations.js.map