zet-lib 1.3.24 → 1.3.25

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/Util.js CHANGED
@@ -1176,10 +1176,10 @@ Util.userAvatar = (img = '') => {
1176
1176
  }
1177
1177
 
1178
1178
  /*
1179
- MYSQL HELPER
1179
+ SQL HELPER
1180
1180
  */
1181
1181
  Util.selectParser = (fields = [], MYMODEL = {}) => {
1182
- //fields = fields.filter((e) => e !== 'actionColumn')
1182
+ let table = MYMODEL.table
1183
1183
  let virtuals = []
1184
1184
  let select = ''
1185
1185
  for (var key in MYMODEL.widgets) {
@@ -1190,19 +1190,40 @@ Util.selectParser = (fields = [], MYMODEL = {}) => {
1190
1190
  let arr = []
1191
1191
  fields.forEach((item) => {
1192
1192
  if (virtuals.includes(item)) {
1193
- select += MYMODEL.widgets[item].fields + ','
1193
+ select += `${MYMODEL.widgets[item].fields},`
1194
1194
  } else if (item === 'no') {
1195
- arr.push('id')
1195
+ arr.push(`${table}.id`)
1196
1196
  } else if (item === 'actionColumn') {
1197
- arr.push('lock')
1197
+ arr.push(`${table}.lock`)
1198
1198
  } else {
1199
- arr.push(item)
1199
+ arr.push(Util.fieldWithTable(item, MYMODEL))
1200
1200
  }
1201
1201
  })
1202
- select += `"${arr.join('","')}"`
1202
+ //select += `"${arr.join('","')}"`
1203
+ select += arr.join(',')
1203
1204
  return select
1204
1205
  }
1205
1206
 
1207
+ Util.fieldWithTable = (field, MYMODEL) => {
1208
+ let name = ''
1209
+ let joinList = MYMODEL.joins && MYMODEL.joins.list.length > 0 ? MYMODEL.joins.list : []
1210
+ if (joinList.length > 0) {
1211
+ if (joinList.includes(field)) {
1212
+ let split = field.split('___')
1213
+ name = `${split[0]}.${split[1]} as ${field}`
1214
+ } else {
1215
+ name = `${MYMODEL.table}.${field}`
1216
+ }
1217
+ } else {
1218
+ name = `${MYMODEL.table}.${field}`
1219
+ }
1220
+ return name
1221
+ }
1222
+
1223
+ Util.tableWithJoin = (MYMODEL) => {
1224
+ return MYMODEL.joins && MYMODEL.joins.list.length > 0 ? MYMODEL.joins.sql : []
1225
+ }
1226
+
1206
1227
  Util.selectMysql = (fields = [], relations = {}) => {
1207
1228
  let obj = {}
1208
1229
  let arr = []
package/lib/zRoute.js CHANGED
@@ -1901,12 +1901,14 @@ zRoute.listDataTable = async (req, res, objData = {}) => {
1901
1901
  try {
1902
1902
  const body = req.body
1903
1903
  const MYMODEL = objData.MYMODEL || {}
1904
+ const table = MYMODEL.table
1904
1905
  const fields = Object.prototype.hasOwnProperty.call(objData, 'fields') ? objData.fields : req.body.fields
1905
1906
  const relations = await zRoute.relations(req, res, MYMODEL.table)
1906
1907
  const select = Object.prototype.hasOwnProperty.call(objData, 'select') ? objData.select : Util.selectParser(fields, MYMODEL)
1907
1908
  const columns = Object.prototype.hasOwnProperty.call(objData, 'columns') ? objData.columns : body.columns
1908
1909
  const asDate = []
1909
1910
  const asJSONb = []
1911
+ const asArray = []
1910
1912
  for (let key in MYMODEL.widgets) {
1911
1913
  if (MYMODEL.widgets[key].name == 'datetime') {
1912
1914
  asDate.push(key)
@@ -1917,16 +1919,23 @@ zRoute.listDataTable = async (req, res, objData = {}) => {
1917
1919
  if (MYMODEL.widgets[key].name == 'dropdown_multi') {
1918
1920
  asJSONb.push(key)
1919
1921
  }
1922
+ if (MYMODEL.widgets[key].name == 'dragdrop') {
1923
+ asJSONb.push(key)
1924
+ }
1920
1925
  if (MYMODEL.widgets[key].name == 'dropdown_checkbox') {
1921
1926
  asJSONb.push(key)
1922
1927
  }
1928
+ if (MYMODEL.widgets[key].name == 'json_array' || MYMODEL.widgets[key].name == 'array' || MYMODEL.widgets[key].name == 'dropzone') {
1929
+ asArray.push(key)
1930
+ }
1923
1931
  }
1924
1932
  let whereArray = []
1925
1933
  if (objData.hasOwnProperty('whereArray')) {
1926
1934
  whereArray = objData['whereArray']
1927
1935
  }
1936
+
1928
1937
  whereArray.push({
1929
- field: 'company_id',
1938
+ field: `${table}.company_id`,
1930
1939
  option: '=',
1931
1940
  value: res.locals.companyId,
1932
1941
  operator: 'AND',
@@ -1941,17 +1950,28 @@ zRoute.listDataTable = async (req, res, objData = {}) => {
1941
1950
  if (asJSONb.includes(fields[item.data])) {
1942
1951
  astype = 'jsonb'
1943
1952
  }
1953
+ if (asArray.includes(fields[item.data])) {
1954
+ astype = 'array'
1955
+ }
1944
1956
  if (astype === 'jsonb') {
1945
1957
  whereArray.push({
1946
- field: fields[item.data],
1958
+ field: Util.fieldWithTable(fields[item.data], MYMODEL),
1947
1959
  option: ' ',
1948
1960
  value: ` @> '["${item.search.value}"]'`,
1949
1961
  operator: 'AND',
1950
1962
  type: 'inline',
1951
1963
  })
1964
+ } else if (astype === 'array') {
1965
+ whereArray.push({
1966
+ field: '',
1967
+ option: '',
1968
+ value: ` ${Util.fieldWithTable(fields[item.data], MYMODEL)}::text ILIKE '${item.search.value}' `,
1969
+ operator: 'AND',
1970
+ type: 'inline',
1971
+ })
1952
1972
  } else {
1953
1973
  whereArray.push({
1954
- field: fields[item.data],
1974
+ field: Util.fieldWithTable(fields[item.data], MYMODEL),
1955
1975
  option: MYMODEL.options[fields[item.data]],
1956
1976
  value: item.search.value,
1957
1977
  operator: 'AND',
@@ -1960,20 +1980,31 @@ zRoute.listDataTable = async (req, res, objData = {}) => {
1960
1980
  }
1961
1981
  }
1962
1982
  })
1963
- const orderColumn = fields[body.order[0].column] == 'actionColumn' ? 'id' : fields[body.order[0].column] == 'no' ? 'id' : fields[body.order[0].column] == 'actionColum' ? 'id' : fields[body.order[0].column]
1964
- const rows = await connection.results({
1983
+
1984
+ const orderColumn = fields[body.order[0].column] == 'actionColumn' ? `${table}.id` : fields[body.order[0].column] == 'no' ? `${table}.id` : fields[body.order[0].column] == 'actionColum' ? `${table}.id` : fields[body.order[0].column]
1985
+ let gridData = {
1965
1986
  select: select,
1966
1987
  table: MYMODEL.table,
1967
1988
  whereArray: whereArray,
1968
1989
  limit: body.length,
1969
1990
  offset: body.start,
1970
- orderBy: [orderColumn, body.order[0].dir],
1971
- })
1972
- const count = await connection.result({
1973
- select: 'count(id) as count',
1991
+ order_by: [`${orderColumn} ${body.order[0].dir}`],
1992
+ }
1993
+ let joins = Util.tableWithJoin(MYMODEL)
1994
+ if (joins.length > 0) {
1995
+ gridData.joins = joins
1996
+ }
1997
+ const rows = await connection.results(gridData)
1998
+ let gridCount = {
1999
+ select: `count(${table}.id) as count`,
1974
2000
  table: MYMODEL.table,
1975
2001
  whereArray: whereArray,
1976
- })
2002
+ }
2003
+ if (joins.length > 0) {
2004
+ gridCount.joins = joins
2005
+ }
2006
+ const count = await connection.result(gridCount)
2007
+
1977
2008
  let datas = []
1978
2009
  const zRole = objData.zRole || require('./zRole')
1979
2010
  const levels = objData.hasOwnProperty('levels') ? objData.levels : zRole.myLevel(req, res, MYMODEL.table)
@@ -2014,6 +2045,7 @@ zRoute.listDataTable = async (req, res, objData = {}) => {
2014
2045
 
2015
2046
  zRoute.listData = async (req, res, MYMODEL, zRole, actionButtonsFn) => {
2016
2047
  const room = res.locals.token || 'aa'
2048
+ const table = MYMODEL.table
2017
2049
  try {
2018
2050
  actionButtonsFn = actionButtonsFn || function () {}
2019
2051
  const relations = await zRoute.relations(req, res, MYMODEL.table)
@@ -2040,7 +2072,7 @@ zRoute.listData = async (req, res, MYMODEL, zRole, actionButtonsFn) => {
2040
2072
  if (MYMODEL.widgets[key].name == 'dropdown_checkbox') {
2041
2073
  asJSONb.push(key)
2042
2074
  }
2043
- if (MYMODEL.widgets[key].name == 'json_array' || MYMODEL.widgets[key].name == 'array') {
2075
+ if (MYMODEL.widgets[key].name == 'json_array' || MYMODEL.widgets[key].name == 'array' || MYMODEL.widgets[key].name == 'dropzone') {
2044
2076
  asArray.push(key)
2045
2077
  }
2046
2078
  }
@@ -2048,7 +2080,7 @@ zRoute.listData = async (req, res, MYMODEL, zRole, actionButtonsFn) => {
2048
2080
  const columns = body.columns
2049
2081
  if (MYMODEL.keys.includes('company_id')) {
2050
2082
  whereArray.push({
2051
- field: 'company_id',
2083
+ field: `${table}.company_id`,
2052
2084
  option: '=',
2053
2085
  value: res.locals.companyId,
2054
2086
  operator: 'AND',
@@ -2068,7 +2100,7 @@ zRoute.listData = async (req, res, MYMODEL, zRole, actionButtonsFn) => {
2068
2100
  }
2069
2101
  if (astype === 'jsonb') {
2070
2102
  whereArray.push({
2071
- field: fields[item.data],
2103
+ field: Util.fieldWithTable(fields[item.data], MYMODEL),
2072
2104
  option: ' ',
2073
2105
  value: ` @> '["${item.search.value}"]'`,
2074
2106
  operator: 'AND',
@@ -2078,13 +2110,13 @@ zRoute.listData = async (req, res, MYMODEL, zRole, actionButtonsFn) => {
2078
2110
  whereArray.push({
2079
2111
  field: '',
2080
2112
  option: '',
2081
- value: ` ${fields[item.data]}::text ILIKE '${item.search.value}' `,
2113
+ value: ` ${Util.fieldWithTable(fields[item.data], MYMODEL)}::text ILIKE '${item.search.value}' `,
2082
2114
  operator: 'AND',
2083
2115
  type: 'inline',
2084
2116
  })
2085
2117
  } else {
2086
2118
  whereArray.push({
2087
- field: fields[item.data],
2119
+ field: Util.fieldWithTable(fields[item.data], MYMODEL),
2088
2120
  option: MYMODEL.options[fields[item.data]],
2089
2121
  value: item.search.value,
2090
2122
  operator: 'AND',
@@ -2093,20 +2125,30 @@ zRoute.listData = async (req, res, MYMODEL, zRole, actionButtonsFn) => {
2093
2125
  }
2094
2126
  }
2095
2127
  })
2096
- const orderColumn = fields[body.order[0].column] == 'actionColumn' ? 'id' : fields[body.order[0].column] == 'no' ? 'id' : fields[body.order[0].column] == 'actionColum' ? 'id' : fields[body.order[0].column]
2097
- const rows = await connection.results({
2128
+
2129
+ const orderColumn = fields[body.order[0].column] == 'actionColumn' ? `${table}.id` : fields[body.order[0].column] == 'no' ? `${table}.id` : fields[body.order[0].column] == 'actionColum' ? `${table}.id` : fields[body.order[0].column]
2130
+ let gridData = {
2098
2131
  select: select,
2099
2132
  table: MYMODEL.table,
2100
2133
  whereArray: whereArray,
2101
2134
  limit: body.length,
2102
2135
  offset: body.start,
2103
- orderBy: [orderColumn, body.order[0].dir],
2104
- })
2105
- const count = await connection.result({
2106
- select: 'count(id) as count',
2136
+ order_by: [`${orderColumn} ${body.order[0].dir}`],
2137
+ }
2138
+ let joins = Util.tableWithJoin(MYMODEL)
2139
+ if (joins.length > 0) {
2140
+ gridData.joins = joins
2141
+ }
2142
+ const rows = await connection.results(gridData)
2143
+ let gridCount = {
2144
+ select: `count(${table}.id) as count`,
2107
2145
  table: MYMODEL.table,
2108
2146
  whereArray: whereArray,
2109
- })
2147
+ }
2148
+ if (joins.length > 0) {
2149
+ gridCount.joins = joins
2150
+ }
2151
+ const count = await connection.result(gridCount)
2110
2152
  let datas = []
2111
2153
  const levels = zRole.myLevel(req, res, MYMODEL.table)
2112
2154
  rows.forEach(function (row, index) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.3.24",
3
+ "version": "1.3.25",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"