Skip to content

Commit b89fa21

Browse files
authored
fix: Allow filtering null values (#1067)
* fix: Allows nulls to be passed into value A user expressed interest in filtering with a null value. This typescript parameter change will now allow users to do so. fixes #958 * Add tests in the system test folder The code in the system test folder reflects the user experience more closely so we want to provide test cases there that will break when compiling typescript if new changes in src are not provided. * Assert statements in system tests Add assert statements to check that the value in the filter created in the system test actually equals null. * Modify tests to assert check for null Slight rename in system tests. Modified tests so that they test to see that the filter has a null value. * Prefer elvis operator in assert statements This change replaces the a longer fragment with elvis operator to make assert statements more concise. * eliminate the need for a variable In two tests we eliminate variables and just have an assert statement so that we can have more concise code.
1 parent 0c8bc83 commit b89fa21

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/query.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,13 @@ class Query {
201201
* const keyQuery = query.filter('__key__', key);
202202
* ```
203203
*/
204-
filter(property: string, value: {}): Query;
205-
filter(property: string, operator: Operator, value: {}): Query;
206-
filter(property: string, operatorOrValue: Operator, value?: {}): Query {
204+
filter(property: string, value: {} | null): Query;
205+
filter(property: string, operator: Operator, value: {} | null): Query;
206+
filter(
207+
property: string,
208+
operatorOrValue: Operator,
209+
value?: {} | null
210+
): Query {
207211
let operator = operatorOrValue as Operator;
208212
if (arguments.length === 2) {
209213
value = operatorOrValue as {};

system-test/datastore.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,20 @@ describe('Datastore', () => {
868868
assert.strictEqual(entities.length, characters.length);
869869
});
870870

871+
it('should construct filters by null status', async () => {
872+
assert.strictEqual(
873+
datastore.createQuery('Character').filter('status', null).filters.pop()
874+
?.val,
875+
null
876+
);
877+
assert.strictEqual(
878+
datastore
879+
.createQuery('Character')
880+
.filter('status', '=', null)
881+
.filters.pop()?.val,
882+
null
883+
);
884+
});
871885
it('should filter by key', async () => {
872886
const key = datastore.key(['Book', 'GoT', 'Character', 'Rickard']);
873887
const q = datastore

test/query.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ describe('Query', () => {
163163
assert.strictEqual(filter.op, '=');
164164
assert.strictEqual(filter.val, 'Stephen');
165165
});
166+
it('should accept null as value', () => {
167+
assert.strictEqual(
168+
new Query(['kind1']).filter('status', null).filters.pop()?.val,
169+
null
170+
);
171+
assert.strictEqual(
172+
new Query(['kind1']).filter('status', '=', null).filters.pop()?.val,
173+
null
174+
);
175+
});
166176
});
167177

168178
describe('hasAncestor', () => {

0 commit comments

Comments
 (0)