waibu-db 1.2.4 → 1.2.5

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.
@@ -11,7 +11,7 @@ async function exportHandler ({ req, reply, model, params = {}, templateDisabled
11
11
  model = model ?? pascalCase(req.params.model)
12
12
  const { schema } = await getSchemaExt(model, 'add', { params })
13
13
  if (schema.disabled.includes('find')) return await reply.view(templateDisabled, { action: 'list' })
14
- const data = prepCrud.call(this, { model, req, reply, args: ['model'] })
14
+ const data = prepCrud.call(getPlugin('waibuDb'), { model, req, reply, args: ['model'] })
15
15
  data.opts = omit(data.opts, ['req', 'reply'])
16
16
  const source = `${this.name}:/export-handler`
17
17
  const worker = 'waibuDb:exportData'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-db",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "description": "DB Helper",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,4 +1,4 @@
1
- async function formatRow ({ data, req, component, schema, options = {} }) {
1
+ async function formatRow ({ data, req, schema, options = {} }) {
2
2
  const { get, find, isFunction, cloneDeep } = this.lib._
3
3
  const { format, callHandler } = this.app.bajo
4
4
  const fields = get(schema, 'view.fields', Object.keys(schema.properties))
@@ -19,25 +19,19 @@ async function formatRow ({ data, req, component, schema, options = {} }) {
19
19
  rec[f] = format(data[f], prop.type, opts)
20
20
  const vf = get(schema, `view.valueFormatter.${f}`)
21
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])
22
+ if (isFunction(vf)) rec[f] = await vf.call(this, data[f], data)
23
+ else rec[f] = await callHandler(vf, { req, value: data[f], data })
30
24
  }
31
25
  }
32
26
  return rec
33
27
  }
34
28
 
35
- async function formatRecord ({ data, req, schema, component, options = {} }) {
29
+ async function formatRecord ({ data, req, schema, options = {} }) {
36
30
  const { isArray } = this.lib._
37
- if (!isArray(data)) return await formatRow.call(this, { data, req, schema, component, options })
31
+ if (!isArray(data)) return await formatRow.call(this, { data, req, schema, options })
38
32
  const items = []
39
33
  for (const d of data) {
40
- const item = await formatRow.call(this, { data: d, req, schema, component, options })
34
+ const item = await formatRow.call(this, { data: d, req, schema, options })
41
35
  items.push(item)
42
36
  }
43
37
  return items
@@ -19,19 +19,33 @@ async function table () {
19
19
  return get(schema, 'view.noWrap', []).includes(field)
20
20
  }
21
21
 
22
+ _defFormatter = async ({ req, key, value, data, schema }) => {
23
+ const { get, find } = this.plugin.lib._
24
+ const { escape } = this.plugin.app.waibu
25
+ const prop = find(schema.properties, { name: key })
26
+ if (!prop) return value
27
+ if (prop.type === 'boolean') {
28
+ value = (await this.component.buildTag({ tag: 'icon', attr: { name: `circle${data[key] ? 'Check' : ''}` } })) +
29
+ ' ' + (req.t(data[key] ? 'Yes' : 'No'))
30
+ } else if (['string', 'text'].includes(prop.type)) {
31
+ if (!get(schema, 'view.noEscape', []).includes(key)) value = escape(value)
32
+ }
33
+ return value
34
+ }
35
+
22
36
  build = async () => {
23
37
  const { req } = this.component
24
38
  const { escape } = this.plugin.app.waibu
25
39
  const { formatRecord } = this.plugin.app.waibuDb
26
40
  const { attrToArray, groupAttrs } = this.plugin.app.waibuMpa
27
- const { get, omit, set, find, isEmpty, without, merge } = this.plugin.app.bajo.lib._
41
+ const { get, omit, set, find, isEmpty, without, merge } = this.plugin.lib._
28
42
  const group = groupAttrs(this.params.attr, ['body', 'head', 'foot'])
29
43
  this.params.attr = group._
30
44
  const prettyUrl = this.params.attr.prettyUrl
31
45
 
32
46
  const schema = get(this, 'component.locals.schema', {})
33
47
  const data = get(this, 'component.locals.list.data', [])
34
- const fdata = await formatRecord.call(this.plugin, { data, req, schema, component: this.component })
48
+ const fdata = await formatRecord.call(this.plugin, { data, req, schema })
35
49
  const filter = get(this, 'component.locals.list.filter', {})
36
50
  const count = get(this, 'component.locals.list.count', 0)
37
51
  if (count === 0) {
@@ -136,16 +150,10 @@ async function table () {
136
150
  if (['array', 'object'].includes(prop.type)) dataValue = escape(JSON.stringify(d[f]))
137
151
  }
138
152
  let value = fd[f]
139
- if (prop.type === 'boolean') {
140
- value = (await this.component.buildTag({ tag: 'icon', attr: { name: `circle${d[f] ? 'Check' : ''}` } })) +
141
- ' ' + (req.t(d[f] ? 'Yes' : 'No'))
142
- } else {
143
- if (!get(schema, 'view.noEscape', []).includes(f)) value = escape(value)
144
- }
145
153
  const attr = { dataValue, dataKey: prop.name, dataType: prop.type }
146
154
  if (!disableds.includes('get')) attr.style = { cursor: 'pointer' }
147
155
  const cellFormatter = get(schema, `view.cellFormatter.${f}`)
148
- if (cellFormatter) merge(attr, await cellFormatter.call(req, dataValue, d))
156
+ if (cellFormatter) merge(attr, await cellFormatter.call(this, dataValue, d))
149
157
  const noWrap = this.isNoWrap(f, schema, group.body.nowrap) ? 'nowrap' : ''
150
158
  if (this.isRightAligned(f, schema)) attr.text = `align:end ${noWrap}`
151
159
  else attr.text = noWrap
@@ -154,6 +162,9 @@ async function table () {
154
162
  const item = find(lookup.values, set({}, lookup.id ?? 'id', d[f]))
155
163
  if (item) value = req.t(item[lookup.field ?? 'name'])
156
164
  }
165
+ const formatter = get(schema, `view.formatter.${f}`)
166
+ if (formatter) value = await formatter.call(this, value, d)
167
+ else value = await this._defFormatter({ req, key: f, schema, value, data: d })
157
168
  const line = await this.component.buildTag({ tag: 'td', attr, html: value })
158
169
  lines.push(line)
159
170
  }