miqro 2.0.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +18 -36
  2. package/build/cli.js +6 -0
  3. package/build/cmd-map.d.ts +79 -0
  4. package/{dist/cli.js → build/cmd-map.js} +22 -49
  5. package/{dist → build}/cmds/doc-json.d.ts +1 -1
  6. package/{dist → build}/cmds/doc-json.js +3 -3
  7. package/build/cmds/doc-md.d.ts +2 -0
  8. package/build/cmds/doc-md.js +17 -0
  9. package/{dist → build}/cmds/handler-main-new.js +21 -15
  10. package/build/cmds/help.d.ts +1 -0
  11. package/build/cmds/help.js +9 -0
  12. package/{dist → build}/cmds/new-test.js +13 -2
  13. package/{dist → build}/cmds/new.js +20 -15
  14. package/{dist → build}/cmds/serve.d.ts +1 -1
  15. package/{dist → build}/cmds/serve.js +12 -7
  16. package/build/utils/doc/json.d.ts +6 -0
  17. package/{dist/utils/doc/index.js → build/utils/doc/json.js} +2 -3
  18. package/build/utils/doc/md.d.ts +18 -0
  19. package/build/utils/doc/md.js +129 -0
  20. package/{dist → build}/utils/index.d.ts +7 -4
  21. package/{dist → build}/utils/index.js +20 -14
  22. package/package.json +13 -14
  23. package/dist/cmds/db-console.d.ts +0 -2
  24. package/dist/cmds/db-console.js +0 -38
  25. package/dist/cmds/db-createmodel.d.ts +0 -2
  26. package/dist/cmds/db-createmodel.js +0 -37
  27. package/dist/cmds/db-dump-data.d.ts +0 -2
  28. package/dist/cmds/db-dump-data.js +0 -43
  29. package/dist/cmds/db-init.d.ts +0 -2
  30. package/dist/cmds/db-init.js +0 -38
  31. package/dist/cmds/db-makemigrations.d.ts +0 -2
  32. package/dist/cmds/db-makemigrations.js +0 -14
  33. package/dist/cmds/db-migrate.d.ts +0 -2
  34. package/dist/cmds/db-migrate.js +0 -12
  35. package/dist/cmds/db-push-data.d.ts +0 -2
  36. package/dist/cmds/db-push-data.js +0 -79
  37. package/dist/cmds/db-sync-makemigrations.d.ts +0 -2
  38. package/dist/cmds/db-sync-makemigrations.js +0 -14
  39. package/dist/cmds/test.d.ts +0 -2
  40. package/dist/cmds/test.js +0 -15
  41. package/dist/utils/db/automigrations/index.d.ts +0 -2
  42. package/dist/utils/db/automigrations/index.js +0 -121
  43. package/dist/utils/db/automigrations/migrate.d.ts +0 -45
  44. package/dist/utils/db/automigrations/migrate.js +0 -750
  45. package/dist/utils/db/index.d.ts +0 -20
  46. package/dist/utils/db/index.js +0 -127
  47. package/dist/utils/doc/index.d.ts +0 -7
  48. /package/{dist → build}/cli.d.ts +0 -0
  49. /package/{dist → build}/cmds/config-bash.d.ts +0 -0
  50. /package/{dist → build}/cmds/config-bash.js +0 -0
  51. /package/{dist → build}/cmds/config-env.d.ts +0 -0
  52. /package/{dist → build}/cmds/config-env.js +0 -0
  53. /package/{dist → build}/cmds/config-init.d.ts +0 -0
  54. /package/{dist → build}/cmds/config-init.js +0 -0
  55. /package/{dist → build}/cmds/config.d.ts +0 -0
  56. /package/{dist → build}/cmds/config.js +0 -0
  57. /package/{dist → build}/cmds/handler-apiroute-new.d.ts +0 -0
  58. /package/{dist → build}/cmds/handler-apiroute-new.js +0 -0
  59. /package/{dist → build}/cmds/handler-main-new.d.ts +0 -0
  60. /package/{dist → build}/cmds/new-test.d.ts +0 -0
  61. /package/{dist → build}/cmds/new.d.ts +0 -0
  62. /package/{dist → build}/cmds/start.d.ts +0 -0
  63. /package/{dist → build}/cmds/start.js +0 -0
  64. /package/{dist → build}/cmds/watch.d.ts +0 -0
  65. /package/{dist → build}/cmds/watch.js +0 -0
  66. /package/{dist → build}/cmds/wc-new.d.ts +0 -0
  67. /package/{dist → build}/cmds/wc-new.js +0 -0
  68. /package/{dist → build}/index.d.ts +0 -0
  69. /package/{dist → build}/index.js +0 -0
  70. /package/{dist → build}/utils/templates.d.ts +0 -0
  71. /package/{dist → build}/utils/templates.js +0 -0
