waibu-db 2.34.4 → 2.36.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.
@@ -1,7 +1,5 @@
1
1
  <c:card border>
2
- <c:card-header>
3
- <c:card-title t:content="<%= attr.name %>" />
4
- </c:card-header>
2
+ <c:card-header tag="h6" t:content="<%= attr.name %>" />
5
3
  <c:card-body>
6
4
  <c:wdb-echarts style="height: 150px;" setting="<%= attr.setting %>">
7
5
  </c:wdb-echarts>
@@ -24,7 +24,7 @@ async function table () {
24
24
  const { escape, attrToArray } = this.app.waibu
25
25
  const { groupAttrs } = this.app.waibuMpa
26
26
  const { isHtmlLink } = this.app.bajoExtra
27
- const { getTruncated } = this.app.bajoTemplate
27
+ const { createTruncatedText } = this.app.lib.aneka
28
28
  const { get, omit, set, find, isEmpty, without, merge, intersection, isPlainObject } = this.app.lib._
29
29
  const { isSet } = this.app.lib.aneka
30
30
  const group = groupAttrs(this.params.attr, ['body', 'head', 'foot'])
@@ -168,7 +168,7 @@ async function table () {
168
168
  }
169
169
  value = formatted
170
170
  }
171
- if (['object', 'array'].includes(prop.type) && !isHtmlLink(value)) value = getTruncated(value, 20) // TODO: should be handle by css instead
171
+ if (['object', 'array'].includes(prop.type) && !isHtmlLink(value)) value = createTruncatedText(value, 20) // TODO: should be handle by css instead
172
172
  if (!get(this.schema, 'view.noEscape', []).includes(f) && !isHtmlLink(value)) value = escape(value)
173
173
  const line = await buildTag({ tag: 'td', attr, html: value })
174
174
  lines.push(line)
@@ -198,27 +198,32 @@ async function query () {
198
198
  } else {
199
199
  let placeholder = this.params.attr.placeholder
200
200
  if (!placeholder) placeholder = scanables.length > 0 ? req.t('queryHint%s', join(scanables, { separator: ', ', lastSeparator: 'or' })) : req.t('query')
201
+ const submitBtn = `
202
+ <c:form-input-addon>
203
+ <c:btn t:content="submit" @click="submit" @on-submit.window="submit()" />
204
+ </c:form-input-addon>`
201
205
  this.params.html = await this.component.buildSentence(`
202
- <c:form-input type="search" placeholder="${placeholder}" id="${id}" x-data="{ query: '' }" x-init="
206
+ <c:div x-data="{
207
+ query: '',
208
+ submit () {
209
+ const val = document.getElementById('${id}').value ?? ''
210
+ const url = new URL(window.location.href)
211
+ const params = new URLSearchParams(url.search)
212
+ params.set('${qsKey.page}', 1)
213
+ params.set('${qsKey.query}', val)
214
+ window.location.href = '?' + params.toString()
215
+ }
216
+ }" x-init="
203
217
  const url = new URL(window.location.href)
204
218
  query = url.searchParams.get('${qsKey.query}') ?? ''
205
- " x-model="query" @on-query.window="query = $event.detail ?? ''" @keyup.enter="$dispatch('on-submit')">
206
- <c:form-input-addon>
207
- ${this.modal({ container, models, columns, fields, id })}
208
- </c:form-input-addon>
209
- <c:form-input-addon>
210
- <c:btn t:content="submit" x-data="{
211
- submit () {
212
- const val = document.getElementById('${id}').value ?? ''
213
- const url = new URL(window.location.href)
214
- const params = new URLSearchParams(url.search)
215
- params.set('${qsKey.page}', 1)
216
- params.set('${qsKey.query}', val)
217
- window.location.href = '?' + params.toString()
218
- }
219
- }" @click="submit" @on-submit.window="submit()" />
220
- </c:form-input-addon>
221
- </c:form-input>
219
+ ">
220
+ <c:form-input type="search" placeholder="${placeholder}" id="${id}" x-model="query" @on-query.window="query = $event.detail ?? ''" @keyup.enter="submit()">
221
+ <c:form-input-addon>
222
+ ${this.modal({ container, models, columns, fields, id })}
223
+ </c:form-input-addon>
224
+ ${this.params.attr.noSubmitButton ? '' : submitBtn}
225
+ </c:form-input>
226
+ </c:div>
222
227
  `)
223
228
  }
224
229
  }
@@ -1,12 +1,15 @@
1
1
  import { prepCrud, processHandler } from '../util.js'
2
2
 
3
3
  async function createRecord ({ model, req, body, reply, options = {}, transaction } = {}) {
4
- const { model: mdl, opts, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
4
+ const { model: mdl, opts, attachment } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
5
5
 
6
6
  async function handler (trx) {
7
7
  if (opts.trx === true) opts.trx = trx
8
- const ret = await mdl.createRecord(body ?? req.body, { ...opts, partial: true, strict: true })
9
- if (attachment) ret.data._attachment = await mdl.findAttachment(ret.data.id, { stats, mimeType })
8
+ const ret = await mdl.createRecord(body ?? req.body, { ...opts, dataOnly: false, partial: true, strict: true })
9
+ if (attachment) {
10
+ const { params, options: attachOpts } = attachment ?? {}
11
+ ret.data._attachment = await mdl.findAttachment(ret.data.id, params, attachOpts)
12
+ }
10
13
  return ret
11
14
  }
12
15
 
@@ -1,14 +1,15 @@
1
1
  import { prepCrud, processHandler } from '../util.js'
2
2
 
3
3
  async function findAllRecord ({ model, req, reply, options = {}, noAutoFilter, transaction } = {}) {
4
- const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], noAutoFilter, transaction })
4
+ const { model: mdl, opts, filter, attachment } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], noAutoFilter, transaction })
5
5
 
6
6
  async function handler (trx) {
7
7
  if (opts.trx === true) opts.trx = trx
8
- const ret = await mdl.findAllRecord(filter, opts)
8
+ const ret = await mdl.findAllRecord(filter, { ...opts, dataOnly: false })
9
9
  if (attachment) {
10
+ const { params, options: attachOpts } = attachment ?? {}
10
11
  for (const d of ret.data) {
11
- d._attachment = await mdl.findAttachment(d.id, { stats, mimeType })
12
+ d._attachment = await mdl.findAttachment(d.id, params, attachOpts)
12
13
  }
13
14
  }
14
15
  return ret
@@ -1,12 +1,15 @@
1
1
  import { prepCrud, processHandler } from '../util.js'
2
2
 
3
3
  async function findOneRecord ({ model, req, reply, options = {}, noAutoFilter, transaction } = {}) {
4
- const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], noAutoFilter, transaction })
4
+ const { model: mdl, opts, filter, attachment } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], noAutoFilter, transaction })
5
5
 
6
6
  async function handler (trx) {
7
7
  if (opts.trx === true) opts.trx = trx
8
- const ret = await mdl.findOneRecord(filter, opts)
9
- if (attachment) ret.data._attachment = await mdl.findAttachment(ret.data.id, { stats, mimeType })
8
+ const ret = await mdl.findOneRecord(filter, { ...opts, dataOnly: false })
9
+ if (attachment) {
10
+ const { params, options: attachOpts } = attachment ?? {}
11
+ ret.data._attachment = await mdl.findAttachment(ret.data.id, params, attachOpts)
12
+ }
10
13
  return ret
11
14
  }
12
15
 
@@ -1,14 +1,15 @@
1
1
  import { prepCrud, processHandler } from '../util.js'
2
2
 
3
3
  async function findRecord ({ model, req, reply, noAutoFilter, options = {}, transaction } = {}) {
4
- const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], noAutoFilter, transaction })
4
+ const { model: mdl, opts, filter, attachment } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], noAutoFilter, transaction })
5
5
 
6
6
  async function handler (trx) {
7
7
  if (opts.trx === true) opts.trx = trx
8
- const ret = await mdl.findRecord(filter, opts)
8
+ const ret = await mdl.findRecord(filter, { ...opts, dataOnly: false })
9
9
  if (attachment) {
10
+ const { params, options: attachOpts } = attachment ?? {}
10
11
  for (const d of ret.data) {
11
- d._attachment = await mdl.findAttachment(d.id, { stats, mimeType })
12
+ d._attachment = await mdl.findAttachment(d.id, params, attachOpts)
12
13
  }
13
14
  }
14
15
  return ret
@@ -1,13 +1,16 @@
1
1
  import { prepCrud, getOneRecord, processHandler } from '../util.js'
2
2
 
3
3
  async function getRecord ({ model, req, reply, id, options = {}, transaction, noAutoFilter } = {}) {
4
- const { model: mdl, recId, filter, attachment, stats, mimeType, opts } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'], transaction, noAutoFilter })
4
+ const { model: mdl, recId, filter, attachment, opts } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'], transaction, noAutoFilter })
5
5
  const me = this
6
6
 
7
7
  async function handler (trx) {
8
8
  if (opts.trx === true) opts.trx = trx
9
- const data = await getOneRecord.call(me, mdl, recId, filter, opts)
10
- if (attachment) data._attachment = await mdl.findAttachment(data.id, { stats, mimeType })
9
+ const data = await getOneRecord.call(me, mdl, recId, filter, { ...opts, dataOnly: false })
10
+ if (attachment) {
11
+ const { params, options: attachOpts } = attachment ?? {}
12
+ data._attachment = await mdl.findAttachment(data.id, params, attachOpts)
13
+ }
11
14
  return data
12
15
  }
13
16
 
@@ -1,17 +1,20 @@
1
1
  import { prepCrud, getOneRecord, processHandler } from '../util.js'
2
2
 
3
3
  async function updateRecord ({ model, req, reply, id, body, options = {}, transaction } = {}) {
4
- const { model: mdl, filter, opts, recId, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'], transaction })
4
+ const { model: mdl, filter, opts, recId, attachment } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'], transaction })
5
5
  const me = this
6
6
 
7
7
  async function handler (trx) {
8
8
  if (opts.trx === true) opts.trx = trx
9
9
  if (!opts._data) {
10
- const resp = await getOneRecord.call(me, mdl, recId, filter, opts)
10
+ const resp = await getOneRecord.call(me, mdl, recId, filter, { ...opts, dataOnly: false })
11
11
  opts._data = resp.data
12
12
  }
13
- const ret = await mdl.updateRecord(recId, body ?? req.body, { ...opts, partial: true, strict: true })
14
- if (attachment) ret.data._attachment = await mdl.findAttachment(id, { stats, mimeType })
13
+ const ret = await mdl.updateRecord(recId, body ?? req.body, { ...opts, dataOnly: false, partial: true, strict: true })
14
+ if (attachment) {
15
+ const { params, options: attachOpts } = attachment ?? {}
16
+ ret.data._attachment = await mdl.findAttachment(recId, params, attachOpts)
17
+ }
15
18
  return ret
16
19
  }
17
20
 
package/lib/util.js CHANGED
@@ -54,12 +54,12 @@ export async function prepCrud (options = {}) {
54
54
  const { importModule } = this.app.bajo
55
55
  const { parseFilter } = this.app.waibu
56
56
  const { pascalCase } = this.app.lib.aneka
57
- const { cloneDeep, has, isString, omit } = this.app.lib._
57
+ const { cloneDeep, has, isString, omit, merge } = this.app.lib._
58
58
  const { parseObject } = this.app.lib
59
59
  const { buildFilterQuery, buildFilterSearch } = await importModule('dobo:/lib/factory/model/helper.js', { asDefaultImport: false })
60
60
 
61
61
  const cfgWeb = this.app.waibu.getConfig()
62
- const opts = cloneDeep(omit(_options, ['trx']))
62
+ const opts = cloneDeep(omit(_options, ['trx', 'query', 'search', 'mergeQuery', 'limit', 'sort', 'page']))
63
63
  opts.throwNotFound = true
64
64
  if (opts.suppressError === true) opts.suppressError = actions
65
65
  else if (isString(opts.suppressError)) opts.suppressError = [opts.suppressError]
@@ -74,10 +74,13 @@ export async function prepCrud (options = {}) {
74
74
  opts.reply = reply
75
75
  opts.trx = opts.trx ?? _options.trx ?? transaction ?? true
76
76
 
77
- let { attachment, stats, mimeType } = opts
78
- attachment = attachment ?? req.query.attachment
79
- stats = stats ?? req.query.stats
80
- mimeType = mimeType ?? req.query.mimeType
77
+ let attachment = req.query.attachment ?? opts.attachment ?? false
78
+ if (attachment) {
79
+ if (req.query.attachment || attachment === true) attachment = { params: {}, options: { stats: true, mimeType: true, thumbnail: true } }
80
+ attachment.options.stats = attachment.options.stats ?? req.query.stats
81
+ attachment.options.mimeType = attachment.options.mimeType ?? req.query.mimeType
82
+ attachment.options.thumbnail = attachment.options.thumbnail ?? req.query.thumbnail
83
+ }
81
84
 
82
85
  let refs = []
83
86
  const headers = parseObject(req.headers, { parseValue: true })
@@ -85,7 +88,7 @@ export async function prepCrud (options = {}) {
85
88
  if (isSet(headers['x-refs'])) refs = headers['x-refs']
86
89
  if (typeof refs === 'string' && !['*', 'all'].includes(refs)) refs = [refs]
87
90
  if (refs.length > 0) _options.refs = refs
88
- _options.attachments = headers['x-attachments'] === 'true'
91
+ _options.attachment = headers['x-attachments'] === 'true'
89
92
 
90
93
  const recId = id ?? params.id ?? req.query.id
91
94
  let mdl = model
@@ -101,14 +104,19 @@ export async function prepCrud (options = {}) {
101
104
  filter.query = buildFilterQuery.call(mdl, filter)
102
105
  filter.search = buildFilterSearch.call(mdl, filter)
103
106
  }
104
- if (_options.query) filter.query = cloneDeep(_options.query)
107
+ if (_options.query) {
108
+ if (_options.mergeQuery) {
109
+ if (filter.query.$and) filter.query.$and.push(_options.query)
110
+ else filter.query = merge({}, filter.query, _options.query)
111
+ } else filter.query = cloneDeep(_options.query)
112
+ }
105
113
  if (_options.search) filter.search = cloneDeep(_options.search)
106
114
  if (_options.limit) filter.limit = _options.limit
107
115
  if (_options.sort) filter.sort = _options.sort
108
116
  if (_options.page) filter.page = _options.page
109
117
  if (opts.fields && opts.fields.length > 0 && mdl.hasProperty('_immutable')) opts.fields.push('_immutable')
110
118
  const input = cloneDeep(req.body) ?? {}
111
- return { model: mdl, input, recId, opts, filter, attachment, stats, mimeType }
119
+ return { model: mdl, input, recId, opts, filter, attachment }
112
120
  }
113
121
 
114
122
  /**
@@ -165,7 +173,8 @@ export async function processHandler (opts = {}) {
165
173
  if (options.trx === true && ['createRecord', 'updateRecord', 'upsertRecord', 'removeRecord'].includes(action)) {
166
174
  return await model.transaction(handler, action, options)
167
175
  }
168
- return await handler()
176
+ const resp = await handler()
177
+ return options.dataOnly ? resp.data : resp
169
178
  } catch (err) {
170
179
  if (options.suppressError.includes(action)) return suppressedReturn.call(this, err)
171
180
  throw err
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-db",
3
- "version": "2.34.4",
3
+ "version": "2.36.0",
4
4
  "description": "DB Helper",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-11-20
4
+
5
+ - [2.35.0] `attachment` options can now accept plain object too
6
+ - [2.35.0] Bug fix in `attachment` handling
7
+ - [2.35.0] Bug fix in `dataOnly` handling
8
+ - [2.35.0] Submit button can be omitted in `query` widget
9
+ - [2.36.0] Refactoring everything related to attachment handling
10
+
3
11
  ## 2026-11-08
4
12
 
5
13
  - [2.34.4] Bug fix in `echarts.js`