From: Ben Thompson Date: Tue, 13 May 2014 17:39:41 +0000 (-0400) Subject: Abstract restQuery formatting into a helper function other models can use. X-Git-Tag: v4.5.0-beta~25^2~9 X-Git-Url: http://git.roojs.org/?a=commitdiff_plain;h=8f015fd32d323859a53e5bb2fdcdb7ab2aeed6b0;p=xtuple Abstract restQuery formatting into a helper function other models can use. --- diff --git a/lib/orm/source/xm/javascript/model.sql b/lib/orm/source/xm/javascript/model.sql index e653b4ce9..ae9e67d77 100644 --- a/lib/orm/source/xm/javascript/model.sql +++ b/lib/orm/source/xm/javascript/model.sql @@ -319,29 +319,20 @@ select xt.install_js('XM','Model','xtuple', $$ }; /** - Returns a complex query's results using the REST query structure. This is a - wrapper for XM.Model.query that reformats the query structure from a - rest_query to our XT.Rest structure. This dispatch function can be used by - a REST API client to query a resource when the query would be too long to - pass to the API as a GET URL query. + Format acomplex query's using the REST query structure into an xTuple's query. + This is a helper function that reformats the query structure from a + rest_query to our XT.Rest structure. This function should be used by reformat + any REST API client queriers. Sample usage: - select xt.post('{ - "username": "admin", - "nameSpace": "XM", - "type": "Model", - "dispatch":{ - "functionName":"restQuery", - "parameters":["Address", {"query": [{"city":{"EQUALS":"Norfolk"}}], "orderby": [{"ASC": "line1"}, {"DESC": "line2"}]}] - } - }'); + XM.Model.restQueryFormat({"query": [{"city":{"EQUALS":"Norfolk"}}], "orderby": [{"ASC": "line1"}, {"DESC": "line2"}]}) - @param {String} recordType to query @param {Object} options: query - @returns Object + @returns {Object} The formated query */ - XM.Model.restQuery = function (recordType, options) { + XM.Model.restQueryFormat = function (options) { options = options || {}; + var order = {}, param = {}, query = {}, @@ -365,7 +356,7 @@ select xt.install_js('XM','Model','xtuple', $$ }; /* Convert from rest_query to XM.Model.query structure. */ - if (recordType && options) { + if (options) { if (options.query) { query.parameters = []; for (var i = 0; i < options.query.length; i++) { @@ -415,7 +406,43 @@ select xt.install_js('XM','Model','xtuple', $$ } } - result = XM.Model.query(recordType, {"query": query}); + return query; + }; + + /** + Returns a complex query's results using the REST query structure. This is a + wrapper for XM.Model.query that reformats the query structure from a + rest_query to our XT.Rest structure. This dispatch function can be used by + a REST API client to query a resource when the query would be too long to + pass to the API as a GET URL query. + + Sample usage: + select xt.post('{ + "username": "admin", + "nameSpace": "XM", + "type": "Model", + "dispatch":{ + "functionName":"restQuery", + "parameters":["Address", {"query": [{"city":{"EQUALS":"Norfolk"}}], "orderby": [{"ASC": "line1"}, {"DESC": "line2"}]}] + } + }'); + + @param {String} recordType to query + @param {Object} options: query + @returns Object + */ + XM.Model.restQuery = function (recordType, options) { + options = options || {}; + var formattedOptions = {}; + + /* Convert from rest_query to XM.Model.query structure. */ + if (recordType && options) { + formattedOptions = { + "query": XM.Model.restQueryFormat(options) + }; + } + + result = XM.Model.query(recordType, formattedOptions); return result; };