The query module exports the [SQL] query method to pass queries to the stored dbs_connections available to the XYZ instance.

Requires

Methods

(async, inner) executeQuery(req, res, template, query)

The method send a parameterised query to a database connection.

The dbs for the query is determined primarily by the template. The layer.dbs is used for layer queries if the dbs on the template is not implicit. The locale.dbs is assumed as the layer.dbs if not defined in JSON layer. The workspace.dbs will be used as fallback if no template, layer, or locale dbs can be determined.

Parameters:
NameTypeDescription
reqreq

HTTP request.

resres

HTTP response.

templateObject

Request template.

querystring

SQL query.

Properties
NameTypeAttributesDescription
req.paramsObject<optional>

Request params.

(async, inner) fieldsMap(req, res)

The method assigns the fieldsMap object property to the request params for layer queries with a fields request parameter.

The fields param is split into an array and template strings of workspace.templates matching a field are set as value to the field key in the fieldsMap object.

Parameters:
NameTypeDescription
reqreq

HTTP request.

resres

HTTP response.

Properties
NameTypeDescription
req.paramsObject

The request params.

params.fieldsArray

An array of string fields is provided for a layer query.

(inner) getQueryFromTemplate(req, template)

In order to prevent SQL injections queries must be build from templates stored in the workspace.templates{}.

A template may have a render method which returns a query string assigned as template.template.

Parameter to be replaced in the SQL query string must be checked to only contain whitelisted character to prevent SQL injection.

Any variables to be replaced on query execution in the database must be replaced with indices in the query string. eg. $1, $2, ...

The substitute values are stored in the ordered params.SQL[] array.

An error will be returned if the substitution fails.

Parameters:
NameTypeDescription
reqreq

HTTP request.

templateObject

Request template.

Properties
NameTypeDescription
req.paramsObject

Request params.

params.filterObject

JSON filter which must be turned into a SQL filter string for substitution.

params.SQLArray

Substitute parameter for SQL query.

template.renderfunction

Method to render template string.

template.templatestring

SQL template string.

(async, inner) infojMap(req, res)

The method assigns the infojMap object property to the request params for layer requests.

The method iterates over the layer.infoj entries and only assigns entry fields valid for a location_get request.

A lookup of template [SQL] strings is attempted only if the template is defined in the entry object.

Parameters:
NameTypeDescription
reqreq

HTTP request.

resres

HTTP response.

Properties
NameTypeDescription
req.paramsObject

The request params.

params.layerArray

The layer object assigned by the layerQuery

(async, inner) layerQuery(req, res, template)

Queries which reference a layer must be checked against the layer JSON in the workspace.

Layer query templates must have a layer request property.

Layer queries have restricted viewport and filter params. These params can not be substituted in the database but must be replaced in the SQL query string.

Any query which references a layer and locale will be passed through the layer query method. The getLayer method will fail return an error if the locale is not defined as param or the layer is not a member of the locale.

/api/query?template=query&locale=uk&layer=retail

The fields request param property may be provided as an array. The string should be replaced with the template property of a matching workspace template.

Parameters:
NameTypeDescription
reqreq

HTTP request.

resres

HTTP response.

templateObject

The query template.

Properties
NameTypeAttributesDescription
template.layerBoolean

A layer query template.

req.paramsObject

Request params.

params.filterObject

JSON filter which must be turned into a SQL filter string for substitution.

params.SQLArray

Substitute parameter for SQL query.

params.fieldsArray

An array of string fields is provided for a layer query.

params.userObject<optional>

Requesting user.

user.rolesArray<optional>

User roles.

(async, inner) query(req, res)

The [SQL] query method requests a query template from the getTemplate method and checks whether the requesting user is permitted to execute the query.

The layerQuery() method must be awaited for queries that reference a layer. The layerQuery must be run before the getTemplate() request since the query template may be defined in the layer [template].

A template is turned into a query by the getQueryFromTemplate() method.

The query is executed by the executeQuery() method.

Parameters:
NameTypeDescription
reqreq

HTTP request.

resres

HTTP response.

Properties
NameTypeAttributesDescription
req.paramsObject

Request params.

params.userObject<optional>

Requesting user.

user.rolesArray<optional>

User roles.

(inner) sendRows(req, res, template, rows)

The method formats the rows returned from a SQL query and sends the formated rows through the HTTP response object.

Parameters:
NameTypeDescription
reqreq

HTTP request.

resres

HTTP response.

templateObject

Request template.

rowsarray

The response from a SQL query.

(inner) templateTables(template) → {Array}

The methods call the internal getObjTables method to iterate through the [layer] template object argument and its nested propertiess. Any 'table' string properties and string values of a 'tables' object are pushed into tables array returned from the method.

Parameters:
NameTypeDescription
templateobject

[Layer] template object to parse for table strings.

Returns:

Array of table strings in layer template object.

Type: 
Array