-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1276 lines (1124 loc) · 62.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const fs = require('fs');
var util = require('util');
/**
* @namespace
* @property {object} Sequelize
* @property {function} Sequelize.BLOB
* @property {function} Sequelize.ENUM
* @property {function} Sequelize.STRING
* @property {function} Sequelize.STRING.BINARY
* @property {function} Sequelize.DATE
* @property {function} Sequelize.ARRAY
* @property {function} Sequelize.BOOLEAN
* @property {function} Sequelize.DOUBLE
* @property {function} Sequelize.FLOAT
* @property {function} Sequelize.INTEGER
* @property {function} Sequelize.BIGINT
*/
var Sequelize = require('sequelize');
var dialect = 'mysql';
/**
* @param {string} newDialect
* @returns {*}
*/
function setDialect(newDialect) {
if (['mysql', 'mariadb', 'sqlite', 'postgres', 'mssql'].indexOf(newDialect) === -1) {
throw new Error('Unknown sequalize dialect');
}
dialect = newDialect;
}
/**
*
* @param {Object|string} swaggerPropertySchema
* @param {Object} swaggerPropertySchema.properties
* @param {Object} swaggerPropertySchema.$ref
* @param {Array} swaggerPropertySchema.enum
* @param {string} swaggerPropertySchema.type
* @param {string} swaggerPropertySchema.format
* @param {Object|string} swaggerPropertySchema.items
* @returns {*}
*/
function getSequalizeType(swaggerPropertySchema) {
if (typeof swaggerPropertySchema === 'string') {
swaggerPropertySchema = {
type: swaggerPropertySchema
}
}
if (swaggerPropertySchema.properties) {
console.log('Warning: encountered', JSON.stringify(swaggerPropertySchema.properties));
console.log('Cannot handle complex subschemas (yet?), falling back to blob');
return Sequelize.BLOB;
}
if (swaggerPropertySchema.$ref) {
console.log('Warning: encountered', JSON.stringify(swaggerPropertySchema.$ref));
console.log('Cannot handle $ref (yet?), falling back to blob');
return Sequelize.BLOB;
}
if (swaggerPropertySchema.enum) {
return Sequelize.ENUM.apply(null, swaggerPropertySchema.enum);
}
// as seen http://swagger.io/specification/#dataTypeType
switch (swaggerPropertySchema.type) {
case 'string':
switch (swaggerPropertySchema.format || "") {
case 'byte':
case 'binary':
if (swaggerPropertySchema.maxLength > 5592415) {
return Sequelize.BLOB('long');
}
if (swaggerPropertySchema.maxLength > 21845) {
return Sequelize.BLOB('medium');
}
// NOTE: VARCHAR(255) may container 255 multibyte chars: it's _NOT_ byte delimited
if (swaggerPropertySchema.maxLength > 255) {
return Sequelize.BLOB();
}
return Sequelize.STRING.BINARY;
case 'date':
return Sequelize.DATEONLY;
case 'date-time':
//return Sequelize.DATETIME; //not working?
return Sequelize.DATE;
default:
if (swaggerPropertySchema.maxLength) {
// http://stackoverflow.com/questions/13932750/tinytext-text-mediumtext-and-longtext-maximum-sto
// http://stackoverflow.com/questions/7755629/varchar255-vs-tinytext-tinyblob-and-varchar65535-v
// NOTE: text may be in multibyte format!
if (swaggerPropertySchema.maxLength > 5592415) {
return Sequelize.TEXT('long');
}
if (swaggerPropertySchema.maxLength > 21845) {
return Sequelize.TEXT('medium');
}
// NOTE: VARCHAR(255) may container 255 multibyte chars: it's _NOT_ byte delimited
if (swaggerPropertySchema.maxLength > 255) {
return Sequelize.TEXT();
}
}
return Sequelize.STRING; // === VARCHAR
}
case 'array':
if (dialect === 'postgres') {
return Sequelize.ARRAY(getSequalizeType(swaggerPropertySchema.items));
}
console.log('Warning: encountered', JSON.stringify(swaggerPropertySchema));
console.log('Can only handle array for postgres (yet?), see http://docs.sequelizejs.com/en/latest/api/datatypes/#array, falling back to blob');
return Sequelize.BLOB;
case 'boolean':
return Sequelize.BOOLEAN;
case 'integer':
switch (swaggerPropertySchema.format || "") {
case 'int32':
if (typeof swaggerPropertySchema.minimum === "number" && swaggerPropertySchema.minimum >= 0) {
return Sequelize.INTEGER.UNSIGNED;
}
return Sequelize.INTEGER;
default:
if (typeof swaggerPropertySchema.minimum === "number" && swaggerPropertySchema.minimum >= 0) {
return Sequelize.BIGINT.UNSIGNED;
}
return Sequelize.BIGINT;
}
case 'number':
switch (swaggerPropertySchema.format || "") {
case 'float':
return Sequelize.FLOAT;
default:
return Sequelize.DOUBLE;
}
default:
console.log('Warning: encountered', JSON.stringify(swaggerPropertySchema));
console.log('Unknown data type, falling back to blob');
return Sequelize.BLOB;
}
}
/**
* This function returns the Sequelize type of the swagger property schema
* Because of natural behaviour of util.inspect function used to stringify the property we need to use tokens
* that will be removed afterward
* @param swaggerPropertySchema
* @returns {*}
*/
function getSequalizeTypeString(swaggerPropertySchema, logThis) {
if (typeof swaggerPropertySchema === 'string') {
swaggerPropertySchema = {
type: swaggerPropertySchema
}
}
if (swaggerPropertySchema.properties) {
console.log('Warning: encountered', JSON.stringify(swaggerPropertySchema.properties));
console.log('Cannot handle complex subschemas (yet?), falling back to blob');
return '##DataTypes.BLOB##';
}
if (swaggerPropertySchema.$ref) {
console.log('Warning: encountered', JSON.stringify(swaggerPropertySchema.$ref));
console.log('$ref means its a foreign key, it will be handle separatly');
console.log('Falling back to blob for now, whole property will be replaced in a method later');
return '##DataTypes.BLOB##';
}
if (swaggerPropertySchema.enum) {
let result = "";
for (let i = 0; i < swaggerPropertySchema.enum.length; i++){
result += "'" + swaggerPropertySchema.enum[i] + "', ";
}
result = result.substring(0, result.length - 2);
return '##DataTypes.ENUM(' + result + ')##';
}
// as seen http://swagger.io/specification/#dataTypeType
if (logThis){
console.log('swaggerPropertySchema => ', swaggerPropertySchema);
}
switch (swaggerPropertySchema.type) {
case 'string':
switch (swaggerPropertySchema.format || "") {
case 'byte':
case 'binary':
if (swaggerPropertySchema.maxLength > 5592415) {
return '##DataTypes.BLOB(\'long\')##';
}
if (swaggerPropertySchema.maxLength > 21845) {
return '##DataTypes.BLOB(\'medium\')##';
}
// NOTE: VARCHAR(255) may container 255 multibyte chars: it's _NOT_ byte delimited
if (swaggerPropertySchema.maxLength > 255) {
return '##DataTypes.BLOB##';
}
return '##DataTypes.STRING.BINARY##';
case 'date':
return '##DataTypes.DATEONLY##';
case 'date-time':
//return Sequelize.DATETIME; //not working?
return '##DataTypes.DATE##';
default:
if (swaggerPropertySchema.maxLength) {
// http://stackoverflow.com/questions/13932750/tinytext-text-mediumtext-and-longtext-maximum-sto
// http://stackoverflow.com/questions/7755629/varchar255-vs-tinytext-tinyblob-and-varchar65535-v
// NOTE: text may be in multibyte format!
if (swaggerPropertySchema.maxLength > 5592415) {
return '##DataTypes.TEXT(\'long\')##';
}
if (swaggerPropertySchema.maxLength > 21845) {
return '##DataTypes.TEXT(\'medium\')##';
}
// NOTE: VARCHAR(255) may container 255 multibyte chars: it's _NOT_ byte delimited
if (swaggerPropertySchema.maxLength > 255) {
return '##DataTypes.TEXT##';
}
}
return '##DataTypes.STRING##'; // === VARCHAR
}
case 'array':
if (dialect === 'postgres') {
return Sequelize.ARRAY(getSequalizeType(swaggerPropertySchema.items));
}
console.log('Warning: encountered', JSON.stringify(swaggerPropertySchema));
console.log('Arrays functionaly means its a 1-N relations, it will be handle separatly');
console.log('Falling back to blob for now, whole property will be replaced in a method later');
return '##DataTypes.ARRAY##';
case 'boolean':
return '##DataTypes.BOOLEAN##';
case 'integer':
switch (swaggerPropertySchema.format || "") {
case 'int32':
if (typeof swaggerPropertySchema.minimum === "number" && swaggerPropertySchema.minimum >= 0) {
return '##DataTypes.INTEGER.UNSIGNED##';
}
return '##DataTypes.INTEGER##';
default:
if (typeof swaggerPropertySchema.minimum === "number" && swaggerPropertySchema.minimum >= 0) {
return '##DataTypes.BIGINT.UNSIGNED##';
}
return '##DataTypes.BIGINT##';
}
case 'number':
switch (swaggerPropertySchema.format || "") {
case 'float':
return '##DataTypes.FLOAT##';
default:
return '##DataTypes.DOUBLE##';
}
default:
console.log('Warning: encountered', JSON.stringify(swaggerPropertySchema));
console.log('Unknown data type, falling back to blob');
return '##DataTypes.BLOB##';
}
}
/**
* Will generate folder structure needed for the generation
*/
function generateFolders() {
return new Promise(function(resolve, reject){
try {
console.log('Creating models folder');
fs.mkdirSync('./models');
} catch (err) {
console.log('models folder already exists');
}
try {
console.log('Creating dao folder');
fs.mkdirSync('./dao');
} catch (err) {
console.log('dao folder already exists');
}
resolve();
});
}
/**
* GenerateFile ASYNC
* @param folderPath
* @param fileName
* @param stringContent
* @returns {Promise}
*/
function generateFile(folderPath, fileName, stringContent) {
return new Promise(function(resolve, reject){
fs.writeFile(folderPath + '/' + fileName, stringContent, function(err) {
if(err) {
return console.log(err);
}
resolve();
});
});
}
/**
* Syncrhone version of generate file
* @param folderPath
* @param fileName
* @param stringContent
*/
function generateFileSync(folderPath, fileName, stringContent) {
fs.writeFileSync(folderPath + '/' + fileName, stringContent);
}
/**
* Delete specified file
* @param folderPath
* @param fileName
* @returns {Promise}
*/
function deleteFile(folderPath, fileName) {
return new Promise(function(resolve, reject){
fs.unlink(folderPath + '/' + fileName,function(err){
if(err) reject(err);
resolve();
});
});
}
/**
* Generate 1 model, from a swagger.json definition
* const readFile = fs.readFileSync('./swagger/swagger.json', 'utf-8');
* const swaggerSpec = JSON.parse(readFile);
* const generated = swaggerSequelize.generate(swaggerSpec.definitions[propertyName]);
* @param schema
*/
function generate (schema) {
var result = JSON.parse(JSON.stringify(schema.properties));
Object.keys(result).forEach((propertyName) => {
var propertySchema = result[propertyName];
// BEGIN: Promote Attribute to primaryKey with autoIncrement
if(propertySchema['x-primary-key'] === true) {
propertySchema.primaryKey = true;
propertySchema.autoIncrement = true;
propertySchema.allowNull = false;
}
// END: Promote Attribute to primaryKey with autoIncrement
propertySchema.type = getSequalizeType(propertySchema);
if (propertySchema.default) {
propertySchema.defaultValue = propertySchema.default;
}
});
return result;
}
function findPrimaryKey(theModel, theModelSchema){
const parsed = JSON.parse(JSON.stringify(theModelSchema));
if (!parsed['x-primary-key']){
throw new Error('No primary key referenced for model ' + theModel + ' !!!');
}
return {pks: parsed['x-primary-key']};
}
/**
* Because of natural function in utils.inspect, We are forced to use some tokens to display what we want.
* This methods removes the tokens
* @param generated
* @returns {string}
*/
function removeEscaped(generated){
return util.inspect(generated,false, 5, false)
.replace(/'##/g, '')
.replace(/##'/g, '')
.replace(/\\'/g, "'")
.replace(/\\\\'/g, "\\'");
}
/**
* generate the index.js for sequelize if and only if it doesn't exists
*/
function generateModelIndex() {
let sequelizeModelContent = '\'use strict\';\n';
sequelizeModelContent += '\n';
sequelizeModelContent += 'const fs = require(\'fs\');\n';
sequelizeModelContent += 'const path = require(\'path\');\n';
sequelizeModelContent += 'const Sequelize = require(\'sequelize\');\n';
sequelizeModelContent += 'const basename = path.basename(__filename);\n';
sequelizeModelContent += 'const env = process.env.NODE_ENV || \'local\';\n';
sequelizeModelContent += 'const config = require(path.join(__dirname + \'/../config/config.js\'))[env];\n';
sequelizeModelContent += '\n';
sequelizeModelContent += 'const db = {};\n';
sequelizeModelContent += 'let sequelize = null;\n';
sequelizeModelContent += 'if (config.use_env_constiable) {\n';
sequelizeModelContent += '\tsequelize = new Sequelize(process.env[config.use_env_constiable], config);\n';
sequelizeModelContent += '} else {\n';
sequelizeModelContent += '\tsequelize = new Sequelize(config.database, config.username, config.password, {\n';
sequelizeModelContent += '\t\thost: config.host,\n';
sequelizeModelContent += '\t\tport: config.dbPort,\n';
sequelizeModelContent += '\t\tlogging: config.logging,\n';
sequelizeModelContent += '\t\tdialect: config.dialect,\n';
sequelizeModelContent += '\t\tpool: {\n';
sequelizeModelContent += '\t\t\tmaxConnections: config.maxConnections,\n';
sequelizeModelContent += '\t\t\tmaxIdleTime: config.maxIdleTime\n';
sequelizeModelContent += '\t\t}\n';
sequelizeModelContent += '\t});\n';
sequelizeModelContent += '\n';
sequelizeModelContent += '\t/* UNCOMENT BELOW TO TEST DATABASE CONNEXION*/\n';
sequelizeModelContent += '\t// sequelize.authenticate().then(function () {\n';
sequelizeModelContent += '\t// console.log("Connexion HAS WORKED! ");\n';
sequelizeModelContent += '\t// }).catch(function (err) {\n';
sequelizeModelContent += '\t// console.log("Connexion FAILED ==> err : ", err);\n';
sequelizeModelContent += '\t// }).done();\n';
sequelizeModelContent += '\n';
sequelizeModelContent += '}\n';
sequelizeModelContent += '\n';
sequelizeModelContent += 'fs\n';
sequelizeModelContent += '\t.readdirSync(__dirname)\n';
sequelizeModelContent += '\t.filter(file => {\n';
sequelizeModelContent += '\t\treturn (file.indexOf(\'.\') !== 0) && (file !== basename) && (file.slice(-3) === \'.js\');\n';
sequelizeModelContent += '\t})\n';
sequelizeModelContent += '\t.forEach(file => {\n';
sequelizeModelContent += '\t\tconst model = sequelize[\'import\'](path.join(__dirname, file));\n';
sequelizeModelContent += '\t\tdb[model.name] = model;\n';
sequelizeModelContent += '\t});\n';
sequelizeModelContent += '\n';
sequelizeModelContent += 'Object.keys(db).forEach(modelName => {\n';
sequelizeModelContent += '\tif (db[modelName].associate) {\n';
sequelizeModelContent += '\t\tdb[modelName].associate(db);\n';
sequelizeModelContent += '\t}\n';
sequelizeModelContent += '});\n';
sequelizeModelContent += '\n';
sequelizeModelContent += 'db.sequelize = sequelize;\n';
sequelizeModelContent += 'db.Sequelize = Sequelize;\n';
sequelizeModelContent += '\n';
sequelizeModelContent += 'module.exports = db;\n';
try {
fs.writeFileSync('./models/index.js', sequelizeModelContent, { flag: 'wx' }, 'utf-8');
} catch (err) {
console.log('models/index.js already exists, NOT OVERRIDING');
}
}
/**
* Generate 1 model, from a PARSED swagger.json definition => supply JSON.parse(JSON.stringify(definition.properties))
*
* Returning a string containing Sequelize type
*
* associations will be populated by found associaitions
*
* example
* const readFile = fs.readFileSync('./swagger/swagger.json', 'utf-8');
* const swaggerSpec = JSON.parse(readFile);
* const generated = swaggerSequelize.generate(swaggerSpec.definitions[propertyName]);
* @param schema
*/
function generateOne (currentModel, currentModelSchema, allModelsSchema, associations) {
var result = currentModelSchema;
// console.log('result => ', result);
const foreignKeys = [];
const throughTable = allModelsSchema[currentModel].throughTable;
Object.keys(result).forEach((propertyName) => { // for each property in model shcema
var propertySchema = result[propertyName];
if (propertySchema.nullable){
propertySchema.allowNull = propertySchema.nullable;
delete propertySchema.nullable;
} else {
propertySchema.allowNull = true;
delete propertySchema.nullable;
}
// If current propertyName is contained withing PKS, promote it to primaryKey (supporting multiple PKS)
const pkeys = allModelsSchema[currentModel]['x-primary-key'];
if (pkeys){
for (let i = 0; i < pkeys.length; i++){
if (propertyName === pkeys[i]) {
propertySchema.primaryKey = true;
propertySchema.allowNull = false;
if (propertySchema['type'] !== 'string' && pkeys.length == 1){
propertySchema.autoIncrement = true;
}
}
}
}
const seqType = getSequalizeTypeString(propertySchema);
// console.log('================> seqType = ', seqType);
propertySchema.type = seqType;
// console.log('================> propertySchema.type = ', propertySchema.type);
// console.log('================> propertySchema = ', propertySchema);
if (propertySchema.enum){
delete propertySchema.enum;
}
if (propertySchema.xml){
delete propertySchema.xml;
}
if (propertySchema.$ref){
const temp = propertySchema.$ref.split('/');
const type = temp[temp.length -1];
const throughTable = propertySchema.throughTable;
const nullable = propertySchema.nullable;
const sourceCardinality = propertySchema.sourceCardinality;
const useThroughTableFields = propertySchema.useThroughTableFields;
foreignKeys.push({
propertyToRemove: propertyName,
propertyType: type,
throughTable: throughTable,
useThroughTableFields:useThroughTableFields,
sourceCardinality: sourceCardinality,
nullable: nullable
});
console.dir(foreignKeys);
}
if (propertySchema.default) {
propertySchema.defaultValue = propertySchema.default;
}
});
// MANAGING FOREIGN KEYS FOR CURRENT MODEL
if (foreignKeys.length > 0){
for(let i = 0; i < foreignKeys.length; i++){
const sourceAttributeName = foreignKeys[i].propertyToRemove;
const typeToFind = foreignKeys[i].propertyType;
delete result[sourceAttributeName];
if (!throughTable){
const pkForTypeToFind = findPrimaryKey(typeToFind, allModelsSchema[typeToFind]);
//flag this association to process l8er
associations[currentModel] = associations[currentModel] || [];
associations[currentModel].push({
referencedModel: typeToFind,
fksUsed: pkForTypeToFind.pks,
throughTable: foreignKeys[i].throughTable,
useThroughTableFields: foreignKeys[i].useThroughTableFields,
sourceCardinality: foreignKeys[i].sourceCardinality,
targetCardinality: foreignKeys[i].throughTable? 'N' : '1',
sourceAttributeName: sourceAttributeName,
nullable: foreignKeys[i].nullable
});
}
}
}
return result;
}
function uncapitalize(text){
return text.charAt(0).toLowerCase() + text.substr(1);
}
function capitalize(text){
return text.charAt(0).toUpperCase() + text.substr(1);
}
/**
* Generate all sequelize models based on completeSwaggerSchema.definitions input
* @param modelSchema
*/
function generateModels(modelSchema) {
return new Promise(function(resolve, reject){
// MANAGING ARRAY TYPES
console.log('============= MANAGING ARRAYS IN DEFINITIONS =============');
let modelSchemas = [];
let arrays = []
let associations = {};
/** index = loop index / key = modelName / value = schema content of the modelName */
for (const [index, [key, value]] of Object.entries(Object.entries(modelSchema))) { // for each entry in swagger spec definitions node
var result = JSON.parse(JSON.stringify(value.properties));
// console.log('PROCESSING ' + key + ' - result : ', result);
Object.keys(result).forEach((propertyName) => { // for each entry in the Object
var propertySchema = result[propertyName];
if (propertySchema.type === 'array'){
let impactedType ='';
let throughTable ='';
// Add property FK to this current model into the referenced item type
if (!propertySchema.items){
throw new Error('swagger definitions contains an array without items definitions : ' + key);
}
if (propertySchema.items.$ref){
const temp = propertySchema.items.$ref.split('/');
impactedType = temp[temp.length-1];
throughTable = propertySchema.throughTable;
useThroughTableFields = propertySchema.useThroughTableFields;
console.log('Referenced ARRAY => ', impactedType + ' for ' + key + ' throughTable = ' + throughTable);
} else if (propertySchema.items.type === 'string') {
console.log('Encountered an ARRAY of string, not processing')
} else {
console.log('Encountered an ARRAY with a type of = ' + propertySchema.items.type + ' for ' + key);
console.log('Not processing it...');
}
// Mark the property to be removed from model, and Model to be impacted
arrays.push({
propertyToRemove: propertyName,
impactedType: impactedType,
toBeReferenced: key,
throughTable: throughTable
});
}
});
if (arrays.length > 0){
for(let i = 0; i < arrays.length; i++){
const refRemove = arrays[i].propertyToRemove;
// console.log('deleting Array propretie : ' + refRemove);
delete result[refRemove];
}
}
const ms = {key: key, modSchema: result};
modelSchemas.push(ms);
}
// IMPACTINC CHANGES DUE TO ARRAYS FOUND
if (arrays.length > 0) {
for (let i = 0; i < modelSchemas.length; i++) { // for each model
const key = modelSchemas[i].key;
const value = modelSchemas[i].modSchema;
for (let i = 0; i < arrays.length; i++) { // for each referenced impacted type in the array
const impactedType = arrays[i].impactedType;
const toBeReferenced = arrays[i].toBeReferenced;
const throughTable = arrays[i].throughTable;
const sourceAttributeName = arrays[i].propertyToRemove;
if (key === impactedType) { // if current model == impactedType in the array
// Found model to be impacted by the array
if (impactedType !== '') {
const tbrSchema = modelSchema[toBeReferenced];
const pkForTypeToFind = findPrimaryKey(toBeReferenced, tbrSchema);
associations[key] = associations[key] || [];
associations[key].push({
referencedModel: toBeReferenced,
sourceCardinality: 'N', // This says toBeReferenced has N key (for arrays N is always the case)
targetCardinality: throughTable? 'N' : '1', // This says if throughTable specified, its [Key]N---N[ToBeReferenced], else [Key]N---1[ToBeReferenced]
sourceAttributeName: sourceAttributeName,
fkUsed: pkForTypeToFind.pks,
throughTable: throughTable,
nullable: true
});
}
}
}
}
}
console.log('============= DONE MANAGING ARRAYS IN DEFINITIONS =============');
// GENERATING MODELS
// INITIALIZATION OF SEQUELIZE FILE WITH DEFINITION
var modelContents = {};
for(let i = 0; i < modelSchemas.length; i++){
const key = modelSchemas[i].key;
const value = modelSchemas[i].modSchema;
// for (const [index, [key, value]] of Object.entries(Object.entries(modelSchema))) {
let sequelizeModelContent = 'module.exports = function(sequelize, DataTypes) {\n';
sequelizeModelContent += '\tvar ' + key +' = sequelize.define(\'' + key + '\', ';
const generated = generateOne(key, value, modelSchema, associations);
console.log('generated =>', generated);
sequelizeModelContent += removeEscaped(generated);
console.log('sequelizeModelContent =>', sequelizeModelContent);
sequelizeModelContent += ', {';
sequelizeModelContent += '\n\t\ttableName: \'' + key + '\',';
sequelizeModelContent += '\n\t\ttimestamps: false';
sequelizeModelContent += '\n\t});';
sequelizeModelContent += '\n';
modelContents[key] = modelContents[key] || {};
modelContents[key] = {model: key, modelContent: sequelizeModelContent};
}
// Manage associations
console.log('ASSOCIATIONS DETAILS =====>');
console.dir(associations, {depth:null, colors:true});
console.log('MANANING FIRST LINE OF ASSOCIATION IN EVERY MODELS : Model.associate = function (models)');
var modelFirstLineDone = {};
// for (const [index, [associationKey, associationValue]] of Object.entries(Object.entries(associations))) {
// modelContents[associationKey].modelContent += '\n\t ' + associationKey + '.associate = function (models) {\n';
// modelFirstLineDone[associationKey] = modelFirstLineDone[associationKey] || {};
// modelFirstLineDone[associationKey] = {model: associationKey, done: true};
// for (let i = 0; i < associationValue.length; i++){
// modelFirstLineDone[associationValue[i].referencedModel] = modelFirstLineDone[associationValue[i].referencedModel] || {};
// if (!(modelFirstLineDone[associationValue[i].referencedModel].done)){
// modelContents[associationValue[i].referencedModel].modelContent += '\n\t ' + associationValue[i].referencedModel + '.associate = function (models) {\n';
// modelFirstLineDone[associationValue[i].referencedModel] = {model: associationValue[i].referencedModel, done: true};
// }
//
// }
// }
for (const [index, [associationKey, associationValue]] of Object.entries(Object.entries(associations))) {
modelFirstLineDone[associationKey] = modelFirstLineDone[associationKey] || {};
if (!(modelFirstLineDone[associationKey].done)) {
modelContents[associationKey].modelContent += '\n\t ' + associationKey + '.associate = function (models) {\n';
modelFirstLineDone[associationKey] = {model: associationKey, done: true};
}
for (let i = 0; i < associationValue.length; i++){
modelFirstLineDone[associationValue[i].referencedModel] = modelFirstLineDone[associationValue[i].referencedModel] || {};
if (!(modelFirstLineDone[associationValue[i].referencedModel].done)){
modelContents[associationValue[i].referencedModel].modelContent += '\n\t ' + associationValue[i].referencedModel + '.associate = function (models) {\n';
modelFirstLineDone[associationValue[i].referencedModel] = {model: associationValue[i].referencedModel, done: true};
}
}
}
for(let i = 0; i < modelSchemas.length; i++) {
const currentKey = modelSchemas[i].key;
if (associations[currentKey]){
console.log('Managing associations for ' + currentKey);
for (const [index, [associationKey, associationValue]] of Object.entries(Object.entries(associations))) {
if (currentKey === associationKey){
for (let i = 0; i< associationValue.length; i++){
// console.log('key = ', associationKey);
// console.log('value = ', associationValue[i]);
// console.log('throughTable = ', associationValue[i].throughTable);
const referencedModel = associationValue[i].referencedModel;
const throughTable = associationValue[i].throughTable;
const sourceCardinality = associationValue[i].sourceCardinality;
const targetCardinality = associationValue[i].targetCardinality;
const sourceAttributeName = associationValue[i].sourceAttributeName;
let throughTableStringValue = '';
if(throughTable){
throughTableStringValue = '\'' + throughTable + '\',';
if (modelContents[throughTable]) {
throughTableStringValue = '{model:models.' + throughTable;
console.log('modelSchema[throughTable] => ', modelSchema[throughTable]);
if (modelSchema[throughTable].uniqueFks){
throughTableStringValue += '},'
} else {
throughTableStringValue += ', unique:false},'
}
}
}
// If cardinality is definined, it can only be 1 or N
if (sourceCardinality && (sourceCardinality !== '1' && sourceCardinality !== 'N')){
throw new Error('Cardinality can only be 1 or N, encountered ' + sourceCardinality + ' for model ' + associationKey);
}
// PART 1 => ASSOCIATE IN MODEL REFERENCING
// if (throughTable){ // N-N associations
console.log('\t[' + associationKey + ']' + sourceCardinality + '---' + targetCardinality + '[' + referencedModel + ']');
console.log('\tManaging into ' + associationKey + ' model');
if (sourceCardinality === 'N' && targetCardinality === 'N') {
if (associationKey === referencedModel) { // Recursive relation
modelContents[associationKey].modelContent += '\n';
modelContents[associationKey].modelContent += '\t\tmodels.' + associationKey +
'.belongsToMany(models.'+ referencedModel +', {as: \'' + uncapitalize(referencedModel) + 's' + 'Source\',' +
'through :' + throughTableStringValue +
'foreignKey: \'id_' + uncapitalize(associationKey) + 'Source\',' +
'otherKey: \'id_' + uncapitalize(referencedModel) + 'Target\'});\n';
} else {
modelContents[associationKey].modelContent += '\n';
// modelContents[associationKey].modelContent += '\t ' + associationKey + '.associate = function (models) {\n';
modelContents[associationKey].modelContent += '\t\tmodels.' + associationKey +
'.belongsToMany(models.'+ referencedModel +', {as: \'' + sourceAttributeName + '\',' +
'through : ' + throughTableStringValue +
'foreignKey: \'id_' + uncapitalize(associationKey) + '\',' +
'otherKey: \'id_' + uncapitalize(referencedModel) + '\'});\n';
// modelContents[associationKey].modelContent += '\t};\n';
}
} else if (sourceCardinality === 'N' && targetCardinality === '1') {
if (associationKey === referencedModel) { // Recursive relation
modelContents[associationKey].modelContent += '\n';
// modelContents[associationKey].modelContent += '\t ' + associationKey + '.associate = function (models) {\n';
modelContents[associationKey].modelContent += '\t\tmodels.' + associationKey + '.belongsTo(models.'+ referencedModel +', {\n';
modelContents[associationKey].modelContent += '\t\t\tonDelete:\'CASCADE\',\n'; // TODO > define proper onDelete, onUpdate strategy here
modelContents[associationKey].modelContent += '\t\t\tforeignKey: \'id_' + uncapitalize(referencedModel) + '_parent\'\n';
modelContents[associationKey].modelContent += '\t\t});\n';
// modelContents[associationKey].modelContent += '\t};\n';
} else {
modelContents[associationKey].modelContent += '\n';
// modelContents[associationKey].modelContent += '\t ' + associationKey + '.associate = function (models) {\n';
modelContents[associationKey].modelContent += '\t\tmodels.' + associationKey + '.belongsTo(models.'+ referencedModel +', {\n';
modelContents[associationKey].modelContent += '\t\t\tonDelete:\'CASCADE\',\n'; // TODO > define proper onDelete, onUpdate strategy here
modelContents[associationKey].modelContent += '\t\t\tforeignKey: \'id_' + uncapitalize(referencedModel) + '\'\n';
modelContents[associationKey].modelContent += '\t\t});\n';
// modelContents[associationKey].modelContent += '\t};\n';
}
} else if (sourceCardinality === '1' && targetCardinality === 'N') {
if (associationKey === referencedModel) { // Recursive relation
modelContents[associationKey].modelContent += '\n';
// modelContents[associationKey].modelContent += '\t ' + associationKey + '.associate = function (models) {\n';
modelContents[associationKey].modelContent += '\t\tmodels.' + associationKey + '.belongsTo(models.'+ referencedModel +', {\n';
modelContents[associationKey].modelContent += '\t\t\tonDelete:\'CASCADE\',\n'; // TODO > define proper onDelete, onUpdate strategy here
modelContents[associationKey].modelContent += '\t\t\tforeignKey: \'id_' + uncapitalize(referencedModel) + '_parent\'\n';
modelContents[associationKey].modelContent += '\t\t});\n';
// modelContents[associationKey].modelContent += '\t};\n';
} else {
modelContents[associationKey].modelContent += '\n';
// modelContents[associationKey].modelContent += '\t ' + associationKey + '.associate = function (models) {\n';
modelContents[associationKey].modelContent += '\t\tmodels.' + associationKey + '.belongsTo(models.'+ referencedModel +', {\n';
modelContents[associationKey].modelContent += '\t\t\tonDelete:\'CASCADE\',\n'; // TODO > define proper onDelete, onUpdate strategy here
modelContents[associationKey].modelContent += '\t\t\tforeignKey: \'id_' + uncapitalize(referencedModel) + '\'\n';
modelContents[associationKey].modelContent += '\t\t});\n';
// modelContents[associationKey].modelContent += '\t};\n';
}
} else if (sourceCardinality === '1' && targetCardinality === '1') {
if (associationKey === referencedModel) { // Recursive relation
modelContents[associationKey].modelContent += '\n';
// modelContents[associationKey].modelContent += '\t ' + associationKey + '.associate = function (models) {\n';
modelContents[associationKey].modelContent += '\t\tmodels.' + associationKey + '.belongsTo(models.'+ referencedModel +', {\n';
modelContents[associationKey].modelContent += '\t\t\tonDelete:\'CASCADE\',\n'; // TODO > define proper onDelete, onUpdate strategy here
modelContents[associationKey].modelContent += '\t\t\tforeignKey: \'id_' + uncapitalize(referencedModel) + '_parent\'\n';
modelContents[associationKey].modelContent += '\t\t});\n';
// modelContents[associationKey].modelContent += '\t};\n';
} else {
modelContents[associationKey].modelContent += '\n';
// modelContents[associationKey].modelContent += '\t ' + associationKey + '.associate = function (models) {\n';
modelContents[associationKey].modelContent += '\t\tmodels.' + associationKey + '.belongsTo(models.'+ referencedModel +', {\n';
modelContents[associationKey].modelContent += '\t\t\tonDelete:\'CASCADE\',\n'; // TODO > define proper onDelete, onUpdate strategy here
modelContents[associationKey].modelContent += '\t\t\tforeignKey: \'id_' + sourceAttributeName + '\'\n';
modelContents[associationKey].modelContent += '\t\t});\n';
// modelContents[associationKey].modelContent += '\t};\n';
}
} else {
throw new Error ('There is a problem with cardinalities');
}
// PART 2 => ASSOCIATE IN MODEL REFERENCED
console.log('\tManaging into ' + referencedModel + ' model');
if (sourceCardinality === 'N' && targetCardinality === 'N') {
if (associationKey === referencedModel) { // Recursive relation
modelContents[referencedModel].modelContent += '\n';
// modelContents[referencedModel].modelContent += '\t ' + referencedModel + '.associate = function (models) {\n';
modelContents[referencedModel].modelContent += '\t\tmodels.' + referencedModel +
'.belongsToMany(models.'+ associationKey +', {as: \'' + uncapitalize(associationKey) + 's' + 'Target\',' +
'through: ' + throughTableStringValue +
'foreignKey: \'id_' + uncapitalize(referencedModel) + 'Target\',' +
'otherKey: \'id_' + uncapitalize(associationKey) + 'Source\'});\n';
// modelContents[referencedModel].modelContent += '\t};\n';
} else {
modelContents[referencedModel].modelContent += '\n';
// modelContents[referencedModel].modelContent += '\t ' + referencedModel + '.associate = function (models) {\n';
modelContents[referencedModel].modelContent += '\t\tmodels.' + referencedModel +
'.belongsToMany(models.'+ associationKey +', {as: \'' + uncapitalize(associationKey) + 's\',' +
'through: ' + throughTableStringValue +
'foreignKey: \'id_' + uncapitalize(referencedModel) + '\',' +
'otherKey: \'id_' + uncapitalize(associationKey) + '\'});\n';
// modelContents[referencedModel].modelContent += '\t};\n';
}
} else if (sourceCardinality === 'N' && targetCardinality === '1') {
if (associationKey === referencedModel) { // Recursive relation
modelContents[referencedModel].modelContent += '\n';
// modelContents[referencedModel].modelContent += '\t ' + referencedModel + '.associate = function (models) {\n';
modelContents[referencedModel].modelContent += '\t\tmodels.' + referencedModel + '.hasMany(models.'+
associationKey +', {foreignKey: \'id_'+ uncapitalize(referencedModel) + '_parent\'});\n';
// modelContents[referencedModel].modelContent += '\t};\n';
} else {
modelContents[referencedModel].modelContent += '\n';
// modelContents[referencedModel].modelContent += '\t ' + referencedModel + '.associate = function (models) {\n';
modelContents[referencedModel].modelContent += '\t\tmodels.' + referencedModel + '.hasMany(models.'+ associationKey +
', {as: \'' + sourceAttributeName + '\', foreignKey: \'id_'+ uncapitalize(referencedModel) + '\', onDelete:\'CASCADE\'});\n';
// modelContents[referencedModel].modelContent += '\t};\n';
}
} else if (sourceCardinality === '1' && targetCardinality === 'N') {
if (associationKey === referencedModel) { // Recursive relation
modelContents[referencedModel].modelContent += '\n';
// modelContents[referencedModel].modelContent += '\t ' + referencedModel + '.associate = function (models) {\n';
modelContents[referencedModel].modelContent += '\t\tmodels.' + referencedModel + '.hasOne(models.'+ associationKey +
', {foreignKey: \'id_'+ uncapitalize(referencedModel) + '_parent\'});\n';
modelContents[referencedModel].modelContent += '\t};\n';
} else {
modelContents[referencedModel].modelContent += '\n';
// modelContents[referencedModel].modelContent += '\t ' + referencedModel + '.associate = function (models) {\n';
modelContents[referencedModel].modelContent += '\t\tmodels.' + referencedModel + '.hasOne(models.'+ associationKey +
', {foreignKey: \'id_'+ uncapitalize(referencedModel) + '\', onDelete:\'CASCADE\'});\n';
// modelContents[referencedModel].modelContent += '\t};\n';
}
} else if (sourceCardinality === '1' && targetCardinality === '1') {
if (associationKey === referencedModel) { // Recursive relation
modelContents[referencedModel].modelContent += '\n';
// modelContents[referencedModel].modelContent += '\t ' + referencedModel + '.associate = function (models) {\n';
modelContents[referencedModel].modelContent += '\t\tmodels.' + referencedModel + '.hasOne(models.'+ associationKey +
', {foreignKey: \'id_'+ uncapitalize(referencedModel) + '_parent\'});\n';
// modelContents[referencedModel].modelContent += '\t};\n';
} else {
modelContents[referencedModel].modelContent += '\n';
// modelContents[referencedModel].modelContent += '\t ' + referencedModel + '.associate = function (models) {\n';
modelContents[referencedModel].modelContent += '\t\tmodels.' + referencedModel + '.hasOne(models.'+ associationKey +
', {foreignKey: \'id_'+ sourceAttributeName + '\', onDelete:\'CASCADE\'});\n';
// modelContents[referencedModel].modelContent += '\t};\n';
}
} else {
throw new Error('this error won\'t happen ever, because error already thrown sooner');
}
}
}
}
// modelContents[currentKey].modelContent += '\t};\n';
}
}
console.log('MANANING LAST LINE OF ASSOCIATION IN EVERY MODELS : };');
console.log('ASSOCIATIONS DETAILS =====>');
console.dir(associations, {depth:null, colors:true});
var modelLastLineDone = {};
// for (const [index, [associationKey, associationValue]] of Object.entries(Object.entries(associations))) {
// modelContents[associationKey].modelContent += '\t};\n';
// modelLastLineDone[associationKey] = modelLastLineDone[associationKey] || {};
// modelLastLineDone[associationKey] = {model: associationKey, done: true};
// for (let i = 0; i < associationValue.length; i++){
// console.log('DEBUG REMOVE THIS associationValue[i].referencedModel = ', associationValue[i].referencedModel);
// modelLastLineDone[associationValue[i].referencedModel] = modelLastLineDone[associationValue[i].referencedModel] || {};
// if (!(modelLastLineDone[associationValue[i].referencedModel].done)){
// modelContents[associationValue[i].referencedModel].modelContent += '\n\t};\n';
// modelLastLineDone[associationValue[i].referencedModel] = {model: associationValue[i].referencedModel, done: true};
// }
//
// }
// }
for (const [index, [associationKey, associationValue]] of Object.entries(Object.entries(associations))) {
modelLastLineDone[associationKey] = modelLastLineDone[associationKey] || {};
if (!(modelLastLineDone[associationKey].done)) {
modelContents[associationKey].modelContent += '\t};\n';
modelLastLineDone[associationKey] = {model: associationKey, done: true};
}
for (let i = 0; i < associationValue.length; i++){
modelLastLineDone[associationValue[i].referencedModel] = modelLastLineDone[associationValue[i].referencedModel] || {};
if (!(modelLastLineDone[associationValue[i].referencedModel].done)){
modelContents[associationValue[i].referencedModel].modelContent += '\n\t};\n';
modelLastLineDone[associationValue[i].referencedModel] = {model: associationValue[i].referencedModel, done: true};
}
}
}
// Finish files
for(let i = 0; i < modelSchemas.length; i++) {
const currentKey = modelSchemas[i].key;
// finishing Sequelize file