-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add filter on array and jsonb field types #7839
Changes from all commits
2879c0d
a7c8588
fe13470
0eb2fe1
8c9b769
da1d23f
6288888
f901804
585789e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,5 @@ export type FilterableFieldType = PickLiteral< | |
| 'MULTI_SELECT' | ||
| 'ACTOR' | ||
| 'ARRAY' | ||
| 'RAW_JSON' | ||
>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { | ||
ActorFilter, | ||
AddressFilter, | ||
ArrayFilter, | ||
CurrencyFilter, | ||
DateFilter, | ||
EmailsFilter, | ||
FloatFilter, | ||
RawJsonFilter, | ||
RecordGqlOperationFilter, | ||
RelationFilter, | ||
StringFilter, | ||
|
@@ -290,6 +292,24 @@ export const applyEmptyFilters = ( | |
], | ||
}; | ||
break; | ||
case 'ARRAY': | ||
emptyRecordFilter = { | ||
or: [ | ||
{ | ||
[correspondingField.name]: { is: 'NULL' } as ArrayFilter, | ||
}, | ||
], | ||
}; | ||
break; | ||
case 'RAW_JSON': | ||
emptyRecordFilter = { | ||
or: [ | ||
{ | ||
[correspondingField.name]: { is: 'NULL' } as RawJsonFilter, | ||
}, | ||
], | ||
}; | ||
break; | ||
Comment on lines
+304
to
+312
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: The RAW_JSON filter only checks for NULL. Consider adding a check for empty object {} |
||
case 'EMAILS': | ||
emptyRecordFilter = { | ||
or: [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ArrayFilter } from '@/object-record/graphql/types/RecordGqlOperationFilter'; | ||
|
||
export const isMatchingArrayFilter = ({ | ||
arrayFilter, | ||
value, | ||
}: { | ||
arrayFilter: ArrayFilter; | ||
value: string[]; | ||
}) => { | ||
if (value === null || !Array.isArray(value)) { | ||
return false; | ||
} | ||
|
||
switch (true) { | ||
case arrayFilter.contains !== undefined: { | ||
return arrayFilter.contains.every((item) => value.includes(item)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: This implementation assumes |
||
} | ||
case arrayFilter.not_contains !== undefined: { | ||
return !arrayFilter.not_contains.some((item) => value.includes(item)); | ||
} | ||
case arrayFilter.is !== undefined: { | ||
if (arrayFilter.is === 'NULL') { | ||
return value === null; | ||
} else { | ||
return value !== null; | ||
} | ||
} | ||
default: { | ||
throw new Error( | ||
`Unexpected value for array filter: ${JSON.stringify(arrayFilter)}`, | ||
); | ||
} | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { RawJsonFilter } from '../../graphql/types/RecordGqlOperationFilter'; | ||
|
||
export const isMatchingRawJsonFilter = ({ | ||
rawJsonFilter, | ||
value, | ||
}: { | ||
rawJsonFilter: RawJsonFilter; | ||
value: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: value is typed as string, but could be null |
||
}) => { | ||
switch (true) { | ||
case rawJsonFilter.like !== undefined: { | ||
const regexPattern = rawJsonFilter.like.replace(/%/g, '.*'); | ||
const regexCaseInsensitive = new RegExp(`^${regexPattern}$`, 'i'); | ||
|
||
const stringValue = JSON.stringify(value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: JSON.stringify may fail if value is not JSON-serializable |
||
|
||
return regexCaseInsensitive.test(stringValue); | ||
} | ||
case rawJsonFilter.is !== undefined: { | ||
if (rawJsonFilter.is === 'NULL') { | ||
return value === null; | ||
} else { | ||
return value !== null; | ||
} | ||
Comment on lines
+20
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: consider using strict equality (===) for null checks |
||
} | ||
default: { | ||
throw new Error( | ||
`Unexpected value for string filter : ${JSON.stringify(rawJsonFilter)}`, | ||
); | ||
} | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Default case returns an empty array. Consider throwing an error or logging a warning for unexpected filter types