Skip to content

Commit

Permalink
Added support for new query operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekel Barzilay committed Feb 16, 2019
1 parent 5893fd1 commit 761a549
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 30 deletions.
38 changes: 34 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,48 @@ Note that all this eager related options are optional.

#### Query Operators

- **`$eager`** - parameter to eager load relations defined in models'
- **`$eager`** - eager load relations defined in models'
`relationMappings` getter methods or in the `namedEagerFilters` option. See
[`eager`](https://vincit.github.io/objection.js/#eager) documentation.
- **`$joinRelation`** - parameter to filter based on a relation's field. See
- **`$joinRelation`** - filter based on a relation's field. See
[`joinRelation`](https://vincit.github.io/objection.js/#joinrelation)
documentation.
- **`$joinEager`** - parameter to filter based on a relation's field using
- **`$joinEager`** - filter based on a relation's field using
`JoinEagerAlgorithm`. See
[`$joinEager`](https://vincit.github.io/objection.js/#joineager)
documentation.
- **`$pick`** - parameter to pick properties from result models. See
- **`$pick`** - pick properties from result models. See
[`pick`](https://vincit.github.io/objection.js/#pick) documentation.

- **`$between`** - filter based on if a column value is between range of values

- **`$notBetween`** - filter based on if a column value is not between range of values

- **`$like`** - filter column value based on a LIKE pattern

- **`$notLike`** - filter column value based on a NOT LIKE pattern

- **`$regexp`** - filter column value based on a REGEXP pattern

- **`$notRegexp`** - filter column value based on a NOT REGEXP pattern

- **`$ilike`** - (Postgres) filter column value based on a case-insensitive LIKE pattern

- **`$notILike`** - (Postgres) filter column value based on a case-insensitive NOT LIKE pattern

- **`$iRegexp`** - (Postgres) filter column value based on a case-insensitive REGEXP pattern

- **`$notIRegexp`** - (Postgres) filter column value based on a case-insensitive NOT REGEXP pattern

- **`$containsKey`** (Postgres) - filter based on if a column contains a key

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

- **`$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

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

#### Params Operators

Expand Down
105 changes: 100 additions & 5 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions 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": "3.1.2",
"version": "3.2.0",
"homepage": "https://github.com/feathersjs-ecosystem/feathers-objection",
"keywords": [
"feathers",
Expand Down Expand Up @@ -56,7 +56,8 @@
},
"dependencies": {
"@feathersjs/adapter-commons": "^2.0.0",
"@feathersjs/errors": "^3.3.6"
"@feathersjs/errors": "^3.3.6",
"pg": "^7.8.0"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
Expand Down
42 changes: 39 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,27 @@ const OPERATORS_MAP = {
$like: 'like',
$notLike: 'not like',
$ilike: 'ilike',
$notILike: 'not ilike'
$notILike: 'not ilike',
$regexp: '~',
$notRegexp: '!~',
$iRegexp: '~*',
$notIRegexp: '!~*',
$between: 'between',
$notBetween: 'not between',
$contains: '@>',
$containsKey: '?',
$contained: '<@',
$any: '?|',
$all: '?&'
};

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

/**
* Class representing an feathers adapter for Objection.js ORM.
* @param {object} options
Expand Down Expand Up @@ -134,7 +152,7 @@ class Service extends AdapterService {
if (params.$pick) { delete params.$pick; }

Object.keys(params || {}).forEach(key => {
const value = params[key];
let value = params[key];

if (utils.isPlainObject(value)) {
return this.objectify(query, value, key, parentKey);
Expand Down Expand Up @@ -176,7 +194,25 @@ class Service extends AdapterService {
let columnType = property && property.type;
if (columnType) {
if (Array.isArray(columnType)) { columnType = columnType[0]; }
if (columnType === 'object' || columnType === 'array') { return query.where(ref(`${this.Model.tableName}.${methodKey || column}:${(methodKey ? column : key).replace(/\(/g, '[').replace(/\)/g, ']')}`).castText(), operator, value); }
if (columnType === 'object' || columnType === 'array') {
let refColumn = null;

if (!methodKey && key[0] === '$') {
refColumn = ref(`${this.Model.tableName}.${methodKey || column}`);
} else {
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);
}

return query.where(refColumn, operator, value);
}
}

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

return operator === '=' ? query.where(column, value) : query.where(column, operator, value);
Expand Down
4 changes: 3 additions & 1 deletion test/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export default class Company extends Model {
}
}
},
jsonArray: { type: ['array', 'null'] }
jsonArray: { type: ['array', 'null'] },
jsonbObject: { type: ['object', 'null'] },
jsonbArray: { type: ['array', 'null'] }
}
}

Expand Down
Loading

0 comments on commit 761a549

Please sign in to comment.