waibu-db 2.7.1 → 2.8.1

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/index.js CHANGED
@@ -114,7 +114,7 @@ async function factory (pkgName) {
114
114
  })
115
115
  const omenu = groupBy(map(models, s => {
116
116
  const item = { name: s.name, ns: s.plugin.ns }
117
- item.nsTitle = getPluginTitle(s.plugin.ns)
117
+ item.nsTitle = getPluginTitle(s.plugin.ns, req)
118
118
  return item
119
119
  }), 'nsTitle')
120
120
  const menu = []
@@ -2,7 +2,7 @@ function buildParams ({ model, req, reply, action, options = {} }) {
2
2
  const { camelCase, kebabCase, map, upperFirst, get } = this.app.lib._
3
3
  const [, ...names] = map(kebabCase(model).split('-'), n => upperFirst(n))
4
4
  const mdl = this.app.dobo.getModel(model)
5
- const prefix = this.app.waibuMpa ? this.app.waibuMpa.getPluginTitle(mdl.plugin.ns) : mdl.plugin.ns
5
+ const prefix = this.app.waibuMpa ? this.app.waibuMpa.getPluginTitle(mdl.plugin.ns, req) : mdl.plugin.ns
6
6
  const modelTitle = req.t(prefix) + ': ' + req.t(camelCase(names.join(' ')))
7
7
  const page = {
8
8
  title: req.t(get(req, 'routeOptions.config.title', this.app[mdl.plugin.ns].title)),
@@ -1,9 +1,14 @@
1
1
  import { prepCrud } from '../util.js'
2
2
 
3
- async function countRecord ({ model, req, reply, options = {} }) {
4
- const { model: mdl, opts, filter } = await prepCrud.call(this, { model, req, reply, options, args: ['model'] })
5
- const ret = await mdl.countRecord(filter, opts)
6
- return ret
3
+ async function countRecord ({ model, req, reply, options = {}, transaction } = {}) {
4
+ const { model: mdl, opts, filter } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
5
+
6
+ async function handler (trx) {
7
+ opts.trx = opts.trx || trx
8
+ return await mdl.countRecord(filter, opts)
9
+ }
10
+
11
+ return opts.trx === true ? await mdl.transaction(handler) : await handler()
7
12
  }
8
13
 
9
14
  export default countRecord
@@ -1,13 +1,19 @@
1
1
  import { prepCrud } from '../util.js'
2
2
 
3
- async function createAggregate ({ model, req, reply, options = {} }) {
4
- const { model: mdl, opts, filter } = await prepCrud.call(this, { model, req, reply, options, args: ['model'] })
3
+ async function createAggregate ({ model, req, reply, options = {}, transaction } = {}) {
4
+ const { model: mdl, opts, filter } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
5
5
  const params = {}
6
6
  for (const item of ['group', 'field', 'aggregates']) {
7
7
  params[item] = options[item] ?? req.params[item] ?? req.query[item]
8
8
  }
9
9
  params.aggregates = params.aggregates ?? ['count']
10
- return await mdl.createAggregate(filter, params, opts)
10
+
11
+ async function handler (trx) {
12
+ opts.trx = opts.trx || trx
13
+ return await mdl.createAggregate(filter, params, opts)
14
+ }
15
+
16
+ return opts.trx === true ? await mdl.transaction(handler) : await handler()
11
17
  }
12
18
 
13
19
  export default createAggregate
@@ -1,13 +1,19 @@
1
1
  import { prepCrud } from '../util.js'
2
2
 
3
- async function createHistogram ({ model, req, reply, options = {} }) {
4
- const { model: mdl, opts, filter } = await prepCrud.call(this, { model, req, reply, options, args: ['model'] })
3
+ async function createHistogram ({ model, req, reply, options = {}, transaction } = {}) {
4
+ const { model: mdl, opts, filter } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
5
5
  const params = {}
6
6
  for (const item of ['type', 'group', 'field', 'aggregates']) {
7
7
  params[item] = options[item] ?? req.params[item] ?? req.query[item]
8
8
  }
9
9
  params.aggregates = params.aggregates ?? ['count']
10
- return await mdl.createHistogram(filter, params, opts)
10
+
11
+ async function handler (trx) {
12
+ opts.trx = opts.trx || trx
13
+ return await mdl.createHistogram(filter, params, opts)
14
+ }
15
+
16
+ return opts.trx === true ? await mdl.transaction(handler) : await handler()
11
17
  }
12
18
 
13
19
  export default createHistogram
@@ -1,10 +1,16 @@
1
1
  import { prepCrud } from '../util.js'
2
2
 
3
- async function createRecord ({ model, req, reply, body, options = {} }) {
4
- const { model: mdl, input, opts, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, body, options, args: ['model'] })
5
- const ret = await mdl.createRecord(input, opts)
6
- if (attachment) ret.data._attachment = await mdl.findAttachment(ret.data.id, { stats, mimeType })
7
- return ret
3
+ async function createRecord ({ model, req, reply, body, options = {}, transaction } = {}) {
4
+ const { model: mdl, input, opts, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, body, options, args: ['model'], transaction })
5
+
6
+ async function handler (trx) {
7
+ if (opts.trx === true) opts.trx = trx
8
+ const ret = await mdl.createRecord(input, opts)
9
+ if (attachment) ret.data._attachment = await mdl.findAttachment(ret.data.id, { stats, mimeType })
10
+ return ret
11
+ }
12
+
13
+ return opts.trx === true ? await mdl.transaction(handler) : await handler()
8
14
  }
9
15
 
10
16
  export default createRecord
@@ -1,14 +1,20 @@
1
1
  import { prepCrud } from '../util.js'
2
2
 
3
- async function findAllRecord ({ model, req, reply, options = {} }) {
4
- const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'] })
5
- const ret = await mdl.findAllRecord(filter, opts)
6
- if (attachment) {
7
- for (const d of ret.data) {
8
- d._attachment = await mdl.findAttachment(d.id, { stats, mimeType })
3
+ async function findAllRecord ({ model, req, reply, options = {}, transaction } = {}) {
4
+ const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
5
+
6
+ async function handler (trx) {
7
+ opts.trx = opts.trx || trx
8
+ const ret = await mdl.findAllRecord(filter, opts)
9
+ if (attachment) {
10
+ for (const d of ret.data) {
11
+ d._attachment = await mdl.findAttachment(d.id, { stats, mimeType })
12
+ }
9
13
  }
14
+ return ret
10
15
  }
11
- return ret
16
+
17
+ return opts.trx === true ? await mdl.transaction(handler) : await handler()
12
18
  }
13
19
 
14
20
  export default findAllRecord
@@ -1,14 +1,16 @@
1
1
  import { prepCrud } from '../util.js'
2
2
 
3
- async function findOneRecord ({ model, req, reply, options = {} }) {
4
- const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'] })
5
- const ret = await mdl.findOneRecord(filter, opts)
6
- if (attachment) {
7
- for (const d of ret.data) {
8
- d._attachment = await mdl.findAttachment(d.id, { stats, mimeType })
9
- }
3
+ async function findOneRecord ({ model, req, reply, options = {}, transaction } = {}) {
4
+ const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
5
+
6
+ async function handler (trx) {
7
+ opts.trx = 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 })
10
+ return ret
10
11
  }
11
- return ret
12
+
13
+ return opts.trx === true ? await mdl.transaction(handler) : await handler()
12
14
  }
13
15
 
14
16
  export default findOneRecord
@@ -1,14 +1,20 @@
1
1
  import { prepCrud } from '../util.js'
2
2
 
3
- async function findRecord ({ model, req, reply, options = {} }) {
4
- const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'] })
5
- const ret = await mdl.findRecord(filter, opts)
6
- if (attachment) {
7
- for (const d of ret.data) {
8
- d._attachment = await mdl.findAttachment(d.id, { stats, mimeType })
3
+ async function findRecord ({ model, req, reply, options = {}, transaction } = {}) {
4
+ const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
5
+
6
+ async function handler (trx) {
7
+ opts.trx = opts.trx || trx
8
+ const ret = await mdl.findRecord(filter, opts)
9
+ if (attachment) {
10
+ for (const d of ret.data) {
11
+ d._attachment = await mdl.findAttachment(d.id, { stats, mimeType })
12
+ }
9
13
  }
14
+ return ret
10
15
  }
11
- return ret
16
+
17
+ return opts.trx === true ? await mdl.transaction(handler) : await handler()
12
18
  }
13
19
 
14
20
  export default findRecord
@@ -1,9 +1,17 @@
1
1
  import { prepCrud, getOneRecord } from '../util.js'
2
2
 
3
- async function getRecord ({ model, req, reply, id, options = {} }) {
4
- const { model: mdl, recId, filter, opts } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'] })
5
- const data = await getOneRecord.call(this, mdl, recId, filter, opts)
6
- return options.dataOnly ? data : { data }
3
+ async function getRecord ({ model, req, reply, id, options = {}, transaction } = {}) {
4
+ const { model: mdl, recId, filter, attachment, stats, mimeType, opts } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'], transaction })
5
+ const me = this
6
+
7
+ async function handler (trx) {
8
+ opts.trx = 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 })
11
+ return { data }
12
+ }
13
+
14
+ return opts.trx === true ? await mdl.transaction(handler) : await handler()
7
15
  }
8
16
 
9
17
  export default getRecord
@@ -1,10 +1,16 @@
1
1
  import { prepCrud, getOneRecord } from '../util.js'
2
2
 
3
- async function removeRecord ({ model, req, reply, id, options = {} }) {
4
- const { model: mdl, recId, opts, filter } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'] })
5
- opts._data = await getOneRecord.call(this, mdl, recId, filter, options)
6
- const result = await mdl.removeRecord(recId, opts)
7
- return result
3
+ async function removeRecord ({ model, req, reply, id, options = {}, transaction } = {}) {
4
+ const { model: mdl, recId, opts, filter } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'], transaction })
5
+ const me = this
6
+
7
+ async function handler (trx) {
8
+ opts.trx = opts.trx || trx
9
+ opts._data = await getOneRecord.call(me, mdl, recId, filter, opts)
10
+ return await mdl.removeRecord(recId, opts)
11
+ }
12
+
13
+ return opts.trx === true ? await mdl.transaction(handler) : await handler()
8
14
  }
9
15
 
10
16
  export default removeRecord
@@ -1,11 +1,18 @@
1
1
  import { prepCrud, getOneRecord } from '../util.js'
2
2
 
3
- async function updateRecord ({ model, req, reply, id, body, options = {} }) {
4
- const { model: mdl, filter, input, opts, recId, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, body, id, options, args: ['model', 'id'] })
5
- opts._data = await getOneRecord.call(this, mdl, recId, filter, options)
6
- const ret = await mdl.updateRecord(recId, input, opts)
7
- if (attachment) ret.data._attachment = await mdl.findAttachment(id, { stats, mimeType })
8
- return ret
3
+ async function updateRecord ({ model, req, reply, id, body, options = {}, transaction } = {}) {
4
+ const { model: mdl, filter, input, opts, recId, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, body, id, options, args: ['model', 'id'], transaction })
5
+ const me = this
6
+
7
+ async function handler (trx) {
8
+ opts.trx = opts.trx || trx
9
+ opts._data = await getOneRecord.call(me, mdl, recId, filter, opts)
10
+ const ret = await mdl.updateRecord(recId, input, opts)
11
+ if (attachment) ret.data._attachment = await mdl.findAttachment(id, { stats, mimeType })
12
+ return ret
13
+ }
14
+
15
+ return opts.trx === true ? await mdl.transaction(handler) : await handler()
9
16
  }
10
17
 
11
18
  export default updateRecord
package/lib/util.js CHANGED
@@ -1,14 +1,15 @@
1
- export async function prepCrud ({ model, body, id, req, reply, options = {}, args }) {
1
+ export async function prepCrud ({ model, body, id, req, reply, transaction, options = {}, args } = {}) {
2
2
  const { isSet } = this.app.lib.aneka
3
3
  const { importModule } = this.app.bajo
4
4
  const { parseFilter } = this.app.waibu
5
5
  const { pascalCase } = this.app.lib.aneka
6
- const { cloneDeep, has, isString } = this.app.lib._
6
+ const { cloneDeep, has, isString, omit } = this.app.lib._
7
7
  const { parseObject } = this.app.lib
8
8
  const { buildFilterQuery, buildFilterSearch } = await importModule('dobo:/lib/factory/model/_util.js', { asDefaultImport: false })
9
9
 
10
10
  const cfgWeb = this.app.waibu.getConfig()
11
- const opts = cloneDeep(options)
11
+ const opts = cloneDeep(omit(options, ['trx']))
12
+ opts.trx = options.trx
12
13
  const params = this.getParams(req, ...args)
13
14
  for (const k of ['count', 'fields']) {
14
15
  opts[k] = opts[k] ?? params[k]
@@ -17,6 +18,7 @@ export async function prepCrud ({ model, body, id, req, reply, options = {}, arg
17
18
  opts.dataOnly = opts.dataOnly ?? false
18
19
  opts.req = req
19
20
  opts.reply = reply
21
+ opts.trx = opts.trx || transaction
20
22
 
21
23
  let { attachment, stats, mimeType } = opts
22
24
  attachment = attachment ?? req.query.attachment
@@ -53,9 +55,9 @@ export async function prepCrud ({ model, body, id, req, reply, options = {}, arg
53
55
 
54
56
  export async function getOneRecord (model, id, filter, options) {
55
57
  const { cloneDeep } = this.app.lib._
56
- let query = cloneDeep(filter.query)
57
- query = { $and: [query ?? {}, { id }] }
58
- const data = await model.findOneRecord({ query }, { dataOnly: true, noHook: true, noDynHook: true, noModelHook: true, forceNoHidden: options.forceNoHidden })
58
+ let query = cloneDeep(filter.query || {})
59
+ query = { $and: [query, { id }] }
60
+ const data = await model.findOneRecord({ query }, { dataOnly: true, noHook: true, noDynHook: true, noModelHook: true, forceNoHidden: options.forceNoHidden, trx: options.trx })
59
61
  if (!data && options.throwNotFound) throw this.error('_notFound')
60
62
  return data
61
63
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-db",
3
- "version": "2.7.1",
3
+ "version": "2.8.1",
4
4
  "description": "DB Helper",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -1,22 +1,30 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-03-08
4
+
5
+ - [2.8.1] Bug fix in ```build-params.js```
6
+
7
+ ## 2026-03-05
8
+
9
+ - [2.8.0] Add dobo's transaction support
10
+
3
11
  ## 2026-02-22
4
12
 
5
- - [2.7.1] Bug fix on query parser
13
+ - [2.7.1] Bug fix in query parser
6
14
 
7
15
  ## 2026-02-20
8
16
 
9
17
  - [2.7.0] Update ```WdbBtnColumns``` to allow untick ID field too
10
- - [2.7.0] Bug fix on theme & iconset resolver
18
+ - [2.7.0] Bug fix in theme & iconset resolver
11
19
  - [2.7.0] Add capability to set value from ```prop.values``` in ```WdbTable```
12
- - [2.7.0] Bug fix on query builder
20
+ - [2.7.0] Bug fix in query builder
13
21
  - [2.7.0] Add capability to handle value from ```prop.values``` in ```getSchemaExt()```
14
22
 
15
23
  ## 2026-02-18
16
24
 
17
25
  - [2.6.0] Update attribute functions from ```waibu```
18
- - [2.6.0] Bug fix on ```getSchemaExt()```'s ```applyLayout()```
19
- - [2.6.1] Bug fix on ```getSchemaExt()```
26
+ - [2.6.0] Bug fix in ```getSchemaExt()```'s ```applyLayout()```
27
+ - [2.6.1] Bug fix in ```getSchemaExt()```
20
28
 
21
29
  ## 2026-02-15
22
30
 
@@ -28,14 +36,14 @@
28
36
 
29
37
  ## 2026-02-04
30
38
 
31
- - [2.3.2] Bug fix on ```detailsHandler()```
32
- - [2.3.2] Bug fix on ```getOneRecord()```
33
- - [2.3.3] Bug fix on calling ```getOneRecord()``` without scope
39
+ - [2.3.2] Bug fix in ```detailsHandler()```
40
+ - [2.3.2] Bug fix in ```getOneRecord()```
41
+ - [2.3.3] Bug fix in calling ```getOneRecord()``` without scope
34
42
 
35
43
  ## 2026-02-03
36
44
 
37
- - [2.3.1] Bug fix on widget menu direction
38
- - [2.3.1] Bug fix on widget's fields visibility
45
+ - [2.3.1] Bug fix in widget menu direction
46
+ - [2.3.1] Bug fix in widget's fields visibility
39
47
 
40
48
  ## 2026-01-30
41
49
 
@@ -53,11 +61,11 @@
53
61
 
54
62
  ## 2026-01-21
55
63
 
56
- - [2.1.7] Bug fix on page titles
64
+ - [2.1.7] Bug fix in page titles
57
65
 
58
66
  ## 2026-01-19
59
67
 
60
- - [2.1.6] Bug fix on app title
68
+ - [2.1.6] Bug fix in app title
61
69
  - [2.1.6] Add some missing translations
62
70
 
63
71
  ## 2026-01-17
@@ -67,11 +75,11 @@
67
75
 
68
76
  ## 2026-01-16
69
77
 
70
- - [2.1.2] Bug fix on model references
78
+ - [2.1.2] Bug fix in model references
71
79
 
72
80
  ## 2026-01-13
73
81
 
74
- - [2.1.1] Bug fix on waibuMpa's widgeting system
82
+ - [2.1.1] Bug fix in waibuMpa's widgeting system
75
83
 
76
84
  ## 2025-12-28
77
85