Skip to content

Commit

Permalink
fix: add nin operator to query typing and builder (#2215)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgabeler-lee-6rs authored and bajtos committed Jan 7, 2019
1 parent 16311c5 commit c38bd4e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/repository/src/query.ts
Expand Up @@ -30,6 +30,7 @@ export type Operators =
| 'lt' // <
| 'lte' // <=
| 'inq' // IN
| 'nin' // NOT IN
| 'between' // BETWEEN [val1, val2]
| 'exists'
| 'and' // AND
Expand All @@ -51,6 +52,7 @@ export type PredicateComparison<PT> = {
lt?: PT;
lte?: PT;
inq?: PT[];
nin?: PT[];
between?: [PT, PT];
exists?: boolean;
like?: PT;
Expand Down Expand Up @@ -373,6 +375,17 @@ export class WhereBuilder<MT extends object = AnyObject> {
return this.add(w);
}

/**
* Add a `nin` condition
* @param key Property name
* @param val An array of property values
*/
nin<K extends KeyOf<MT>>(key: K, val: MT[K][]): this {
const w: Where<MT> = {};
w[key] = {nin: val};
return this.add(w);
}

/**
* Add a `between` condition
* @param key Property name
Expand Down
9 changes: 9 additions & 0 deletions packages/repository/test/unit/query/query-builder.unit.ts
Expand Up @@ -44,6 +44,15 @@ describe('WhereBuilder', () => {
expect(where).to.eql({x: {inq: [1, 2, 3]}, y: {inq: ['a', 'b']}});
});

it('builds where object with nin', () => {
const whereBuilder = new WhereBuilder();
const where = whereBuilder
.nin('x', [1, 2, 3])
.nin('y', ['a', 'b'])
.build();
expect(where).to.eql({x: {nin: [1, 2, 3]}, y: {nin: ['a', 'b']}});
});

it('builds where object with neq', () => {
const whereBuilder = new WhereBuilder();
const where = whereBuilder.neq('x', 1).build();
Expand Down

0 comments on commit c38bd4e

Please sign in to comment.