feat(xo.getAllObjects): accepts filter and limit params (#604)
This commit is contained in:
parent
22772a5fac
commit
1b76ad4252
@ -28,13 +28,18 @@ exportConfig.permission = 'admin'
|
|||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
export function getAllObjects () {
|
export function getAllObjects ({ filter, limit }) {
|
||||||
return this.getObjects()
|
return this.getObjects({ filter, limit })
|
||||||
}
|
}
|
||||||
|
|
||||||
getAllObjects.permission = ''
|
getAllObjects.permission = ''
|
||||||
getAllObjects.description = 'Returns all XO objects'
|
getAllObjects.description = 'Returns all XO objects'
|
||||||
|
|
||||||
|
getAllObjects.params = {
|
||||||
|
filter: { type: 'object', optional: true },
|
||||||
|
limit: { type: 'number', optional: true }
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
export async function importConfig () {
|
export async function importConfig () {
|
||||||
|
@ -210,7 +210,7 @@ export default class Api {
|
|||||||
return remove
|
return remove
|
||||||
}
|
}
|
||||||
|
|
||||||
async callApiMethod (session, name, params) {
|
async callApiMethod (session, name, params = {}) {
|
||||||
const startTime = Date.now()
|
const startTime = Date.now()
|
||||||
|
|
||||||
const method = this._methods[name]
|
const method = this._methods[name]
|
||||||
|
44
src/xo.js
44
src/xo.js
@ -1,9 +1,18 @@
|
|||||||
import includes from 'lodash/includes'
|
|
||||||
import XoCollection from 'xo-collection'
|
import XoCollection from 'xo-collection'
|
||||||
import XoUniqueIndex from 'xo-collection/unique-index'
|
import XoUniqueIndex from 'xo-collection/unique-index'
|
||||||
import {createClient as createRedisClient} from 'redis'
|
import {createClient as createRedisClient} from 'redis'
|
||||||
import {EventEmitter} from 'events'
|
import {EventEmitter} from 'events'
|
||||||
import { noSuchObject } from 'xo-common/api-errors'
|
import { noSuchObject } from 'xo-common/api-errors'
|
||||||
|
import {
|
||||||
|
forEach,
|
||||||
|
includes,
|
||||||
|
isEmpty,
|
||||||
|
isFunction,
|
||||||
|
isString,
|
||||||
|
iteratee,
|
||||||
|
map as mapToArray,
|
||||||
|
stubTrue
|
||||||
|
} from 'lodash'
|
||||||
|
|
||||||
import mixins from './xo-mixins'
|
import mixins from './xo-mixins'
|
||||||
import Connection from './connection'
|
import Connection from './connection'
|
||||||
@ -13,12 +22,7 @@ import {
|
|||||||
} from './decorators'
|
} from './decorators'
|
||||||
import {
|
import {
|
||||||
createRawObject,
|
createRawObject,
|
||||||
forEach,
|
|
||||||
generateToken,
|
generateToken,
|
||||||
isEmpty,
|
|
||||||
isFunction,
|
|
||||||
isString,
|
|
||||||
mapToArray,
|
|
||||||
noop
|
noop
|
||||||
} from './utils'
|
} from './utils'
|
||||||
|
|
||||||
@ -160,8 +164,32 @@ export default class Xo extends EventEmitter {
|
|||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
|
|
||||||
getObjects () {
|
getObjects ({ filter, limit } = {}) {
|
||||||
return this._objects.all
|
const { all } = this._objects
|
||||||
|
|
||||||
|
if (filter === undefined) {
|
||||||
|
if (limit === undefined || limit === Infinity) {
|
||||||
|
return all
|
||||||
|
}
|
||||||
|
filter = stubTrue
|
||||||
|
} else {
|
||||||
|
filter = iteratee(filter)
|
||||||
|
if (limit === undefined) {
|
||||||
|
limit = Infinity
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const results = createRawObject(null)
|
||||||
|
for (const id in all) {
|
||||||
|
const object = all[id]
|
||||||
|
if (filter(object, id, all)) {
|
||||||
|
if (limit-- <= 0) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
results[id] = object
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------
|
// -----------------------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user