dobo 1.0.9 → 1.0.11
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,5 +1,6 @@
|
|
|
1
1
|
function buildPageSkipLimit (filter) {
|
|
2
2
|
let limit = parseInt(filter.limit) || this.config.default.filter.limit
|
|
3
|
+
if (limit === -1) limit = this.config.default.filter.maxLimit
|
|
3
4
|
if (limit > this.config.default.filter.maxLimit) limit = this.config.default.filter.maxLimit
|
|
4
5
|
if (limit < 1) limit = 1
|
|
5
6
|
let page = parseInt(filter.page) || 1
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import resolveMethod from '../../../lib/resolve-method.js'
|
|
2
|
+
|
|
3
|
+
async function count (name, filter = {}, opts = {}) {
|
|
4
|
+
const { runHook } = this.app.bajo
|
|
5
|
+
const { get, set } = this.cache ?? {}
|
|
6
|
+
const { cloneDeep, camelCase, omit } = this.app.bajo.lib._
|
|
7
|
+
const options = cloneDeep(omit(opts, ['req']))
|
|
8
|
+
options.req = opts.req
|
|
9
|
+
options.dataOnly = options.dataOnly ?? true
|
|
10
|
+
let { dataOnly, noHook, noCache } = options
|
|
11
|
+
options.dataOnly = false
|
|
12
|
+
await this.modelExists(name, true)
|
|
13
|
+
const { handler, schema, driver } = await resolveMethod.call(this, name, 'record-count', options)
|
|
14
|
+
if (!schema.cacheable) noCache = true
|
|
15
|
+
filter.query = await this.buildQuery({ filter, schema, options }) ?? {}
|
|
16
|
+
if (options.queryHandler) filter.query = await options.queryHandler.call(opts.req ? this.app[opts.req.ns] : this, filter.query, opts.req)
|
|
17
|
+
filter.match = this.buildMatch({ input: filter.match, schema, options }) ?? {}
|
|
18
|
+
if (!noHook) {
|
|
19
|
+
await runHook(`${this.name}:beforeRecordCount`, name, filter, options)
|
|
20
|
+
await runHook(`${this.name}.${camelCase(name)}:beforeRecordCount`, filter, options)
|
|
21
|
+
}
|
|
22
|
+
if (get && !noCache) {
|
|
23
|
+
const cachedResult = await get({ model: name, filter, options })
|
|
24
|
+
if (cachedResult) {
|
|
25
|
+
cachedResult.cached = true
|
|
26
|
+
return dataOnly ? cachedResult.data : cachedResult
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const count = await handler.call(this.app[driver.ns], { schema, filter, options })
|
|
30
|
+
if (!noHook) {
|
|
31
|
+
await runHook(`${this.name}.${camelCase(name)}:afterRecordCount`, filter, options, count)
|
|
32
|
+
await runHook(`${this.name}:afterRecordCount`, name, filter, options, count)
|
|
33
|
+
}
|
|
34
|
+
if (set && !noCache) await set({ model: name, filter, options, count })
|
|
35
|
+
return dataOnly ? count.data : count
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default count
|