Skip to content

Commit

Permalink
add $contains, $contained_by, and $overlap operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg committed Jan 11, 2023
1 parent 17ee6da commit 191f4bf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,66 @@ Through the REST API:
/messages?text[$ilike]=hello%
```

### $contains

For PostgreSQL only, for array-type fields, finds records that contain _all_ of the given values. The following query retrieves all messages whose labels contain all of the values `important`, `work`, or `urgent` :

```js
app.service('messages').find({
query: {
labels: {
$contains: ['important', 'work', 'urgent']
}
}
});
```

Through the REST API:

```
/messages?label[$contains][0]=important&label[$contains][1]=work&label[$contains][2]=urgent
```

### $contained_by

For PostgreSQL only, for array-type fields, finds records that are contained by the given list of values, i.e do not contain values other than those given. The following query retrieves all messages whose labels contain any of the values `important`, `work`, or `urgent`, but no values outside that list :

```js
app.service('messages').find({
query: {
labels: {
$contained_by: ['important', 'work', 'urgent']
}
}
});
```

Through the REST API:

```
/messages?label[$contained_by][0]=important&label[$contained_by][1]=work&label[$contained_by][2]=urgent
```

### $overlap

For PostgreSQL only, for array-type fields, finds records that overlap (have points in common) with the given values. The following query retrieves all messages whose labels contain one or more of the values `important`, `work`, or `urgent` :

```js
app.service('messages').find({
query: {
labels: {
$overlap: ['important', 'work', 'urgent']
}
}
});
```

Through the REST API:

```
/messages?label[$overlap][0]=important&label[$overlap][1]=work&label[$overlap][2]=urgent
```


## Transaction Support

Expand Down
9 changes: 6 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const OPERATORS = {
$gte: '>=',
$like: 'like',
$notlike: 'not like',
$ilike: 'ilike'
$ilike: 'ilike',
$overlap: '&&',
$contains: '@>',
$contained_by: '<@'
};

// Create the service.
Expand All @@ -42,8 +45,8 @@ class Service extends AdapterService {
super(Object.assign({
id: 'id'
}, options, {
whitelist: whitelist.concat(['$like', '$notlike', '$ilike', '$and'])
}));
whitelist: whitelist.concat(['$like', '$notlike', '$ilike', '$and', '$overlap', '$contains', '$contained_by'])
}));

this.table = options.name;
this.schema = options.schema;
Expand Down

0 comments on commit 191f4bf

Please sign in to comment.