Navigation Menu

Skip to content

Commit

Permalink
Resolve query name automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Feb 7, 2014
1 parent 981d707 commit ce93b63
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/adapter/api/rest-request-builder.js
@@ -1,3 +1,6 @@
var inflection = require('inflection');


var NUMERIC = /^-?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)$/;
function getNumericValue(params, key) {
var value = params[key];
Expand All @@ -23,7 +26,14 @@ function searchRequestBuilder(request) {

var queries = {};
var condition = {};
queries.result = {
var queryName = params.tableName;
try {
queryName = inflection.tableize(queryName);
queryName = inflection.dasherize(queryName);
} catch(error) {
queryName = params.tableName.toLowerCase();
}
queries[queryName] = {
source: params.tableName,
output: {
elements: [
Expand All @@ -37,12 +47,12 @@ function searchRequestBuilder(request) {
};

if (typeof queryParams.attributes == 'string') {
queries.result.output.attributes = queryParams.attributes.split(',');
queries[queryName].output.attributes = queryParams.attributes.split(',');
} else {
queries.result.output.attributes = [];
queries[queryName].output.attributes = [];
}
if (typeof queryParams.limit == 'string')
queries.result.output.limit = getIntegerValue(queryParams, 'limit');
queries[queryName].output.limit = getIntegerValue(queryParams, 'limit');

if (typeof queryParams.match_escalation_threshold == 'string')
condition.matchEscalationThreshold = getIntegerValue(queryParams, 'match_escalation_threshold');
Expand All @@ -51,12 +61,12 @@ function searchRequestBuilder(request) {
if (typeof queryParams.query == 'string')
condition.query = queryParams.query;
if (Object.keys(condition).length > 0)
queries.result.condition = condition;
queries[queryName].condition = condition;

if (typeof queryParams.offset == 'string')
queries.result.offset = getIntegerValue(queryParams, 'offset');
queries[queryName].offset = getIntegerValue(queryParams, 'offset');
if (typeof queryParams.sort_by == 'string')
queries.result.sortBy = queryParams.sort_by.split(',');
queries[queryName].sortBy = queryParams.sort_by.split(',');

var message = {};
message.queries = queries;
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"connect": "*",
"fluent-logger": "*",
"inflection": "*",
"jade": "*",
"less-middleware": "*",
"msgpack": "*",
Expand Down

0 comments on commit ce93b63

Please sign in to comment.