Skip to content

Commit 04a7961

Browse files
jmdobrytechniq
authored andcommitted
Add support for custom queries.
1 parent a1498f9 commit 04a7961

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

src/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,18 @@ class DSSqlAdapter {
242242

243243
filterQuery (resourceConfig, params, options) {
244244
let table = getTable(resourceConfig)
245-
let query = options && options.transaction || this.query
246-
query = query.select(`${table}.*`).from(table)
245+
let query
246+
247+
if (params instanceof Object.getPrototypeOf(this.query.client).QueryBuilder) {
248+
query = params
249+
params = {}
250+
} else if (options && options.query) {
251+
query = options.query || this.query
252+
} else {
253+
query = options && options.transaction || this.query
254+
query = query.select(`${table}.*`).from(table)
255+
}
256+
247257
params = params || {}
248258
params.where = params.where || {}
249259
params.orderBy = params.orderBy || params.sort

test/filterQuery.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
describe('DSSqlAdapter#filterQuery', function () {
2+
3+
it('should use built-in query if no custom query provided', function* () {
4+
var filterQuery = adapter.filterQuery(User);
5+
assert.equal(filterQuery.toString(), 'select `user`.* from `user`')
6+
});
7+
8+
it('should use custom query if passed as params (second parameter)', function* () {
9+
var query = adapter.query.from('test');
10+
var filterQuery = adapter.filterQuery(User, query);
11+
assert.equal(filterQuery.toString(), 'select * from `test`')
12+
});
13+
14+
it('should use custom query if passed as options.query', function* () {
15+
var query = adapter.query.from('test');
16+
var filterQuery = adapter.filterQuery(User, null, { query });
17+
assert.equal(filterQuery.toString(), 'select * from `test`')
18+
});
19+
20+
it('should apply where from params to custom query', function* () {
21+
var query = adapter.query.from('test');
22+
var filterQuery = adapter.filterQuery(User, { name: 'Sean' }, { query });
23+
assert.equal(filterQuery.toString(), 'select * from `test` where `name` = \'Sean\'')
24+
});
25+
26+
it('should apply limit from params to custom query', function* () {
27+
var query = adapter.query.from('test');
28+
var filterQuery = adapter.filterQuery(User, { limit: 2 }, { query });
29+
assert.equal(filterQuery.toString(), 'select * from `test` limit 2')
30+
});
31+
32+
it('should apply order from params to custom query', function* () {
33+
var query = adapter.query.from('test');
34+
var filterQuery = adapter.filterQuery(User, { orderBy: 'name' }, { query });
35+
assert.equal(filterQuery.toString(), 'select * from `test` order by `name` asc')
36+
});
37+
});

test/find.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var Promise = require('bluebird');
21
describe('DSSqlAdapter#find', function () {
32
it('should find a user in a Sql db', function* () {
43
var user = yield adapter.create(User, {name: 'John'});

test/findAll.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var Promise = require('bluebird');
21
describe('DSSqlAdapter#findAll', function () {
32
it('should filter users', function* () {
43
var users = yield adapter.findAll(User, { age: 30 });

0 commit comments

Comments
 (0)