Skip to content

Commit 51725fa

Browse files
authored
feat: Query filters for datastore (#936)
* First change - tests still passing * Enhanced test to test new properties * linting fixes * Update comment on operators. * Added 3 system tests * Count fix
1 parent faaec0d commit 51725fa

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

src/entity.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,9 @@ export namespace entity {
11931193
'<': 'LESS_THAN',
11941194
'<=': 'LESS_THAN_OR_EQUAL',
11951195
HAS_ANCESTOR: 'HAS_ANCESTOR',
1196+
'!=': 'NOT_EQUAL',
1197+
IN: 'IN',
1198+
NOT_IN: 'NOT_IN',
11961199
};
11971200

11981201
const SIGN_TO_ORDER = {

src/query.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ import {Transaction} from './transaction';
2222
import {CallOptions} from 'google-gax';
2323
import {RunQueryStreamOptions} from '../src/request';
2424

25-
export type Operator = '=' | '<' | '>' | '<=' | '>=' | 'HAS_ANCESTOR';
25+
export type Operator =
26+
| '='
27+
| '<'
28+
| '>'
29+
| '<='
30+
| '>='
31+
| 'HAS_ANCESTOR'
32+
| '!='
33+
| 'IN'
34+
| 'NOT_IN';
2635

2736
export interface OrderOptions {
2837
descending?: boolean;
@@ -154,8 +163,7 @@ class Query {
154163

155164
/**
156165
* Datastore allows querying on properties. Supported comparison operators
157-
* are `=`, `<`, `>`, `<=`, and `>=`. "Not equal" and `IN` operators are
158-
* currently not supported.
166+
* are `=`, `<`, `>`, `<=`, `>=`, `!=`, `HAS_ANCESTOR`, `IN` and `NOT_IN`.
159167
*
160168
* *To filter by ancestors, see {module:datastore/query#hasAncestor}.*
161169
*

system-test/datastore.ts

+27
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,33 @@ describe('Datastore', () => {
725725
assert.strictEqual(entities!.length, 6);
726726
});
727727

728+
it('should filter queries with NOT_EQUAL', async () => {
729+
const q = datastore
730+
.createQuery('Character')
731+
.hasAncestor(ancestor)
732+
.filter('appearances', '!=', 9);
733+
const [entities] = await datastore.runQuery(q);
734+
assert.strictEqual(entities!.length, 6);
735+
});
736+
737+
it('should filter queries with IN', async () => {
738+
const q = datastore
739+
.createQuery('Character')
740+
.hasAncestor(ancestor)
741+
.filter('appearances', 'IN', [9, 25]);
742+
const [entities] = await datastore.runQuery(q);
743+
assert.strictEqual(entities!.length, 3);
744+
});
745+
746+
it('should filter queries with NOT_IN', async () => {
747+
const q = datastore
748+
.createQuery('Character')
749+
.hasAncestor(ancestor)
750+
.filter('appearances', 'NOT_IN', [9, 25]);
751+
const [entities] = await datastore.runQuery(q);
752+
assert.strictEqual(entities!.length, 5);
753+
});
754+
728755
it('should filter queries with defined indexes', async () => {
729756
const q = datastore
730757
.createQuery('Character')

test/query.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ describe('Query', () => {
7575
.filter('name', '=', 'Title')
7676
.filter('count', '>', 20)
7777
.filter('size', '<', 10)
78-
.filter('something', '>=', 11);
78+
.filter('something', '>=', 11)
79+
.filter('neProperty', '!=', 12)
80+
.filter('inProperty', 'IN', 13)
81+
.filter('notInProperty', 'NOT_IN', 14);
7982

8083
assert.strictEqual(query.filters[0].name, 'date');
8184
assert.strictEqual(query.filters[0].op, '<=');
@@ -96,6 +99,18 @@ describe('Query', () => {
9699
assert.strictEqual(query.filters[4].name, 'something');
97100
assert.strictEqual(query.filters[4].op, '>=');
98101
assert.strictEqual(query.filters[4].val, 11);
102+
103+
assert.strictEqual(query.filters[5].name, 'neProperty');
104+
assert.strictEqual(query.filters[5].op, '!=');
105+
assert.strictEqual(query.filters[5].val, 12);
106+
107+
assert.strictEqual(query.filters[6].name, 'inProperty');
108+
assert.strictEqual(query.filters[6].op, 'IN');
109+
assert.strictEqual(query.filters[6].val, 13);
110+
111+
assert.strictEqual(query.filters[7].name, 'notInProperty');
112+
assert.strictEqual(query.filters[7].op, 'NOT_IN');
113+
assert.strictEqual(query.filters[7].val, 14);
99114
});
100115

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

0 commit comments

Comments
 (0)