q-koa 13.2.8 → 13.3.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.
package/core/app.js CHANGED
@@ -55,6 +55,15 @@ const runVm = (_str) => {
55
55
  return nodeVm.runInNewContext(_str)
56
56
  }
57
57
 
58
+ const getLiteral = (str) => {
59
+ const reg = /literal\((.+)\)/
60
+ const result = str.match(reg)
61
+ if (result) {
62
+ return result[1] // 返回第一个捕获组
63
+ }
64
+ return ''
65
+ }
66
+
58
67
  const memRunVm = _.memoize(runVm)
59
68
  const connectDatabase = (database) => (config) => {
60
69
  if (!database) throw new Error('请配置数据库名称')
@@ -504,7 +513,15 @@ class APP {
504
513
  ) {
505
514
  try {
506
515
  this.app[appName].appConfig.router(router)(this.app[appName])
507
- } catch (e) {}
516
+ } catch (e) {
517
+ this.app[appName].service.log &&
518
+ this.app[appName].service.log.push({
519
+ app: this.app[appName],
520
+ appName,
521
+ message: e.message,
522
+ })
523
+ console.log(e.message)
524
+ }
508
525
  }
509
526
 
510
527
  if (this.config.defaultRouter) {
@@ -1053,7 +1070,7 @@ class APP {
1053
1070
  }
1054
1071
  }
1055
1072
 
