waibu-db 2.35.0 → 2.37.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,8 +1,8 @@
1
1
  <c:grid-row gutter="3">
2
2
  <c:grid-col col="6-lg">
3
- <c:wdb-recs-info pages recs-per-page recs-per-page-values="10 15 25 50" />
3
+ <c:wdb-recs-info pages recs-per-page recs-per-page-values="10 15 25 50" <% if (attr.dataKey) { %> data-key="<%= attr.dataKey %>" <% } %> />
4
4
  </c:grid-col>
5
5
  <c:grid-col col="6-lg" flex="justify-content:end-lg">
6
- <c:wdb-pagination />
6
+ <c:wdb-pagination <% if (attr.dataKey) { %> data-key="<%= attr.dataKey %>" <% } %> />
7
7
  </c:grid-col>
8
8
  </c:grid-row>
@@ -65,7 +65,8 @@ async function form () {
65
65
  ao.position = ao.position ?? 'append'
66
66
  }
67
67
  let html
68
- if (widget.component === 'form-plaintext' || this.params.attr.method !== 'POST') {
68
+ const roCmps = ['createdAt', 'updatedAt']
69
+ if (widget.component === 'form-plaintext' || this.params.attr.method !== 'POST' || roCmps.includes(widget.name)) {
69
70
  html = await WdbForm.handleRo.call(this, { attr, prop, widget })
70
71
  } else {
71
72
  html = await WdbForm.handleRw.call(this, { attr, prop, widget })
@@ -21,20 +21,21 @@ async function pagination () {
21
21
  const { attrToObject } = this.app.waibu
22
22
  const { get, isNumber } = this.app.lib._
23
23
  const schema = get(this, 'component.locals.schema', {})
24
+ const dataKey = this.params.attr.dataKey ?? 'list'
24
25
  if (schema.view.disabled.includes('find')) {
25
26
  this.params.html = ''
26
27
  return
27
28
  }
28
29
  let { count, limit, page } = attrToObject(this.params.attr.options)
29
- count = count ?? get(this, 'component.locals.list.count', 0)
30
- const data = get(this, 'component.locals.list.data', [])
30
+ count = count ?? get(this, `component.locals.${dataKey}.count`, 0)
31
+ const data = get(this, `component.locals.${dataKey}.data`, [])
31
32
  if (count === 0 || data.length === 0) {
32
33
  this.params.noTag = true
33
34
  this.params.html = ''
34
35
  return
35
36
  }
36
- limit = limit ?? get(this, 'component.locals.list.limit', 25)
37
- page = page ?? get(this, 'component.locals.list.page', 1)
37
+ limit = limit ?? get(this, `component.locals.${dataKey}.limit`, 25)
38
+ page = page ?? get(this, `component.locals.${dataKey}.page`, 1)
38
39
  const pages = paginationLayout(count, limit, page) ?? []
39
40
  this.params.noTag = true
40
41
  const group = groupAttrs(this.params.attr, ['pagination'])
@@ -211,7 +211,7 @@ async function query () {
211
211
  const params = new URLSearchParams(url.search)
212
212
  params.set('${qsKey.page}', 1)
213
213
  params.set('${qsKey.query}', val)
214
- window.location.href = '?' + params.toString()
214
+ window.location.href = '${this.params.attr.href ?? ''}?' + params.toString()
215
215
  }
216
216
  }" x-init="
217
217
  const url = new URL(window.location.href)
@@ -11,21 +11,22 @@ async function recsInfo () {
11
11
  const { attrToObject, attrToArray } = this.app.waibu
12
12
  const { get, isEmpty, omit, merge } = this.app.lib._
13
13
  const schema = get(this, 'component.locals.schema', {})
14
+ const dataKey = this.params.attr.dataKey ?? 'list'
14
15
  if (schema.view.disabled.includes('find')) {
15
16
  this.params.html = ''
16
17
  return
17
18
  }
18
19
  let { count, limit, page, pages } = attrToObject(this.params.attr.options)
19
- count = count ?? get(this, 'component.locals.list.count', 0)
20
- const data = get(this, 'component.locals.list.data', [])
20
+ count = count ?? get(this, `component.locals.${dataKey}.count`, 0)
21
+ const data = get(this, `component.locals.${dataKey}.data`, [])
21
22
  if (count === 0 || data.length === 0) {
22
23
  this.params.noTag = true
23
24
  this.params.html = ''
24
25
  return
25
26
  }
26
- page = get(this, 'component.locals.list.page', page ?? 1)
27
- limit = get(this, 'component.locals.list.limit', limit ?? 25)
28
- pages = get(this, 'component.locals.list.pages', pages ?? 0)
27
+ page = get(this, `component.locals.${dataKey}.page`, page ?? 1)
28
+ limit = get(this, `component.locals.${dataKey}.limit`, limit ?? 25)
29
+ pages = get(this, `component.locals.${dataKey}.pages`, pages ?? 0)
29
30
 
30
31
  this.params.tag = 'div'
31
32
  this.params.attr.flex = 'justify-center:start align-items:center'
@@ -1,3 +1,5 @@
1
+ import { getPageTemplate } from './helper.js'
2
+
1
3
  /**
2
4
  * Handler for rendering the add view of a model.
3
5
  *
@@ -71,8 +73,7 @@ async function addHandler (opts = {}) {
71
73
  }
72
74
  const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, error, options }) : undefined
73
75
  merge(params, { form, schema, error, addOns })
74
- if (schema.template) template = schema.template
75
- if (schema.layout) params.page.layout = schema.layout
76
+ template = (await getPageTemplate.call(this, schema, params)) ?? template
76
77
  return await reply.view(template, params)
77
78
  }
78
79
 
@@ -1,4 +1,4 @@
1
- import { attachmentHandler, notFoundTpl } from './helper.js'
1
+ import { attachmentHandler, notFoundTpl, getPageTemplate } from './helper.js'
2
2
 
3
3
  /**
4
4
  * Handler for rendering the details view of a model.
@@ -44,8 +44,7 @@ async function detailsHandler (opts = {}) {
44
44
  const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, options }) : undefined
45
45
  const attachments = await attachmentHandler.call(this, { schema, id, options })
46
46
  merge(params, { form, schema, addOns, attachments })
47
- if (schema.template) template = schema.template
48
- if (schema.layout) params.page.layout = schema.layout
47
+ template = (await getPageTemplate.call(this, schema, params)) ?? template
49
48
  return await reply.view(template, params)
50
49
  }
51
50
 
@@ -1,4 +1,4 @@
1
- import { attachmentHandler, notFoundTpl } from './helper.js'
1
+ import { attachmentHandler, notFoundTpl, getPageTemplate } from './helper.js'
2
2
 
3
3
  /**
4
4
  * Handler for rendering the edit view of a model.
@@ -79,8 +79,7 @@ async function editHandler (opts = {}) {
79
79
  const addOns = addOnsHandler ? await addOnsHandler.call(this.app[req.ns], { req, reply, params, data: resp, schema, error, options }) : undefined
80
80
  const attachments = await attachmentHandler.call(this, { schema, id, options })
81
81
  merge(params, { oldData: old.data, form, schema, error, addOns, attachments })
82
- if (schema.template) template = schema.template
83
- if (schema.layout) params.page.layout = schema.layout
82
+ template = (await getPageTemplate.call(this, schema, params)) ?? template
84
83
  return await reply.view(template, params)
85
84
  }
86
85
 
@@ -113,3 +113,12 @@ export function buildParams (opts = {}) {
113
113
  * @memberof module:CRUDHandler/Helper
114
114
  */
115
115
  export const notFoundTpl = 'waibuDb.template:/crud/not-found.html'
116
+
117
+ export async function getPageTemplate (schema, params = {}) {
118
+ const { get } = this.app.lib._
119
+ const page = get(schema, 'view.page', {})
120
+ if (page.layout) params.page.layout = page.layout
121
+ params.page.features = params.page.features ?? []
122
+ params.page.features.push(...(page.features ?? []))
123
+ return page.template
124
+ }
@@ -1,3 +1,5 @@
1
+ import { getPageTemplate } from './helper.js'
2
+
1
3
  /**
2
4
  * Handle the list view for a given model, rendering the appropriate template with the retrieved data.
3
5
  *
@@ -50,8 +52,7 @@ async function listHandler (opts) {
50
52
  if (!isArray(addOns)) addOns = [addOns]
51
53
  }
52
54
  merge(params, { list, schema, addOns })
53
- if (schema.template) template = schema.template
54
- if (schema.layout) params.page.layout = schema.layout
55
+ template = (await getPageTemplate.call(this, schema, params)) ?? template
55
56
  return await reply.view(template, params)
56
57
  }
57
58
 
@@ -1,13 +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 { isEmpty } = this.app.lib._
5
4
  const { model: mdl, opts, attachment } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
6
5
 
7
6
  async function handler (trx) {
8
7
  if (opts.trx === true) opts.trx = trx
9
8
  const ret = await mdl.createRecord(body ?? req.body, { ...opts, dataOnly: false, partial: true, strict: true })
10
- if (!isEmpty(attachment)) ret.data._attachment = await mdl.findAttachment(ret.data.id, attachment)
9
+ if (attachment && ret.data) {
10
+ const { params, options: attachOpts } = attachment ?? {}
11
+ ret.data._attachment = await mdl.findAttachment(ret.data.id, params, attachOpts)
12
+ }
11
13
  return ret
12
14
  }
13
15
 
@@ -1,15 +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 { isEmpty } = this.app.lib._
5
4
  const { model: mdl, opts, filter, attachment } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], noAutoFilter, transaction })
6
5
 
7
6
  async function handler (trx) {
8
7
  if (opts.trx === true) opts.trx = trx
9
8
  const ret = await mdl.findAllRecord(filter, { ...opts, dataOnly: false })
10
- if (!isEmpty(attachment)) {
9
+ if (attachment) {
10
+ const { params, options: attachOpts } = attachment ?? {}
11
11
  for (const d of ret.data) {
12
- d._attachment = await mdl.findAttachment(d.id, attachment)
12
+ d._attachment = await mdl.findAttachment(d.id, params, attachOpts)
13
13
  }
14
14
  }
15
15
  return ret
@@ -1,13 +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 { isEmpty } = this.app.lib._
5
4
  const { model: mdl, opts, filter, attachment } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], noAutoFilter, transaction })
6
5
 
7
6
  async function handler (trx) {
8
7
  if (opts.trx === true) opts.trx = trx
9
8
  const ret = await mdl.findOneRecord(filter, { ...opts, dataOnly: false })
10
- if (!isEmpty(attachment)) ret.data._attachment = await mdl.findAttachment(ret.data.id, attachment)
9
+ if (attachment && ret.data) {
10
+ const { params, options: attachOpts } = attachment ?? {}
11
+ ret.data._attachment = await mdl.findAttachment(ret.data.id, params, attachOpts)
12
+ }
11
13
  return ret
12
14
  }
13
15
 
@@ -1,15 +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 { isEmpty } = this.app.lib._
5
4
  const { model: mdl, opts, filter, attachment } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], noAutoFilter, transaction })
6
5
 
7
6
  async function handler (trx) {
8
7
  if (opts.trx === true) opts.trx = trx
9
8
  const ret = await mdl.findRecord(filter, { ...opts, dataOnly: false })
10
- if (!isEmpty(attachment)) {
9
+ if (attachment) {
10
+ const { params, options: attachOpts } = attachment ?? {}
11
11
  for (const d of ret.data) {
12
- d._attachment = await mdl.findAttachment(d.id, attachment)
12
+ d._attachment = await mdl.findAttachment(d.id, params, attachOpts)
13
13
  }
14
14
  }
15
15
  return ret
@@ -1,14 +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 { isEmpty } = this.app.lib._
5
4
  const { model: mdl, recId, filter, attachment, opts } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'], transaction, noAutoFilter })
6
5
  const me = this
7
6
 
8
7
  async function handler (trx) {
9
8
  if (opts.trx === true) opts.trx = trx
10
9
  const data = await getOneRecord.call(me, mdl, recId, filter, { ...opts, dataOnly: false })
11
- if (!isEmpty(attachment)) data._attachment = await mdl.findAttachment(data.id, attachment)
10
+ if (attachment && data) {
11
+ const { params, options: attachOpts } = attachment ?? {}
12
+ data._attachment = await mdl.findAttachment(data.id, params, attachOpts)
13
+ }
12
14
  return data
13
15
  }
14
16
 
@@ -5,6 +5,7 @@ const defFormatter = {}
5
5
  function getCommons (action, schema, ext, options = {}) {
6
6
  const { defaultsDeep } = this.app.lib.aneka
7
7
  const { merge, map, get, set, without, uniq, pull } = this.app.lib._
8
+ const page = get(ext, `view.${action}.page`, get(ext, 'common.page', {}))
8
9
  const forceVisible = get(ext, `view.${action}.forceVisible`, get(ext, 'common.forceVisible', []))
9
10
  const widget = defaultsDeep(get(ext, `view.${action}.widget`), get(ext, 'common.widget', {}))
10
11
  const noEscape = get(ext, `view.${action}.noEscape`, get(ext, 'common.noEscape', []))
@@ -38,6 +39,7 @@ function getCommons (action, schema, ext, options = {}) {
38
39
  set(schema, 'view.disabled', disabled)
39
40
  set(schema, 'view.control', control)
40
41
  set(schema, 'view.x', x)
42
+ set(schema, 'view.page', page)
41
43
  if (schema.disabled.length > 0) schema.view.disabled.push(...schema.disabled)
42
44
  let fields = forFields.filter(f => allFields.includes(f))
43
45
  fields = uniq(without(fields, ...hidden))
@@ -89,6 +91,7 @@ async function applyLayout (action, schema, ext, options) {
89
91
  const { defaultsDeep } = this.app.lib.aneka
90
92
  const { set, get, isEmpty, find } = this.app.lib._
91
93
  const { fields, card } = getCommons.call(this, action, schema, ext, options)
94
+ const page = get(ext, `view.${action}.page`, get(ext, 'common.page', {}))
92
95
  const layout = get(ext, `view.${action}.layout`, get(ext, 'common.layout', []))
93
96
  const readonly = get(ext, `view.${action}.readonly`, get(ext, 'common.readonly', defReadonly))
94
97
  const widget = {}
@@ -153,6 +156,7 @@ async function applyLayout (action, schema, ext, options) {
153
156
  */
154
157
  widget[f] = result
155
158
  }
159
+ set(schema, 'view.page', page)
156
160
  set(schema, 'view.widget', widget)
157
161
  if (isEmpty(layout)) autoLayout.call(this, { layout, schema, action, ext, options })
158
162
  else customLayout.call(this, { layout, schema, action, ext, readonly, options })
@@ -1,7 +1,6 @@
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 { isEmpty } = this.app.lib._
5
4
  const { model: mdl, filter, opts, recId, attachment } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'], transaction })
6
5
  const me = this
7
6
 
@@ -12,7 +11,10 @@ async function updateRecord ({ model, req, reply, id, body, options = {}, transa
12
11
  opts._data = resp.data
13
12
  }
14
13
  const ret = await mdl.updateRecord(recId, body ?? req.body, { ...opts, dataOnly: false, partial: true, strict: true })
15
- if (!isEmpty(attachment)) ret.data._attachment = await mdl.findAttachment(recId, attachment)
14
+ if (attachment && ret.data) {
15
+ const { params, options: attachOpts } = attachment ?? {}
16
+ ret.data._attachment = await mdl.findAttachment(ret.data.id, params, attachOpts)
17
+ }
16
18
  return ret
17
19
  }
18
20
 
package/lib/util.js CHANGED
@@ -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 = opts.attachment ?? req.query.attachment ?? {}
78
- if (attachment === true) attachment = { stats: true, mimeType: true, thumbnail: true }
79
- attachment.stats = attachment.stats ?? req.query.stats
80
- attachment.mimeType = attachment.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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-db",
3
- "version": "2.35.0",
3
+ "version": "2.37.0",
4
4
  "description": "DB Helper",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -1,11 +1,20 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-11-22
4
+
5
+ - [2.37.0] `recs-info` widget now accept `data-key` option
6
+ - [2.37.0] `pagination` widget now accept `data-key` option
7
+ - [2.37.0] Bug fix in `form` widget
8
+ - [2.37.0] Crud handlers now support `page` locals
9
+ - [2.37.0] Bug fix in crud methods when no data is returned
10
+
3
11
  ## 2026-11-20
4
12
 
5
13
  - [2.35.0] `attachment` options can now accept plain object too
6
14
  - [2.35.0] Bug fix in `attachment` handling
7
15
  - [2.35.0] Bug fix in `dataOnly` handling
8
16
  - [2.35.0] Submit button can be omitted in `query` widget
17
+ - [2.36.0] Refactoring everything related to attachment handling
9
18
 
10
19
  ## 2026-11-08
11
20