Skip to content

Commit

Permalink
Include query key when building key in parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
jstayton committed Aug 29, 2019
1 parent 245ae28 commit c405b57
Show file tree
Hide file tree
Showing 18 changed files with 222 additions and 111 deletions.
5 changes: 2 additions & 3 deletions src/orchestrators/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ class BaseOrchestrator {
}

apply(data, key = null) {
const querierMethod = key && `${this.queryKey}:${key}`
const args = [this.querier.builder, data]

this.querier.builder =
querierMethod && is.fn(this.querier[querierMethod])
? this.querier[querierMethod](...args)
key && is.fn(this.querier[key])
? this.querier[key](...args)
: this.querier.adapter[this.queryKey](...args)

return this.querier.builder
Expand Down
12 changes: 5 additions & 7 deletions src/orchestrators/filterer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Filterer extends BaseOrchestrator {
return filters.reduce(
(accumulator, [key, filter]) => ({
...accumulator,
[`${this.queryKey}:${key}`]: filter.value,
[key]: filter.value,
}),
{}
)
Expand All @@ -50,10 +50,7 @@ class Filterer extends BaseOrchestrator {
if (!this._validate) {
this._validate =
this.parser.validate() &&
this.querier.adapter.validator.validateFilters(
this.parse(),
this.queryKey
) &&
this.querier.adapter.validator.validateFilters(this.parse()) &&
this.querier.validator.validate(this.parseFlat())
}

Expand All @@ -69,10 +66,11 @@ class Filterer extends BaseOrchestrator {
return this.querier
}

const keys = this.schema.keys()
let key
let filter

for (const key of keys) {
for (const filterSchema of this.schema.values()) {
key = this.parser.buildKey(filterSchema)
filter = filters.get(key)

if (filter) {
Expand Down
17 changes: 8 additions & 9 deletions src/orchestrators/pager.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ class Pager extends BaseOrchestrator {
)
}

parseFlat() {
parseFlat(includeQueryKey = true) {
if (!this.isEnabled) {
return {}
}

return Object.entries(this.parse()).reduce(
(accumulator, [field, value]) => ({
const page = Array.from(this.parse().values())

return page.reduce(
(accumulator, pageField) => ({
...accumulator,
[`${this.queryKey}:${field}`]: value,
[this.parser.buildKey(pageField, includeQueryKey)]: pageField.value,
}),
{}
)
Expand All @@ -45,10 +47,7 @@ class Pager extends BaseOrchestrator {
if (!this._validate) {
this._validate =
this.parser.validate() &&
this.querier.adapter.validator.validatePage(
this.parse(),
this.queryKey
) &&
this.querier.adapter.validator.validatePage(this.parse()) &&
this.querier.validator.validate(this.parseFlat())
}

Expand All @@ -61,7 +60,7 @@ class Pager extends BaseOrchestrator {
const page = this.parse()

if (page) {
this.apply(page)
this.apply(this.parseFlat(false))
}

return this.querier
Expand Down
7 changes: 2 additions & 5 deletions src/orchestrators/sorter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Sorter extends BaseOrchestrator {
return sorts.reduce(
(accumulator, [key, sort]) => ({
...accumulator,
[`${this.queryKey}:${key}`]: sort.order,
[key]: sort.order,
}),
{}
)
Expand All @@ -47,10 +47,7 @@ class Sorter extends BaseOrchestrator {
if (!this._validate) {
this._validate =
this.parser.validate() &&
this.querier.adapter.validator.validateSorts(
this.parse(),
this.queryKey
) &&
this.querier.adapter.validator.validateSorts(this.parse()) &&
this.querier.validator.validate(this.parseFlat())
}

Expand Down
4 changes: 4 additions & 0 deletions src/parsers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class BaseParser {
this._validate = null
}

buildKey(/* parsed */) {
throw new NotImplementedError()
}

parse() {
throw new NotImplementedError()
}
Expand Down
8 changes: 3 additions & 5 deletions src/parsers/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class FilterParser extends BaseParser {
}
}

static buildKey(filter) {
return `${filter.field}[${filter.operator}]`
buildKey({ field, operator }) {
return `${this.queryKey}:${field}[${operator}]`
}

defineValidation(schema) {
Expand Down Expand Up @@ -79,9 +79,7 @@ class FilterParser extends BaseParser {
}
}

return new Map(
filters.map(filter => [this.constructor.buildKey(filter), filter])
)
return new Map(filters.map(filter => [this.buildKey(filter), filter]))
}
}

Expand Down
17 changes: 16 additions & 1 deletion src/parsers/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ class PageParser extends BaseParser {
}
}

buildKey({ field }, includeQueryKey = true) {
let key = field

if (includeQueryKey) {
key = `${this.queryKey}:${key}`
}

return key
}

defineValidation(schema) {
return schema.alternatives().try([
schema
Expand Down Expand Up @@ -58,7 +68,12 @@ class PageParser extends BaseParser {

page.offset = (page.number - 1) * page.size

return page
return new Map(
Object.entries(page).map(([field, value]) => [
this.buildKey({ field }),
{ field, value },
])
)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/parsers/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class SortParser extends BaseParser {
}
}

static buildKey(sort) {
return sort.field
buildKey({ field }) {
return `${this.queryKey}:${field}`
}

defineValidation(schema) {
Expand Down Expand Up @@ -72,7 +72,7 @@ class SortParser extends BaseParser {
sorts.push(...this.parseObject(this.query))
}

return new Map(sorts.map(sort => [this.constructor.buildKey(sort), sort]))
return new Map(sorts.map(sort => [this.buildKey(sort), sort]))
}
}

Expand Down
20 changes: 7 additions & 13 deletions src/validators/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,39 @@ class AdapterValidator {
return true
}

validateFilters(filters, queryKey) {
validateFilters(filters) {
if (!this.schema) {
return true
}

for (const [key, filter] of filters) {
this.validateValue(
`filter:${filter.operator}`,
`${queryKey}:${key}`,
filter.value
)
this.validateValue(`filter:${filter.operator}`, key, filter.value)
}

return true
}

validateSorts(sorts, queryKey) {
validateSorts(sorts) {
const schemaKey = 'sort'

if (!this.schema || !this.schema[schemaKey]) {
return true
}

for (const [key, sort] of sorts) {
this.validateValue(schemaKey, `${queryKey}:${key}`, sort.order)
this.validateValue(schemaKey, key, sort.order)
}

return true
}

validatePage(page, queryKey) {
validatePage(page) {
if (!this.schema) {
return true
}

const entries = Object.entries(page)

for (const [field, value] of entries) {
this.validateValue(`page:${field}`, `${queryKey}:${field}`, value)
for (const [key, pageField] of page) {
this.validateValue(`page:${pageField.field}`, key, pageField.value)
}

return true
Expand Down
2 changes: 1 addition & 1 deletion test/src/orchestrators/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('apply', () => {
jest.spyOn(orchestrator, 'queryKey', 'get').mockReturnValue('sort')
querier['sort:test'] = jest.fn(builder => builder)

expect(orchestrator.apply(data, 'test')).toBe(querier.builder)
expect(orchestrator.apply(data, 'sort:test')).toBe(querier.builder)
expect(querier['sort:test']).toHaveBeenCalledWith(querier.builder, data)
})

Expand Down
8 changes: 4 additions & 4 deletions test/src/orchestrators/filterer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('parse', () => {
)
)

expect(filterer.parse().has('test[=]')).toBe(true)
expect(filterer.parse().has('filter:test[=]')).toBe(true)
})

test('calls/uses `querier.defaultFilter` if no query', () => {
Expand All @@ -86,7 +86,7 @@ describe('parse', () => {

expect(filterer.query).toBeFalsy()
expect(defaultFilter).toHaveBeenCalled()
expect(parsed.has('test[=]')).toBe(true)
expect(parsed.has('filter:test[=]')).toBe(true)

defaultFilter.mockRestore()
})
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('run', () => {
operator: '=',
value: 123,
},
'test[=]'
'filter:test[=]'
)

expect(filterer.apply).toHaveBeenNthCalledWith(
Expand All @@ -165,7 +165,7 @@ describe('run', () => {
operator: '!=',
value: 456,
},
'testing[!=]'
'filter:testing[!=]'
)
})

Expand Down
21 changes: 19 additions & 2 deletions test/src/orchestrators/pager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ describe('parseFlat', () => {
})
})

test('returns object keys without the query key, if specified', () => {
const pager = new Pager(
new TestQuerier(
{
page: 2,
},
knex('test')
)
)

expect(pager.parseFlat(false)).toEqual({
size: 20,
number: 2,
offset: 20,
})
})

test('returns empty object if pagination is disabled', () => {
const pager = new Pager(new TestQuerier({}, knex('true')))

Expand All @@ -73,7 +90,7 @@ describe('parse', () => {
)
)

expect(pager.parse().number).toBe(2)
expect(pager.parse().get('page:number').value).toBe(2)
})

test('calls/uses `querier.defaultPage` if no query', () => {
Expand All @@ -88,7 +105,7 @@ describe('parse', () => {

expect(pager.query).toBeFalsy()
expect(defaultPage).toHaveBeenCalled()
expect(parsed.number).toBe(2)
expect(parsed.get('page:number').value).toBe(2)

defaultPage.mockRestore()
})
Expand Down
8 changes: 4 additions & 4 deletions test/src/orchestrators/sorter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('parse', () => {
)
)

expect(sorter.parse().has('test')).toBe(true)
expect(sorter.parse().has('sort:test')).toBe(true)
})

test('calls/uses `querier.defaultSort` if no query', () => {
Expand All @@ -86,7 +86,7 @@ describe('parse', () => {

expect(sorter.query).toBeFalsy()
expect(defaultSort).toHaveBeenCalled()
expect(parsed.has('test')).toBe(true)
expect(parsed.has('sort:test')).toBe(true)

defaultSort.mockRestore()
})
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('run', () => {
field: 'testing',
order: 'asc',
},
'testing'
'sort:testing'
)

expect(sorter.apply).toHaveBeenNthCalledWith(
Expand All @@ -156,7 +156,7 @@ describe('run', () => {
field: 'test',
order: 'asc',
},
'test'
'sort:test'
)
})

Expand Down
8 changes: 8 additions & 0 deletions test/src/parsers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe('constructor', () => {
})
})

describe('buildKey', () => {
test('throws `NotImplementedError` when not extended', () => {
const parser = new BaseParser('test', {}, new Schema())

expect(() => parser.buildKey()).toThrow(NotImplementedError)
})
})

describe('parse', () => {
test('throws `NotImplementedError` when not extended', () => {
const parser = new BaseParser('test', {}, new Schema())
Expand Down
Loading

0 comments on commit c405b57

Please sign in to comment.