dobo 2.38.0 → 2.39.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/.bootorder +0 -0
- package/.github/FUNDING.yml +0 -0
- package/.github/workflows/repo-lockdown.yml +0 -0
- package/.jsdoc.conf.json +0 -0
- package/LICENSE +0 -0
- package/README.md +0 -0
- package/docs/Dobo.html +0 -0
- package/docs/DoboAction.html +0 -0
- package/docs/DoboAdapter.html +0 -0
- package/docs/DoboConnection.html +0 -0
- package/docs/DoboFeature.html +0 -0
- package/docs/DoboModel.html +0 -0
- package/docs/data/search.json +0 -0
- package/docs/extend_bajo_hook-docs.js.html +0 -0
- package/docs/external-Tools.html +0 -0
- package/docs/fonts/Inconsolata-Regular.ttf +0 -0
- package/docs/fonts/OpenSans-Regular.ttf +0 -0
- package/docs/fonts/WorkSans-Bold.ttf +0 -0
- package/docs/global.html +0 -0
- package/docs/index.html +0 -0
- package/docs/index.js.html +0 -0
- package/docs/lib_factory_action.js.html +0 -0
- package/docs/lib_factory_adapter.js.html +0 -0
- package/docs/lib_factory_connection.js.html +0 -0
- package/docs/lib_factory_feature.js.html +0 -0
- package/docs/lib_factory_model.js.html +0 -0
- package/docs/lib_factory_model_build.js.html +0 -0
- package/docs/lib_factory_model_clear-record.js.html +0 -0
- package/docs/lib_factory_model_count-record.js.html +0 -0
- package/docs/lib_factory_model_create-record.js.html +0 -0
- package/docs/lib_factory_model_drop.js.html +0 -0
- package/docs/lib_factory_model_exists.js.html +0 -0
- package/docs/lib_factory_model_find-all-record.js.html +0 -0
- package/docs/lib_factory_model_find-one-record.js.html +0 -0
- package/docs/lib_factory_model_find-record.js.html +0 -0
- package/docs/lib_factory_model_get-record.js.html +0 -0
- package/docs/lib_factory_model_helper.js.html +0 -0
- package/docs/lib_factory_model_remove-record.js.html +0 -0
- package/docs/lib_factory_model_sanitize-body.js.html +0 -0
- package/docs/lib_factory_model_sanitize-fixture.js.html +0 -0
- package/docs/lib_factory_model_sanitize-record.js.html +0 -0
- package/docs/lib_factory_model_update-record.js.html +0 -0
- package/docs/lib_factory_model_upsert-record.js.html +0 -0
- package/docs/lib_factory_model_validate.js.html +0 -0
- package/docs/lib_helper.js.html +0 -0
- package/docs/module-Helper.html +0 -0
- package/docs/module-Helper_Model.html +0 -0
- package/docs/module-Hook.html +0 -0
- package/docs/scripts/core.js +0 -0
- package/docs/scripts/core.min.js +0 -0
- package/docs/scripts/resize.js +0 -0
- package/docs/scripts/search.js +0 -0
- package/docs/scripts/search.min.js +5 -5
- package/docs/scripts/third-party/Apache-License-2.0.txt +0 -0
- package/docs/scripts/third-party/fuse.js +0 -0
- package/docs/scripts/third-party/hljs-line-num-original.js +0 -0
- package/docs/scripts/third-party/hljs-line-num.js +0 -0
- package/docs/scripts/third-party/hljs-original.js +0 -0
- package/docs/scripts/third-party/hljs.js +0 -0
- package/docs/scripts/third-party/popper.js +0 -0
- package/docs/scripts/third-party/tippy.js +0 -0
- package/docs/scripts/third-party/tocbot.js +0 -0
- package/docs/scripts/third-party/tocbot.min.js +0 -0
- package/docs/static/bitcoin.jpeg +0 -0
- package/docs/static/home.md +0 -0
- package/docs/static/logo-ecosystem.png +0 -0
- package/docs/static/logo.png +0 -0
- package/docs/styles/clean-jsdoc-theme-base.css +0 -0
- package/docs/styles/clean-jsdoc-theme-dark.css +0 -0
- package/docs/styles/clean-jsdoc-theme-light.css +0 -0
- package/docs/styles/clean-jsdoc-theme-scrollbar.css +0 -0
- package/docs/styles/clean-jsdoc-theme-without-scrollbar.min.css +0 -0
- package/docs/styles/clean-jsdoc-theme.min.css +0 -0
- package/extend/bajo/hook.js +236 -236
- package/extend/bajo/intl/en-US.json +0 -0
- package/extend/bajo/intl/id.json +0 -0
- package/extend/bajoCli/applet/aggregate.js +0 -0
- package/extend/bajoCli/applet/clear-record.js +0 -0
- package/extend/bajoCli/applet/connection.js +0 -0
- package/extend/bajoCli/applet/count-record.js +0 -0
- package/extend/bajoCli/applet/create-record.js +0 -0
- package/extend/bajoCli/applet/find-record.js +0 -0
- package/extend/bajoCli/applet/get-record.js +0 -0
- package/extend/bajoCli/applet/histogram.js +0 -0
- package/extend/bajoCli/applet/lib/post-process.js +0 -0
- package/extend/bajoCli/applet/model.js +0 -0
- package/extend/bajoCli/applet/rebuild-model.js +0 -0
- package/extend/bajoCli/applet/remove-record.js +0 -0
- package/extend/bajoCli/applet/update-record.js +0 -0
- package/extend/bajoCli/applet.js +0 -0
- package/extend/dobo/adapter/memory.js +175 -175
- package/extend/dobo/feature/created-at.js +0 -0
- package/extend/dobo/feature/dt.js +0 -0
- package/extend/dobo/feature/image.js +0 -0
- package/extend/dobo/feature/immutable.js +0 -0
- package/extend/dobo/feature/removed-at.js +0 -0
- package/extend/dobo/feature/unique.js +0 -0
- package/extend/dobo/feature/updated-at.js +0 -0
- package/extend/waibuMpa/route/attachment/@model/@id/@field/@file.js +0 -0
- package/extend/waibuStatic/virtual.json +0 -0
- package/index.js +0 -0
- package/lib/factory/action.js +287 -287
- package/lib/factory/adapter.js +1055 -1055
- package/lib/factory/connection.js +109 -109
- package/lib/factory/feature.js +52 -52
- package/lib/factory/model/aggregate.js +23 -23
- package/lib/factory/model/build.js +25 -25
- package/lib/factory/model/bulk-create-record.js +36 -36
- package/lib/factory/model/clear-record.js +30 -30
- package/lib/factory/model/count-record.js +42 -42
- package/lib/factory/model/create-attachment.js +36 -36
- package/lib/factory/model/create-record.js +49 -49
- package/lib/factory/model/drop.js +26 -26
- package/lib/factory/model/exists.js +26 -26
- package/lib/factory/model/find-all-record.js +112 -112
- package/lib/factory/model/find-attachment.js +28 -28
- package/lib/factory/model/find-one-record.js +38 -38
- package/lib/factory/model/find-record.js +82 -82
- package/lib/factory/model/get-attachment.js +15 -15
- package/lib/factory/model/get-record.js +52 -52
- package/lib/factory/model/helper.js +575 -576
- package/lib/factory/model/histogram.js +23 -23
- package/lib/factory/model/list-attachment.js +40 -40
- package/lib/factory/model/load-fixtures.js +63 -63
- package/lib/factory/model/remove-attachment.js +24 -24
- package/lib/factory/model/remove-record.js +44 -44
- package/lib/factory/model/sanitize-body.js +57 -57
- package/lib/factory/model/sanitize-fixture.js +56 -56
- package/lib/factory/model/sanitize-record.js +80 -80
- package/lib/factory/model/transaction.js +15 -15
- package/lib/factory/model/update-attachment.js +9 -9
- package/lib/factory/model/update-record.js +58 -58
- package/lib/factory/model/upsert-record.js +71 -71
- package/lib/factory/model/validate.js +0 -0
- package/lib/factory/model.js +503 -503
- package/lib/helper.js +463 -463
- package/package.json +1 -1
- package/test/e2e/_run.js +12 -12
- package/test/e2e/e2e-factory-process.test.js +25 -25
- package/test/e2e/e2e-model-adapter-process.test.js +52 -52
- package/test/integration/aspect-01-start-connections.test.js +33 -33
- package/test/integration/aspect-02-query-regex.test.js +34 -34
- package/test/integration/aspect-03-model-adapter-flow.test.js +88 -88
- package/test/unit/_stub.js +244 -244
- package/test/unit/dobo-action.test.js +58 -58
- package/test/unit/dobo-connection.test.js +37 -37
- package/test/unit/dobo-core.test.js +136 -136
- package/test/unit/dobo-feature.test.js +25 -25
- package/test/unit/dobo-model.test.js +115 -115
- package/test/unit/helper-sanitize-all.test.js +40 -40
- package/test/unit/helper-sanitize-ref.test.js +64 -64
- package/wiki/APPLETS.md +0 -0
- package/wiki/CHANGES.md +477 -469
- package/wiki/CONFIG.md +0 -0
- package/wiki/CONTRIBUTING.md +0 -0
- package/wiki/DEV-GUIDE.md +0 -0
- package/wiki/ECOSYSTEM.md +0 -0
- package/wiki/GETTING-STARTED.md +0 -0
- package/wiki/QUERY-LANGUAGE.md +0 -0
- package/wiki/USER-GUIDE.md +0 -0
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import { getFilterAndOptions, execHook, execModelHook, execDynHook, getRefs, sanitizeQuery } from './helper.js'
|
|
2
|
-
const action = 'findRecord'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @typedef TFilter
|
|
6
|
-
* @type {Object}
|
|
7
|
-
* @memberof DoboModel
|
|
8
|
-
* @see Dobo#recordFind
|
|
9
|
-
* @see Dobo#recordFindOne
|
|
10
|
-
* @see Dobo#recordFindAll
|
|
11
|
-
* @property {(string|Object)} [query={}] - Query definition. See {@tutorial query-language} for more
|
|
12
|
-
* @property {number} limit - Maximum number of records to return
|
|
13
|
-
* @property {number} page - Which page to return
|
|
14
|
-
* @property {number} skip - Records to skip
|
|
15
|
-
* @property {DoboModel.TSort} sort - Sort order info
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Finds records in the model's underlying data store that match the provided filter criteria.
|
|
20
|
-
*
|
|
21
|
-
* If no arguments are provided, it automatically turns into a chainable {@link DoboAction} object.
|
|
22
|
-
* @async
|
|
23
|
-
* @memberof DoboModel
|
|
24
|
-
* @method
|
|
25
|
-
* @param {DoboModel.TFilter} [filter={}] - The filter criteria to apply when finding records.
|
|
26
|
-
* @param {DoboModel.TOptions} [opts={}] - Options object.
|
|
27
|
-
* @returns {DoboAction|DoboModel.TResultFindRecord|Array.<DoboModel.TRecord>}
|
|
28
|
-
* @see {@link module:Hook.beforeFindRecord}
|
|
29
|
-
* @see {@link module:Hook.afterFindRecord}
|
|
30
|
-
*/
|
|
31
|
-
async function findRecord (...args) {
|
|
32
|
-
if (args.length === 0) return this.action(action, ...args)
|
|
33
|
-
const { getDefaultValues, t } = this.app.dobo
|
|
34
|
-
const [params = {}, opts = {}] = args
|
|
35
|
-
const { isSet } = this.app.lib.aneka
|
|
36
|
-
const { pick, omit, cloneDeep } = this.app.lib._
|
|
37
|
-
const { get, set } = this.app.bajoCache ?? {}
|
|
38
|
-
opts.dataOnly = opts.dataOnly ?? true
|
|
39
|
-
const { dataOnly } = opts
|
|
40
|
-
const { filter, options } = await getFilterAndOptions.call(this, params, opts, action)
|
|
41
|
-
const { hardCap, warnings } = getDefaultValues(options)
|
|
42
|
-
if (dataOnly) options.count = false
|
|
43
|
-
const { noResultSanitizer } = options
|
|
44
|
-
await execHook.call(this, 'beforeFindRecord', filter, options)
|
|
45
|
-
await execModelHook.call(this, 'beforeFindRecord', filter, options)
|
|
46
|
-
await execDynHook.call(this, 'beforeFindRecord', filter, options)
|
|
47
|
-
filter.query = sanitizeQuery.call(this, filter.query)
|
|
48
|
-
const cFilter = cloneDeep(filter)
|
|
49
|
-
if (get) {
|
|
50
|
-
const resp = await get({ model: this, action, filter: cFilter, options })
|
|
51
|
-
if (resp) {
|
|
52
|
-
resp.cached = true
|
|
53
|
-
return dataOnly ? resp.data : resp
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
let result = options.record ?? (await this.adapter._findRecord(this, filter, options)) ?? {}
|
|
57
|
-
result.page = filter.page
|
|
58
|
-
result.limit = filter.limit
|
|
59
|
-
result.filter = pick(filter, ['query', 'search', 'sort'])
|
|
60
|
-
result.warnings = result.warnings ?? []
|
|
61
|
-
if (!options.count) result = omit(result, ['count', 'pages'])
|
|
62
|
-
else if (options.count && result.count > hardCap) {
|
|
63
|
-
result.warnings.push(t('hardCapWarning%s%s', result.count, hardCap))
|
|
64
|
-
result.count = hardCap
|
|
65
|
-
result.hardCapped = true
|
|
66
|
-
}
|
|
67
|
-
result.pages = options.count ? Math.ceil(result.count / filter.limit) : undefined
|
|
68
|
-
if (!warnings) delete result.warnings
|
|
69
|
-
if (isSet(options.refs)) await getRefs.call(this, result.data, options)
|
|
70
|
-
if (!noResultSanitizer) {
|
|
71
|
-
for (const idx in result.data) {
|
|
72
|
-
result.data[idx] = await this.sanitizeRecord(result.data[idx], options)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
await execDynHook.call(this, 'afterFindRecord', filter, result, options)
|
|
76
|
-
await execModelHook.call(this, 'afterFindRecord', filter, result, options)
|
|
77
|
-
await execHook.call(this, 'afterFindRecord', filter, result, options)
|
|
78
|
-
if (set) await set({ model: this, action, filter: cFilter, options, result })
|
|
79
|
-
return dataOnly ? result.data : result
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export default findRecord
|
|
1
|
+
import { getFilterAndOptions, execHook, execModelHook, execDynHook, getRefs, sanitizeQuery } from './helper.js'
|
|
2
|
+
const action = 'findRecord'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef TFilter
|
|
6
|
+
* @type {Object}
|
|
7
|
+
* @memberof DoboModel
|
|
8
|
+
* @see Dobo#recordFind
|
|
9
|
+
* @see Dobo#recordFindOne
|
|
10
|
+
* @see Dobo#recordFindAll
|
|
11
|
+
* @property {(string|Object)} [query={}] - Query definition. See {@tutorial query-language} for more
|
|
12
|
+
* @property {number} limit - Maximum number of records to return
|
|
13
|
+
* @property {number} page - Which page to return
|
|
14
|
+
* @property {number} skip - Records to skip
|
|
15
|
+
* @property {DoboModel.TSort} sort - Sort order info
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Finds records in the model's underlying data store that match the provided filter criteria.
|
|
20
|
+
*
|
|
21
|
+
* If no arguments are provided, it automatically turns into a chainable {@link DoboAction} object.
|
|
22
|
+
* @async
|
|
23
|
+
* @memberof DoboModel
|
|
24
|
+
* @method
|
|
25
|
+
* @param {DoboModel.TFilter} [filter={}] - The filter criteria to apply when finding records.
|
|
26
|
+
* @param {DoboModel.TOptions} [opts={}] - Options object.
|
|
27
|
+
* @returns {DoboAction|DoboModel.TResultFindRecord|Array.<DoboModel.TRecord>}
|
|
28
|
+
* @see {@link module:Hook.beforeFindRecord}
|
|
29
|
+
* @see {@link module:Hook.afterFindRecord}
|
|
30
|
+
*/
|
|
31
|
+
async function findRecord (...args) {
|
|
32
|
+
if (args.length === 0) return this.action(action, ...args)
|
|
33
|
+
const { getDefaultValues, t } = this.app.dobo
|
|
34
|
+
const [params = {}, opts = {}] = args
|
|
35
|
+
const { isSet } = this.app.lib.aneka
|
|
36
|
+
const { pick, omit, cloneDeep } = this.app.lib._
|
|
37
|
+
const { get, set } = this.app.bajoCache ?? {}
|
|
38
|
+
opts.dataOnly = opts.dataOnly ?? true
|
|
39
|
+
const { dataOnly } = opts
|
|
40
|
+
const { filter, options } = await getFilterAndOptions.call(this, params, opts, action)
|
|
41
|
+
const { hardCap, warnings } = getDefaultValues(options)
|
|
42
|
+
if (dataOnly) options.count = false
|
|
43
|
+
const { noResultSanitizer } = options
|
|
44
|
+
await execHook.call(this, 'beforeFindRecord', filter, options)
|
|
45
|
+
await execModelHook.call(this, 'beforeFindRecord', filter, options)
|
|
46
|
+
await execDynHook.call(this, 'beforeFindRecord', filter, options)
|
|
47
|
+
filter.query = sanitizeQuery.call(this, filter.query)
|
|
48
|
+
const cFilter = cloneDeep(filter)
|
|
49
|
+
if (get) {
|
|
50
|
+
const resp = await get({ model: this, action, filter: cFilter, options })
|
|
51
|
+
if (resp) {
|
|
52
|
+
resp.cached = true
|
|
53
|
+
return dataOnly ? resp.data : resp
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
let result = options.record ?? (await this.adapter._findRecord(this, filter, options)) ?? {}
|
|
57
|
+
result.page = filter.page
|
|
58
|
+
result.limit = filter.limit
|
|
59
|
+
result.filter = pick(filter, ['query', 'search', 'sort'])
|
|
60
|
+
result.warnings = result.warnings ?? []
|
|
61
|
+
if (!options.count) result = omit(result, ['count', 'pages'])
|
|
62
|
+
else if (options.count && result.count > hardCap) {
|
|
63
|
+
result.warnings.push(t('hardCapWarning%s%s', result.count, hardCap))
|
|
64
|
+
result.count = hardCap
|
|
65
|
+
result.hardCapped = true
|
|
66
|
+
}
|
|
67
|
+
result.pages = options.count ? Math.ceil(result.count / filter.limit) : undefined
|
|
68
|
+
if (!warnings) delete result.warnings
|
|
69
|
+
if (isSet(options.refs)) await getRefs.call(this, result.data, options)
|
|
70
|
+
if (!noResultSanitizer) {
|
|
71
|
+
for (const idx in result.data) {
|
|
72
|
+
result.data[idx] = await this.sanitizeRecord(result.data[idx], options)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
await execDynHook.call(this, 'afterFindRecord', filter, result, options)
|
|
76
|
+
await execModelHook.call(this, 'afterFindRecord', filter, result, options)
|
|
77
|
+
await execHook.call(this, 'afterFindRecord', filter, result, options)
|
|
78
|
+
if (set) await set({ model: this, action, filter: cFilter, options, result })
|
|
79
|
+
return dataOnly ? result.data : result
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export default findRecord
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
const action = 'getAttachment'
|
|
2
|
-
|
|
3
|
-
async function getAttachment (...args) {
|
|
4
|
-
if (!this.options.attachment) return
|
|
5
|
-
if (args.length === 0) return this.action(action, ...args)
|
|
6
|
-
let [id, field, file, opts = {}] = args
|
|
7
|
-
const { find } = this.app.lib._
|
|
8
|
-
const all = await this.findAttachment(id, opts)
|
|
9
|
-
if (field === 'null') field = null
|
|
10
|
-
const data = find(all, { field, file })
|
|
11
|
-
if (!data) throw this.error('notFound', { statusCode: 404 })
|
|
12
|
-
return data
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export default getAttachment
|
|
1
|
+
const action = 'getAttachment'
|
|
2
|
+
|
|
3
|
+
async function getAttachment (...args) {
|
|
4
|
+
if (!this.options.attachment) return
|
|
5
|
+
if (args.length === 0) return this.action(action, ...args)
|
|
6
|
+
let [id, field, file, opts = {}] = args
|
|
7
|
+
const { find } = this.app.lib._
|
|
8
|
+
const all = await this.findAttachment(id, opts)
|
|
9
|
+
if (field === 'null') field = null
|
|
10
|
+
const data = find(all, { field, file })
|
|
11
|
+
if (!data) throw this.error('notFound', { statusCode: 404 })
|
|
12
|
+
return data
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default getAttachment
|
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import { getFilterAndOptions, execHook, execModelHook, execDynHook, getRefs } from './helper.js'
|
|
2
|
-
const action = 'getRecord'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Gets a record from the model's underlying data store based on the provided ID.
|
|
6
|
-
*
|
|
7
|
-
* If no arguments are provided, it automatically turns into a chainable {@link DoboAction} object.
|
|
8
|
-
* @async
|
|
9
|
-
* @memberof DoboModel
|
|
10
|
-
* @method
|
|
11
|
-
* @param {string|number} id - The ID of the record to be retrieved.
|
|
12
|
-
* @param {DoboModel.TOptions} [opts] - Options object.
|
|
13
|
-
* @returns {DoboAction|DoboModel.TResultGetRecord|DoboModel.TRecord}
|
|
14
|
-
* @see {@link module:Hook.beforeGetRecord}
|
|
15
|
-
* @see {@link module:Hook.afterGetRecord}
|
|
16
|
-
*/
|
|
17
|
-
async function getRecord (...args) {
|
|
18
|
-
if (args.length === 0) return this.action(action, ...args)
|
|
19
|
-
let [id, opts = {}] = args
|
|
20
|
-
const { getDefaultValues } = this.app.dobo
|
|
21
|
-
const { isEmpty } = this.app.lib._
|
|
22
|
-
const { isSet } = this.app.lib.aneka
|
|
23
|
-
const { get, set } = this.app.bajoCache ?? {}
|
|
24
|
-
opts.dataOnly = opts.dataOnly ?? true
|
|
25
|
-
const { dataOnly } = opts
|
|
26
|
-
const { options } = await getFilterAndOptions.call(this, null, opts, action)
|
|
27
|
-
const { noResultSanitizer } = options
|
|
28
|
-
id = this.sanitizeId(id)
|
|
29
|
-
await execHook.call(this, 'beforeGetRecord', id, options)
|
|
30
|
-
await execModelHook.call(this, 'beforeGetRecord', id, options)
|
|
31
|
-
await execDynHook.call(this, 'beforeGetRecord', id, options)
|
|
32
|
-
if (get) {
|
|
33
|
-
const resp = await get({ model: this, action, id, options })
|
|
34
|
-
if (resp) {
|
|
35
|
-
resp.cached = true
|
|
36
|
-
return dataOnly ? resp.data : resp
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
const result = options.record ?? (await this.adapter._getRecord(this, id, options)) ?? {}
|
|
40
|
-
const { warnings } = getDefaultValues(options)
|
|
41
|
-
if (!warnings) delete result.warnings
|
|
42
|
-
if (isEmpty(result.data) && !options.throwNotFound) return dataOnly ? undefined : { data: undefined }
|
|
43
|
-
if (isSet(options.refs)) await getRefs.call(this, [result.data], options)
|
|
44
|
-
if (!noResultSanitizer) result.data = await this.sanitizeRecord(result.data, options)
|
|
45
|
-
await execDynHook.call(this, 'afterGetRecord', id, result, options)
|
|
46
|
-
await execModelHook.call(this, 'afterGetRecord', id, result, options)
|
|
47
|
-
await execHook.call(this, 'afterGetRecord', id, result, options)
|
|
48
|
-
if (set) await set({ model: this, action, id, options, result })
|
|
49
|
-
return dataOnly ? result.data : result
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export default getRecord
|
|
1
|
+
import { getFilterAndOptions, execHook, execModelHook, execDynHook, getRefs } from './helper.js'
|
|
2
|
+
const action = 'getRecord'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Gets a record from the model's underlying data store based on the provided ID.
|
|
6
|
+
*
|
|
7
|
+
* If no arguments are provided, it automatically turns into a chainable {@link DoboAction} object.
|
|
8
|
+
* @async
|
|
9
|
+
* @memberof DoboModel
|
|
10
|
+
* @method
|
|
11
|
+
* @param {string|number} id - The ID of the record to be retrieved.
|
|
12
|
+
* @param {DoboModel.TOptions} [opts] - Options object.
|
|
13
|
+
* @returns {DoboAction|DoboModel.TResultGetRecord|DoboModel.TRecord}
|
|
14
|
+
* @see {@link module:Hook.beforeGetRecord}
|
|
15
|
+
* @see {@link module:Hook.afterGetRecord}
|
|
16
|
+
*/
|
|
17
|
+
async function getRecord (...args) {
|
|
18
|
+
if (args.length === 0) return this.action(action, ...args)
|
|
19
|
+
let [id, opts = {}] = args
|
|
20
|
+
const { getDefaultValues } = this.app.dobo
|
|
21
|
+
const { isEmpty } = this.app.lib._
|
|
22
|
+
const { isSet } = this.app.lib.aneka
|
|
23
|
+
const { get, set } = this.app.bajoCache ?? {}
|
|
24
|
+
opts.dataOnly = opts.dataOnly ?? true
|
|
25
|
+
const { dataOnly } = opts
|
|
26
|
+
const { options } = await getFilterAndOptions.call(this, null, opts, action)
|
|
27
|
+
const { noResultSanitizer } = options
|
|
28
|
+
id = this.sanitizeId(id)
|
|
29
|
+
await execHook.call(this, 'beforeGetRecord', id, options)
|
|
30
|
+
await execModelHook.call(this, 'beforeGetRecord', id, options)
|
|
31
|
+
await execDynHook.call(this, 'beforeGetRecord', id, options)
|
|
32
|
+
if (get) {
|
|
33
|
+
const resp = await get({ model: this, action, id, options })
|
|
34
|
+
if (resp) {
|
|
35
|
+
resp.cached = true
|
|
36
|
+
return dataOnly ? resp.data : resp
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const result = options.record ?? (await this.adapter._getRecord(this, id, options)) ?? {}
|
|
40
|
+
const { warnings } = getDefaultValues(options)
|
|
41
|
+
if (!warnings) delete result.warnings
|
|
42
|
+
if (isEmpty(result.data) && !options.throwNotFound) return dataOnly ? undefined : { data: undefined }
|
|
43
|
+
if (isSet(options.refs)) await getRefs.call(this, [result.data], options)
|
|
44
|
+
if (!noResultSanitizer) result.data = await this.sanitizeRecord(result.data, options)
|
|
45
|
+
await execDynHook.call(this, 'afterGetRecord', id, result, options)
|
|
46
|
+
await execModelHook.call(this, 'afterGetRecord', id, result, options)
|
|
47
|
+
await execHook.call(this, 'afterGetRecord', id, result, options)
|
|
48
|
+
if (set) await set({ model: this, action, id, options, result })
|
|
49
|
+
return dataOnly ? result.data : result
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default getRecord
|