waibu-db 2.0.1 → 2.1.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.
Files changed (124) hide show
  1. package/.github/FUNDING.yml +0 -0
  2. package/.github/workflows/repo-lockdown.yml +0 -0
  3. package/.jsdoc.conf.json +0 -0
  4. package/LICENSE +0 -0
  5. package/README.md +1 -1
  6. package/docs/WaibuDb.html +1 -1
  7. package/docs/data/search.json +0 -0
  8. package/docs/fonts/Inconsolata-Regular.ttf +0 -0
  9. package/docs/fonts/OpenSans-Regular.ttf +0 -0
  10. package/docs/fonts/WorkSans-Bold.ttf +0 -0
  11. package/docs/global.html +1 -1
  12. package/docs/index.html +0 -0
  13. package/docs/index.js.html +130 -21
  14. package/docs/scripts/core.js +0 -0
  15. package/docs/scripts/core.min.js +0 -0
  16. package/docs/scripts/resize.js +0 -0
  17. package/docs/scripts/search.js +0 -0
  18. package/docs/scripts/search.min.js +0 -0
  19. package/docs/scripts/third-party/Apache-License-2.0.txt +0 -0
  20. package/docs/scripts/third-party/fuse.js +0 -0
  21. package/docs/scripts/third-party/hljs-line-num-original.js +0 -0
  22. package/docs/scripts/third-party/hljs-line-num.js +0 -0
  23. package/docs/scripts/third-party/hljs-original.js +0 -0
  24. package/docs/scripts/third-party/hljs.js +0 -0
  25. package/docs/scripts/third-party/popper.js +0 -0
  26. package/docs/scripts/third-party/tippy.js +0 -0
  27. package/docs/scripts/third-party/tocbot.js +0 -0
  28. package/docs/scripts/third-party/tocbot.min.js +0 -0
  29. package/docs/static/bitcoin.jpeg +0 -0
  30. package/docs/static/home.md +0 -0
  31. package/docs/static/logo-ecosystem.png +0 -0
  32. package/docs/static/logo.png +0 -0
  33. package/docs/styles/clean-jsdoc-theme-base.css +0 -0
  34. package/docs/styles/clean-jsdoc-theme-dark.css +0 -0
  35. package/docs/styles/clean-jsdoc-theme-light.css +0 -0
  36. package/docs/styles/clean-jsdoc-theme-scrollbar.css +0 -0
  37. package/docs/styles/clean-jsdoc-theme-without-scrollbar.min.css +0 -0
  38. package/docs/styles/clean-jsdoc-theme.min.css +0 -0
  39. package/extend/bajo/hook/waibu-rest-api@after-init.js +3 -2
  40. package/extend/bajo/intl/en-US.json +0 -0
  41. package/extend/bajo/intl/id.json +0 -0
  42. package/extend/bajoTemplate/partial/crud/_add-attachment.html +0 -0
  43. package/extend/bajoTemplate/partial/crud/_add-btns.html +2 -2
  44. package/extend/bajoTemplate/partial/crud/_addons.html +0 -0
  45. package/extend/bajoTemplate/partial/crud/_details-attachment.html +0 -0
  46. package/extend/bajoTemplate/partial/crud/_details-btns.html +0 -0
  47. package/extend/bajoTemplate/partial/crud/_edit-attachment.html +0 -0
  48. package/extend/bajoTemplate/partial/crud/_edit-btns.html +2 -0
  49. package/extend/bajoTemplate/partial/crud/_list-attachment.html +0 -0
  50. package/extend/bajoTemplate/partial/crud/_list-btns.html +0 -0
  51. package/extend/bajoTemplate/partial/crud/_list-footer.html +0 -0
  52. package/extend/bajoTemplate/partial/crud/add-handler.html +0 -0
  53. package/extend/bajoTemplate/partial/crud/details-handler.html +0 -0
  54. package/extend/bajoTemplate/partial/crud/edit-handler.html +0 -0
  55. package/extend/bajoTemplate/partial/crud/list-handler.html +1 -1
  56. package/extend/bajoTemplate/partial/crud/~echarts-window.html +0 -0
  57. package/extend/bajoTemplate/template/crud/add.html +0 -0
  58. package/extend/bajoTemplate/template/crud/details.html +0 -0
  59. package/extend/bajoTemplate/template/crud/edit.html +0 -0
  60. package/extend/bajoTemplate/template/crud/list.html +0 -0
  61. package/extend/bajoTemplate/template/disabled.html +0 -0
  62. package/extend/waibuBootstrap/theme/component/factory/btn-add.js +0 -0
  63. package/extend/waibuBootstrap/theme/component/factory/btn-back.js +1 -1
  64. package/extend/waibuBootstrap/theme/component/factory/btn-clone.js +0 -0
  65. package/extend/waibuBootstrap/theme/component/factory/btn-columns.js +2 -2
  66. package/extend/waibuBootstrap/theme/component/factory/btn-delete.js +1 -1
  67. package/extend/waibuBootstrap/theme/component/factory/btn-details.js +1 -1
  68. package/extend/waibuBootstrap/theme/component/factory/btn-edit.js +1 -1
  69. package/extend/waibuBootstrap/theme/component/factory/btn-export.js +0 -0
  70. package/extend/waibuBootstrap/theme/component/factory/echarts-bar.js +1 -1
  71. package/extend/waibuBootstrap/theme/component/factory/echarts-pie.js +1 -1
  72. package/extend/waibuBootstrap/theme/component/factory/echarts.js +2 -2
  73. package/extend/waibuBootstrap/theme/component/factory/form.js +1 -1
  74. package/extend/waibuBootstrap/theme/component/factory/pagination.js +1 -1
  75. package/extend/waibuBootstrap/theme/component/factory/query.js +3 -3
  76. package/extend/waibuBootstrap/theme/component/factory/recs-info.js +1 -1
  77. package/extend/waibuBootstrap/theme/component/factory/table.js +7 -7
  78. package/extend/waibuBootstrap/theme/component/wdb-base.js +0 -0
  79. package/extend/waibuMpa/extend/waibuAdmin/route/@model/@action.js +0 -0
  80. package/extend/waibuRestApi/route/@model/@id/get.js +1 -1
  81. package/extend/waibuRestApi/route/@model/@id/remove.js +1 -1
  82. package/extend/waibuRestApi/route/@model/@id/update.js +1 -1
  83. package/extend/waibuRestApi/route/@model/create.js +1 -1
  84. package/extend/waibuRestApi/route/@model/find.js +2 -2
  85. package/extend/waibuRestApi/route/@model/stat/@stat/find.js +8 -0
  86. package/index.js +127 -21
  87. package/lib/crud/add-handler.js +8 -7
  88. package/lib/crud/all-handler.js +0 -0
  89. package/lib/crud/delete-handler.js +4 -3
  90. package/lib/crud/details-handler.js +4 -4
  91. package/lib/crud/edit-handler.js +7 -8
  92. package/lib/crud/export-handler.js +3 -3
  93. package/lib/crud/helper/add-ons-handler.js +2 -2
  94. package/lib/crud/helper/attachment-handler.js +2 -2
  95. package/lib/crud/helper/build-params.js +3 -4
  96. package/lib/crud/list-handler.js +3 -3
  97. package/lib/method/count-record.js +9 -0
  98. package/lib/method/create-aggregate.js +13 -0
  99. package/lib/method/create-histogram.js +13 -0
  100. package/lib/method/create-record.js +10 -0
  101. package/lib/method/find-one-record.js +14 -0
  102. package/lib/method/find-record.js +14 -0
  103. package/lib/method/get-record.js +10 -0
  104. package/{method → lib/method}/get-schema-ext.js +6 -7
  105. package/lib/method/remove-record.js +9 -0
  106. package/lib/method/update-record.js +10 -0
  107. package/lib/prep-crud.js +8 -9
  108. package/package.json +8 -2
  109. package/wiki/CHANGES.md +5 -0
  110. package/wiki/CONFIG.md +0 -0
  111. package/wiki/CONTRIBUTING.md +0 -0
  112. package/method/format-record.js +0 -41
  113. package/method/get-lookup-data.js +0 -13
  114. package/method/get-params.js +0 -18
  115. package/method/method-map.js +0 -9
  116. package/method/record/count.js +0 -10
  117. package/method/record/create.js +0 -11
  118. package/method/record/find-one.js +0 -15
  119. package/method/record/find.js +0 -15
  120. package/method/record/get.js +0 -11
  121. package/method/record/remove.js +0 -10
  122. package/method/record/update.js +0 -11
  123. package/method/stat/aggregate.js +0 -14
  124. package/method/stat/histogram.js +0 -14
