q-koa 13.4.1 → 13.4.2

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
@@ -23,7 +23,23 @@ const jwt = require('jsonwebtoken')
23
23
  const verify = util.promisify(jwt.verify)
24
24
  const fsExtra = require('fs-extra')
25
25
  const { getObject, getAppConfig, formatLiteralObj } = require('./file/utils')
26
-
26
+ const redisVirtual = {
27
+ hSet: () => {
28
+ console.log('redisVirtual hSet')
29
+ },
30
+ hGet: () => {
31
+ console.log('redisVirtual hGet')
32
+ },
33
+ set: () => {
34
+ console.log('redisVirtual set')
35
+ },
36
+ get: () => {
37
+ console.log('redisVirtual get')
38
+ },
39
+ publish: () => {
40
+ console.log('redisVirtual publish')
41
+ },
42
+ }
27
43
  const restc = require('./restc')
28
44
  const APP_DIR = 'app'
29
45
  const PLUGINS_DIR = 'plugins'
@@ -467,6 +483,7 @@ class APP {
467
483
  } else {
468
484
  this.app[appName] = {
469
485
  sequelize: db,
486
+ redisClient: redisVirtual,
470
487
  appConfig: _.defaultsDeep(
471
488
  configExist ? require(path.resolve(__dirname, configPath)) : {},
472
489
  this.config.app,
@@ -686,7 +703,53 @@ class APP {
686
703
  const indexes = _.cloneDeep(
687
704
  _.get(this.app[appName].config[folder], 'indexList', [])
688
705
  )
706
+ let hookList = _.cloneDeep(
707
+ _.get(this.app[appName].config[folder], 'hookList', [])
708
+ )
709
+ if (
710
+ hookList.some((h) =>
711
+ ['afterDestroy', 'beforeDestroy'].includes(h)
712
+ ) &&
713
+ !hookList.includes('beforeBulkDestroy')
714
+ ) {
715
+ hookList = [...hookList, 'beforeBulkDestroy']
716
+ }
717
+ let reduceHook = {}
718
+ for (let i = 0; i < hookList.length; i++) {
719
+ const hook = hookList[i]
720
+ reduceHook = {
721
+ ...reduceHook,
722
+ [hook]: (target, t) => {
723
+ if (['beforeBulkDestroy'].includes(hook)) {
724
+ target.individualHooks = true
725
+ }
726
+ try {
727
+ this.app[appName].service &&
728
+ this.app[appName].service[folder] &&
729
+ this.app[appName].service[folder][hook] &&
730
+ this.app[appName].service[folder][hook]({
731
+ app: this.app[appName],
732
+ model: folder,
733
+ hook,
734
+ ...(target.toJSON
735
+ ? {
736
+ target: target.toJSON(),
737
+ }
738
+ : {
739
+ target,
740
+ }),
741
+ ...t,
742
+ })
743
+ } catch (e) {
744
+ console.log(e)
745
+ }
689
746
 
747
+ if (['beforeBulkDestroy'].includes(hook)) {
748
+ return target
749
+ }
750
+ },
751
+ }
752
+ }
690
753
  const instance = this.app[appName].sequelize.define(
691
754
  folder,
692
755
  attributes,
@@ -701,6 +764,7 @@ class APP {
701
764
  plural: `${folder}s`,
702
765
  },
703
766
  hooks: {
767
+ ...reduceHook,
704
768
  beforeCreate: (res, t) => {
705
769
  try {
706
770
  if (folder === 'log') {
@@ -1120,7 +1184,7 @@ class APP {
1120
1184
  } = ctx.request.body
1121
1185
  if (['findOne', 'findAll', 'findAndCountAll'].includes(fn)) {
1122
1186
  if (!_.isEmpty(other)) {
1123
- throw new Error('参数不合理,条件需要放在where里')
1187
+ throw new Error('参数不合理')
1124
1188
  }
1125
1189
  }
1126
1190
 
@@ -219,3 +219,10 @@ exports.alias = {
219
219
  return name + model
220
220
  },
221
221
  }
222
+
223
+ exports.hookList = {
224
+ type: Sequelize.JSON,
225
+ comment: 'hookd',
226
+ allowNull: false,
227
+ defaultValue: [],
228
+ }
@@ -111,6 +111,7 @@ exports.loadModel = async ({ app, appName }) => {
111
111
  deleteCheckList: config.deleteCheckList,
112
112
  bulkCreateList: config.bulkCreateList,
113
113
  indexList: config.indexList,
114
+ hookList: config.hookList,
114
115
  list: attributes.map((attr, index) => {
115
116
  const name = attr.match(/exports.(.*)=/)[1].trim()
116
117
  let defaultValue = ''
@@ -230,12 +230,19 @@ exports.customRouter = async (ctx) => {
230
230
  return list
231
231
  }
232
232
 
233
- exports.showTables = async (ctx) => {
233
+ exports.showTables = async (ctx, _data) => {
234
234
  const { app, appName } = getAppByCtx(ctx)
235
+ const { omit = [] } = _data || ctx.request.body
235
236
 
237
+ const mapFn = (item) => {
238
+ if (omit.length === 0) return item
239
+ return lodash.omit(item.toJSON ? item.toJSON() : item, omit)
240
+ }
236
241
  if (app.cache && app.cache.get('showTables')) {
237
- ctx.SUCCESS(app.cache.get('showTables'))
238
- return app.cache.get('showTables')
242
+ const cacheResult = app.cache.get('showTables')
243
+ const payLoad = cacheResult.map(mapFn)
244
+ ctx.SUCCESS(payLoad)
245
+ return payLoad
239
246
  }
240
247
 
241
248
  const pluginDir = path.resolve(
@@ -315,7 +322,13 @@ exports.showTables = async (ctx) => {
315
322
  // 可排序字段
316
323
  order: lodash.get(target, 'order', []),
317
324
  // 默认排序
318
- defaultOrder: lodash.get(target, 'defaultOrder', []),
325
+ defaultOrder: getValueByModelName(
326
+ target,
327
+ 'defaultOrder',
328
+ [],
329
+ model,
330
+ _model
331
+ ),
319
332
  // 下拉筛选
320
333
  select: getValueByModelName(target, 'select', [], model, _model),
321
334
  // 是否可以选择批量删除
@@ -356,19 +369,25 @@ exports.showTables = async (ctx) => {
356
369
  // can be mock boolean
357
370
  mock,
358
371
  // reference model query
359
- referenceSelect: lodash.get(target, 'referenceSelect', []),
372
+ referenceSelect: getValueByModelName(
373
+ target,
374
+ 'referenceSelect',
375
+ [],
376
+ model,
377
+ _model
378
+ ),
360
379
  // 是否可以选择批量删除
361
380
  availableSort: lodash.get(target, 'availableSort', false),
362
381
  // 用于后台管理不要查询太多数据,常用于表多的model
363
382
  autoData: lodash.get(target, 'autoData', null),
364
383
  // 有些model需要前置查询其它model数据
365
- initList: lodash.get(target, 'initList', []),
384
+ initList: getValueByModelName(target, 'initList', [], model, _model),
366
385
  // 有些table需要前置查询数据
367
386
  initTableList: lodash.get(target, 'initTableList', []),
368
387
  // 局部更新列表
369
388
  editInline: lodash.get(target, 'editInline', {}),
370
389
  // 有些model需要前置查询其它model数据
371
- comment: lodash.get(target, 'comment', {}),
390
+ comment: getValueByModelName(target, 'comment', {}, model, _model),
372
391
  // 默认查询,覆盖之前
373
392
  modelQuery: lodash.get(target, 'modelQuery', {}),
374
393
  // 默认更新,覆盖之前
@@ -386,9 +405,10 @@ exports.showTables = async (ctx) => {
386
405
  }
387
406
  })
388
407
 
389
- ctx.SUCCESS(models)
390
- app.cache && app.cache.set('showTables', models)
391
- return models
408
+ const payLoad = models.map(mapFn)
409
+ ctx.SUCCESS(payLoad)
410
+ app.cache && app.cache.set('showTables', models.map(mapFn))
411
+ return payLoad
392
412
  }
393
413
 
394
414
  exports.getTable = async (ctx) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "13.4.1",
3
+ "version": "13.4.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {