waibu-db 2.35.0 → 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,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) {
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) {
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) {
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
 
@@ -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) {
15
+ const { params, options: attachOpts } = attachment ?? {}
16
+ ret.data._attachment = await mdl.findAttachment(recId, 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.36.0",
4
4
  "description": "DB Helper",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -6,6 +6,7 @@
6
6
  - [2.35.0] Bug fix in `attachment` handling
7
7
  - [2.35.0] Bug fix in `dataOnly` handling
8
8
  - [2.35.0] Submit button can be omitted in `query` widget
9
+ - [2.36.0] Refactoring everything related to attachment handling
9
10
 
10
11
  ## 2026-11-08
11
12