waibu-db 1.1.12 → 1.1.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-db",
3
- "version": "1.1.12",
3
+ "version": "1.1.13",
4
4
  "description": "DB Helper",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,46 @@
1
+ async function formatRow ({ data, req, component, schema, options = {} }) {
2
+ const { get, find, isFunction, cloneDeep } = this.lib._
3
+ const { format, callHandler } = this.app.bajo
4
+ const fields = get(schema, 'view.fields', Object.keys(schema.properties))
5
+ const rec = cloneDeep(data)
6
+ for (const f of fields) {
7
+ if (f === '_rel') continue
8
+ let prop = find(schema.properties, { name: f })
9
+ if (!prop) prop = find(schema.view.calcFields, { name: f })
10
+ if (!prop) continue
11
+ const opts = {
12
+ lang: options.lang ?? (req ? req.lang : undefined),
13
+ longitude: ['lng', 'longitude'].includes(f),
14
+ latitude: ['lat', 'latitude'].includes(f),
15
+ speed: ['speed'].includes(f),
16
+ degree: ['course', 'heading'].includes(f),
17
+ distance: ['distance'].includes(f)
18
+ }
19
+ rec[f] = format(data[f], prop.type, opts)
20
+ const vf = get(schema, `view.valueFormatter.${f}`)
21
+ if (vf) {
22
+ if (isFunction(vf)) rec[f] = await vf.call(req ?? this, data[f], data)
23
+ else rec[f] = await callHandler(vf, req, data[f], data)
24
+ }
25
+ const formatter = get(schema, `view.formatter.${f}`)
26
+ if (formatter && component) {
27
+ if (isFunction(formatter)) rec[f] = await formatter.call(req ?? this, data[f], data)
28
+ else rec[f] = await callHandler(formatter, req, data[f], data)
29
+ rec[f] = await component.buildSentence(rec[f])
30
+ }
31
+ }
32
+ return rec
33
+ }
34
+
35
+ async function formatRecord ({ data, req, schema, component, options = {} }) {
36
+ const { isArray } = this.lib._
37
+ if (!isArray(data)) return await formatRow.call(this, { data, req, schema, component, options })
38
+ const items = []
39
+ for (const d of data) {
40
+ const item = await formatRow.call(this, { data: d, req, schema, component, options })
41
+ items.push(item)
42
+ }
43
+ return items
44
+ }
45
+
46
+ export default formatRecord
@@ -2,36 +2,12 @@ import path from 'path'
2
2
 
3
3
  const defReadonly = ['id', 'createdAt', 'updatedAt']
4
4
 
5
- const defFormatter = {
6
- lat: function (val, rec) {
7
- const format = this.format ?? this.app.bajo.format
8
- return format(val, 'double', { latitude: true })
9
- },
10
- lng: function (val, rec) {
11
- const format = this.format ?? this.app.bajo.format
12
- return format(val, 'double', { longitude: true })
13
- },
14
- speed: function (val, rec) {
15
- const format = this.format ?? this.app.bajo.format
16
- return format(val, 'float', { speed: true, withType: true })
17
- },
18
- course: function (val, rec) {
19
- const format = this.format ?? this.app.bajo.format
20
- return format(val, 'float', { degree: true })
21
- },
22
- heading: function (val, rec) {
23
- const format = this.format ?? this.app.bajo.format
24
- return format(val, 'float', { degree: true })
25
- },
26
- distance: function (val, rec) {
27
- const format = this.format ?? this.app.bajo.format
28
- return format(val, 'float', { distance: true })
29
- }
30
- }
5
+ const defFormatter = {}
31
6
 
