Skip to content
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 $contains, $contained_by, and $overlap operators #236

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,85 @@ 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
```

### $fulltext

For PostgreSQL only, for fulltext-indexed fields, finds records that match useing postgres' fulltext natural-langauge search. 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
```

## Transaction Support

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

// Create the service.
Expand All @@ -42,8 +46,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', '$fulltext'])
}));

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