Skip to content

Commit

Permalink
feat: Query filters for datastore (#936)
Browse files Browse the repository at this point in the history
* First change - tests still passing

* Enhanced test to test new properties

* linting fixes

* Update comment on operators.

* Added 3 system tests

* Count fix
  • Loading branch information
danieljbruce committed Jun 13, 2022
1 parent faaec0d commit 51725fa
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,9 @@ export namespace entity {
'<': 'LESS_THAN',
'<=': 'LESS_THAN_OR_EQUAL',
HAS_ANCESTOR: 'HAS_ANCESTOR',
'!=': 'NOT_EQUAL',
IN: 'IN',
NOT_IN: 'NOT_IN',
};

const SIGN_TO_ORDER = {
Expand Down
14 changes: 11 additions & 3 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ import {Transaction} from './transaction';
import {CallOptions} from 'google-gax';
import {RunQueryStreamOptions} from '../src/request';

export type Operator = '=' | '<' | '>' | '<=' | '>=' | 'HAS_ANCESTOR';
export type Operator =
| '='
| '<'
| '>'
| '<='
| '>='
| 'HAS_ANCESTOR'
| '!='
| 'IN'
| 'NOT_IN';

export interface OrderOptions {
descending?: boolean;
Expand Down Expand Up @@ -154,8 +163,7 @@ class Query {

/**
* Datastore allows querying on properties. Supported comparison operators
* are `=`, `<`, `>`, `<=`, and `>=`. "Not equal" and `IN` operators are
* currently not supported.
* are `=`, `<`, `>`, `<=`, `>=`, `!=`, `HAS_ANCESTOR`, `IN` and `NOT_IN`.
*
* *To filter by ancestors, see {module:datastore/query#hasAncestor}.*
*
Expand Down
27 changes: 27 additions & 0 deletions system-test/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,33 @@ describe('Datastore', () => {
assert.strictEqual(entities!.length, 6);
});

it('should filter queries with NOT_EQUAL', async () => {
const q = datastore
.createQuery('Character')
.hasAncestor(ancestor)
.filter('appearances', '!=', 9);
const [entities] = await datastore.runQuery(q);
assert.strictEqual(entities!.length, 6);
});

it('should filter queries with IN', async () => {
const q = datastore
.createQuery('Character')
.hasAncestor(ancestor)
.filter('appearances', 'IN', [9, 25]);
const [entities] = await datastore.runQuery(q);
assert.strictEqual(entities!.length, 3);
});

it('should filter queries with NOT_IN', async () => {
const q = datastore
.createQuery('Character')
.hasAncestor(ancestor)
.filter('appearances', 'NOT_IN', [9, 25]);
const [entities] = await datastore.runQuery(q);
assert.strictEqual(entities!.length, 5);
});

it('should filter queries with defined indexes', async () => {
const q = datastore
.createQuery('Character')
Expand Down
17 changes: 16 additions & 1 deletion test/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ describe('Query', () => {
.filter('name', '=', 'Title')
.filter('count', '>', 20)
.filter('size', '<', 10)
.filter('something', '>=', 11);
.filter('something', '>=', 11)
.filter('neProperty', '!=', 12)
.filter('inProperty', 'IN', 13)
.filter('notInProperty', 'NOT_IN', 14);

assert.strictEqual(query.filters[0].name, 'date');
assert.strictEqual(query.filters[0].op, '<=');
Expand All @@ -96,6 +99,18 @@ describe('Query', () => {
assert.strictEqual(query.filters[4].name, 'something');
assert.strictEqual(query.filters[4].op, '>=');
assert.strictEqual(query.filters[4].val, 11);

assert.strictEqual(query.filters[5].name, 'neProperty');
assert.strictEqual(query.filters[5].op, '!=');
assert.strictEqual(query.filters[5].val, 12);

assert.strictEqual(query.filters[6].name, 'inProperty');
assert.strictEqual(query.filters[6].op, 'IN');
assert.strictEqual(query.filters[6].val, 13);

assert.strictEqual(query.filters[7].name, 'notInProperty');
assert.strictEqual(query.filters[7].op, 'NOT_IN');
assert.strictEqual(query.filters[7].val, 14);
});

it('should remove any whitespace surrounding the filter name', () => {
Expand Down

0 comments on commit 51725fa

Please sign in to comment.