Skip to content

Commit

Permalink
Merge branch 'raw-clause' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesfer committed Nov 20, 2017
2 parents 40fc3d9 + ac3e540 commit 64b3e3b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/clauses/raw.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Raw } from './raw';
import { expect } from 'chai';

describe('Raw', function() {
it('should return the same string it is given', function() {
let query = new Raw('ADD INDEX node.id');
expect(query.build()).to.equal('ADD INDEX node.id');
});

it('should accept parameters', function() {
let query = new Raw('SET n.id = $id', { id: 3 });
expect(query.build()).to.equal('SET n.id = $id');
expect(query.getParams()).to.have.property('id')
.that.equals(3);
});
});
16 changes: 16 additions & 0 deletions src/clauses/raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Statement } from '../statement';
import { Dictionary } from 'lodash';

export class Raw extends Statement {
constructor(public clause: string, public params: Dictionary<any> = {}) {
super();

for (let key in params) {
this.addParam(params[key], key);
}
}

build() {
return this.clause;
}
}
4 changes: 4 additions & 0 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,8 @@ export class Connection implements Builder {
orderBy(fields: Many<string> | OrderConstraints, dir?: Direction) {
return this.query().orderBy(fields, dir);
}

raw(clause: string, params: Dictionary<any> = {}) {
return this.query().raw(clause, params);
}
}
5 changes: 5 additions & 0 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Skip } from './clauses/skip';
import { Limit } from './clauses/limit';
import { AnyConditions } from './clauses/where-utils';
import { Direction, OrderBy, OrderConstraints } from './clauses/order-by';
import { Raw } from './clauses/raw';

export class Query extends Statement implements Builder {
protected statements: Statement[] = [];
Expand Down Expand Up @@ -100,6 +101,10 @@ export class Query extends Statement implements Builder {
return this.addStatement(new OrderBy(fields, dir));
}

raw(clause: string, params: Dictionary<any> = {}) {
return this.addStatement(new Raw(clause, params));
}

build() {
return join(map(this.statements, s => s.build()), '\n') + ';';
}
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface Builder {
limit(amount: number | string): Builder;
where(conditions: AnyConditions): Builder;
orderBy(fields: Many<string> | OrderConstraints, dir?: Direction);
raw(clause: string, params?: Dictionary<any>);
}


Expand Down

0 comments on commit 64b3e3b

Please sign in to comment.