1056
- if (fn === 'findAll') {
1073
+ if (['findAll', 'findAndCountAll'].includes(fn)) {
1057
1074
  const disabledFindAllList = _.get(
1058
1075
  app[appName],
1059
1076
  'appConfig.disabledFindAllList',
@@ -1067,7 +1084,7 @@ class APP {
1067
1084
  (where && !_.isEmpty(where))
1068
1085
  )
1069
1086
  ) {
1070
- throw new Error(`请使用条件`)
1087
+ throw new Error('请使用条件')
1071
1088
  }
1072
1089
  }
1073
1090
 
@@ -1229,20 +1246,21 @@ class APP {
1229
1246
  if (
1230
1247
  fn === 'findOne' &&
1231
1248
  attributes &&
1249
+ Array.isArray(attributes) &&
1232
1250
  attributes.length > 0 &&
1233
1251
  attributes.every(
1234
1252
  (attribute) =>
1235
1253
  Array.isArray(attribute) &&
1236
1254
  attribute.length === 2 &&
1237
- attribute.some((o) => o.includes('SUM'))
1255
+ attribute.some((o) => o.startsWith('literal'))
1238
1256
  )
1239
1257
  ) {
1240
1258
  const target = await app[appName].model[controller][fn]({
1241
1259
  attributes: attributes.map((attribute) => {
1242
1260
  if (Array.isArray(attribute)) {
1243
1261
  return attribute.map((att) => {
1244
- if (att.startsWith('SUM')) {
1245
- return Sequelize.literal(att)
1262
+ if (att.startsWith('literal')) {
1263
+ return Sequelize.literal(getLiteral(att))
1246
1264
  }
1247
1265
  return att
1248
1266
  })
@@ -1956,8 +1974,6 @@ class APP {
1956
1974
  (ctx.request[`${appName}-user`] &&
1957
1975
  ((ctx.request[`${appName}-user`].name &&
1958
1976
  ctx.request[`${appName}-user`].name === 'admin') ||
1959
- (ctx.request[`${appName}-user`].mobile &&
1960
- ctx.request[`${appName}-user`].mobile === '18978909244') ||
1961
1977
  ctx.request[`${appName}-user`].is_admin ||
1962
1978
  ctx.request[`${appName}-user`].is_dev))
1963
1979
  ? ['updated_at']
@@ -8,5 +8,5 @@ module.exports = {
8
8
  defaultOrder: [],
9
9
  comment: {},
10
10
  reference: [],
11
-
12
- };
11
+ indexList: [{ fields: ['model_id'] }],
12
+ }
@@ -141,6 +141,10 @@ exports.html = async (ctx) => {
141
141
  code,
142
142
  },
143
143
  })
144
+ if (!target) {
145
+ ctx.body = `没有code`
146
+ return
147
+ }
144
148
  ctx.body = `<!DOCTYPE html>
145
149
  <html lang="en">
146
150
  <head>
@@ -39,9 +39,13 @@ const compareIndex = (tableIndexList, modelIndexList = [], model) => {
39
39
  }
40
40
  }
41
41
 
42
- const flag = tableIndexList.every((i) => {
43
- return modelIndexList.some((m) => isSameArray(m.fields, i.fields, true))
44
- })
42
+ const flag =
43
+ tableIndexList.every((i) => {
44
+ return modelIndexList.some((m) => isSameArray(m.fields, i.fields, true))
45
+ }) &&
46
+ modelIndexList.every((i) => {
47
+ return tableIndexList.some((m) => isSameArray(m.fields, i.fields, true))
48
+ })
45
49
 
46
50
  const intersection = lodash.intersectionWith(
47
51
  tableIndexList,
@@ -145,7 +149,7 @@ exports.initModel = async (ctx) => {
145
149
  )
146
150
  .then((res) => res.data)
147
151
  if (code === 200) {
148
- const findList = Object.keys(productionModel).filter((key) => {
152
+ const findList = Object.keys(productionModel || {}).filter((key) => {
149
153
  return (
150
154
  !['id', 'created_at', 'createdid', 'updated_at'].includes(key) &&
151
155
  !Object.keys(app.attributes[model]).includes(key)
@@ -8,7 +8,14 @@ const runVm = (_str) => {
8
8
  }
9
9
 
10
10
  const memRunVm = lodash.memoize(runVm)
11
-
11
+ const getLiteral = (str) => {
12
+ const reg = /literal\((.+)\)/
13
+ const result = str.match(reg)
14
+ if (result) {
15
+ return result[1] // 返回第一个捕获组
16
+ }
17
+ return ''
18
+ }
12
19
  exports.initData = async ({ includes, excludes, app, ctx }) => {
13
20
  const appConfig = getConfig(app)
14
21
  const { version: cacheVersion } = await appConfig.getObject('base')
@@ -211,6 +218,40 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
211
218
  }
212
219
  })
213
220
  } else {
221
+ if (data && lodash.isObject(data)) {
222
+ if (
223
+ fn === 'findOne' &&
224
+ 'attributes' in data &&
225
+ Array.isArray(data.attributes) &&
226
+ data.attributes.every((attribute) =>
227
+ Array.isArray(attribute)
228
+ ) &&
229
+ data.attributes.some(
230
+ (attribute) =>
231
+ Array.isArray(attribute) &&
232
+ attribute.some((att) => att.startsWith('literal'))
233
+ )
234
+ ) {
235
+ return app.model[model][fn]({
236
+ where: data.where,
237
+ attributes: data.attributes.map((attribute) => {
238
+ if (Array.isArray(attribute)) {
239
+ return attribute.map((att) => {
240
+ if (att.startsWith('literal')) {
241
+ return Sequelize.literal(getLiteral(att))
242
+ }
243
+ return att
244
+ })
245
+ }
246
+ return attribute
247
+ }),
248
+ }).then((res) => {
249
+ return {
250
+ [item]: res,
251
+ }
252
+ })
253
+ }
254
+ }
214
255
  const formatData =
215
256
  fn === 'findAll' &&
216
257
  data &&
@@ -145,9 +145,10 @@ module.exports = class Singleton {
145
145
  }
146
146
  }
147
147
 
148
- async getPrintList(machineCode, pageIndex = 1, pageSize = 10) {
148
+ async getPrintList(_machineCode, pageIndex = 1, pageSize = 10) {
149
149
  const app = this.config.app
150
150
  const accessToken = await this.getAccessToken()
151
+ const machineCode = _machineCode || this.config.machine_code
151
152
  try {
152
153
  const RpcClient = new yly.RpcClient(accessToken, this.oauthConfig)
153
154
  const Print = new yly.Printer(RpcClient)
@@ -214,14 +214,45 @@ const isDateTime = (str) => {
214
214
  /^(\d{4}-\d{2}-\d{2})([T\s](\d{2}:\d{2}(:\d{2}(\.\d+)?)?)([+-]\d{2}:\d{2}|Z)?)?$/
215
215
  return dateRegex.test(str)
216
216
  }
217
+
218
+ const getCol = (str) => {
219
+ const reg = /col\((.+)\)/
220
+ const result = str.match(reg)
221
+ if (result) {
222
+ return result[1] // 返回第一个捕获组
223
+ }
224
+ return ''
225
+ }
226
+
227
+ const getLiteral = (str) => {
228
+ const reg = /literal\((.+)\)/
229
+ const result = str.match(reg)
230
+ if (result) {
231
+ return result[1] // 返回第一个捕获组
232
+ }
233
+ return ''
234
+ }
217
235
  const $lt = {
218
236
  check: (param) => typeof param === 'string',
219
237
  result: (param, key) => {
238
+ if (getCol(param[key])) {
239
+ return {
240
+ [key]: Sequelize.col(getCol(param[key])),
241
+ }
242
+ }
243
+
244
+ if (getLiteral(param[key])) {
245
+ return {
246
+ [key]: Sequelize.literal(getLiteral(param[key])),
247
+ }
248
+ }
249
+
220
250
  if (isDateTime(param[key])) {
221
251
  return {
222
252
  [key]: param[key],
223
253
  }
224
254
  }
255
+
225
256
  return {
226
257
  [key]: Sequelize.literal(param[key]),
227
258
  }
@@ -230,6 +261,17 @@ const $lt = {
230
261
  const $in = {
231
262
  check: (param) => typeof param === 'string',
232
263
  result: (param, key) => {
264
+ if (getCol(param[key])) {
265
+ return {
266
+ [key]: Sequelize.col(getCol(param[key])),
267
+ }
268
+ }
269
+
270
+ if (getLiteral(param[key])) {
271
+ return {
272
+ [key]: Sequelize.literal(getLiteral(param[key])),
273
+ }
274
+ }
233
275
  return {
234
276
  [key]: [Sequelize.literal(param[key])],
235
277
  }
@@ -237,8 +279,13 @@ const $in = {
237
279
  }
238
280
  const $between = {
239
281
  check: (param) =>
240
- Array.isArray(param) && param.every((p) => typeof param === 'string'),
282
+ Array.isArray(param) && param.every((p) => typeof p === 'string'),
241
283
  result: (param, key) => {
284
+ if (param[key].every((i) => getLiteral(i))) {
285
+ return {
286
+ [key]: param[key].map((item) => Sequelize.literal(getLiteral(item))),
287
+ }
288
+ }
242
289
  if (param[key].every((i) => isDateTime(i))) {
243
290
  return {
244
291
  [key]: param[key],
@@ -275,7 +322,7 @@ exports.formatLiteralObj = (target) => {
275
322
  return target
276
323
  }
277
324
 
278
- exports.filterFunction = (target, data) => {
325
+ const filterFunction = (target, data) => {
279
326
  if (!target) {
280
327
  return false
281
328
  }
@@ -286,6 +333,16 @@ exports.filterFunction = (target, data) => {
286
333
  if (typeof target[key] === 'boolean') {
287
334
  return Boolean(data[key]) === target[key]
288
335
  }
336
+ if ('$and' in target) {
337
+ return target.$and.every((item) => {
338
+ return filterFunction(item, data)
339
+ })
340
+ }
341
+ if ('$or' in target) {
342
+ return target.$or.some((item) => {
343
+ return filterFunction(item, data)
344
+ })
345
+ }
289
346
  if (Array.isArray(target[key])) {
290
347
  return target[key].includes(data[key])
291
348
  }
@@ -339,3 +396,5 @@ exports.filterFunction = (target, data) => {
339
396
  return data[key] === target[key]
340
397
  })
341
398
  }
399
+
400
+ exports.filterFunction = filterFunction
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "13.2.8",
3
+ "version": "13.3.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {