Skip to content

Add support for custom queries. #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,18 @@ class DSSqlAdapter {

filterQuery (resourceConfig, params, options) {
let table = getTable(resourceConfig)
let query = options && options.transaction || this.query
query = query.select(`${table}.*`).from(table)
let query

if (params instanceof Object.getPrototypeOf(this.query.client).QueryBuilder) {
query = params
params = {}
} else if (options && options.query) {
query = options.query || this.query
} else {
query = options && options.transaction || this.query
query = query.select(`${table}.*`).from(table)
}

params = params || {}
params.where = params.where || {}
params.orderBy = params.orderBy || params.sort
Expand Down
37 changes: 37 additions & 0 deletions test/filterQuery.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe('DSSqlAdapter#filterQuery', function () {

it('should use built-in query if no custom query provided', function* () {
var filterQuery = adapter.filterQuery(User);
assert.equal(filterQuery.toString(), 'select `user`.* from `user`')
});

it('should use custom query if passed as params (second parameter)', function* () {
var query = adapter.query.from('test');
var filterQuery = adapter.filterQuery(User, query);
assert.equal(filterQuery.toString(), 'select * from `test`')
});

it('should use custom query if passed as options.query', function* () {
var query = adapter.query.from('test');
var filterQuery = adapter.filterQuery(User, null, { query });
assert.equal(filterQuery.toString(), 'select * from `test`')
});

it('should apply where from params to custom query', function* () {
var query = adapter.query.from('test');
var filterQuery = adapter.filterQuery(User, { name: 'Sean' }, { query });
assert.equal(filterQuery.toString(), 'select * from `test` where `name` = \'Sean\'')
});

it('should apply limit from params to custom query', function* () {
var query = adapter.query.from('test');
var filterQuery = adapter.filterQuery(User, { limit: 2 }, { query });
assert.equal(filterQuery.toString(), 'select * from `test` limit 2')
});

it('should apply order from params to custom query', function* () {
var query = adapter.query.from('test');
var filterQuery = adapter.filterQuery(User, { orderBy: 'name' }, { query });
assert.equal(filterQuery.toString(), 'select * from `test` order by `name` asc')
});
});
1 change: 0 additions & 1 deletion test/find.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Promise = require('bluebird');
describe('DSSqlAdapter#find', function () {
it('should find a user in a Sql db', function* () {
var user = yield adapter.create(User, {name: 'John'});
Expand Down
1 change: 0 additions & 1 deletion test/findAll.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Promise = require('bluebird');
describe('DSSqlAdapter#findAll', function () {
it('should filter users', function* () {
var users = yield adapter.findAll(User, { age: 30 });
Expand Down