Skip to content

Commit

Permalink
Exposes function creation to allow use of unsupported functions (bria…
Browse files Browse the repository at this point in the history
  • Loading branch information
hstanford authored and brianc committed Aug 6, 2017
1 parent 9135b0b commit c7334f3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var getFunctionCallCreator = function(name) {

// creates a hash of functions for a sql instance
var getFunctions = function(functionNames) {
if (typeof functionNames === 'string')
return getFunctionCallCreator(functionNames);

var functions = _.reduce(functionNames, function(reducer, name) {
reducer[name] = getFunctionCallCreator(name);
return reducer;
Expand Down Expand Up @@ -68,4 +71,5 @@ var getStandardFunctions = function() {
return getFunctions(standardFunctionNames);
};

module.exports.getFunctions = getFunctions;
module.exports.getStandardFunctions = getStandardFunctions;
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var Sql = function(dialect, config) {

// attach the standard SQL functions to this instance
this.functions = functions.getStandardFunctions();
this.function = functions.getFunctions;
};

// Define a table
Expand Down
6 changes: 6 additions & 0 deletions test/function-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,10 @@ suite('function', function() {
assert.equal(query.text, 'SELECT (AVG((DISTINCT((COUNT("user"."id") + MAX("user"."id"))) - MIN("user"."id"))) * $1) FROM "user"');
assert.equal(query.values[0], 100);
});

test('use custom function', function() {
var query = user.select(sql.function('PHRASE_TO_TSQUERY')('simple', user.name)).toQuery();
assert.equal(query.text, 'SELECT PHRASE_TO_TSQUERY($1, "user"."name") FROM "user"');
assert.equal(query.values[0], 'simple');
});
});

0 comments on commit c7334f3

Please sign in to comment.