Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit 5bcaf43

Browse files
author
Dekel Barzilay
committed
- Fixed support and docs for JSON specific query operators
- Bumped version to 4.4.2
1 parent fbef040 commit 5bcaf43

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ Note that all this eager related options are optional.
198198

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

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

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

205205
#### Params Operators
206206

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "feathers-objection",
33
"description": "A service plugin for ObjectionJS an ORM based on KnexJS",
4-
"version": "4.4.1",
4+
"version": "4.4.2",
55
"homepage": "https://github.com/feathersjs-ecosystem/feathers-objection",
66
"keywords": [
77
"feathers",

src/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ const OPERATORS_MAP = {
5151
$all: '?&'
5252
};
5353

54-
const RANGE_OPERATORS = [
54+
const DESERIALIZED_ARRAY_OPERATORS = [
5555
'between',
5656
'not between',
5757
'?|',
5858
'?&'
5959
];
6060

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

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

220222
return query.where(
221-
JSON_OPERATORS.includes(operator) ? refColumn : refColumn.castText(),
223+
NON_COMPARISON_OPERATORS.includes(operator) ? refColumn : refColumn.castText(),
222224
operator,
223225
value
224226
);
225227
}
226228
}
227229

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

test/index.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ describe('Feathers Objection Service', () => {
12631263
{
12641264
name: 'Apple',
12651265
jsonbObject: { z: 0 },
1266-
jsonbArray: [0]
1266+
jsonbArray: ['a', 'b']
12671267
}
12681268
]);
12691269
});
@@ -1284,12 +1284,24 @@ describe('Feathers Objection Service', () => {
12841284
});
12851285
});
12861286

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

1293+
it('$contains array of numbers', () => {
1294+
return companies.find({ query: { jsonbArray: { $contains: [2, 1] } } }).then(data => {
1295+
expect(data[0].name).to.be.equal('Google');
1296+
});
1297+
});
1298+
1299+
it('$contains array of string', () => {
1300+
return companies.find({ query: { jsonbArray: { $contains: ['b', 'a'] } } }).then(data => {
1301+
expect(data[0].name).to.be.equal('Apple');
1302+
});
1303+
});
1304+
12931305
it('$contains - nested', () => {
12941306
return companies.find({ query: { jsonbObject: { e: { $contains: 4 } } } }).then(data => {
12951307
expect(data[0].name).to.be.equal('Google');

0 commit comments

Comments
 (0)