Skip to content

Feature/wip filtering relationship #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 99 additions & 5 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ function attachQueries (request) {
request.options = {}

// Iterate over dynamic query strings.
for (const parameter of Object.keys(query)) {
for (const parameter of Object.keys(query))
// Attach fields option.
if (parameter.match(isField)) {
const sparseField = Array.isArray(query[parameter]) ?
Expand All @@ -330,10 +330,28 @@ function attachQueries (request) {
const field = inflect(matches[1])
const filterType = matches[2]

if (!(field in fields)) throw new BadRequestError(
`The field "${field}" is non-existent.`)
if (isRelationFilter(field)) {
const relationPath = field
if (filterType !== 'fuzzy-match')
throw new BadRequestError(
`Filtering relationship only
supported on fuzzy-match for now
`)

const isValidPath =
isValidRelationPath( recordTypes,
fields,
getRelationFilterSegments(field) )
if (! isValidPath )
throw new BadRequestError(`Path ${relationPath} is not valid`)
}

else if (!(field in fields))
throw new BadRequestError(`The field "${field}" is non-existent.`)

const fieldType = fields[field][keys.type]
const filterSegments = getRelationFilterSegments(field)
const fieldType = getLastTypeInPath( recordTypes,
fields, filterSegments )[keys.type]

if (filterType === void 0) {
if (!('match' in request.options)) request.options.match = {}
Expand All @@ -354,10 +372,31 @@ function attachQueries (request) {
request.options.range[field][index] =
castValue(query[parameter], fieldType, options)
}
else if (filterType === 'fuzzy-match') {
const lastTypeInPath =
getLastTypeInPath( recordTypes,
fields,
getRelationFilterSegments(field) )
if ( ! lastTypeInPath[keys.type] )
throw new BadRequestError(
`fuzzy-match only allowed on attributes. For ${field}` )


if ( lastTypeInPath[keys.type].name !== 'String')
throw new BadRequestError(
`fuzzy-match only allowed on String types.
${field} is of type ${lastTypeInPath[keys.type].name}
` )


if (!('fuzzyMatch' in request.options))
request.options['fuzzyMatch'] = {}
request.options.fuzzyMatch[field] = query[parameter]
}
else throw new BadRequestError(
`The filter "${filterType}" is not valid.`)
}
}


// Attach include option.
if (reservedKeys.include in query) {
Expand Down Expand Up @@ -516,3 +555,58 @@ function setInflectType (inflect, types) {

return out
}

function isValidRelationPath ( recordTypes,
fieldsCurrentType,
remainingPathSegments ) {
if ( !remainingPathSegments.length )
return false

const pathSegment = remainingPathSegments[0]

if ( !fieldsCurrentType[pathSegment] )
return false

if ( fieldsCurrentType[pathSegment].type
&& remainingPathSegments.length === 1 )
return true


const nextTypeToCompare = fieldsCurrentType[pathSegment].link
if ( nextTypeToCompare )
return isValidRelationPath( recordTypes,
recordTypes[nextTypeToCompare],
remainingPathSegments.slice(1) )

return false
}

function getLastTypeInPath ( recordTypes,
fieldsCurrentType,
remainingPathSegments ) {
if ( !remainingPathSegments.length )
return fieldsCurrentType


const pathSegment = remainingPathSegments[0]

if ( !fieldsCurrentType[pathSegment] )
return {}


const nextType = fieldsCurrentType[pathSegment].link
if ( nextType )
return getLastTypeInPath( recordTypes,
recordTypes[nextType],
remainingPathSegments.slice(1) )

return fieldsCurrentType[pathSegment]
}

function isRelationFilter ( field ) {
return field.split(':').length > 1
}

function getRelationFilterSegments ( field ) {
return field.split(':')
}
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"chalk": "^3.0.0",
"eslint": "^6.8.0",
"eslint-config-boss": "^1.0.6",
"fortune": "^5.5.17",
"fortune": "https://github.com/cecemel/fortune.git#64a6f63874b369fc2738c2f0a81e8fe8abd691fa",
"fortune-http": "^1.2.26",
"tapdance": "^5.1.1"
},
Expand Down
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,21 @@ run((assert, comment) => {
})
})

run((assert, comment) => {
comment(`filter fuzzy-match: Jane and John have
a common friend called something like "soft".`)
return test(`/users?${qs.stringify({
'filter[friends:name][fuzzy-match]': 'soft'
})}`, null, response => {
assert(validate(response.body), 'response adheres to json api')
assert(response.status === 200, 'status is correct')
assert(~response.body.links.self.indexOf('/users'), 'link is correct')
assert(deepEqual(
response.body.data.map(record => record.attributes.name).sort(),
[ 'Jane Doe', 'John Doe' ]), 'match is correct')
})
})

run((assert, comment) => {
comment('dasherizes the camel cased fields')
return test('/users/1', null, response => {
Expand Down