nesoi 3.0.9 → 3.0.11

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 (99) hide show
  1. package/lib/compiler/apps/monolyth/monolyth_compiler.d.ts +1 -1
  2. package/lib/compiler/apps/monolyth/monolyth_compiler.js +7 -7
  3. package/lib/elements/blocks/block.builder.js +1 -1
  4. package/lib/elements/blocks/job/job.builder.js +3 -2
  5. package/lib/elements/blocks/machine/machine.builder.js +12 -1
  6. package/lib/elements/blocks/machine/machine.schema.d.ts +2 -1
  7. package/lib/elements/blocks/machine/machine.schema.js +2 -1
  8. package/lib/elements/edge/controller/adapters/controller_adapter.d.ts +3 -1
  9. package/lib/elements/edge/controller/adapters/controller_adapter.js +2 -1
  10. package/lib/elements/edge/controller/controller.config.d.ts +3 -2
  11. package/lib/elements/edge/controller/controller.d.ts +3 -3
  12. package/lib/elements/edge/controller/controller.js +3 -3
  13. package/lib/elements/entities/bucket/adapters/bucket_adapter.d.ts +2 -1
  14. package/lib/elements/entities/bucket/bucket.config.d.ts +3 -3
  15. package/lib/elements/entities/bucket/bucket.d.ts +3 -2
  16. package/lib/elements/entities/bucket/bucket.js +4 -4
  17. package/lib/elements/entities/bucket/model/bucket_model.schema.d.ts +1 -1
  18. package/lib/elements/entities/bucket/model/bucket_model_field.builder.d.ts +4 -0
  19. package/lib/elements/entities/bucket/model/bucket_model_field.builder.js +3 -0
  20. package/lib/elements/entities/bucket/query/nql_compiler.js +2 -1
  21. package/lib/elements/entities/message/template/message_template_field.builder.d.ts +6 -0
  22. package/lib/elements/entities/message/template/message_template_field.builder.js +3 -0
  23. package/lib/elements/entities/message/template/message_template_parser.js +5 -0
  24. package/lib/engine/apps/app.config.d.ts +12 -11
  25. package/lib/engine/apps/app.d.ts +11 -22
  26. package/lib/engine/apps/app.js +9 -9
  27. package/lib/engine/apps/inline.app.d.ts +9 -8
  28. package/lib/engine/apps/inline.app.js +24 -24
  29. package/lib/engine/apps/monolyth/monolyth.app.d.ts +5 -5
  30. package/lib/engine/apps/monolyth/monolyth.app.js +6 -6
  31. package/lib/engine/apps/service.d.ts +30 -0
  32. package/lib/engine/apps/service.js +15 -0
  33. package/lib/engine/auth/authn.d.ts +10 -1
  34. package/lib/engine/auth/zero.authn_provider.d.ts +7 -3
  35. package/lib/engine/auth/zero.authn_provider.js +9 -2
  36. package/lib/engine/cli/cli.d.ts +2 -2
  37. package/lib/engine/cli/cli.js +1 -1
  38. package/lib/engine/cli/ui.js +1 -1
  39. package/lib/engine/daemon.d.ts +12 -12
  40. package/lib/engine/daemon.js +18 -18
  41. package/lib/engine/data/duration.d.ts +53 -0
  42. package/lib/engine/data/duration.js +62 -0
  43. package/lib/engine/data/error.d.ts +7 -0
  44. package/lib/engine/data/error.js +9 -1
  45. package/lib/engine/dependency.d.ts +2 -1
  46. package/lib/engine/dependency.js +18 -0
  47. package/lib/engine/module.d.ts +6 -4
  48. package/lib/engine/module.js +25 -10
  49. package/lib/engine/transaction/nodes/bucket.trx_node.js +1 -2
  50. package/lib/engine/transaction/nodes/bucket_query.trx_node.js +5 -10
  51. package/lib/engine/transaction/nodes/job.trx_node.js +2 -4
  52. package/lib/engine/transaction/nodes/machine.trx_node.js +2 -4
  53. package/lib/engine/transaction/nodes/queue.trx_node.js +1 -2
  54. package/lib/engine/transaction/nodes/resource.trx_node.js +2 -4
  55. package/lib/engine/transaction/trx.d.ts +6 -4
  56. package/lib/engine/transaction/trx.js +2 -1
  57. package/lib/engine/transaction/trx_engine.config.d.ts +2 -2
  58. package/lib/engine/transaction/trx_engine.d.ts +4 -3
  59. package/lib/engine/transaction/trx_engine.js +20 -17
  60. package/lib/engine/transaction/trx_node.d.ts +4 -2
  61. package/lib/engine/transaction/trx_node.js +13 -1
  62. package/lib/engine/tree.js +6 -17
  63. package/lib/engine/util/i18n.js +3 -3
  64. package/lib/engine/util/parse.d.ts +5 -0
  65. package/lib/engine/util/parse.js +10 -0
  66. package/lib/schema.d.ts +1 -1
  67. package/package.json +2 -3
  68. package/tools/joaquin/job.js +2 -2
  69. package/tools/joaquin/message.js +2 -2
  70. package/tools/joaquin/mock.d.ts +6 -6
  71. package/tsconfig.build.tsbuildinfo +1 -1
  72. package/lib/adapters/postgres/src/migrator/csv.d.ts +0 -7
  73. package/lib/adapters/postgres/src/migrator/csv.js +0 -72
  74. package/lib/adapters/postgres/src/migrator/database.d.ts +0 -34
  75. package/lib/adapters/postgres/src/migrator/database.js +0 -88
  76. package/lib/adapters/postgres/src/migrator/generator/generator.d.ts +0 -22
  77. package/lib/adapters/postgres/src/migrator/generator/generator.js +0 -326
  78. package/lib/adapters/postgres/src/migrator/generator/migration.d.ts +0 -66
  79. package/lib/adapters/postgres/src/migrator/generator/migration.js +0 -249
  80. package/lib/adapters/postgres/src/migrator/generator/provider.d.ts +0 -19
  81. package/lib/adapters/postgres/src/migrator/generator/provider.js +0 -74
  82. package/lib/adapters/postgres/src/migrator/index.d.ts +0 -47
  83. package/lib/adapters/postgres/src/migrator/index.js +0 -22
  84. package/lib/adapters/postgres/src/migrator/runner/runner.d.ts +0 -17
  85. package/lib/adapters/postgres/src/migrator/runner/runner.js +0 -249
  86. package/lib/adapters/postgres/src/migrator/runner/status.d.ts +0 -17
  87. package/lib/adapters/postgres/src/migrator/runner/status.js +0 -55
  88. package/lib/adapters/postgres/src/postgres.bucket_adapter.d.ts +0 -42
  89. package/lib/adapters/postgres/src/postgres.bucket_adapter.js +0 -294
  90. package/lib/adapters/postgres/src/postgres.cli.d.ts +0 -76
  91. package/lib/adapters/postgres/src/postgres.cli.js +0 -207
  92. package/lib/adapters/postgres/src/postgres.config.d.ts +0 -5
  93. package/lib/adapters/postgres/src/postgres.config.js +0 -2
  94. package/lib/adapters/postgres/src/postgres.nql.d.ts +0 -16
  95. package/lib/adapters/postgres/src/postgres.nql.js +0 -123
  96. package/lib/adapters/postgres/src/postgres.provider.d.ts +0 -18
  97. package/lib/adapters/postgres/src/postgres.provider.js +0 -77
  98. package/lib/adapters/postgres/test/postgres.bucket_adapter.test.d.ts +0 -1
  99. package/lib/adapters/postgres/test/postgres.bucket_adapter.test.js +0 -210