32
7
  function getCommons (action, schema, ext, opts = {}) {
33
8
  const { merge, map, get, set, without, uniq } = this.lib._
34
9
  const calcFields = get(ext, `view.${action}.calcFields`, get(ext, 'common.calcFields', []))
10
+ const noEscape = get(ext, `view.${action}.noEscape`, get(ext, 'common.noEscape', []))
35
11
  const valueFormatter = get(ext, `view.${action}.valueFormatter`, get(ext, 'common.valueFormatter', {}))
36
12
  const formatter = get(ext, `view.${action}.formatter`, get(ext, 'common.formatter', {}))
37
13
  const label = get(ext, `view.${action}.label`, get(ext, 'common.label', {}))
@@ -44,6 +20,7 @@ function getCommons (action, schema, ext, opts = {}) {
44
20
  const allFields = without(map(schema.properties, 'name'), ...hidden)
45
21
  const forFields = get(ext, `view.${action}.fields`, get(ext, 'common.fields', allFields))
46
22
  set(schema, 'view.calcFields', calcFields)
23
+ set(schema, 'view.noEscape', noEscape)
47
24
  set(schema, 'view.valueFormatter', valueFormatter)
48
25
  set(schema, 'view.formatter', merge({}, defFormatter, formatter))
49
26
  set(schema, 'view.stat.aggregate', aggregate)
@@ -21,15 +21,17 @@ async function table () {
21
21
 
22
22
  build = async () => {
23
23
  const { req } = this.component
24
- const { callHandler } = this.plugin.app.bajo
25
24
  const { escape } = this.plugin.app.waibu
25
+ const { formatRecord } = this.plugin.app.waibuDb
26
26
  const { attrToArray, groupAttrs } = this.plugin.app.waibuMpa
27
- const { get, omit, set, find, isEmpty, without, isFunction, merge } = this.plugin.app.bajo.lib._
27
+ const { get, omit, set, find, isEmpty, without, merge } = this.plugin.app.bajo.lib._
28
28
  const group = groupAttrs(this.params.attr, ['body', 'head', 'foot'])
29
29
  this.params.attr = group._
30
30
  const prettyUrl = this.params.attr.prettyUrl
31
31
 
32
+ const schema = get(this, 'component.locals.schema', {})
32
33
  const data = get(this, 'component.locals.list.data', [])
34
+ const fdata = await formatRecord.call(this.plugin, { data, req, schema, component: this.component })
33
35
  const filter = get(this, 'component.locals.list.filter', {})
34
36
  const count = get(this, 'component.locals.list.count', 0)
35
37
  if (count === 0) {
@@ -38,7 +40,6 @@ async function table () {
38
40
  this.params.html = await this.component.buildSentence(alert)
39
41
  return
40
42
  }
41
- const schema = get(this, 'component.locals.schema', {})
42
43
  const disableds = get(schema, 'view.disabled', [])
43
44
  if (disableds.includes('find')) {
44
45
  this.params.html = ''
@@ -112,7 +113,9 @@ async function table () {
112
113
  html.push(await this.component.buildTag({ tag: 'thead', attr: group.head, html: header }))
113
114
  // body
114
115
  items = []
115
- for (const d of data) {
116
+ for (const idx in data) {
117
+ const d = data[idx]
118
+ const fd = fdata[idx]
116
119
  const lines = []
117
120
  if (selection) {
118
121
  const tag = selection === 'single' ? 'formRadio' : 'formCheck'
@@ -130,17 +133,12 @@ async function table () {
130
133
  if (['datetime'].includes(prop.type)) dataValue = escape(dataValue.toISOString())
131
134
  if (['string', 'text'].includes(prop.type)) dataValue = escape(dataValue)
132
135
  if (['array', 'object'].includes(prop.type)) dataValue = escape(JSON.stringify(d[f]))
133
- const opts = {}
134
- let value = req.format(d[f], prop.type, opts)
136
+ let value = fd[f]
135
137
  if (prop.type === 'boolean') {
136
138
  value = (await this.component.buildTag({ tag: 'icon', attr: { name: `circle${d[f] ? 'Check' : ''}` } })) +
137
139
  ' ' + (req.t(d[f] ? 'Yes' : 'No'))
138
- } else value = escape(value)
139
- const vf = get(schema, `view.valueFormatter.${f}`)
140
- if (vf) {
141
- if (isFunction(vf)) dataValue = escape(await vf.call(req, d[f], d))
142
- else dataValue = await callHandler(vf, req, d[f], d)
143
- value = dataValue
140
+ } else {
141
+ if (!get(schema, 'view.noEscape', []).includes(f)) value = escape(value)
144
142
  }
145
143
  const attr = { dataValue, dataKey: prop.name, dataType: prop.type }
146
144
  if (!disableds.includes('get')) attr.style = { cursor: 'pointer' }
@@ -154,12 +152,6 @@ async function table () {
154
152
  const item = find(lookup.values, set({}, lookup.id ?? 'id', d[f]))
155
153
  if (item) value = req.t(item[lookup.field ?? 'name'])
156
154
  }
157
- const formatter = get(schema, `view.formatter.${f}`)
158
- if (formatter) {
159
- if (isFunction(formatter)) value = await formatter.call(req, d[f], d)
160
- else value = await callHandler(formatter, req, d[f], d)
161
- value = await this.component.buildSentence(value)
162
- }
163
155
  const line = await this.component.buildTag({ tag: 'td', attr, html: value })
164
156
  lines.push(line)
165
157
  }