q-koa 11.8.1 → 11.8.3

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 = {
@@ -0,0 +1,23 @@
1
+ module.exports = {
2
+ name: '微信开放平台用户',
3
+ belongs: 'page',
4
+ multiple: false,
5
+ availableSort: false,
6
+ order: ['created_at'],
7
+ select: [{ key: 'user_id' }, { key: 'nick_name' }],
8
+ excludes: [],
9
+ limit: 20,
10
+ defaultOrder: [{ sort: 'descending', type: 'id' }],
11
+ comment: { created_at: '创建时间' },
12
+ sortOrder: 1,
13
+ reference: [],
14
+ excludeAuth: [],
15
+ indexList: [
16
+ {
17
+ name: 'openid',
18
+ unique: true,
19
+ fields: ['openid'],
20
+ },
21
+ { fields: ['user_id'] },
22
+ ],
23
+ }
@@ -0,0 +1,59 @@
1
+ const { Sequelize, Random } = require('q-koa')
2
+
3
+ exports.openid = {
4
+ type: Sequelize.STRING,
5
+ comment: 'openid',
6
+ allowNull: false,
7
+ is_mock: false,
8
+ defaultValue: '',
9
+ sortOrder: 1,
10
+ unique: ['openid'],
11
+ }
12
+
13
+ exports.user_id = {
14
+ type: Sequelize.MEDIUMINT.UNSIGNED,
15
+ comment: '用户',
16
+ allowNull: false,
17
+ is_mock: false,
18
+ defaultValue: 0,
19
+ sortOrder: 2,
20
+ hasOne: true,
21
+ }
22
+
23
+ exports.nick_name = {
24
+ type: Sequelize.STRING,
25
+ comment: '昵称',
26
+ allowNull: false,
27
+ is_mock: false,
28
+ defaultValue: '',
29
+ sortOrder: 3,
30
+ }
31
+
32
+ exports.avatar = {
33
+ type: Sequelize.STRING(1000),
34
+ comment: '头像',
35
+ allowNull: false,
36
+ is_mock: false,
37
+ defaultValue: '',
38
+ sortOrder: 8,
39
+ }
40
+
41
+ exports.appid = {
42
+ type: Sequelize.STRING,
43
+ comment: 'app_id',
44
+ allowNull: false,
45
+ is_mock: true,
46
+ defaultValue: '',
47
+ sortOrder: 10,
48
+ mock: () => Random.ctitle(),
49
+ }
50
+
51
+ exports.unionid = {
52
+ type: Sequelize.STRING,
53
+ comment: 'unionid',
54
+ allowNull: false,
55
+ is_mock: true,
56
+ defaultValue: '',
57
+ sortOrder: 11,
58
+ mock: () => Random.ctitle(),
59
+ }
@@ -11,6 +11,37 @@ 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
32
+ .filter((i) => i.name !== 'PRIMARY')
33
+ .some((m) => m.name === i.name)) &&
34
+ modelIndexList
35
+ .filter((i) => i.name !== 'PRIMARY')
36
+ .some((m) => isSameArray(m.fields, i.fields, true))
37
+ )
38
+ }).length === 0 &&
39
+ tableIndexList.filter((i) => i.name !== 'PRIMARY').length ===
40
+ modelIndexList.filter((i) => i.name !== 'PRIMARY').length
41
+
42
+ return flag
43
+ }
44
+
14
45
  exports.initModel = async (ctx) => {
15
46
  const { app, appName } = getAppByCtx(ctx)
16
47
  const { model, option } = ctx.request.body
@@ -674,3 +705,69 @@ exports.copyOther = async (ctx) => {
674
705
  }
675
706
  ctx.SUCCESS({ successList, failList })
676
707
  }
708
+
709
+ exports.checkIndex = async (ctx) => {
710
+ const { app, appName } = getAppByCtx(ctx)
711
+
712
+ const { model } = ctx.request.body
713
+ if (!model) return ctx.ERROR('model不能为空')
714
+
715
+ if (model === 'all') {
716
+ const modelList = Object.keys(app.model)
717
+ let res = {}
718
+ for (let i = 0; i < modelList.length; i++) {
719
+ const current = modelList[i]
720
+ try {
721
+ const tableIndexList = await app.service.system.getTableIndex({
722
+ app,
723
+ model: current,
724
+ })
725
+ const modelIndexList = app.config[current].indexList
726
+
727
+ const isSame = compareIndex(tableIndexList, modelIndexList, current)
728
+ if (isSame) {
729
+ } else {
730
+ res = {
731
+ ...res,
732
+ [current]: {
733
+ isSame,
734
+ tableIndexList,
735
+ },
736
+ }
737
+ }
738
+ } catch (e) {
739
+ throw new Error(`${current}: ${e.message}`)
740
+ }
741
+ }
742
+ return ctx.SUCCESS(res)
743
+ } else {
744
+ const tableIndexList = await app.service.system.getTableIndex({
745
+ app,
746
+ model,
747
+ })
748
+
749
+ const modelIndexList = app.config[model].indexList
750
+
751
+ const isSame = compareIndex(tableIndexList, modelIndexList, model)
752
+ return ctx.SUCCESS({
753
+ isSame,
754
+ ...(isSame ? {} : { tableIndexList }),
755
+ })
756
+ }
757
+ }
758
+
759
+ exports.dropIndex = async (ctx) => {
760
+ const { app, appName } = getAppByCtx(ctx)
761
+ const { model, key } = ctx.request.body
762
+
763
+ if (!model) return ctx.ERROR(`drop model?`)
764
+ const keyList = Array.isArray(key) ? key : [key]
765
+ if (keyList.length === 0) return ctx.ERROR(`drop key?`)
766
+
767
+ const dropStr = keyList.map((item) => `DROP KEY \`${item}\``).join(',')
768
+ const result = await app.sequelize.query(
769
+ `ALTER TABLE \`${model}\` ${dropStr}`
770
+ )
771
+
772
+ ctx.SUCCESS(result)
773
+ }
@@ -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
+ }
@@ -3,13 +3,21 @@ module.exports = {
3
3
  belongs: 'page',
4
4
  multiple: false,
5
5
  availableSort: false,
6
- order: ["created_at"],
7
- select: [{"key":"user_id"},{"key":"nick_name"}],
6
+ order: ['created_at'],
7
+ select: [{ key: 'user_id' }, { key: 'nick_name' }],
8
8
  excludes: [],
9
9
  limit: 20,
10
10
  defaultOrder: [{ sort: 'descending', type: 'id' }],
11
- comment: {"created_at":"创建时间"},
11
+ comment: { created_at: '创建时间' },
12
12
  sortOrder: 1,
13
13
  reference: [],
14
14
  excludeAuth: [],
15
+ indexList: [
16
+ {
17
+ name: 'openid',
18
+ unique: true,
19
+ fields: ['openid'],
20
+ },
21
+ { fields: ['user_id'] },
22
+ ],
15
23
  }
@@ -4,7 +4,7 @@ exports.openid = {
4
4
  type: Sequelize.STRING,
5
5
  comment: 'openid',
6
6
  allowNull: false,
7
- unique: true,
7
+ unique: ['openid'],
8
8
  }
9
9
 
10
10
  exports.nick_name = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "11.8.1",
3
+ "version": "11.8.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {