waibu-db 2.10.2 → 2.11.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.
@@ -37,6 +37,7 @@
37
37
  "tsv": "TSV",
38
38
  "ndjson": "NDJSON",
39
39
  "dataValue": "Data value",
40
+ "suppressedError": "Error occured and suppressed. Please check error log for details",
40
41
  "op": {
41
42
  "equals": "Equals",
42
43
  "notEquals": "Not Equals",
@@ -37,6 +37,7 @@
37
37
  "tsv": "TSV",
38
38
  "ndjson": "NDJSON",
39
39
  "dataValue": "Nilai data",
40
+ "suppressedError": "Kesalahan terjadi dan tidak ditampilkan. Silahkan cek log kesalahan untuk detilnya",
40
41
  "op": {
41
42
  "equals": "Sama Dengan",
42
43
  "notEquals": "Tidak Sama Dengan",
@@ -1,4 +1,4 @@
1
- import { prepCrud } from '../util.js'
1
+ import { prepCrud, processHandler } from '../util.js'
2
2
 
3
3
  async function countRecord ({ model, req, reply, options = {}, transaction } = {}) {
4
4
  const { model: mdl, opts, filter } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
@@ -8,7 +8,7 @@ async function countRecord ({ model, req, reply, options = {}, transaction } = {
8
8
  return await mdl.countRecord(filter, opts)
9
9
  }
10
10
 
11
- return opts.trx === true ? await mdl.transaction(handler) : await handler()
11
+ return await processHandler.call(this, { action: 'countRecord', model: mdl, handler, options: opts })
12
12
  }
13
13
 
14
14
  export default countRecord
@@ -1,4 +1,4 @@
1
- import { prepCrud } from '../util.js'
1
+ import { prepCrud, processHandler } from '../util.js'
2
2
 
3
3
  async function createAggregate ({ model, req, reply, options = {}, transaction } = {}) {
4
4
  const { model: mdl, opts, filter } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
@@ -13,7 +13,7 @@ async function createAggregate ({ model, req, reply, options = {}, transaction }
13
13
  return await mdl.createAggregate(filter, params, opts)
14
14
  }
15
15
 
16
- return opts.trx === true ? await mdl.transaction(handler) : await handler()
16
+ return await processHandler.call(this, { action: 'createAggregate', model: mdl, handler, options: opts })
17
17
  }
18
18
 
19
19
  export default createAggregate
@@ -1,4 +1,4 @@
1
- import { prepCrud } from '../util.js'
1
+ import { prepCrud, processHandler } from '../util.js'
2
2
 
3
3
  async function createHistogram ({ model, req, reply, options = {}, transaction } = {}) {
4
4
  const { model: mdl, opts, filter } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
@@ -13,7 +13,7 @@ async function createHistogram ({ model, req, reply, options = {}, transaction }
13
13
  return await mdl.createHistogram(filter, params, opts)
14
14
  }
15
15
 
16
- return opts.trx === true ? await mdl.transaction(handler) : await handler()
16
+ return await processHandler.call(this, { action: 'createHistogram', model: mdl, handler, options: opts })
17
17
  }
18
18
 
19
19
  export default createHistogram
@@ -1,4 +1,4 @@
1
- import { prepCrud } from '../util.js'
1
+ import { prepCrud, processHandler } from '../util.js'
2
2
 
3
3
  async function createRecord ({ model, req, reply, body, options = {}, transaction } = {}) {
4
4
  const { model: mdl, input, opts, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, body, options, args: ['model'], transaction })
@@ -10,7 +10,7 @@ async function createRecord ({ model, req, reply, body, options = {}, transactio
10
10
  return ret
11
11
  }
12
12
 
13
- return opts.trx === true ? await mdl.transaction(handler) : await handler()
13
+ return await processHandler.call(this, { action: 'createRecord', model: mdl, handler, options: opts })
14
14
  }
15
15
 
16
16
  export default createRecord
@@ -1,4 +1,4 @@
1
- import { prepCrud } from '../util.js'
1
+ import { prepCrud, processHandler } from '../util.js'
2
2
 
3
3
  async function findAllRecord ({ model, req, reply, options = {}, transaction } = {}) {
4
4
  const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
@@ -14,7 +14,7 @@ async function findAllRecord ({ model, req, reply, options = {}, transaction } =
14
14
  return ret
15
15
  }
16
16
 
17
- return opts.trx === true ? await mdl.transaction(handler) : await handler()
17
+ return await processHandler.call(this, { action: 'findAllRecord', model: mdl, handler, options: opts })
18
18
  }
19
19
 
20
20
  export default findAllRecord
@@ -1,4 +1,4 @@
1
- import { prepCrud } from '../util.js'
1
+ import { prepCrud, processHandler } from '../util.js'
2
2
 
3
3
  async function findOneRecord ({ model, req, reply, options = {}, transaction } = {}) {
4
4
  const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
@@ -10,7 +10,7 @@ async function findOneRecord ({ model, req, reply, options = {}, transaction } =
10
10
  return ret
11
11
  }
12
12
 
13
- return opts.trx === true ? await mdl.transaction(handler) : await handler()
13
+ return await processHandler.call(this, { action: 'findOneRecord', model: mdl, handler, options: opts })
14
14
  }
15
15
 
16
16
  export default findOneRecord
@@ -1,4 +1,4 @@
1
- import { prepCrud } from '../util.js'
1
+ import { prepCrud, processHandler } from '../util.js'
2
2
 
3
3
  async function findRecord ({ model, req, reply, options = {}, transaction } = {}) {
4
4
  const { model: mdl, opts, filter, attachment, stats, mimeType } = await prepCrud.call(this, { model, req, reply, options, args: ['model'], transaction })
@@ -14,7 +14,7 @@ async function findRecord ({ model, req, reply, options = {}, transaction } = {}
14
14
  return ret
15
15
  }
16
16
 
17
- return opts.trx === true ? await mdl.transaction(handler) : await handler()
17
+ return await processHandler.call(this, { action: 'findRecord', model: mdl, handler, options: opts })
18
18
  }
19
19
 
20
20
  export default findRecord
@@ -1,4 +1,4 @@
1
- import { prepCrud, getOneRecord } from '../util.js'
1
+ import { prepCrud, getOneRecord, processHandler } from '../util.js'
2
2
 
3
3
  async function getRecord ({ model, req, reply, id, options = {}, transaction } = {}) {
4
4
  const { model: mdl, recId, filter, attachment, stats, mimeType, opts } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'], transaction })
@@ -8,10 +8,10 @@ async function getRecord ({ model, req, reply, id, options = {}, transaction } =
8
8
  opts.trx = opts.trx || trx
9
9
  const data = await getOneRecord.call(me, mdl, recId, filter, opts)
10
10
  if (attachment) data._attachment = await mdl.findAttachment(data.id, { stats, mimeType })
11
- return { data }
11
+ return data
12
12
  }
13
13
 
14
- return opts.trx === true ? await mdl.transaction(handler) : await handler()
14
+ return await processHandler.call(this, { action: 'getRecord', model: mdl, handler, options: opts })
15
15
  }
16
16
 
17
17
  export default getRecord
@@ -1,4 +1,4 @@
1
- import { prepCrud, getOneRecord } from '../util.js'
1
+ import { prepCrud, getOneRecord, processHandler } from '../util.js'
2
2
 
3
3
  async function removeRecord ({ model, req, reply, id, options = {}, transaction } = {}) {
4
4
  const { model: mdl, recId, opts, filter } = await prepCrud.call(this, { model, req, reply, id, options, args: ['model', 'id'], transaction })
@@ -6,11 +6,12 @@ async function removeRecord ({ model, req, reply, id, options = {}, transaction
6
6
 
7
7
  async function handler (trx) {
8
8
  opts.trx = opts.trx || trx
9
- opts._data = await getOneRecord.call(me, mdl, recId, filter, opts)
9
+ const resp = await getOneRecord.call(me, mdl, recId, filter, opts)
10
+ opts._data = resp.data
10
11
  return await mdl.removeRecord(recId, opts)
11
12
  }
12
13
 
13
- return opts.trx === true ? await mdl.transaction(handler) : await handler()
14
+ return await processHandler.call(this, { action: 'removeRecord', model: mdl, handler, options: opts })
14
15
  }
15
16
 
16
17
  export default removeRecord
@@ -1,4 +1,4 @@
1
- import { prepCrud, getOneRecord } from '../util.js'
1
+ import { prepCrud, getOneRecord, processHandler } from '../util.js'
2
2
 
3
3
  async function updateRecord ({ model, req, reply, id, body, options = {}, transaction } = {}) {
4
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 })
@@ -6,13 +6,14 @@ async function updateRecord ({ model, req, reply, id, body, options = {}, transa
6
6
 
7
7
  async function handler (trx) {
8
8
  opts.trx = opts.trx || trx
9
- opts._data = await getOneRecord.call(me, mdl, recId, filter, opts)
9
+ const resp = await getOneRecord.call(me, mdl, recId, filter, opts)
10
+ opts._data = resp.data
10
11
  const ret = await mdl.updateRecord(recId, input, opts)
11
12
  if (attachment) ret.data._attachment = await mdl.findAttachment(id, { stats, mimeType })
12
13
  return ret
13
14
  }
14
15
 
15
- return opts.trx === true ? await mdl.transaction(handler) : await handler()
16
+ return await processHandler.call(this, { action: 'updateRecord', model: mdl, handler, options: opts })
16
17
  }
17
18
 
18
19
  export default updateRecord
package/lib/util.js CHANGED
@@ -54,12 +54,29 @@ export async function prepCrud ({ model, body, id, req, reply, transaction, opti
54
54
  }
55
55
 
56
56
  export async function getOneRecord (model, id, filter, options) {
57
- const { cloneDeep, pick } = this.app.lib._
57
+ const { cloneDeep, pick, isEmpty } = this.app.lib._
58
58
  let query = cloneDeep(filter.query || {})
59
59
  query = { $and: [query, { id }] }
60
60
  const opts = pick(options, ['forceNoHidden', 'trx', 'req', 'refs'])
61
- opts.dataOnly = true
61
+ opts.dataOnly = false
62
62
  const data = await model.findOneRecord({ query }, opts)
63
- if (!data && options.throwNotFound) throw this.error('_notFound')
63
+ if (isEmpty(data.data) && options.throwNotFound) throw this.error('_notFound')
64
64
  return data
65
65
  }
66
+
67
+ export async function processHandler ({ action, model, handler, options } = {}) {
68
+ function suppressedReturn () {
69
+ if (action === 'countRecord') return options.dataOnly ? 0 : { data: 0, warnings: [options.req.t('supppressedError')] }
70
+ if (['createRecord', 'getRecord'].includes(action)) return options.dataOnly ? {} : { data: {}, warnings: [options.req.t('supppressedError')] }
71
+ if (action === 'removeRecord') return options.dataOnly ? {} : { oldData: {}, warnings: [options.req.t('supppressedError')] }
72
+ if (action === 'updateRecord') return options.dataOnly ? {} : { data: {}, oldData: {}, warnings: [options.req.t('supppressedError')] }
73
+ return options.dataOnly ? [] : { data: [], count: 0, page: 1, warnings: [options.req.t('supppressedError')] }
74
+ }
75
+
76
+ try {
77
+ return options.trx === true ? (await model.transaction(handler)) : (await handler())
78
+ } catch (err) {
79
+ if (options.suppressError) return suppressedReturn()
80
+ throw err
81
+ }
82
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-db",
3
- "version": "2.10.2",
3
+ "version": "2.11.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-03-26
4
+
5
+ - [2.11.0] Adding ```suppressError``` to model options
6
+
7
+ ## 2026-03-25
8
+
9
+ - [2.10.3] Bug fix in ```getOneRecord()```
10
+
3
11
  ## 2026-03-22
4
12
 
5
13
  - [2.10.1] Bug fix in reference records