@@ -1,750 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writeMigration = exports.getMigration = exports.sortActions = exports.parseDifference = exports.reverseModels = void 0;
4
- const deep_diff_1 = require("deep-diff");
5
- const fs_1 = require("fs");
6
- const path_1 = require("path");
7
- /* tslint:disable */
8
- const reverseSequelizeColType = (DataTypes, col, prefix = "Sequelize.") => {
9
- const attrName = col.type.key;
10
- const attrObj = col.type;
11
- const options = (col.type.options) ? col.type.options : {};
12
- // noinspection SpellCheckingInspection,DuplicateCaseLabelJS
13
- switch (attrName) {
14
- // CHAR(length, binary)
15
- case DataTypes.CHAR.key:
16
- if (options.binary) {
17
- return prefix + "CHAR.BINARY";
18
- }
19
- return prefix + "CHAR(" + options.length + ")";
20
- // STRING(length, binary).BINARY
21
- case DataTypes.STRING.key:
22
- return prefix + "STRING" + ((options.length) ? "(" + options.length + ")" : "") +
23
- ((options.binary) ? ".BINARY" : "");
24
- // TEXT(length)
25
- case DataTypes.TEXT.key:
26
- if (!options.length) {
27
- return prefix + "TEXT";
28
- }
29
- return prefix + "TEXT(" + options.length.toLowerCase() + ")";
30
- // NUMBER(length, decimals).UNSIGNED.ZEROFILL
31
- case DataTypes.NUMBER.key:
32
- case DataTypes.TINYINT.key:
33
- case DataTypes.SMALLINT.key:
34
- case DataTypes.MEDIUMINT.key:
35
- case DataTypes.BIGINT.key:
36
- case DataTypes.FLOAT.key:
37
- case DataTypes.REAL.key:
38
- case DataTypes.DOUBLE.key:
39
- case DataTypes.DECIMAL.key:
40
- case DataTypes.INTEGER.key: {
41
- let ret = attrName;
42
- if (options.length) {
43
- ret += "(" + options.length;
44
- if (options.decimals) {
45
- ret += ", " + options.decimals;
46
- }
47
- ret += ")";
48
- }
49
- if (options.precision) {
50
- ret += "(" + options.precision;
51
- if (options.scale) {
52
- ret += ", " + options.scale;
53
- }
54
- ret += ")";
55
- }
56
- ret = [ret];
57
- if (options.zerofill) {
58
- ret.push("ZEROFILL");
59
- }
60
- if (options.unsigned) {
61
- ret.push("UNSIGNED");
62
- }
63
- return prefix + ret.join(".");
64
- }
65
- case DataTypes.ENUM.key:
66
- return prefix + "ENUM('" + options.values.join("', '") + "')";
67
- case DataTypes.BLOB.key:
68
- if (!options.length) {
69
- return prefix + "BLOB";
70
- }
71
- return prefix + "BLOB(" + options.length.toLowerCase() + ")";
72
- case DataTypes.GEOMETRY.key:
73
- if (options.type) {
74
- if (options.srid) {
75
- return prefix + "GEOMETRY('" + options.type + "', " + options.srid + ")";
76
- }
77
- else {
78
- return prefix + "GEOMETRY('" + options.type + "')";
79
- }
80
- }
81
- return prefix + "GEOMETRY";
82
- case DataTypes.GEOGRAPHY.key:
83
- return prefix + "GEOGRAPHY";
84
- case DataTypes.ARRAY.key:
85
- const _type = attrObj.toString();
86
- let arrayType;
87
- if (_type === "INTEGER[]" || _type === "STRING[]") {
88
- arrayType = prefix + _type.replace("[]", "");
89
- }
90
- else {
91
- arrayType = (col.seqType === "Sequelize.ARRAY(Sequelize.INTEGER)") ? prefix + "INTEGER" : prefix + "STRING";
92
- }
93
- return prefix + `ARRAY(${arrayType})`;
94
- case DataTypes.RANGE.key:
95
- console.warn(attrName + " type not supported, you should make it by");
96
- return prefix + attrObj.toSql();
97
- // BOOLEAN
98
- // TIME
99
- // DATE
100
- // DATEONLY
101
- // HSTORE
102
- // JSONB
103
- // UUID
104
- // UUIDV1
105
- // UUIDV4
106
- // VIRTUAL
107
- // INET
108
- // MACADDR
109
- default:
110
- return prefix + attrName;
111
- }
112
- };
113
- const reverseSequelizeDefValueType = (defaultValue, prefix = "Sequelize.") => {
114
- if (typeof defaultValue === "object") {
115
- if (defaultValue.constructor && defaultValue.constructor.name) {
116
- return { internal: true, value: prefix + defaultValue.constructor.name };
117
- }
118
- }
119
- if (typeof defaultValue === "function") {
120
- return { notSupported: true, value: "" };
121
- }
122
- return { value: defaultValue };
123
- };
124
- const parseIndex = (idx) => {
125
- delete idx.parser;
126
- if (idx.type == "") {
127
- delete idx.type;
128
- }
129
- const options = {};
130
- if (idx.name) {
131
- options.name = options.indexName = idx.name;
132
- } // The name of the index. Default is __
133
- // @todo: UNIQUE|FULLTEXT|SPATIAL
134
- if (idx.unique) {
135
- options.type = options.indicesType = "UNIQUE";
136
- }
137
- if (idx.method) {
138
- options.indexType = idx.type;
139
- } // Set a type for the index, e.g. BTREE. See the documentation of the used dialect
140
- if (idx.parser && idx.parser != "") {
141
- options.parser = idx.parser;
142
- } // For FULLTEXT columns set your parser
143
- idx.options = options;
144
- idx.hash = JSON.stringify(idx);
145
- // log ('PI:', JSON.stringify(idx, null, 4));
146
- return idx;
147
- };
148
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
149
- const reverseModels = (sequelizeModule, sequelize, realModels, logger) => {
150
- const models = {};
151
- for (const model in realModels) {
152
- if (model !== "default") {
153
- models[model] = {
154
- tableName: realModels[model].tableName,
155
- options: {
156
- ...realModels[model].options
157
- },
158
- rawAttributes: {
159
- ...realModels[model].rawAttributes
160
- }
161
- };
162
- }
163
- }
164
- const tables = {};
165
- delete models.default;
166
- for (const model in models) {
167
- // noinspection JSUnfilteredForInLoop
168
- const attributes = models[model].rawAttributes;
169
- for (const column in attributes) {
170
- // noinspection JSUnfilteredForInLoop
171
- delete attributes[column].Model;
172
- // noinspection JSUnfilteredForInLoop
173
- delete attributes[column].fieldName;
174
- // delete attributes[column].field;
175
- // noinspection JSUnfilteredForInLoop
176
- for (const property in attributes[column]) {
177
- // noinspection JSUnfilteredForInLoop
178
- if (property.startsWith("_")) {
179
- // noinspection JSUnfilteredForInLoop
180
- delete attributes[column][property];
181
- continue;
182
- }
183
- if (property === "defaultValue") {
184
- // noinspection JSUnfilteredForInLoop
185
- const _val = reverseSequelizeDefValueType(attributes[column][property]);
186
- if (_val.notSupported) {
187
- // noinspection JSUnfilteredForInLoop
188
- logger.info(`[Not supported] Skip defaultValue column of attribute ${model}:${column}`);
189
- // noinspection JSUnfilteredForInLoop
190
- delete attributes[column][property];
191
- continue;
192
- }
193
- // noinspection JSUnfilteredForInLoop
194
- attributes[column][property] = _val;
195
- }
196
- // remove getters, setters...
197
- // noinspection JSUnfilteredForInLoop
198
- if (typeof attributes[column][property] == "function") {
199
- // noinspection JSUnfilteredForInLoop
200
- delete attributes[column][property];
201
- }
202
- }
203
- // noinspection JSUnfilteredForInLoop
204
- if (typeof attributes[column].type === "undefined") {
205
- // noinspection JSUnfilteredForInLoop
206
- if (!attributes[column].seqType) {
207
- // noinspection JSUnfilteredForInLoop
208
- logger.info(`[Not supported] Skip column with undefined type ${model}:${column}`);
209
- // noinspection JSUnfilteredForInLoop
210
- delete attributes[column];
211
- continue;
212
- }
213
- else {
214
- // noinspection JSUnfilteredForInLoop
215
- if (!["Sequelize.ARRAY(Sequelize.INTEGER)", "Sequelize.ARRAY(Sequelize.STRING)"].includes(attributes[column].seqType)) {
216
- // noinspection JSUnfilteredForInLoop
217
- delete attributes[column];
218
- continue;
219
- }
220
- // noinspection JSUnfilteredForInLoop
221
- attributes[column].type = {
222
- key: sequelizeModule.ARRAY.key
223
- };
224
- }
225
- }
226
- // noinspection JSUnfilteredForInLoop
227
- let seqType = reverseSequelizeColType(sequelizeModule.DataTypes, attributes[column]);
228
- // NO virtual types in migration
229
- if (seqType === "Sequelize.VIRTUAL") {
230
- // noinspection JSUnfilteredForInLoop
231
- logger.info(`[SKIP] Skip Sequelize.VIRTUAL column "${column}"", defined in model "${model}"`);
232
- // noinspection JSUnfilteredForInLoop
233
- delete attributes[column];
234
- continue;
235
- }
236
- if (!seqType) {
237
- // noinspection JSUnfilteredForInLoop
238
- if (typeof attributes[column].type.options !== "undefined" && typeof attributes[column].type.options.toString === "function") {
239
- // noinspection JSUnfilteredForInLoop
240
- seqType = attributes[column].type.options.toString(sequelize);
241
- }
242
- // noinspection JSUnfilteredForInLoop
243
- if (typeof attributes[column].type.toString === "function") {
244
- // noinspection JSUnfilteredForInLoop
245
- seqType = attributes[column].type.toString(sequelize);
246
- }
247
- }
248
- // noinspection JSUnfilteredForInLoop
249
- attributes[column].seqType = seqType;
250
- // noinspection JSUnfilteredForInLoop
251
- delete attributes[column].type;
252
- // noinspection JSUnfilteredForInLoop
253
- delete attributes[column].values; // ENUM
254
- }
255
- // noinspection JSUnfilteredForInLoop
256
- tables[models[model].tableName] = {
257
- // noinspection JSUnfilteredForInLoop
258
- tableName: models[model].tableName,
259
- schema: attributes
260
- };
261
- // noinspection JSUnfilteredForInLoop
262
- if (models[model].options.indexes.length > 0) {
263
- const idxOut = {};
264
- // noinspection JSUnfilteredForInLoop
265
- for (const _i in models[model].options.indexes) {
266
- // noinspection JSUnfilteredForInLoop
267
- const index = parseIndex(models[model].options.indexes[_i]);
268
- idxOut[index.hash + ""] = index;
269
- delete index.hash;
270
- // make it immutable
271
- Object.freeze(index);
272
- }
273
- // noinspection JSUnfilteredForInLoop
274
- // models[model].options.indexes = idxOut;
275
- tables[models[model].tableName].indexes = idxOut;
276
- }
277
- // noinspection JSUnfilteredForInLoop
278
- if (typeof models[model].options.charset !== "undefined") {
279
- // noinspection JSUnfilteredForInLoop
280
- tables[models[model].tableName].charset = models[model].options.charset;
281
- }
282
- // noinspection JSUnfilteredForInLoop
283
- tables[models[model].tableName].indexes = models[model].options.indexes;
284
- }
285
- return tables;
286
- };
287
- exports.reverseModels = reverseModels;
288
- const parseDifference = (previousState, currentState, logger) => {
289
- // log(JSON.stringify(currentState, null, 4));
290
- const actions = [];
291
- const difference = (0, deep_diff_1.diff)(previousState, currentState);
292
- if (difference) {
293
- for (const df of difference) {
294
- // log (JSON.stringify(df, null, 4));
295
- switch (df.kind) {
296
- // add new
297
- case "N": {
298
- // new table created
299
- if (df.path && df.path.length === 1) {
300
- const depends = [];
301
- const tableName = df.rhs.tableName;
302
- if (df.rhs.schema) {
303
- Object.keys(df.rhs.schema).forEach((v) => {
304
- if (v.references) {
305
- depends.push(v.references.model);
306
- }
307
- });
308
- }
309
- const options = {};
310
- if (typeof df.rhs.charset !== "undefined") {
311
- options.charset = df.rhs.charset;
312
- }
313
- actions.push({
314
- actionType: "createTable",
315
- tableName,
316
- attributes: df.rhs.schema,
317
- options,
318
- depends
319
- });
320
- // create indexes
321
- if (df.rhs.indexes) {
322
- for (const _i in df.rhs.indexes) {
323
- // noinspection JSUnfilteredForInLoop
324
- actions.push({
325
- ...{
326
- actionType: "addIndex",
327
- tableName,
328
- depends: [tableName]
329
- }, ...{ ...df.rhs.indexes[_i] }
330
- });
331
- }
332
- }
333
- break;
334
- }
335
- const tableName = df.path && df.path[0];
336
- const depends = [tableName];
337
- if (df.path && df.path[1] === "schema") {
338
- // if (df.path.length === 3) - new field
339
- if (df.path.length === 3) {
340
- // new field
341
- if (df.rhs && df.rhs.references) {
342
- depends.push(df.rhs.references.model);
343
- }
344
- actions.push({
345
- actionType: "addColumn",
346
- tableName,
347
- attributeName: df.path[2],
348
- options: df.rhs,
349
- depends
350
- });
351
- break;
352
- }
353
- // if (df.path.length > 3) - add new attribute to column (change col)
354
- if (df.path && df.path.length > 3) {
355
- if (df.path[1] === "schema") {
356
- // new field attributes
357
- const options = currentState[tableName].schema[df.path[2]];
358
- if (options.references) {
359
- depends.push(options.references.nodel);
360
- }
361
- actions.push({
362
- actionType: "changeColumn",
363
- tableName,
364
- attributeName: df.path[2],
365
- options,
366
- depends
367
- });
368
- break;
369
- }
370
- }
371
- }
372
- // new index
373
- if (df.path && df.path[1] === "indexes") {
374
- const tableName = df.path[0];
375
- const index = {
376
- ...df.rhs,
377
- actionType: "addIndex",
378
- tableName: tableName,
379
- depends: "addIndex",
380
- };
381
- index.depends = [tableName];
382
- actions.push(index);
383
- break;
384
- }
385
- break;
386
- }
387
- // drop
388
- case "D": {
389
- const tableName = df.path && df.path[0];
390
- const depends = [tableName];
391
- if (df.path && df.path.length === 1) {
392
- // drop table
393
- actions.push({
394
- actionType: "dropTable",
395
- tableName,
396
- depends: []
397
- });
398
- break;
399
- }
400
- if (df.path && df.path[1] === "schema") {
401
- // if (df.path.length === 3) - drop field
402
- if (df.path.length === 3) {
403
- // drop column
404
- actions.push({
405
- actionType: "removeColumn",
406
- tableName,
407
- columnName: df.path[2],
408
- depends: [tableName],
409
- options: df.lhs
410
- });
411
- break;
412
- }
413
- // if (df.path.length > 3) - drop attribute from column (change col)
414
- if (df.path.length > 3) {
415
- // new field attributes
416
- const options = currentState[tableName].schema[df.path[2]];
417
- if (options.references) {
418
- depends.push(options.references.nodel);
419
- }
420
- actions.push({
421
- actionType: "changeColumn",
422
- tableName,
423
- attributeName: df.path[2],
424
- options,
425
- depends
426
- });
427
- break;
428
- }
429
- }
430
- if (df.path && df.path[1] === "indexes") {
431
- // log(df)
432
- actions.push({
433
- actionType: "removeIndex",
434
- tableName,
435
- fields: df.lhs.fields,
436
- options: df.lhs.options,
437
- depends: [tableName]
438
- });
439
- break;
440
- }
441
- break;
442
- }
443
- // edit
444
- case "E": {
445
- const tableName = df.path && df.path[0];
446
- const depends = [tableName];
447
- if (df.path && df.path[1] === "schema") {
448
- // new field attributes
449
- const options = currentState[tableName].schema[df.path[2]];
450
- if (options.references) {
451
- depends.push(options.references.nodel);
452
- }
453
- actions.push({
454
- actionType: "changeColumn",
455
- tableName,
456
- attributeName: df.path[2],
457
- options,
458
- depends
459
- });
460
- }
461
- // updated index
462
- // only support updating and dropping indexes
463
- if (df.path && df.path[1] === "indexes") {
464
- const tableName = df.path && df.path[0];
465
- let keys = Object.keys(df.rhs);
466
- // noinspection LoopStatementThatDoesntLoopJS
467
- for (const k in keys) {
468
- const key = keys[k];
469
- // noinspection JSUnusedLocalSymbols
470
- actions.push({
471
- actionType: "addIndex",
472
- tableName,
473
- fields: df.rhs[key].fields,
474
- options: df.rhs[key].options,
475
- depends: [tableName]
476
- });
477
- break;
478
- }
479
- keys = Object.keys(df.lhs);
480
- // noinspection LoopStatementThatDoesntLoopJS
481
- for (const k in keys) {
482
- const key = keys[k];
483
- // noinspection JSUnusedLocalSymbols
484
- actions.push({
485
- actionType: "removeIndex",
486
- tableName,
487
- fields: df.lhs[key].fields,
488
- options: df.lhs[key].options,
489
- depends: [tableName]
490
- });
491
- break;
492
- }
493
- }
494
- break;
495
- }
496
- // array change indexes
497
- case "A": {
498
- logger.info("[Not supported] Array model changes! Problems are possible. Please, check result more carefully!");
499
- logger.info("[Not supported] Difference: ");
500
- logger.info(JSON.stringify(df, null, 4));
501
- break;
502
- }
503
- default:
504
- // code
505
- break;
506
- }
507
- }
508
- }
509
- return actions;
510
- };
511
- exports.parseDifference = parseDifference;
512
- const sortActions = (actions) => {
513
- const orderedActionTypes = [
514
- "removeIndex",
515
- "removeColumn",
516
- "dropTable",
517
- "createTable",
518
- "addColumn",
519
- "changeColumn",
520
- "addIndex"
521
- ];
522
- // test
523
- // actions = shuffleArray(actions);
524
- actions.sort((a, b) => {
525
- if (orderedActionTypes.indexOf(a.actionType) < orderedActionTypes.indexOf(b.actionType)) {
526
- return -1;
527
- }
528
- if (orderedActionTypes.indexOf(a.actionType) > orderedActionTypes.indexOf(b.actionType)) {
529
- return 1;
530
- }
531
- if (a.depends.length === 0 && b.depends.length > 0) {
532
- return -1;
533
- } // a < b
534
- if (b.depends.length === 0 && a.depends.length > 0) {
535
- return 1;
536
- } // b < a
537
- return 0;
538
- });
539
- for (let k = 0; k <= actions.length; k++) {
540
- for (let i = 0; i < actions.length; i++) {
541
- if (!actions[i].depends) {
542
- continue;
543
- }
544
- if (actions[i].depends.length === 0) {
545
- continue;
546
- }
547
- const a = actions[i];
548
- for (let j = 0; j < actions.length; j++) {
549
- if (!actions[j].depends) {
550
- continue;
551
- }
552
- if (actions[j].depends.length === 0) {
553
- continue;
554
- }
555
- const b = actions[j];
556
- if (a.actionType != b.actionType) {
557
- continue;
558
- }
559
- if (b.depends.indexOf(a.tableName) !== -1 && i > j) {
560
- const c = actions[i];
561
- actions[i] = actions[j];
562
- actions[j] = c;
563
- }
564
- }
565
- }
566
- }
567
- };
568
- exports.sortActions = sortActions;
569
- // noinspection SpellCheckingInspection
570
- const getMigration = (actions) => {
571
- const propertyToStr = (obj) => {
572
- // noinspection SpellCheckingInspection
573
- const vals = [];
574
- for (const k in obj) {
575
- if (k === "seqType") {
576
- // noinspection JSUnfilteredForInLoop
577
- vals.push('"type": ' + obj[k]);
578
- continue;
579
- }
580
- if (k === "defaultValue") {
581
- // noinspection JSUnfilteredForInLoop
582
- if (obj[k].internal) {
583
- // noinspection JSUnfilteredForInLoop
584
- vals.push('"defaultValue": ' + obj[k].value);
585
- continue;
586
- }
587
- // noinspection JSUnfilteredForInLoop
588
- if (obj[k].notSupported) {
589
- continue;
590
- }
591
- const x = {};
592
- // noinspection JSUnfilteredForInLoop
593
- x[k] = obj[k].value;
594
- vals.push(JSON.stringify(x).slice(1, -1));
595
- continue;
596
- }
597
- const x = {};
598
- // noinspection JSUnfilteredForInLoop
599
- x[k] = obj[k];
600
- vals.push(JSON.stringify(x).slice(1, -1));
601
- }
602
- return "{ " + vals.reverse().join(", ") + " }";
603
- };
604
- const getAttributes = (attrs) => {
605
- const ret = [];
606
- for (const attrName in attrs) {
607
- // noinspection JSUnfilteredForInLoop
608
- ret.push(` "${attrName}": ${propertyToStr(attrs[attrName])}`);
609
- }
610
- return " { \n" + ret.join(", \n") + "\n }";
611
- };
612
- const commandsUp = [];
613
- const consoleOut = [];
614
- for (const _i in actions) {
615
- // noinspection JSUnfilteredForInLoop
616
- const action = actions[_i];
617
- // noinspection FallThroughInSwitchStatementJS,FallThroughInSwitchStatementJS
618
- switch (action.actionType) {
619
- case "createTable":
620
- {
621
- const resUp = `{ fn: "createTable", params: [
622
- "${action.tableName}",
623
- ${getAttributes(action.attributes)},
624
- ${JSON.stringify(action.options)}
625
- ] }`;
626
- commandsUp.push(resUp);
627
- consoleOut.push(`createTable "${action.tableName}", deps: [${action.depends.join(", ")}]`);
628
- }
629
- break;
630
- case "dropTable":
631
- {
632
- const res = `{ fn: "dropTable", params: ["${action.tableName}"] }`;
633
- commandsUp.push(res);
634
- consoleOut.push(`dropTable "${action.tableName}"`);
635
- }
636
- break;
637
- case "addColumn":
638
- {
639
- const resUp = `{ fn: "addColumn", params: [
640
- "${action.tableName}",
641
- "${(action.options && action.options.field) ? action.options.field : action.attributeName}",
642
- ${propertyToStr(action.options)}
643
- ] }`;
644
- commandsUp.push(resUp);
645
- consoleOut.push(`addColumn "${action.attributeName}" to table "${action.tableName}"`);
646
- }
647
- break;
648
- case "removeColumn":
649
- {
650
- const res = `{ fn: "removeColumn", params: ["${action.tableName}", "${(action.options && action.options.field) ? action.options.field : action.columnName}"] }`;
651
- commandsUp.push(res);
652
- consoleOut.push(`removeColumn "${(action.options && action.options.field) ? action.options.field : action.columnName}" from table "${action.tableName}"`);
653
- }
654
- break;
655
- case "changeColumn":
656
- {
657
- const res = `{ fn: "changeColumn", params: [
658
- "${action.tableName}",
659
- "${(action.options && action.options.field) ? action.options.field : action.attributeName}",
660
- ${propertyToStr(action.options)}
661
- ] }`;
662
- commandsUp.push(res);
663
- consoleOut.push(`changeColumn "${action.attributeName}" on table "${action.tableName}"`);
664
- }
665
- break;
666
- case "addIndex":
667
- {
668
- const res = `{ fn: "addIndex", params: [
669
- "${action.tableName}",
670
- ${JSON.stringify(action.fields)},
671
- ${JSON.stringify(action.options)}
672
- ] }`;
673
- commandsUp.push(res);
674
- const nameOrAttrs = (action.options && action.options.indexName && action.options.indexName != "") ? `"${action.options.indexName}"` : JSON.stringify(action.fields);
675
- consoleOut.push(`addIndex ${nameOrAttrs} to table "${action.tableName}"`);
676
- }
677
- break;
678
- case "removeIndex": {
679
- // log(action)
680
- const nameOrAttrs = (action.options && action.options.indexName && action.options.indexName != "") ? `"${action.options.indexName}"` : JSON.stringify(action.fields);
681
- const res = `{ fn: "removeIndex", params: [
682
- "${action.tableName}",
683
- ${nameOrAttrs}
684
- ] }`;
685
- commandsUp.push(res);
686
- consoleOut.push(`removeIndex ${nameOrAttrs} from table "${action.tableName}"`);
687
- }
688
- default:
689
- // code
690
- }
691
- }
692
- return { commandsUp, consoleOut };
693
- };
694
- exports.getMigration = getMigration;
695
- const writeMigration = (revision, migration, migrationsDir, name = "", comment = "") => {
696
- const _commands = "var migrationCommands = [ \n" + migration.commandsUp.join(", \n") + " \n];\n";
697
- const _actions = " * " + migration.consoleOut.join("\n * ");
698
- //_commands = js_beautify(_commands);
699
- const info = {
700
- revision,
701
- name,
702
- created: new Date(),
703
- comment
704
- };
705
- const template = `'use strict';
706
-
707
- const Sequelize = require('sequelize');
708
-
709
- /**
710
- * Actions summary:
711
- *
712
- ${_actions}
713
- *
714
- **/
715
-
716
- const info = ${JSON.stringify(info, null, 4)};
717
-
718
- ${_commands}
719
-
720
- module.exports = {
721
- pos: 0,
722
- up: function(queryInterface, Sequelize)
723
- {
724
- var index = this.pos;
725
- return new Promise(function(resolve, reject) {
726
- function next() {
727
- if (index < migrationCommands.length)
728
- {
729
- let command = migrationCommands[index];
730
- console.log("[#"+index+"] execute: " + command.fn);
731
- index++;
732
- queryInterface[command.fn].apply(queryInterface, command.params).then(next, reject);
733
- }
734
- else
735
- resolve();
736
- }
737
- next();
738
- });
739
- },
740
- info: info
741
- };
742
- `;
743
- name = name.replace(" ", "_");
744
- const now = new Date();
745
- const timestamp = `${now.getFullYear()}${now.getMonth() + 1 < 10 ? "0" : ""}${now.getMonth() + 1}${now.getDate() < 10 ? "0" : ""}${now.getDate()}${now.getHours() < 10 ? "0" : ""}${now.getHours()}${now.getMinutes() < 10 ? "0" : ""}${now.getMinutes()}${now.getSeconds() < 10 ? "0" : ""}${now.getSeconds()}-${revision}`;
746
- const filename = (0, path_1.join)(migrationsDir, timestamp + ((name != "") ? `-${name}` : "") + ".js");
747
- (0, fs_1.writeFileSync)(filename, template);
748
- return { filename, info };
749
- };
750
- exports.writeMigration = writeMigration;