Skip to content

Commit

Permalink
fix(Where): remove side effects from build
Browse files Browse the repository at this point in the history
Previously, calling `build()` on a where clause would add more parameters each time. This side
effect has been moved so parameters are created in the constructor.

Closes #42
  • Loading branch information
jamesfer committed Aug 8, 2018
1 parent 350bb63 commit f664ebe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
7 changes: 7 additions & 0 deletions src/clauses/where.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@ describe('Where', () => {
upperAge: 65,
});
});

it('should not have side effects on the parameters', () => {
const query = new Where({ age: between(18, 65) });
const params = query.getParams();
query.build();
expect(query.getParams()).to.deep.equal(params);
});
});
});
21 changes: 4 additions & 17 deletions src/clauses/where.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
import { Clause } from '../clause';
import { AnyConditions, stringCons } from './where-utils';

/**
* where({
* n: {
* name: 'james',
* active: eq(1),
* age: gt(20),
* }
* });
* where({
* 'n.name': 'james'
* });
* where([
* { 'n.name': 'james' },
* { 'n.age': gt(20) },
* ]);
*/
export class Where extends Clause {
protected query: string;

constructor(public conditions: AnyConditions) {
super();
this.query = stringCons(this.parameterBag, this.conditions);
}

build() {
return 'WHERE ' + stringCons(this.parameterBag, this.conditions);
return 'WHERE ' + this.query;
}
}

0 comments on commit f664ebe

Please sign in to comment.