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.
- package/extend/bajo/intl/en-US.json +1 -0
- package/extend/bajo/intl/id.json +1 -0
- package/lib/method/count-record.js +2 -2
- package/lib/method/create-aggregate.js +2 -2
- package/lib/method/create-histogram.js +2 -2
- package/lib/method/create-record.js +2 -2
- package/lib/method/find-all-record.js +2 -2
- package/lib/method/find-one-record.js +2 -2
- package/lib/method/find-record.js +2 -2
- package/lib/method/get-record.js +3 -3
- package/lib/method/remove-record.js +4 -3
- package/lib/method/update-record.js +4 -3
- package/lib/util.js +20 -3
- package/package.json +1 -1
- package/wiki/CHANGES.md +8 -0
package/extend/bajo/intl/id.json
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
17
|
+
return await processHandler.call(this, { action: 'findRecord', model: mdl, handler, options: opts })
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
export default findRecord
|
package/lib/method/get-record.js
CHANGED
|
@@ -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
|
|
11
|
+
return data
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
return
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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 =
|
|
61
|
+
opts.dataOnly = false
|
|
62
62
|
const data = await model.findOneRecord({ query }, opts)
|
|
63
|
-
if (
|
|
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