q-koa 11.8.1 → 11.8.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
@@ -685,9 +685,13 @@ class APP {
685
685
  this.app[appName]
686
686
  )
687
687
  } else if (
688
- ['controller', 'validate', 'afterExecute', 'service'].includes(
689
- n
690
- )
688
+ [
689
+ 'config',
690
+ 'controller',
691
+ 'validate',
692
+ 'afterExecute',
693
+ 'service',
694
+ ].includes(n)
691
695
  ) {
692
696
  const defaultPath = path.resolve(
693
697
  __dirname,
@@ -16,4 +16,11 @@ module.exports = {
16
16
  is_split_count: false,
17
17
  show_virtual: false,
18
18
  deleteCheckList: [],
19
+ indexList: [
20
+ {
21
+ name: 'key',
22
+ unique: true,
23
+ fields: ['key'],
24
+ },
25
+ ],
19
26
  }
@@ -5,7 +5,7 @@ exports.key = {
5
5
  comment: 'key',
6
6
  allowNull: false,
7
7
  is_mock: true,
8
- unique: true,
8
+ unique: 'key',
9
9
  sortOrder: 1,
10
10
  mock: () => Random.ctitle(),
11
11
  }
@@ -16,4 +16,12 @@ module.exports = {
16
16
  sortOrder: 1,
17
17
  reference: [],
18
18
  excludeAuth: [],
19
+ indexList: [
20
+ {
21
+ name: 'openid',
22
+ unique: true,
23
+ fields: ['openid'],
24
+ },
25
+ { fields: ['user_id'] },
26
+ ],
19
27
  }
@@ -7,7 +7,7 @@ exports.openid = {
7
7
  is_mock: false,
8
8
  defaultValue: '',
9
9
  sortOrder: 1,
10
- unique: true,
10
+ unique: ['openid'],
11
11
  }
12
12
 
13
13
  exports.user_id = {
@@ -11,6 +11,32 @@ const cache = new LRU({
11
11
  maxAge: 1000 * 60 * 60,
12
12
  })
13
13
 
14
+ const isSameArray = (_arr1 = [], _arr2 = [], deep = true) => {
15
+ const arr1 = Array.isArray(_arr1) ? _arr1 : [_arr1]
16
+ const arr2 = Array.isArray(_arr2) ? _arr2 : [_arr2]
17
+ return lodash.isEqualWith(
18
+ deep ? arr1 : arr1.sort(),
19
+ deep ? arr2 : arr2.sort(),
20
+ lodash.isEqual
21
+ )
22
+ }
23
+ const compareIndex = (tableIndexList, modelIndexList = [], model) => {
24
+ const flag =
25
+ tableIndexList
26
+ .filter((i) => i.name !== 'PRIMARY')
27
+ .filter((i) => {
28
+ return !(
29
+ ((i.name.startsWith(model) &&
30
+ i.fields.every((f) => i.name.includes(f))) ||
31
+ modelIndexList.some((m) => m.name === i.name)) &&
32
+ modelIndexList.some((m) => isSameArray(m.fields, i.fields, true))
33
+ )
34
+ }).length === 0 &&
35
+ tableIndexList.filter((i) => i.name !== 'PRIMARY').length ===
36
+ modelIndexList.length
37
+ return flag
38
+ }
39
+
14
40
  exports.initModel = async (ctx) => {
15
41
  const { app, appName } = getAppByCtx(ctx)
16
42
  const { model, option } = ctx.request.body
@@ -674,3 +700,69 @@ exports.copyOther = async (ctx) => {
674
700
  }
675
701
  ctx.SUCCESS({ successList, failList })
676
702
  }
703
+
704
+ exports.checkIndex = async (ctx) => {
705
+ const { app, appName } = getAppByCtx(ctx)
706
+
707
+ const { model } = ctx.request.body
708
+ if (!model) return ctx.ERROR('model不能为空')
709
+
710
+ if (model === 'all') {
711
+ const modelList = Object.keys(app.model)
712
+ let res = {}
713
+ for (let i = 0; i < modelList.length; i++) {
714
+ const current = modelList[i]
715
+ try {
716
+ const tableIndexList = await app.service.system.getTableIndex({
717
+ app,
718
+ model: current,
719
+ })
720
+ const modelIndexList = app.config[current].indexList
721
+
722
+ const isSame = compareIndex(tableIndexList, modelIndexList, current)
723
+ if (isSame) {
724
+ } else {
725
+ res = {
726
+ ...res,
727
+ [current]: {
728
+ isSame,
729
+ tableIndexList,
730
+ },
731
+ }
732
+ }
733
+ } catch (e) {
734
+ throw new Error(`${current}: ${e.message}`)
735
+ }
736
+ }
737
+ return ctx.SUCCESS(res)
738
+ } else {
739
+ const tableIndexList = await app.service.system.getTableIndex({
740
+ app,
741
+ model,
742
+ })
743
+
744
+ const modelIndexList = app.config[model].indexList
745
+
746
+ const isSame = compareIndex(tableIndexList, modelIndexList, model)
747
+ return ctx.SUCCESS({
748
+ isSame,
749
+ ...(isSame ? {} : { tableIndexList }),
750
+ })
751
+ }
752
+ }
753
+
754
+ exports.dropIndex = async (ctx) => {
755
+ const { app, appName } = getAppByCtx(ctx)
756
+ const { model, key } = ctx.request.body
757
+
758
+ if (!model) return ctx.ERROR(`drop model?`)
759
+ const keyList = Array.isArray(key) ? key : [key]
760
+ if (keyList.length === 0) return ctx.ERROR(`drop key?`)
761
+
762
+ const dropStr = keyList.map((item) => `DROP KEY \`${item}\``).join(',')
763
+ const result = await app.sequelize.query(
764
+ `ALTER TABLE \`${model}\` ${dropStr}`
765
+ )
766
+
767
+ ctx.SUCCESS(result)
768
+ }
@@ -327,3 +327,17 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
327
327
  }
328
328
  return obj
329
329
  }
330
+
331
+ exports.getTableIndex = async ({ app, model }) => {
332
+ if (!model) throw new Error(`没有${model}`)
333
+
334
+ const res = await app.sequelize.query(`show keys from \`${model}\``)
335
+ const groupRes = lodash.groupBy(res[0], 'Key_name')
336
+ const result = Object.keys(groupRes).map((Key_name) => {
337
+ return {
338
+ name: groupRes[Key_name][0].Key_name,
339
+ fields: groupRes[Key_name].map((i) => i.Column_name),
340
+ }
341
+ })
342
+ return result
343
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "11.8.1",
3
+ "version": "11.8.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {