ts-db-helper
Version:
Simple ORM based on TypeScript
48 lines • 1.67 kB
JavaScript
import { QueryError } from '../../errors/query.error';
import { QueryManager } from '../../managers/query-manager';
import { DbQuery } from '../db-query.model';
/**
* @public
* @function RawQuery
*
* @description
* this function allow integrators to make RawQuery, any query not permitted
* by the current api will be able here
*
* @param {string} query the query string
* @param {Array<any>} params the query paraeters
* @param {number} size optional result size
* @param {number} page optional result page
*
* @return {Object} query object to execute with 'exec()' method
*/
export function RawQuery(query, params, size, page) {
var types = ['PRAGMA', 'ALTER', 'DROP', 'CREATE', 'SELECT', 'UPDATE', 'INSERT', 'DELETE'];
var dbQuery = new DbQuery();
dbQuery.query = query;
dbQuery.params = params;
dbQuery.size = size || 1000;
dbQuery.page = page || 0;
var trimedQuery = query.trim();
var firstWord = trimedQuery.substr(0, trimedQuery.indexOf(' '));
firstWord = firstWord ? firstWord.toUpperCase() : '';
if (firstWord && types.indexOf(firstWord[0])) {
dbQuery.type = firstWord;
}
else {
throw new QueryError('Unsupported raw query', query, params.join(', '));
}
var q = {
/**
* @public
* @method exec to execute the query and asynchronously retreive result.
*
* @return observable to subscribe
*/
exec: function () {
return QueryManager.getInstance().query(dbQuery);
}
};
return q;
}
//# sourceMappingURL=raw-query.js.map