diff --git a/packages/memory/src/index.ts b/packages/memory/src/index.ts index a4a3b10d1a..87f33dbae4 100644 --- a/packages/memory/src/index.ts +++ b/packages/memory/src/index.ts @@ -75,26 +75,49 @@ export class MemoryAdapter, P extends Params = Params> e const { paginate } = this.getOptions(params) const { query, filters } = this.getQuery(params) - let values = _.values(this.store).filter(this.options.matcher(query)) + let values = _.values(this.store) const total = values.length + const hasSkip = filters.$skip !== undefined + const hasSort = filters.$sort !== undefined + const hasLimit = filters.$limit !== undefined + const hasQuery = _.keys(query).length > 0 - if (filters.$sort !== undefined) { + if (hasSort) { values.sort(this.options.sorter(filters.$sort)) } - if (filters.$skip !== undefined) { - values = values.slice(filters.$skip) - } + if (hasQuery || hasLimit || hasSkip) { + let skipped = 0 + const matcher = this.options.matcher(query) + const matched = [] + + for (let index = 0, length = values.length; index < length; index++) { + const value = values[index] + + if (hasQuery && !matcher(value, index, values)) { + continue + } + + if (hasSkip && filters.$skip > skipped) { + skipped++ + continue + } + + matched.push(_select(value, params)) + + if (hasLimit && filters.$limit === matched.length) { + break + } + } - if (filters.$limit !== undefined) { - values = values.slice(0, filters.$limit) + values = matched } const result: Paginated = { - total, + total: hasQuery ? values.length : total, limit: filters.$limit, skip: filters.$skip || 0, - data: values.map((value) => _select(value, params)) + data: filters.$limit === 0 ? [] : values } if (!paginate) { diff --git a/packages/memory/test/index.test.ts b/packages/memory/test/index.test.ts index 0084f141b2..4a25cebc51 100644 --- a/packages/memory/test/index.test.ts +++ b/packages/memory/test/index.test.ts @@ -172,7 +172,7 @@ describe('Feathers Memory Service', () => { ) await app.service('matcher').find({ - query: { $sort: { something: 1 } } + query: { something: 1, $sort: { something: 1 } } }) assert.ok(sorterCalled, 'sorter called')