zet-lib 1.3.23 → 1.3.24

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.
package/lib/connection.js CHANGED
@@ -353,6 +353,26 @@ connection.constraintList = (table, schema = 'public') => {
353
353
  WHERE nsp.nspname = '${schema}' AND rel.relname = '${table}'; `
354
354
  }
355
355
 
356
+ //find foreign key
357
+ connection.foreignKeyList = (table) => {
358
+ return `SELECT
359
+ tc.table_schema,
360
+ tc.constraint_name,
361
+ tc.table_name,
362
+ kcu.column_name,
363
+ ccu.table_schema AS foreign_table_schema,
364
+ ccu.table_name AS foreign_table_name,
365
+ ccu.column_name AS foreign_column_name
366
+ FROM information_schema.table_constraints AS tc
367
+ JOIN information_schema.key_column_usage AS kcu
368
+ ON tc.constraint_name = kcu.constraint_name
369
+ AND tc.table_schema = kcu.table_schema
370
+ JOIN information_schema.constraint_column_usage AS ccu
371
+ ON ccu.constraint_name = tc.constraint_name
372
+ WHERE tc.constraint_type = 'FOREIGN KEY'
373
+ AND tc.table_name='${table}';`
374
+ }
375
+
356
376
  var toNumber = function (num) {
357
377
  num = num + ''
358
378
  var t = replaceAll(num, '.', '')
@@ -418,7 +418,6 @@ const generate = async (req, res) => {
418
418
  //console.log(JSON.stringify(filesKey.modelObj))
419
419
  //check if has chains here
420
420
  MYMODEL = filesKey.modelObj
421
-
422
421
  //delete/clean zgrid after changes
423
422
  await connection.delete({
424
423
  table: 'zgrid',
@@ -503,7 +502,7 @@ const generate = async (req, res) => {
503
502
  fs.writeFileSync(`${dirRoot}/models/${filesKey.filename}`, newModel)
504
503
  }
505
504
  //check model if it has select relation in concat
506
- await scanning(MYMODEL)
505
+ await scanning(MYMODEL, result)
507
506
  }
508
507
  //generate views
509
508
  let DIR_VIEW = `${dirRoot}/views/${table}`
@@ -662,6 +661,113 @@ router.post('/savetabs', async (req, res) => {
662
661
  res.send(json)
663
662
  })
664
663
 
664
+ const buildJoin = async (MYMODEL, result) => {
665
+ let joins = result.joins || {}
666
+ let table = MYMODEL.table
667
+ let table_joins = []
668
+ let join_list = []
669
+
670
+ if (result.joins && Object.keys(joins).length > 0) {
671
+ let newDataObj = {}
672
+ for (let key in joins) {
673
+ //find relation
674
+ //sql.push(` LEFT JOIN ${key} ON ${key}.id = ${table}.${key}_id `)
675
+ let THEIR_MODEL = require(`${dirRoot}/models/${key}`)
676
+ //console.log(THEIR_MODEL)
677
+ let arr = joins[key] || []
678
+ table_joins.push(key)
679
+ if (arr.length > 0) {
680
+ arr.map((item) => {
681
+ //console.log(item)
682
+ //console.log(typeof item)
683
+ const originKey = key.replace(`${key}___`, '')
684
+ newDataObj[item.key] = item.value
685
+ MYMODEL.keys.push(item.key)
686
+ MYMODEL.keysExcel.push(item.key)
687
+ MYMODEL.labels[item.key] = item.value
688
+ MYMODEL.fields[item.key] = THEIR_MODEL.fields[originKey]
689
+ MYMODEL.options[item.key] = THEIR_MODEL.options[originKey]
690
+ MYMODEL.grids.invisibles.push(item.key)
691
+ MYMODEL.datas[item.key] = THEIR_MODEL.datas[originKey]
692
+ //MYMODEL.widgets[item.key] = THEIR_MODEL.widgets[originKey];
693
+ join_list.push(item.key)
694
+ })
695
+ }
696
+ }
697
+
698
+ let sql = []
699
+ //get all foreign key
700
+ let rows = await connection.query(connection.foreignKeyList(table))
701
+ let foreign_table_name = {}
702
+ let foreignTables = []
703
+ rows.map((row) => {
704
+ foreignTables.push(row.foreign_table_name)
705
+ foreign_table_name[row.foreign_table_name] = ` LEFT JOIN ${row.foreign_table_name} ON ${row.foreign_table_name}.id = ${table}.${row.column_name} `
706
+ })
707
+
708
+ //step by step
709
+ let table_steps = []
710
+ table_joins.map((item) => {
711
+ if (foreignTables.includes(item)) {
712
+ sql.push(foreign_table_name[item])
713
+ table_steps.push(item)
714
+ }
715
+ })
716
+
717
+ if (table_steps.length > 0) {
718
+ for (let table_steps_item of table_steps) {
719
+ let rows = await connection.query(connection.foreignKeyList(table_steps_item))
720
+ let foreign_table_name = {}
721
+ let foreignTables = []
722
+ rows.map((row) => {
723
+ foreignTables.push(row.foreign_table_name)
724
+ foreign_table_name[row.foreign_table_name] = ` LEFT JOIN ${row.foreign_table_name} ON ${row.foreign_table_name}.id = ${table_steps_item}.${row.column_name} `
725
+ })
726
+ console.log(foreignTables)
727
+ table_joins.map((item) => {
728
+ if (!table_steps.includes(item)) {
729
+ if (foreignTables.includes(item)) {
730
+ sql.push(foreign_table_name[item])
731
+ table_steps.push(item)
732
+ }
733
+ }
734
+ })
735
+ }
736
+ }
737
+
738
+ if (table_steps.length > 0) {
739
+ for (let table_steps_item of table_steps) {
740
+ let rows = await connection.query(connection.foreignKeyList(table_steps_item))
741
+ let foreign_table_name = {}
742
+ let foreignTables = []
743
+ rows.map((row) => {
744
+ foreignTables.push(row.foreign_table_name)
745
+ foreign_table_name[row.foreign_table_name] = ` LEFT JOIN ${row.foreign_table_name} ON ${row.foreign_table_name}.id = ${table_steps_item}.${row.column_name} `
746
+ })
747
+ console.log(foreignTables)
748
+ table_joins.map((item) => {
749
+ if (!table_steps.includes(item)) {
750
+ if (foreignTables.includes(item)) {
751
+ sql.push(foreign_table_name[item])
752
+ table_steps.push(item)
753
+ }
754
+ }
755
+ })
756
+ }
757
+ }
758
+
759
+ //add join
760
+ let JOINS_DATA = {}
761
+ JOINS_DATA.tables = table_joins
762
+ JOINS_DATA.list = join_list
763
+ JOINS_DATA.data = joins
764
+ JOINS_DATA.sql = sql
765
+
766
+ MYMODEL.joins = JOINS_DATA
767
+ }
768
+ return MYMODEL
769
+ }
770
+
665
771
  const columnLR = (items, dataName) => {
666
772
  return `<div class="col-md-6"><ol class="mydragable divboxlittle" data-name="${dataName}">${items}</ol></div>`
667
773
  }
@@ -1074,13 +1180,15 @@ var saveToZFields = async (body) => {
1074
1180
  data.joins = JSON.stringify(joinsTables)
1075
1181
  }
1076
1182
 
1077
- await connection.update({
1183
+ let result = await connection.update({
1078
1184
  table: 'zfields',
1079
1185
  data: data,
1080
1186
  where: {
1081
1187
  table: table,
1082
1188
  },
1083
1189
  })
1190
+
1191
+ return result
1084
1192
  }
1085
1193
 
1086
1194
  //drop down chains effect
@@ -1218,16 +1326,15 @@ router.post('/import', async (req, res) => {
1218
1326
  /*
1219
1327
  Scanning relation id to name
1220
1328
  */
1221
- const scanning = async (MYMODEL) => {
1329
+ const scanning = async (MYMODEL, result) => {
1222
1330
  //let MYMODEL = require(`${dirRoot}/models/${table}`);
1223
- //console.log(JSON.stringify(MYMODEL))
1224
1331
  let table = MYMODEL.table
1225
1332
  const widgets = MYMODEL.widgets
1226
1333
  const not_scanning = ['created_by', 'updated_by']
1227
1334
  const concat_bracket = /(?<=\().+?(?=\))/g
1228
1335
  let changeWidgets = {}
1229
1336
  let changes = []
1230
- for (let key in widgets) {
1337
+ /*for (let key in widgets) {
1231
1338
  if (widgets[key].name == 'relation' || widgets[key].name == 'dropdown_multi' || widgets[key].name == 'typeahead') {
1232
1339
  if (!Util.in_array(key, not_scanning)) {
1233
1340
  let fields = widgets[key].fields
@@ -1262,7 +1369,7 @@ const scanning = async (MYMODEL) => {
1262
1369
  }
1263
1370
  }
1264
1371
  }
1265
- }
1372
+ }*/
1266
1373
 
1267
1374
  if (changes.length) {
1268
1375
  //rewrite model
@@ -1274,6 +1381,18 @@ const scanning = async (MYMODEL) => {
1274
1381
  fs.writeFileSync(`${dirRoot}/models/${MYMODEL.table}.js`, newModel)
1275
1382
  }
1276
1383
 
1384
+ //check if has join
1385
+ //build join
1386
+ //console.log(JSON.stringify(MYMODEL))
1387
+ MYMODEL = await buildJoin(MYMODEL, result)
1388
+ //console.log(JSON.stringify(MYMODEL))
1389
+
1390
+ let newModel = `module.exports = { ${Util.newLine}`
1391
+ newModel += zRoute.buildFileModel(MYMODEL)
1392
+ newModel += `${Util.newLine}`
1393
+ newModel += `}`
1394
+ fs.writeFileSync(`${dirRoot}/models/${MYMODEL.table}.js`, newModel)
1395
+
1277
1396
  //return MYMODEL;
1278
1397
  }
1279
1398
 
@@ -1430,7 +1549,7 @@ router.post('/joins', async (req, res) => {
1430
1549
  },
1431
1550
  })
1432
1551
  let resultJoins = result.joins || {}
1433
- resultJoins[table_join] = {}
1552
+ resultJoins[table_join] = []
1434
1553
  let myzfield = await connection.update({
1435
1554
  table: 'zfields',
1436
1555
  data: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.3.23",
3
+ "version": "1.3.24",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"