Skip to content

Commit

Permalink
- Fixed support and docs for JSON specific query operators
Browse files Browse the repository at this point in the history
- Bumped version to 4.4.2
  • Loading branch information
Dekel Barzilay committed Oct 11, 2019
1 parent fbef040 commit 5bcaf43
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ Note that all this eager related options are optional.

- **`$all`** (Postgres) - filter based on if a column contains all keys from array of strings

- **`$contains`** (Postgres) - filter based on if a column contains a value
- **`$contains`** (Postgres) - filter based on if a column contains all values from array of values

- **`$contained`** (Postgres) - filter based on if a column is contained in a value
- **`$contained`** (Postgres) - filter based on if a column is contained within a serialized object

#### Params Operators

Expand Down
2 changes: 1 addition & 1 deletion 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
@@ -1,7 +1,7 @@
{
"name": "feathers-objection",
"description": "A service plugin for ObjectionJS an ORM based on KnexJS",
"version": "4.4.1",
"version": "4.4.2",
"homepage": "https://github.com/feathersjs-ecosystem/feathers-objection",
"keywords": [
"feathers",
Expand Down
14 changes: 8 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ const OPERATORS_MAP = {
$all: '?&'
};

const RANGE_OPERATORS = [
const DESERIALIZED_ARRAY_OPERATORS = [
'between',
'not between',
'?|',
'?&'
];

const JSON_OPERATORS = [
const NON_COMPARISON_OPERATORS = [
'@>',
'?',
'<@',
Expand Down Expand Up @@ -213,19 +213,21 @@ class Service extends AdapterService {
refColumn = ref(`${this.Model.tableName}.${methodKey || column}:${(methodKey ? column : key).replace(/\(/g, '[').replace(/\)/g, ']')}`);
}

if (RANGE_OPERATORS.includes(operator) && typeof value === 'string' && value[0] === '[' && value[value.length - 1] === ']') {
value = JSON.parse(value);
if (operator === '@>') {
if (Array.isArray(value)) { value = JSON.stringify(value); }
} else if (DESERIALIZED_ARRAY_OPERATORS.includes(operator)) {
if (typeof value === 'string' && value[0] === '[' && value[value.length - 1] === ']') { value = JSON.parse(value); }
}

return query.where(
JSON_OPERATORS.includes(operator) ? refColumn : refColumn.castText(),
NON_COMPARISON_OPERATORS.includes(operator) ? refColumn : refColumn.castText(),
operator,
value
);
}
}

if (RANGE_OPERATORS.includes(operator) && typeof value === 'string' && value[0] === '[' && value[value.length - 1] === ']') {
if (DESERIALIZED_ARRAY_OPERATORS.includes(operator) && typeof value === 'string' && value[0] === '[' && value[value.length - 1] === ']') {
value = JSON.parse(value);
}

Expand Down
16 changes: 14 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ describe('Feathers Objection Service', () => {
{
name: 'Apple',
jsonbObject: { z: 0 },
jsonbArray: [0]
jsonbArray: ['a', 'b']
}
]);
});
Expand All @@ -1284,12 +1284,24 @@ describe('Feathers Objection Service', () => {
});
});

it('$contains', () => {
it('$contains number', () => {
return companies.find({ query: { jsonbArray: { $contains: 1 } } }).then(data => {
expect(data[0].name).to.be.equal('Google');
});
});

it('$contains array of numbers', () => {
return companies.find({ query: { jsonbArray: { $contains: [2, 1] } } }).then(data => {
expect(data[0].name).to.be.equal('Google');
});
});

it('$contains array of string', () => {
return companies.find({ query: { jsonbArray: { $contains: ['b', 'a'] } } }).then(data => {
expect(data[0].name).to.be.equal('Apple');
});
});

it('$contains - nested', () => {
return companies.find({ query: { jsonbObject: { e: { $contains: 4 } } } }).then(data => {
expect(data[0].name).to.be.equal('Google');
Expand Down

0 comments on commit 5bcaf43

Please sign in to comment.