@@ -1,249 +0,0 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.$Migration = exports.$MigrationField = void 0;
37
- const string_1 = require("../../../../../engine/util/string");
38
- const path = __importStar(require("path"));
39
- const fs = __importStar(require("fs"));
40
- const datetime_1 = require("../../../../../engine/data/datetime");
41
- const crypto_1 = require("crypto");
42
- class $MigrationField {
43
- constructor(column, operation) {
44
- this.column = column;
45
- this.operation = operation;
46
- }
47
- describe() {
48
- const col_str = (0, string_1.colored)(this.column, 'lightblue');
49
- if ('create' in this.operation) {
50
- const type_str = (0, string_1.colored)(this.operation.create.type, 'purple');
51
- return `Create column ${col_str} as ${type_str}`;
52
- }
53
- else if ('rename' in this.operation) {
54
- const op = this.operation.rename;
55
- const name_str = op.name ? (0, string_1.colored)(op.name, 'lightcyan') : undefined;
56
- return `Rename column ${col_str} to ${name_str}`;
57
- }
58
- else if ('alter_type' in this.operation) {
59
- const op = this.operation.alter_type;
60
- const from_str = op.from ? (0, string_1.colored)(op.from, 'purple') : undefined;
61
- const to_str = op.to ? (0, string_1.colored)(op.to, 'purple') : undefined;
62
- return `Alter column ${col_str} type from ${from_str} to ${to_str};`;
63
- }
64
- else if ('alter_null' in this.operation) {
65
- const op = this.operation.alter_null;
66
- const from_str = (0, string_1.colored)(op.from ? 'NULL' : 'NOT NULL', 'purple');
67
- const to_str = (0, string_1.colored)(op.to ? 'NULL' : 'NOT NULL', 'purple');
68
- return `Alter column ${col_str} from ${from_str} to ${to_str};`;
69
- }
70
- else if ('drop' in this.operation) {
71
- return `Drop column ${col_str}`;
72
- }
73
- else if ('create_fk' in this.operation) {
74
- const op = this.operation.create_fk;
75
- const table_str = (0, string_1.colored)(op.table, 'lightcyan');
76
- const field_str = (0, string_1.colored)(op.field, 'purple');
77
- return `Create foreign key from ${col_str} to ${table_str}.${field_str}`;
78
- }
79
- else if ('drop_fk' in this.operation) {
80
- const op = this.operation.drop_fk;
81
- const table_str = (0, string_1.colored)(op.table, 'lightcyan');
82
- const field_str = (0, string_1.colored)(op.field, 'purple');
83
- return `Drop foreign key from ${col_str} to ${table_str}.${field_str}`;
84
- }
85
- else {
86
- return (0, string_1.colored)(`Unknown: ${this.operation}`, 'lightred');
87
- }
88
- }
89
- sqlUp(table_op) {
90
- if ('create' in this.operation) {
91
- const notNull = this.operation.create.nullable ? '' : ' NOT NULL';
92
- if (table_op === 'create') {
93
- return `"${this.column}" ${this.operation.create.type} ${notNull}`;
94
- }
95
- else {
96
- return `ADD "${this.column}" ${this.operation.create.type} ${notNull}`;
97
- }
98
- }
99
- else if ('rename' in this.operation) {
100
- return `RENAME COLUMN "${this.column}" TO "${this.operation.rename.name}"`;
101
- }
102
- else if ('alter_type' in this.operation) {
103
- return `ALTER COLUMN "${this.column}" TYPE ${this.operation.alter_type.to} USING ${this.operation.alter_type.using.up}`;
104
- }
105
- else if ('alter_null' in this.operation) {
106
- return `ALTER COLUMN "${this.column}" ${this.operation.alter_null.to ? 'DROP' : 'SET'} NOT NULL`;
107
- }
108
- else if ('drop' in this.operation) {
109
- return `DROP COLUMN "${this.column}"`;
110
- }
111
- return '';
112
- }
113
- sqlDown() {
114
- if ('create' in this.operation) {
115
- return `DROP COLUMN "${this.column}"`;
116
- }
117
- else if ('rename' in this.operation) {
118
- return `RENAME COLUMN "${this.operation.rename.name}" TO "${this.column}"`;
119
- }
120
- else if ('alter_type' in this.operation) {
121
- return `ALTER COLUMN "${this.column}" TYPE ${this.operation.alter_type.from} USING ${this.operation.alter_type.using.down}`;
122
- }
123
- else if ('alter_null' in this.operation) {
124
- return `ALTER COLUMN "${this.column}" ${this.operation.alter_null.from ? 'DROP' : 'SET'} NOT NULL`;
125
- }
126
- else if ('drop' in this.operation) {
127
- const notNull = this.operation.drop.nullable ? '' : ' NOT NULL';
128
- const defaul = this.operation.drop.default;
129
- return `ADD COLUMN "${this.column}" ${this.operation.drop.type}${notNull}${defaul ? (' DEFAULT ' + defaul) : ''}`;
130
- }
131
- return '';
132
- }
133
- }
134
- exports.$MigrationField = $MigrationField;
135
- class $Migration {
136
- constructor(module, type, tableName, fields, description) {
137
- this.module = module;
138
- this.type = type;
139
- this.tableName = tableName;
140
- this.fields = fields;
141
- this.description = description;
142
- this.name = `${datetime_1.NesoiDatetime.now().epoch}_${this.tableName}`;
143
- }
144
- describe() {
145
- let str = '';
146
- str += '┌\n';
147
- str += `│ ${(0, string_1.colored)('module: ' + this.module, 'darkgray')}\n`;
148
- str += `│ ${(0, string_1.colored)(this.name, 'lightcyan')}\n`;
149
- str += '└\n\n';
150
- if (this.type === 'create') {
151
- str += `◆ Create table ${(0, string_1.colored)(this.tableName, 'lightblue')}\n`;
152
- }
153
- else if (this.type === 'alter') {
154
- str += `◆ Alter table '${this.tableName}'\n`;
155
- }
156
- this.fields.forEach(field => {
157
- str += `└ ${field.describe()}\n`;
158
- });
159
- str += '\n';
160
- str += `${(0, string_1.colored)('▲ UP', 'lightgreen')}:\n`;
161
- str += this.sqlUp().join('\n');
162
- str += '\n';
163
- str += `${(0, string_1.colored)('▼ DOWN', 'yellow')}:\n`;
164
- str += this.sqlDown().join('\n');
165
- return str;
166
- }
167
- sqlUp() {
168
- if (this.type === 'create') {
169
- return [`CREATE TABLE ${this.tableName} (\n` +
170
- this.fields.map(field => '\t' + field.sqlUp('create')).join(',\n')
171
- + '\n)'];
172
- }
173
- else if (this.type === 'alter') {
174
- return this.fields.map(field => `ALTER TABLE ${this.tableName} ` + field.sqlUp('alter'));
175
- }
176
- return [];
177
- }
178
- sqlDown() {
179
- if (this.type === 'create') {
180
- return [`DROP TABLE ${this.tableName}`];
181
- }
182
- else if (this.type === 'alter') {
183
- return this.fields.map(field => `ALTER TABLE ${this.tableName} ` + field.sqlDown());
184
- }
185
- return [];
186
- }
187
- save(dirpath = './migrations') {
188
- const filedir = path.join('modules', this.module, dirpath);
189
- fs.mkdirSync(filedir, { recursive: true });
190
- const filepath = path.join(filedir, this.name + '.ts');
191
- let str = '';
192
- str += 'import { migration } from \'nesoi/lib/adapters/postgres/src/migrator\';\n';
193
- str += '\n';
194
- str += '/**\n';
195
- str += ` * $migration[${this.name}]\n`;
196
- str += ' *\n';
197
- str += ` * $type[${this.type}]\n`;
198
- if (this.type !== 'custom') {
199
- str += ` * $table[${this.tableName}]\n`;
200
- str += ' *\n';
201
- str += ' * Migration auto-generated by @nesoi/postgres. Don\'t modify it manually.\n';
202
- }
203
- str += ' */\n';
204
- str += '\n';
205
- str += 'export default migration({\n';
206
- if (this.type !== 'custom')
207
- str += `\thash: '${this.hash()}',\n`;
208
- str += `\tdescription: '${this.description || ''}',\n`;
209
- str += '\tup: ' + this.fnUp().replace(/\n/g, '\n\t') + ',\n';
210
- str += '\tdown: ' + this.fnDown().replace(/\n/g, '\n\t') + '\n';
211
- str += '})';
212
- fs.writeFileSync(filepath, str);
213
- return filepath;
214
- }
215
- fnUp() {
216
- let str = '';
217
- str += 'async ({ sql }) => {\n';
218
- this.sqlUp().forEach(sql => {
219
- str += '\tawait sql`\n';
220
- str += '\t\t' + sql.replace(/\n/g, '\n\t\t') + '\n';
221
- str += '\t`\n';
222
- });
223
- str += '}';
224
- return str;
225
- }
226
- fnDown() {
227
- let str = '';
228
- str += 'async ({ sql }) => {\n';
229
- this.sqlDown().forEach(sql => {
230
- str += '\tawait sql`\n';
231
- str += '\t\t' + sql.replace(/\n/g, '\n\t\t') + '\n';
232
- str += '\t`\n';
233
- });
234
- str += '}';
235
- return str;
236
- }
237
- hash() {
238
- const hash = (0, crypto_1.createHash)('md5');
239
- const up = this.fnUp().replace(/\s*/g, '');
240
- hash.update(up);
241
- const down = this.fnDown().replace(/\s*/g, '');
242
- hash.update(down);
243
- return hash.digest('hex');
244
- }
245
- static empty(module, name) {
246
- return new $Migration(module, 'custom', name, []);
247
- }
248
- }
249
- exports.$Migration = $Migration;
@@ -1,19 +0,0 @@
1
- import postgres from 'postgres';
2
- import { $Space } from "../../../../../elements";
3
- import { AnyDaemon } from "../../../../../engine/daemon";
4
- import { MigrationRunnerStatus } from '../runner/status';
5
- import { $Migration } from './migration';
6
- export type MigratorConfig = {
7
- dirpath?: string;
8
- postgres?: postgres.Options<any>;
9
- };
10
- export declare class MigrationProvider<S extends $Space> {
11
- protected daemon: AnyDaemon;
12
- private sql;
13
- dirpath: string;
14
- status: MigrationRunnerStatus;
15
- private constructor();
16
- static create(daemon: AnyDaemon, sql: postgres.Sql<any>): Promise<MigrationProvider<$Space>>;
17
- generate(): Promise<$Migration[]>;
18
- generateForBucket<ModuleName extends keyof S['modules']>(module: ModuleName, bucket: keyof S['modules'][ModuleName]['buckets'], tableName: string): Promise<$Migration | undefined>;
19
- }
@@ -1,74 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MigrationProvider = void 0;
4
- const log_1 = require("../../../../../engine/util/log");
5
- const runner_1 = require("../runner/runner");
6
- const daemon_1 = require("../../../../../engine/daemon");
7
- const postgres_bucket_adapter_1 = require("../../postgres.bucket_adapter");
8
- const string_1 = require("../../../../../engine/util/string");
9
- const generator_1 = require("./generator");
10
- class MigrationProvider {
11
- constructor(daemon, sql, dirpath = './migrations') {
12
- this.daemon = daemon;
13
- this.sql = sql;
14
- this.dirpath = dirpath;
15
- }
16
- static async create(daemon, sql) {
17
- const provider = new MigrationProvider(daemon, sql);
18
- const oldTable = await provider.sql `
19
- SELECT * FROM pg_catalog.pg_tables WHERE tablename = ${runner_1.MigrationRunner.MIGRATION_TABLE_NAME};
20
- `;
21
- if (!oldTable.length) {
22
- await provider.sql `CREATE TABLE ${provider.sql(runner_1.MigrationRunner.MIGRATION_TABLE_NAME)} (
23
- id SERIAL PRIMARY KEY,
24
- module VARCHAR NOT NULL,
25
- name VARCHAR NOT NULL,
26
- description VARCHAR,
27
- batch INT4 NOT NULL,
28
- timestamp TIMESTAMP NOT NULL,
29
- hash VARCHAR
30
- )`;
31
- }
32
- provider.status = await runner_1.MigrationRunner.status(daemon, provider.sql, provider.dirpath);
33
- return provider;
34
- }
35
- async generate() {
36
- const modules = daemon_1.Daemon.getModules(this.daemon);
37
- const migrations = [];
38
- for (const module of modules) {
39
- const buckets = daemon_1.Daemon.getModule(this.daemon, module.name).buckets;
40
- for (const bucket in buckets) {
41
- const schema = buckets[bucket].schema;
42
- // Avoid external buckets
43
- if (schema.module !== module.name)
44
- continue;
45
- // Avoid non-postgres buckets
46
- const adapter = buckets[bucket].adapter;
47
- if (!(adapter instanceof postgres_bucket_adapter_1.PostgresBucketAdapter))
48
- continue;
49
- const migration = await this.generateForBucket(module.name, bucket, adapter.tableName);
50
- if (migration) {
51
- migrations.push(migration);
52
- }
53
- }
54
- }
55
- return migrations;
56
- }
57
- async generateForBucket(module, bucket, tableName) {
58
- const generator = new generator_1.MigrationGenerator(this.daemon, this.sql, module, bucket, tableName);
59
- const migration = await generator.generate();
60
- const tag = (0, string_1.colored)(`${module}::bucket:${bucket}`, 'lightcyan');
61
- if (!migration) {
62
- log_1.Log.info('migrator', 'bucket', `No migrations for ${tag}.`);
63
- return undefined;
64
- }
65
- const hash = migration.hash();
66
- const alreadyExists = this.status.items.find(item => item.hash === hash);
67
- if (alreadyExists && alreadyExists?.state === 'pending') {
68
- log_1.Log.warn('migrator', 'bucket', `A similar migration for ${tag} was found pending, ignoring this one.`);
69
- return undefined;
70
- }
71
- return migration;
72
- }
73
- }
74
- exports.MigrationProvider = MigrationProvider;
@@ -1,47 +0,0 @@
1
- import postgres from 'postgres';
2
- import { AnyTrxNode } from "../../../../engine/transaction/trx_node";
3
- type MigrationFn = ($: {
4
- sql: postgres.Sql<any>;
5
- trx: AnyTrxNode;
6
- }) => Promise<void>;
7
- /**
8
- * An entry on a bucket adapter describing one migration.
9
- */
10
- export type MigrationRow = {
11
- id: number;
12
- module: string;
13
- name: string;
14
- description?: string;
15
- batch: number;
16
- timestamp: string;
17
- hash: string;
18
- };
19
- /**
20
- * A file on disk describing one migration.
21
- */
22
- export type MigrationFile = {
23
- module: string;
24
- name: string;
25
- path: string;
26
- routine: MigrationRoutine;
27
- };
28
- /**
29
- * A migration routine, composed of up and down methods.
30
- */
31
- export declare class MigrationRoutine {
32
- hash?: string;
33
- description?: string;
34
- up: MigrationFn;
35
- down: MigrationFn;
36
- constructor($: {
37
- hash?: string;
38
- description?: string;
39
- up: MigrationFn;
40
- down: MigrationFn;
41
- });
42
- }
43
- /**
44
- * Function used on migration files to declare a routine
45
- */
46
- export declare function migration(...$: ConstructorParameters<typeof MigrationRoutine>): MigrationRoutine;
47
- export {};
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MigrationRoutine = void 0;
4
- exports.migration = migration;
5
- /**
6
- * A migration routine, composed of up and down methods.
7
- */
8
- class MigrationRoutine {
9
- constructor($) {
10
- this.hash = $.hash;
11
- this.description = $.description;
12
- this.up = $.up;
13
- this.down = $.down;
14
- }
15
- }
16
- exports.MigrationRoutine = MigrationRoutine;
17
- /**
18
- * Function used on migration files to declare a routine
19
- */
20
- function migration(...$) {
21
- return new MigrationRoutine(...$);
22
- }
@@ -1,17 +0,0 @@
1
- import postgres from 'postgres';
2
- import { AnyDaemon } from "../../../../../engine/daemon";
3
- import { MigrationRunnerStatus } from './status';
4
- import { $Migration } from '../generator/migration';
5
- export declare class MigrationRunner {
6
- static MIGRATION_TABLE_NAME: string;
7
- private static scanFiles;
8
- private static scanRows;
9
- static status(daemon: AnyDaemon, sql: postgres.Sql<any>, migrations_dir: string): Promise<MigrationRunnerStatus>;
10
- static up(daemon: AnyDaemon, sql: postgres.Sql<any>, mode?: 'one' | 'batch', dirpath?: string): Promise<void>;
11
- static down(daemon: AnyDaemon, sql: postgres.Sql<any>, mode?: 'one' | 'batch', dirpath?: string): Promise<void>;
12
- static fromSchema: {
13
- up: (daemon: AnyDaemon, sql: postgres.Sql<any>, migration: $Migration, dirpath?: string) => Promise<void>;
14
- };
15
- private static migrateUp;
16
- private static migrateDown;
17
- }
@@ -1,249 +0,0 @@
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
- var _a;
39
- Object.defineProperty(exports, "__esModule", { value: true });
40
- exports.MigrationRunner = void 0;
41
- const fs = __importStar(require("fs"));
42
- const path = __importStar(require("path"));
43
- const string_1 = require("../../../../../engine/util/string");
44
- const log_1 = require("../../../../../engine/util/log");
45
- const datetime_1 = require("../../../../../engine/data/datetime");
46
- const daemon_1 = require("../../../../../engine/daemon");
47
- const trx_1 = require("../../../../../engine/transaction/trx");
48
- const status_1 = require("./status");
49
- const __1 = require("..");
50
- const ui_1 = __importDefault(require("../../../../../engine/cli/ui"));
51
- class MigrationRunner {
52
- // Scan (to generate status)
53
- static async scanFiles(daemon, migrations_dir) {
54
- const modules = daemon_1.Daemon.getModules(daemon);
55
- // Read migration files of each module
56
- const files = [];
57
- for (const module of modules) {
58
- const modulepath = path.join('modules', module.name, migrations_dir);
59
- if (!fs.existsSync(modulepath))
60
- continue;
61
- fs.readdirSync(modulepath, { withFileTypes: true })
62
- .forEach(node => {
63
- const nodePath = path.resolve(modulepath, node.name);
64
- if (nodePath.endsWith('.d.ts')) {
65
- return;
66
- }
67
- files.push({
68
- module: module.name,
69
- name: node.name,
70
- path: nodePath
71
- });
72
- });
73
- }
74
- // Extract migration routine of each file
75
- const migrationFiles = [];
76
- for (const file of files) {
77
- const { default: routine } = await Promise.resolve(`${file.path}`).then(s => __importStar(require(s)));
78
- if (routine instanceof __1.MigrationRoutine) {
79
- const name = file.name.replace(/'.[t|j]s'/, '');
80
- migrationFiles.push({
81
- ...file,
82
- name,
83
- routine
84
- });
85
- }
86
- else {
87
- log_1.Log.warn('migrator', 'scan', `File at ${file.path} doesn't appear to be a migration. Skipping it.`);
88
- }
89
- }
90
- return migrationFiles;
91
- }
92
- static async scanRows(sql) {
93
- const db = await sql `
94
- SELECT * FROM ${sql(_a.MIGRATION_TABLE_NAME)}
95
- ORDER BY id
96
- `;
97
- return db;
98
- }
99
- static async status(daemon, sql, migrations_dir) {
100
- const migrationFiles = await _a.scanFiles(daemon, migrations_dir);
101
- const migrationRows = await _a.scanRows(sql);
102
- return new status_1.MigrationRunnerStatus(migrationFiles, migrationRows);
103
- }
104
- // Public Up / Down
105
- static async up(daemon, sql, mode = 'one', dirpath = 'migrations') {
106
- let status = await _a.status(daemon, sql, dirpath);
107
- console.log(status.describe());
108
- const pending = status.items.filter(item => item.state === 'pending');
109
- if (!pending.length) {
110
- log_1.Log.info('migrator', 'up', 'No migrations to run.');
111
- return;
112
- }
113
- const n = (mode === 'one' ? 1 : pending.length).toString();
114
- const confirm = await ui_1.default.yesOrNo(`Run ${(0, string_1.colored)(n, 'green')} migration(s) ${(0, string_1.colored)('▲ UP', 'lightgreen')}?`);
115
- if (!confirm) {
116
- return;
117
- }
118
- await sql.begin(async (sql) => {
119
- if (mode === 'one') {
120
- const migration = pending[0];
121
- await this.migrateUp(daemon, sql, migration, status.batch + 1);
122
- }
123
- else {
124
- for (const migration of pending) {
125
- await this.migrateUp(daemon, sql, migration, status.batch + 1);
126
- }
127
- }
128
- });
129
- status = await _a.status(daemon, sql, dirpath);
130
- console.log(status.describe());
131
- }
132
- static async down(daemon, sql, mode = 'one', dirpath = 'migrations') {
133
- let status = await _a.status(daemon, sql, dirpath);
134
- console.log(status.describe());
135
- const lastBatch = status.items.filter(item => item.batch === status.batch);
136
- if (!lastBatch.length) {
137
- log_1.Log.info('migrator', 'down', 'No migrations to rollback.');
138
- return;
139
- }
140
- const n = mode === 'one' ? 'one' : 'last batch of';
141
- const confirm = await ui_1.default.yesOrNo(`Rollback ${(0, string_1.colored)(n, 'green')} migration(s) ${(0, string_1.colored)('▼ DOWN', 'red')}?`);
142
- if (!confirm) {
143
- return;
144
- }
145
- await sql.begin(async (sql) => {
146
- if (mode === 'one') {
147
- const migration = lastBatch.at(-1);
148
- await this.migrateDown(daemon, sql, migration);
149
- }
150
- else {
151
- for (const migration of lastBatch) {
152
- await this.migrateDown(daemon, sql, migration);
153
- }
154
- }
155
- });
156
- status = await _a.status(daemon, sql, dirpath);
157
- console.log(status.describe());
158
- }
159
- // Implementation Up/Down
160
- static async migrateUp(daemon, sql, migration, batch) {
161
- log_1.Log.info('migrator', 'up', `Running migration ${(0, string_1.colored)('▲ UP', 'lightgreen')} ${(0, string_1.colored)(migration.name, 'lightblue')}`);
162
- const status = await daemon.trx(migration.module)
163
- .run(async (trx) => {
164
- trx_1.Trx.set(trx, 'sql', sql);
165
- await migration.routine.up({
166
- sql,
167
- trx
168
- });
169
- });
170
- if (status.state !== 'ok') {
171
- throw new Error('Migration failed. Rolling back all batch changes.');
172
- }
173
- const row = {
174
- module: migration.module,
175
- name: migration.name,
176
- description: migration.routine.description,
177
- batch,
178
- timestamp: datetime_1.NesoiDatetime.now(),
179
- hash: migration.hash || null
180
- };
181
- if (migration.description) {
182
- row.description = migration.description;
183
- }
184
- await sql `
185
- INSERT INTO ${sql(_a.MIGRATION_TABLE_NAME)}
186
- ${sql(row)}
187
- `;
188
- }
189
- static async migrateDown(daemon, sql, migration) {
190
- const name = (0, string_1.colored)(migration.name, 'lightblue');
191
- log_1.Log.info('migrator', 'up', `Running migration ${(0, string_1.colored)('▼ DOWN', 'yellow')} ${name}`);
192
- if (migration.state === 'lost') {
193
- const del = await ui_1.default.yesOrNo(`The migration ${name} is ${(0, string_1.colored)('lost', 'red')}, skip and delete it? ${(0, string_1.colored)('Warning: this might cause inconsistencies', 'red')}.`);
194
- if (!del) {
195
- throw new Error(`Migration ${migration.name} was lost, unable to migrate down.`);
196
- }
197
- }
198
- else {
199
- const status = await daemon.trx(migration.module)
200
- .run(async (trx) => {
201
- trx_1.Trx.set(trx, 'sql', sql);
202
- await migration.routine.down({
203
- sql,
204
- trx
205
- });
206
- });
207
- if (status.state !== 'ok') {
208
- throw new Error('Migration failed. Rolling back all batch changes.');
209
- }
210
- }
211
- await sql `
212
- DELETE FROM ${sql(_a.MIGRATION_TABLE_NAME)}
213
- WHERE id = ${migration.id}
214
- `;
215
- }
216
- }
217
- exports.MigrationRunner = MigrationRunner;
218
- _a = MigrationRunner;
219
- MigrationRunner.MIGRATION_TABLE_NAME = '__nesoi_migrations';
220
- MigrationRunner.fromSchema = {
221
- up: async (daemon, sql, migration, dirpath = 'migrations') => {
222
- let status = await _a.status(daemon, sql, dirpath);
223
- console.log(status.describe());
224
- const routine = new __1.MigrationRoutine({
225
- description: migration.description,
226
- up: async ($) => {
227
- for (const sql of migration.sqlUp()) {
228
- await $.sql.unsafe(sql);
229
- }
230
- },
231
- down: async ($) => {
232
- for (const sql of migration.sqlDown()) {
233
- await $.sql.unsafe(sql);
234
- }
235
- }
236
- });
237
- const mig = {
238
- ...migration,
239
- state: 'pending',
240
- hash: routine.hash,
241
- routine
242
- };
243
- await sql.begin(async (sql) => {
244
- await _a.migrateUp(daemon, sql, mig, status.batch + 1);
245
- });
246
- status = await _a.status(daemon, sql, dirpath);
247
- console.log(status.describe());
248
- }
249
- };