zet-lib 1.2.22 → 1.2.23

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 (2) hide show
  1. package/lib/zRoute.js +85 -50
  2. package/package.json +1 -1
package/lib/zRoute.js CHANGED
@@ -501,7 +501,7 @@ zRoute.relations = async (req, res, table) => {
501
501
  select: widget.fields.join(',') + ' as zname ',
502
502
  table: widget.table,
503
503
  where: { company_id: company_id },
504
- orderBy: ['zname', 'asc'],
504
+ order_by: widget.hasOwnProperty('order_by') && widget.order_by ? [`${widget.order_by}`] : ['zname asc'],
505
505
  }
506
506
  if (Util.in_array(widget.table, zRoute.tableHasNoCompanyId)) {
507
507
  delete obj.where
@@ -559,7 +559,7 @@ zRoute.relations = async (req, res, table) => {
559
559
  select: select,
560
560
  table: widget.table,
561
561
  where: { company_id: company_id },
562
- orderBy: ['zname', 'asc'],
562
+ order_by: widget.hasOwnProperty('order_by') && widget.order_by ? [`${widget.order_by}`] : ['zname asc'],
563
563
  }
564
564
  if (Util.in_array(widget.table, zRoute.tableHasNoCompanyId)) {
565
565
  delete obj.where
@@ -618,7 +618,7 @@ zRoute.relations = async (req, res, table) => {
618
618
  select: select,
619
619
  table: widget.table,
620
620
  where: { company_id: company_id },
621
- orderBy: ['zname', 'asc'],
621
+ order_by: widget.hasOwnProperty('order_by') && widget.order_by ? [`${widget.order_by}`] : ['zname asc'],
622
622
  }
623
623
  if (Util.in_array(widget.table, zRoute.tableHasNoCompanyId)) {
624
624
  delete obj.where
@@ -3667,6 +3667,7 @@ cache models in so it's no need call database repeatly
3667
3667
 
3668
3668
  zRoute.modelsCache = async () => {
3669
3669
  let obj = {}
3670
+ let orderBy = {}
3670
3671
  if (process.env.APP_IS_CACHE == 1) {
3671
3672
  let models = zRoute.MYMODELS() || {}
3672
3673
  delete models.zrole
@@ -3686,6 +3687,13 @@ zRoute.modelsCache = async () => {
3686
3687
  }
3687
3688
  if (!Util.in_array(key, nots)) {
3688
3689
  obj[table][`${keys}___${key}`] = `${widgets[key].fields[1]} as ${key}`
3690
+ if (Object.prototype.hasOwnProperty.call(models[keys].widgets, key)) {
3691
+ if (Object.prototype.hasOwnProperty.call(models[keys].widgets[key], 'order_by')) {
3692
+ if (models[keys].widgets[key].order_by) {
3693
+ orderBy[table] = models[keys].widgets[key].order_by
3694
+ }
3695
+ }
3696
+ }
3689
3697
  }
3690
3698
  }
3691
3699
  }
@@ -3699,7 +3707,9 @@ zRoute.modelsCache = async () => {
3699
3707
  created_by: 'fullname as created_by',
3700
3708
  updated_by: 'fullname as updated_by',
3701
3709
  }
3702
-
3710
+ //set order by to cache
3711
+ myCache.set(`Z___ORDER_BY`, orderBy)
3712
+ //console.log(JSON.stringify(obj))
3703
3713
  try {
3704
3714
  for (let keys in obj) {
3705
3715
  let checks = await connection.query(`SELECT EXISTS ( SELECT 1 FROM pg_tables WHERE tablename = '${keys}') AS oke;`)
@@ -3711,22 +3721,32 @@ zRoute.modelsCache = async () => {
3711
3721
  }
3712
3722
  selects += `id, company_id`
3713
3723
  for (const company of companies) {
3714
- const results = await connection.results({
3724
+ //console.log('selects : '+ selects + ' table : '+keys + ' ')
3725
+ let objectSQL = {
3715
3726
  table: keys,
3716
3727
  select: selects,
3717
3728
  where: {
3718
3729
  company_id: company.id,
3719
3730
  },
3720
- })
3731
+ }
3732
+ if (orderBy[keys]) {
3733
+ objectSQL.order_by = [`${orderBy[keys]}`]
3734
+ }
3735
+ const results = await connection.results(objectSQL)
3721
3736
  for (let key in obj[keys]) {
3737
+ //console.log('key : '+ key + ' obj[keys] : '+ JSON.stringify(obj[keys]) + ' ')
3722
3738
  const splits = key.split('___') || []
3723
3739
  let item = splits.length > 1 ? splits[1] : key
3724
3740
  let arr = []
3725
3741
  for (const result of results) {
3726
3742
  arr.push({ id: result.id, zname: result[item] })
3727
3743
  }
3728
- const myarray = Util.sortArray(arr, 'zname')
3729
- myCache.set(`${keys}_${key}_${company.id}`, myarray)
3744
+ if (orderBy[keys]) {
3745
+ myCache.set(`${keys}_${key}_${company.id}`, arr)
3746
+ } else {
3747
+ const myarray = Util.sortArray(arr, 'zname')
3748
+ myCache.set(`${keys}_${key}_${company.id}`, myarray)
3749
+ }
3730
3750
  }
3731
3751
  }
3732
3752
  }
@@ -3735,32 +3755,36 @@ zRoute.modelsCache = async () => {
3735
3755
  } catch (e) {
3736
3756
  //debug(req,res,e.toString());
3737
3757
  console.log('modelsCache :', e + '')
3758
+ console.log(e)
3738
3759
  }
3739
3760
  }
3740
3761
  return obj
3741
3762
  }
3742
3763
 
3743
3764
  zRoute.modelsCacheRenew = (table, companyId) => {
3744
- if (process.env.APP_IS_CACHE == 1) {
3745
- if (myCache.has('MODELS_RELATIONS')) {
3746
- let MODELS = myCache.get('MODELS_RELATIONS')
3747
- let arr = Object.keys(MODELS) || []
3748
- let obj = MODELS[table] || {}
3749
- if (Util.in_array(table, arr)) {
3750
- let selects = ''
3751
- for (let key in obj) {
3752
- selects += `${obj[key]},`
3753
- }
3754
- selects += `id`
3755
- connection
3756
- .results({
3765
+ try {
3766
+ if (process.env.APP_IS_CACHE == 1) {
3767
+ if (myCache.has('MODELS_RELATIONS')) {
3768
+ let MODELS = myCache.get('MODELS_RELATIONS')
3769
+ let arr = Object.keys(MODELS) || []
3770
+ let obj = MODELS[table] || {}
3771
+ let orderBy = myCache.get('Z___ORDER_BY') || {}
3772
+ if (Util.in_array(table, arr)) {
3773
+ let selects = ''
3774
+ for (let key in obj) {
3775
+ selects += `${obj[key]},`
3776
+ }
3777
+ selects += `id`
3778
+
3779
+ let objectSQL = {
3757
3780
  select: selects,
3758
3781
  table: table,
3759
- where: {
3760
- company_id: companyId,
3761
- },
3762
- })
3763
- .then(function (results) {
3782
+ where: { company_id: companyId },
3783
+ }
3784
+ if (orderBy[table]) {
3785
+ objectSQL.order_by = [`${orderBy[table]}`]
3786
+ }
3787
+ connection.results(objectSQL).then(function (results) {
3764
3788
  for (let key in obj) {
3765
3789
  const splits = key.split('___') || []
3766
3790
  let item = splits.length > 1 ? splits[1] : key
@@ -3768,43 +3792,54 @@ zRoute.modelsCacheRenew = (table, companyId) => {
3768
3792
  for (const result of results) {
3769
3793
  arr.push({ id: result.id, zname: result[item] })
3770
3794
  }
3771
- const myarray = Util.sortArray(arr, 'zname')
3772
- myCache.set(`${table}_${key}_${companyId}`, myarray)
3795
+ if (orderBy[table]) {
3796
+ myCache.set(`${table}_${key}_${companyId}`, arr)
3797
+ } else {
3798
+ const myarray = Util.sortArray(arr, 'zname')
3799
+ myCache.set(`${table}_${key}_${companyId}`, myarray)
3800
+ }
3773
3801
  }
3774
3802
  })
3803
+ }
3775
3804
  }
3776
3805
  }
3806
+ } catch (err) {
3807
+ console.log(err)
3777
3808
  }
3778
3809
  }
3779
3810
 
3780
3811
  zRoute.makeFunctionsSystem = () => {
3781
- let obj = myCache.get('ZFUNCTIONS')
3782
- let content = ``
3783
- for (let key in obj) {
3784
- if (obj[key].systems == 1) {
3785
- content += obj[key].code
3786
- content += Util.newLine
3812
+ try {
3813
+ let obj = myCache.get('ZFUNCTIONS')
3814
+ let content = ``
3815
+ for (let key in obj) {
3816
+ if (obj[key].systems == 1) {
3817
+ content += obj[key].code
3818
+ content += Util.newLine
3819
+ }
3787
3820
  }
3788
- }
3789
- let templateSystem = `const { connection, Util, io, zCache, myCache, zDebug, zDataTable, moduleLib, zFunction, zFn, zMail, zRoute, zRole} = require('zet-lib');
3821
+ let templateSystem = `const { connection, Util, io, zCache, myCache, zDebug, zDataTable, moduleLib, zFunction, zFn, zMail, zRoute, zRole} = require('zet-lib');
3790
3822
  module.exports = (req, res, next) => {`
3791
- let fileSystem = `${dirRoot}/components/zFunctionsSystem.js`
3792
- templateSystem += content
3793
- templateSystem += ` next();
3823
+ let fileSystem = `${dirRoot}/components/zFunctionsSystem.js`
3824
+ templateSystem += content
3825
+ templateSystem += ` next();
3794
3826
  };
3795
3827
  `
3796
- Util.writeFile(fileSystem, templateSystem)
3797
- if (process.env.NODE_ENV === 'production') {
3798
- setTimeout(function () {
3799
- pm2.connect(function (err) {
3800
- if (err) {
3801
- console.log(err.toString())
3802
- }
3803
- pm2.restart(process.env.PM2_NAME, (err, proc) => {
3804
- //io.to(room).emit("message","Restart done")
3828
+ Util.writeFile(fileSystem, templateSystem)
3829
+ if (process.env.NODE_ENV === 'production') {
3830
+ setTimeout(function () {
3831
+ pm2.connect(function (err) {
3832
+ if (err) {
3833
+ console.log(err.toString())
3834
+ }
3835
+ pm2.restart(process.env.PM2_NAME, (err, proc) => {
3836
+ //io.to(room).emit("message","Restart done")
3837
+ })
3805
3838
  })
3806
- })
3807
- }, 3000)
3839
+ }, 3000)
3840
+ }
3841
+ } catch (err) {
3842
+ console.log(err)
3808
3843
  }
3809
3844
  }
3810
3845
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.2.22",
3
+ "version": "1.2.23",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"