@@ -6,7 +6,7 @@ async function btnBack () {
6
6
  return class WdbBtnBack extends WdbBase {
7
7
  build = async () => {
8
8
  const { isEmpty } = this.app.lib._
9
- const { attrToArray } = this.plugin.app.waibuMpa
9
+ const { attrToArray } = this.app.waibuMpa
10
10
  const { req } = this.component
11
11
  this.params.noTag = true
12
12
  if (isEmpty(this.params.attr.content)) this.params.attr.content = req.t('back')
@@ -6,9 +6,9 @@ async function btnColumns () {
6
6
  return class WdbBtnColumns extends WdbBase {
7
7
  build = async () => {
8
8
  const { get, isEmpty, without } = this.app.lib._
9
- const { jsonStringify } = this.plugin.app.waibuMpa
9
+ const { jsonStringify } = this.app.waibuMpa
10
10
  const { req } = this.component
11
- const qsKey = this.plugin.app.waibu.config.qsKey
11
+ const qsKey = this.app.waibu.config.qsKey
12
12
  const schema = get(this, 'component.locals.schema', {})
13
13
  const count = get(this, 'component.locals.list.count', 0)
14
14
  if (count === 0) this.params.attr.triggerDisabled = true
@@ -6,7 +6,7 @@ async function btnDelete () {
6
6
  return class WdbBtnDelete extends WdbBase {
7
7
  build = async () => {
8
8
  const { req } = this.component
9
- const { generateId } = this.plugin.app.bajo
9
+ const { generateId } = this.app.lib.aneka
10
10
  const { isEmpty, get } = this.app.lib._
11
11
  this.params.noTag = true
12
12
  const schema = get(this, 'component.locals.schema', {})
@@ -6,7 +6,7 @@ async function btnDetails () {
6
6
  return class WdbBtnDetails extends WdbBase {
7
7
  build = async () => {
8
8
  const { req } = this.component
9
- const { generateId } = this.plugin.app.bajo
9
+ const { generateId } = this.app.lib.aneka
10
10
  const { isEmpty, get } = this.app.lib._
11
11
  this.params.noTag = true
12
12
  const schema = get(this, 'component.locals.schema', {})
@@ -6,7 +6,7 @@ async function btnEdit () {
6
6
  return class WdbBtnEdit extends WdbBase {
7
7
  build = async () => {
8
8
  const { req } = this.component
9
- const { generateId } = this.plugin.app.bajo
9
+ const { generateId } = this.app.lib.aneka
10
10
  const { isEmpty, get } = this.app.lib._
11
11
  this.params.noTag = true
12
12
  const schema = get(this, 'component.locals.schema', {})
@@ -5,7 +5,7 @@ async function echartsBar () {
5
5
 
6
6
  return class WdbEchartsBar extends WdbEcharts {
7
7
  build = async () => {
8
- const { jsonStringify } = this.plugin.app.waibuMpa
8
+ const { jsonStringify } = this.app.waibuMpa
9
9
  const { merge } = this.app.lib._
10
10
  merge(this.setting, {
11
11
  tooltip: {
@@ -5,7 +5,7 @@ async function echartsPie () {
5
5
 
6
6
  return class WdbEchartsPie extends WdbEcharts {
7
7
  build = async () => {
8
- const { jsonStringify } = this.plugin.app.waibuMpa
8
+ const { jsonStringify } = this.app.waibuMpa
9
9
  const { merge } = this.app.lib._
10
10
  merge(this.setting, {
11
11
  tooltip: {
@@ -10,7 +10,7 @@ async function echarts () {
10
10
 
11
11
  constructor (options) {
12
12
  super(options)
13
- const { generateId } = this.plugin.app.bajo
13
+ const { generateId } = this.app.lib.aneka
14
14
  this.defSetting = {
15
15
  grid: {
16
16
  top: 8,
@@ -27,7 +27,7 @@ async function echarts () {
27
27
  }
28
28
 
29
29
  _build = ({ setting = {}, onLoad = [] } = {}) => {
30
- const { jsonStringify } = this.plugin.app.waibuMpa
30
+ const { jsonStringify } = this.app.waibuMpa
31
31
  const { merge, isArray } = this.app.lib._
32
32
  if (!isArray(onLoad)) onLoad = [onLoad]
33
33
  const option = merge({}, this.defSetting, this.setting, setting)
@@ -6,7 +6,7 @@ async function form () {
6
6
  return class WdbForm extends WdbBase {
7
7
  build = async () => {
8
8
  const { get, find, filter, forOwn, isEmpty } = this.app.lib._
9
- const { base64JsonEncode } = this.plugin.app.waibuMpa
9
+ const { base64JsonEncode } = this.app.waibuMpa
10
10
  const schema = get(this, 'component.locals.schema', {})
11
11
  const body = []
12
12
  const xModels = get(schema, 'view.x.model', [])
@@ -17,7 +17,7 @@ async function pagination () {
17
17
  return class WdbPagination extends WdbBase {
18
18
  build = async () => {
19
19
  const { req } = this.component
20
- const { attrToObject, paginationLayout, groupAttrs } = this.plugin.app.waibuMpa
20
+ const { attrToObject, paginationLayout, groupAttrs } = this.app.waibuMpa
21
21
  const { get, isNumber } = this.app.lib._
22
22
  const schema = get(this, 'component.locals.schema', {})
23
23
  if (schema.view.disabled.includes('find')) {
@@ -5,10 +5,10 @@ async function query () {
5
5
 
6
6
  return class WdbQuery extends WdbBase {
7
7
  build = async () => {
8
- const { generateId } = this.plugin.app.bajo
9
- const { jsonStringify } = this.plugin.app.waibuMpa
8
+ const { generateId } = this.app.lib.aneka
9
+ const { jsonStringify } = this.app.waibuMpa
10
10
  const { find, get, without, isEmpty, filter, upperFirst } = this.app.lib._
11
- const qsKey = this.plugin.app.waibu.config.qsKey
11
+ const qsKey = this.app.waibu.config.qsKey
12
12
  const schema = get(this, 'component.locals.schema', {})
13
13
  if (schema.view.disabled.includes('find')) {
14
14
  this.params.html = ''
@@ -7,7 +7,7 @@ async function recsInfo () {
7
7
  return class WdbRecsInfo extends WdbBase {
8
8
  build = async () => {
9
9
  const { req } = this.component
10
- const { attrToObject, groupAttrs, attrToArray } = this.plugin.app.waibuMpa
10
+ const { attrToObject, groupAttrs, attrToArray } = this.app.waibuMpa
11
11
  const { get, isEmpty, omit, merge } = this.app.lib._
12
12
  const schema = get(this, 'component.locals.schema', {})
13
13
  if (schema.view.disabled.includes('find')) {
@@ -21,11 +21,11 @@ async function table () {
21
21
 
22
22
  _defFormatter = async ({ req, key, value, data, schema }) => {
23
23
  const { get, find } = this.app.lib._
24
- const { escape } = this.plugin.app.waibu
24
+ const { escape } = this.app.waibu
25
25
  const prop = find(schema.properties, { name: key })
26
26
  if (!prop) return value
27
27
  if (prop.type === 'boolean') {
28
- value = (await this.component.buildTag({ tag: 'icon', attr: { name: `circle${data[key] ? 'Check' : ''}` } })) +
28
+ value = (await this.component.buildTag({ tag: 'icon', attr: { name: `circle${data[key] ? 'Check' : 'Cross'}` } })) +
29
29
  ' ' + (req.t(data[key] ? 'Yes' : 'No'))
30
30
  } else if (['string', 'text'].includes(prop.type)) {
31
31
  if (!get(schema, 'view.noEscape', []).includes(key)) value = escape(value)
@@ -35,9 +35,9 @@ async function table () {
35
35
 
36
36
  build = async () => {
37
37
  const { req } = this.component
38
- const { escape } = this.plugin.app.waibu
39
- const { formatRecord } = this.plugin.app.waibuDb
40
- const { attrToArray, groupAttrs } = this.plugin.app.waibuMpa
38
+ const { escape } = this.app.waibu
39
+ const { formatRecord } = this.app.waibuDb
40
+ const { attrToArray, groupAttrs } = this.app.waibuMpa
41
41
  const { get, omit, set, find, isEmpty, without, merge } = this.app.lib._
42
42
  const group = groupAttrs(this.params.attr, ['body', 'head', 'foot'])
43
43
  this.params.attr = group._
@@ -59,7 +59,7 @@ async function table () {
59
59
  this.params.html = ''
60
60
  return
61
61
  }
62
- const qsKey = this.plugin.app.waibu.config.qsKey
62
+ const qsKey = this.app.waibu.config.qsKey
63
63
  let fields = without(get(this, `component.locals._meta.query.${qsKey.fields}`, '').split(','), '')
64
64
  if (isEmpty(fields)) fields = schema.view.fields
65
65
  if (!isEmpty(schema.view.hidden)) fields = without(fields, ...schema.view.hidden)
@@ -101,7 +101,7 @@ async function table () {
101
101
  const attr = this.isRightAligned(f, schema) ? { text: 'align:end' } : {}
102
102
  const content = [
103
103
  await this.component.buildTag({ tag: 'div', attr, html: head }),
104
- await this.component.buildTag({ tag: 'a', attr: { icon, href }, prepend: '<div class="ms-1">', append: '</div>' })
104
+ await this.component.buildTag({ tag: 'a', attr: { icon, href, noIconLink: true }, prepend: '<div class="ms-1">', append: '</div>' })
105
105
  ]
106
106
  head = await this.component.buildTag({ tag: 'div', attr: { flex: 'justify-content:between align-items:end' }, html: content.join('\n') })
107
107
  }
File without changes
@@ -1,5 +1,5 @@
1
1
  async function get (req, reply) {
2
- return await this.recordGet({ req, reply })
2
+ return await this.getRecord({ req, reply })
3
3
  }
4
4
 
5
5
  export default get
@@ -1,5 +1,5 @@
1
1
  async function remove (req, reply) {
2
- return await this.recordRemove({ req, reply })
2
+ return await this.removeRecord({ req, reply })
3
3
  }
4
4
 
5
5
  export default remove
@@ -1,5 +1,5 @@
1
1
  async function update (req, reply) {
2
- return await this.recordUpdate({ req, reply })
2
+ return await this.updateRecord({ req, reply })
3
3
  }
4
4
 
5
5
  export default update
@@ -1,5 +1,5 @@
1
1
  async function create (req, reply) {
2
- return await this.recordCreate({ req, reply })
2
+ return await this.createRecord({ req, reply })
3
3
  }
4
4
 
5
5
  export default create
@@ -1,6 +1,6 @@
1
1
  async function find (req, reply) {
2
2
  const { isSet } = this.app.lib.aneka
3
- const { parseObject } = this.app.bajo
3
+ const { parseObject } = this.app.lib
4
4
  let { fields, count } = this.getParams(req)
5
5
  let rels = []
6
6
  const headers = parseObject(req.headers, { parseValue: true })
@@ -8,7 +8,7 @@ async function find (req, reply) {
8
8
  if (isSet(headers['x-rels'])) rels = headers['x-rels']
9
9
  if (typeof rels === 'string' && !['*', 'all'].includes(rels)) rels = [rels]
10
10
  const options = { fields, count, rels }
11
- return await this.recordFind({ req, reply, options })
11
+ return await this.findRecord({ req, reply, options })
12
12
  }
13
13
 
14
14
  export default find
@@ -0,0 +1,8 @@
1
+ async function stat (req, reply) {
2
+ const { camelCase } = this.app.lib._
3
+ const method = camelCase(`create ${req.params.stat}`)
4
+ if (!this[method]) throw this.error('_notFound')
5
+ return await this[method]({ req, reply })
6
+ }
7
+
8
+ export default stat
package/index.js CHANGED
@@ -1,3 +1,14 @@
1
+ import countRecord from './lib/method/count-record.js'
2
+ import createAggregate from './lib/method/create-aggregate.js'
3
+ import createHistogram from './lib/method/create-histogram.js'
4
+ import createRecord from './lib/method/create-record.js'
5
+ import findOneRecord from './lib/method/find-one-record.js'
6
+ import findRecord from './lib/method/find-record.js'
7
+ import getSchemaExt from './lib/method/get-schema-ext.js'
8
+ import getRecord from './lib/method/get-record.js'
9
+ import removeRecord from './lib/method/remove-record.js'
10
+ import updateRecord from './lib/method/update-record.js'
11
+
1
12
  /**
2
13
  * Plugin factory
3
14
  *
@@ -12,10 +23,7 @@ async function factory (pkgName) {
12
23
  *
13
24
  * @class
14
25
  */
15
- class WaibuDb extends this.app.pluginClass.base {
16
- static alias = 'wdb'
17
- static dependencies = ['dobo', 'waibu', 'bajo-queue', 'dobo-extra']
18
-
26
+ class WaibuDb extends this.app.baseClass.Base {
19
27
  constructor () {
20
28
  super(pkgName, me.app)
21
29
  this.config = {
@@ -34,15 +42,34 @@ async function factory (pkgName) {
34
42
  count: false,
35
43
  patchEnabled: false
36
44
  },
37
- modelRestApi: false
45
+ enableRestApiForModel: false
46
+ }
47
+ this.methodMap = {
48
+ create: 'POST',
49
+ find: 'GET',
50
+ get: 'GET',
51
+ update: 'PUT',
52
+ remove: 'DELETE'
38
53
  }
54
+
55
+ this.selfBind([
56
+ 'countRecord',
57
+ 'createAggregate',
58
+ 'createHistogram',
59
+ 'createRecord',
60
+ 'findOneRecord',
61
+ 'findRecord',
62
+ 'getSchemaExt',
63
+ 'getRecord',
64
+ 'removeRecord',
65
+ 'updateRecord'
66
+ ])
39
67
  }
40
68
 
41
69
  exportData = async (params) => {
42
70
  const { getPlugin } = this.app.bajo
43
71
  const { get } = this.app.lib._
44
72
  const { fs } = this.app.lib
45
- const { recordUpdate } = this.app.dobo
46
73
  const { exportTo } = this.app.doboExtra
47
74
  const { downloadDir } = getPlugin('sumba')
48
75
  const model = get(params, 'payload.data.name')
@@ -55,14 +82,14 @@ async function factory (pkgName) {
55
82
  fields
56
83
  }
57
84
  options.filter.sort = 'id:1'
58
- const dmodel = 'SumbaDownload'
85
+ const dmodel = this.app.dobo.getModel('SumbaDownload')
59
86
  try {
60
- await recordUpdate(dmodel, id, { status: 'PROCESSING' })
87
+ await dmodel.updateRecord(id, { status: 'PROCESSING' })
61
88
  await exportTo(model, dest, options)
62
89
  const { size } = fs.statSync(dest)
63
- await recordUpdate(dmodel, id, { size, status: 'COMPLETE' })
90
+ await dmodel.updateRecord(id, { size, status: 'COMPLETE' })
64
91
  } catch (err) {
65
- await recordUpdate(dmodel, id, { status: 'FAIL' })
92
+ await dmodel.updateRecord(id, { status: 'FAIL' })
66
93
  }
67
94
  }
68
95
 
@@ -70,23 +97,22 @@ async function factory (pkgName) {
70
97
  const { getPluginPrefix } = this.app.waibu
71
98
  const { pascalCase } = this.app.lib.aneka
72
99
  const { getAppTitle } = this.app.waibuMpa
73
- const { camelCase, map, pick, groupBy, keys, kebabCase, filter, get } = this.app.lib._
100
+ const { camelCase, map, groupBy, keys, kebabCase, filter, get, isArray } = this.app.lib._
74
101
 
75
102
  const prefix = getPluginPrefix(this.ns)
76
- const schemas = filter(this.app.dobo.schemas, s => {
103
+ const allModels = this.app.dobo.models
104
+ const models = filter(allModels, s => {
77
105
  const byModelFind = !s.disabled.includes('find')
78
- let modelDisabled = get(this, `app.${s.ns}.config.waibuAdmin.modelDisabled`)
79
- if (modelDisabled) {
80
- const allModels = map(filter(this.app.dobo.schemas, { ns: s.ns }), 'name')
81
- if (modelDisabled === 'all') modelDisabled = allModels
82
- else modelDisabled = map(modelDisabled, m => pascalCase(`${this.app[s.ns].alias} ${m}`))
83
- } else modelDisabled = []
106
+ const disabled = get(this, `app.${s.plugin.ns}.config.waibuAdmin.modelDisabled`, [])
107
+ let modelDisabled = []
108
+ if (['*', 'all'].includes(disabled)) modelDisabled = map(filter(allModels, m => m.plugin.ns === s.plugin.ns), 'name')
109
+ else if (isArray(disabled)) modelDisabled = map(disabled, m => pascalCase(`${this.app[s.plugin.ns].alias} ${m}`))
84
110
  const byDbDisabled = !modelDisabled.includes(s.name)
85
111
  return byModelFind && byDbDisabled
86
112
  })
87
- const omenu = groupBy(map(schemas, s => {
88
- const item = pick(s, ['name', 'ns'])
89
- item.nsTitle = getAppTitle(s.ns)
113
+ const omenu = groupBy(map(models, s => {
114
+ const item = { name: s.name, ns: s.plugin.ns }
115
+ item.nsTitle = getAppTitle(s.plugin.ns)
90
116
  return item
91
117
  }), 'nsTitle')
92
118
  const menu = []
@@ -105,6 +131,86 @@ async function factory (pkgName) {
105
131
  }
106
132
  return menu
107
133
  }
134
+
135
+ getParams = (req, ...items) => {
136
+ const { map, trim, get } = this.app.lib._
137
+ let fields
138
+ req.query = req.query ?? {}
139
+ req.params = req.params ?? {}
140
+ if (req.query.fields) fields = map((req.query.fields ?? '').split(','), i => trim(i))
141
+ const params = {
142
+ fields,
143
+ count: get(this, 'config.dbModel.count', false),
144
+ body: req.body
145
+ }
146
+ items.forEach(i => {
147
+ params[i] = req.params[i]
148
+ })
149
+ return params
150
+ }
151
+
152
+ getLookupData = async ({ model, req, data, id = 'id', field, query }) => {
153
+ const { set, map } = this.app.lib._
154
+ const $in = map(data, id)
155
+ const q = query ?? set({}, field, { $in })
156
+ const options = {
157
+ dataOnly: true,
158
+ limit: -1,
159
+ query: q
160
+ }
161
+ return await this.findRecord({ model, req, options })
162
+ }
163
+
164
+ formatRecord = async ({ data, req, schema, options = {} }) => {
165
+ const { isArray } = this.app.lib._
166
+ if (!isArray(data)) return await this.formatRow({ data, req, schema, options })
167
+ const items = []
168
+ for (const d of data) {
169
+ const item = await this.formatRow({ data: d, req, schema, options })
170
+ items.push(item)
171
+ }
172
+ return items
173
+ }
174
+
175
+ formatRow = async ({ data, req, schema, options = {} }) => {
176
+ const { get, find, isFunction, cloneDeep } = this.app.lib._
177
+ const { format, callHandler } = this.app.bajo
178
+ const { escape } = this.app.waibu
179
+ const fields = get(schema, 'view.fields', Object.keys(schema.properties))
180
+ const rec = cloneDeep(data)
181
+ for (const f of fields) {
182
+ if (f === '_rel') continue
183
+ let prop = find(schema.properties, { name: f })
184
+ if (!prop) prop = find(schema.view.calcFields, { name: f })
185
+ if (!prop) continue
186
+ const opts = {
187
+ lang: options.lang ?? (req ? req.lang : undefined),
188
+ longitude: ['lng', 'longitude'].includes(f),
189
+ latitude: ['lat', 'latitude'].includes(f),
190
+ speed: ['speed'].includes(f),
191
+ degree: ['course', 'heading'].includes(f),
192
+ distance: ['distance'].includes(f)
193
+ }
194
+ rec[f] = format(data[f], prop.type, opts)
195
+ const vf = get(schema, `view.valueFormatter.${f}`)
196
+ if (vf) {
197
+ if (isFunction(vf)) rec[f] = await vf.call(this, data[f], data)
198
+ else rec[f] = await callHandler(vf, { req, value: data[f], data })
199
+ } else if (['string', 'text'].includes(prop.type)) rec[f] = escape(rec[f])
200
+ }
201
+ return rec
202
+ }
203
+
204
+ countRecord = countRecord
205
+ createAggregate = createAggregate
206
+ createHistogram = createHistogram
207
+ createRecord = createRecord
208
+ findOneRecord = findOneRecord
209
+ findRecord = findRecord
210
+ getSchemaExt = getSchemaExt
211
+ getRecord = getRecord
212
+ removeRecord = removeRecord
213
+ updateRecord = updateRecord
108
214
  }
109
215
 
110
216
  return WaibuDb
@@ -1,18 +1,18 @@
1
1
  async function addHandler ({ req, reply, model, params = {}, template, addOnsHandler, templateDisabled = 'waibuDb.template:/disabled.html', options = {} } = {}) {
2
2
  const { pascalCase } = this.app.lib.aneka
3
- const { recordCreate, recordGet, getSchemaExt } = this.app.waibuDb
3
+ const { createRecord, getRecord, getSchemaExt } = this.app.waibuDb
4
4
  const { buildUrl } = this.app.waibuMpa
5
5
  const { defaultsDeep } = this.app.lib.aneka
6
- const { pick, map, merge, omit, isEmpty } = this.app.lib._
6
+ const { pick, map, merge, omit } = this.app.lib._
7
7
  const opts = {}
8
- model = model ?? pascalCase(req.params.model)
8
+ model = pascalCase(model ?? req.params.model)
9
9
  const { schema } = await getSchemaExt(model, 'add', merge({}, { params }, options))
10
10
  if (schema.disabled.includes('create')) return await reply.view(templateDisabled, { action: 'add' })
11
11
  // req.query.attachment = true
12
12
  opts.fields = schema.view.fields
13
13
  let def = {}
14
14
  if (req.method === 'GET' && req.query.mode === 'clone' && req.query.id) {
15
- const resp = await recordGet({ model, req, id: req.query.id, options: { fields: map(schema.properties, 'name') } })
15
+ const resp = await getRecord({ model, req, id: req.query.id, options: { fields: map(schema.properties, 'name') } })
16
16
  def = omit(resp.data, ['id', 'createdAt', 'updatedAt'])
17
17
  }
18
18
  let form = defaultsDeep(req.body, def)
@@ -22,9 +22,9 @@ async function addHandler ({ req, reply, model, params = {}, template, addOnsHan
22
22
  req.session[`wdb${model}AddMore`] = form._addmore
23
23
  req.session[`wdb${model}ClonePrev`] = form._cloneprev
24
24
  try {
25
- resp = await recordCreate({ model, req, reply, options: opts })
26
- if (isEmpty(form._addmore)) return reply.redirectTo(buildUrl({ url: req.url, base: 'list', params: { page: 1 }, exclude: ['id', 'mode'] }))
27
- if (isEmpty(form._cloneprev)) form = pick(form, ['_addmore', '_cloneprev'])
25
+ resp = await createRecord({ model, req, reply, options: opts })
26
+ if (!form._addmore) return reply.redirectTo(buildUrl({ url: req.url, base: 'list', params: { page: 1 }, exclude: ['id', 'mode'] }))
27
+ if (!form._cloneprev) form = pick(form, ['_addmore', '_cloneprev'])
28
28
  } catch (err) {
29
29
  error = err
30
30
  }
@@ -32,6 +32,7 @@ async function addHandler ({ req, reply, model, params = {}, template, addOnsHan
32
32
  form._addmore = req.session[`wdb${model}AddMore`]
33
33
  form._cloneprev = req.session[`wdb${model}ClonePrev`]
34
34
  }
35
+ console.log(form)
35
36
  const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, error, options }) : undefined
36
37
  merge(params, { form, schema, error, addOns })
37
38
  if (schema.template) template = schema.template
File without changes
@@ -1,10 +1,10 @@
1
1
  async function deleteHandler ({ req, reply, model, params = {}, templateDisabled = 'waibuDb.template:/disabled.html', options = {} } = {}) {
2
2
  const { pascalCase } = this.app.lib.aneka
3
- const { recordRemove, getSchemaExt } = this.app.waibuDb
3
+ const { removeRecord, getSchemaExt } = this.app.waibuDb
4
4
  const { buildUrl } = this.app.waibuMpa
5
5
  const { reduce, merge } = this.app.lib._
6
6
  const opts = {}
7
- model = model ?? pascalCase(req.params.model)
7
+ model = pascalCase(model ?? req.params.model)
8
8
  const { schema } = await getSchemaExt(model, 'add', merge({}, { params }, options))
9
9
  if (schema.disabled.includes('remove')) return await reply.view(templateDisabled, { action: 'delete' })
10
10
  opts.fields = schema.view.fields
@@ -15,9 +15,10 @@ async function deleteHandler ({ req, reply, model, params = {}, templateDisabled
15
15
  opts.noFlash = true
16
16
  for (const id of ids) {
17
17
  try {
18
- await recordRemove({ model, id, req, reply, options: opts })
18
+ await removeRecord({ model, id, req, reply, options: opts })
19
19
  result.push(true)
20
20
  } catch (err) {
21
+ console.log(err)
21
22
  result.push(err.message)
22
23
  }
23
24
  }
@@ -2,16 +2,16 @@ import attachmentHandler from './helper/attachment-handler.js'
2
2
 
3
3
  async function detailsHandler ({ req, reply, model, params = {}, id, template, addOnsHandler, templateDisabled = 'waibuDb.template:/disabled.html', options } = {}) {
4
4
  const { pascalCase } = this.app.lib.aneka
5
- const { recordGet, getSchemaExt } = this.app.waibuDb
5
+ const { getRecord, getSchemaExt } = this.app.waibuDb
6
6
  const { merge } = this.app.lib._
7
- const opts = { rels: '*' }
8
- model = model ?? pascalCase(req.params.model)
7
+ const opts = { refs: '*' }
8
+ model = pascalCase(model ?? req.params.model)
9
9
  const { schema } = await getSchemaExt(model, 'details', merge({}, { params }, options))
10
10
  if (schema.disabled.includes('get')) return await reply.view(templateDisabled, { action: 'details' })
11
11
  // req.query.attachment = true
12
12
  opts.fields = schema.view.fields
13
13
  id = id ?? req.params.id ?? req.query.id
14
- const resp = await recordGet({ model, req, id, options: opts })
14
+ const resp = await getRecord({ model, req, id, options: opts })
15
15
  const form = resp.data
16
16
  const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, options }) : undefined
17
17
  const attachments = await attachmentHandler.call(this, { schema, id, options })
@@ -3,7 +3,7 @@ import attachmentHandler from './helper/attachment-handler.js'
3
3
  async function editHandler ({ req, reply, model, id, params = {}, template, addOnsHandler, templateDisabled = 'waibuDb.template:/disabled.html', options = {} } = {}) {
4
4
  const { pascalCase } = this.app.lib.aneka
5
5
  const { getPluginDataDir } = this.app.bajo
6
- const { recordUpdate, recordGet, getSchemaExt } = this.app.waibuDb
6
+ const { updateRecord, getRecord, getSchemaExt } = this.app.waibuDb
7
7
  const { buildUrl } = this.app.waibuMpa
8
8
  const { fs } = this.app.lib
9
9
  const { defaultsDeep } = this.app.lib.aneka
@@ -12,17 +12,16 @@ async function editHandler ({ req, reply, model, id, params = {}, template, addO
12
12
  let error
13
13
  let resp
14
14
  let form
15
- model = model ?? pascalCase(req.params.model)
15
+ model = pascalCase(model ?? req.params.model)
16
16
  const { schema } = await getSchemaExt(model, 'edit', merge({}, { params }, options))
17
17
  if (schema.disabled.includes('update')) return await reply.view(templateDisabled, { action: 'edit' })
18
18
  // req.query.attachment = true
19
19
  opts.fields = schema.view.fields
20
20
  id = id ?? req.params.id ?? req.query.id
21
- if (req.method === 'GET') {
22
- const old = await recordGet({ model, req, id, options: opts })
23
- form = defaultsDeep(req.body, old.data)
24
- } else {
25
- form = omit(req.body, ['_action', '_value'])
21
+ const old = await getRecord({ model, req, id, options: opts })
22
+ form = defaultsDeep(req.body, old.data)
23
+ if (req.method !== 'GET') {
24
+ form = omit(form, ['_action', '_value'])
26
25
  if (req.body._action === 'removeatt' && !isEmpty(req.body._value)) {
27
26
  const root = `${getPluginDataDir('dobo')}/attachment`
28
27
  for (const item of req.body._value) {
@@ -34,7 +33,7 @@ async function editHandler ({ req, reply, model, id, params = {}, template, addO
34
33
  if (req && req.flash) req.flash('notify', req.t('attachmentRemoved'))
35
34
  } else {
36
35
  try {
37
- resp = await recordUpdate({ model, req, id, reply, options: opts })
36
+ resp = await updateRecord({ model, req, id, reply, options: opts })
38
37
  form = resp.data
39
38
  return reply.redirectTo(buildUrl({ url: req.url, base: req.params.base ?? req.query.base ?? 'list', params: { page: 1 }, exclude: ['id'] }))
40
39
  } catch (err) {
@@ -8,12 +8,12 @@ async function exportHandler ({ req, reply, model, params = {}, templateDisabled
8
8
  const { getSchemaExt } = this.app.waibuDb
9
9
  const { buildUrl } = this.app.waibuMpa
10
10
  const { pushDownload } = getPlugin('sumba')
11
- model = model ?? pascalCase(req.params.model)
11
+ model = pascalCase(model ?? req.params.model)
12
12
  const { schema } = await getSchemaExt(model, 'add', merge({}, { params }, options))
13
13
  if (schema.disabled.includes('find')) return await reply.view(templateDisabled, { action: 'list' })
14
- const data = prepCrud.call(getPlugin('waibuDb'), { model, req, reply, args: ['model'] })
14
+ const data = await prepCrud.call(getPlugin('waibuDb'), { model, req, reply, args: ['model'] })
15
15
  data.opts = omit(data.opts, ['req', 'reply'])
16
- const source = `${this.ns}:/export-handler`
16
+ const source = `${this.name}:/export-handler`
17
17
  const worker = 'waibuDb:exportData'
18
18
  const type = get(data, 'input.ftype', 'json')
19
19
  const settings = get(data, 'input.options', '').split(',')
@@ -1,7 +1,7 @@
1
1
  async function addOnsHandler ({ req, reply, data, schema, options = {} }) {
2
2
  const { escape } = this.app.waibu
3
3
  const { base64JsonEncode } = this.app.waibuMpa
4
- const { statAggregate } = this.app.waibuDb
4
+ const { createAggregate } = this.app.waibuDb
5
5
  const { get, map, pick, pullAt } = this.app.lib._
6
6
  const opts = map(get(schema, 'view.stat.aggregate', []), item => {
7
7
  const dbOpts = pick(item, ['fields', 'group', 'aggregate'])
@@ -13,7 +13,7 @@ async function addOnsHandler ({ req, reply, data, schema, options = {} }) {
13
13
  for (const idx in opts) {
14
14
  const o = opts[idx]
15
15
  try {
16
- const resp = await statAggregate({ model: schema.name, req, reply, options: o.dbOpts })
16
+ const resp = await createAggregate({ model: schema.name, req, reply, options: o.dbOpts })
17
17
  const data = []
18
18
  for (const d of resp.data) {
19
19
  const key = o.dbOpts.fields[0]
@@ -1,7 +1,7 @@
1
1
  async function attachmentHandler ({ schema, id, options = {} }) {
2
- const { listAttachments } = this.app.dobo
3
2
  if (!schema.view.attachment) return []
4
- return await listAttachments({ model: schema.name, id })
3
+ const model = this.app.dobo.getModel(schema.name)
4
+ return await model.listAttachment({ id })
5
5
  }
6
6
 
7
7
  export default attachmentHandler