UNPKG

ng-db-helper

Version:

Simple db helper for typescript like an orm with plugable connectors.

222 lines 8.96 kB
import { QueryResultWrapper } from './query-result-wrapper'; import { UnsatisfiedRequirementError, QueryError } from 'ts-db-helper'; import { Observable } from 'rxjs/Observable'; /** * @class WebsqlConnector * * @description * This class is a default connector for rdb module see {@link NgDbHelperModuleConfig} * This class provides config key to add copy informations. * * This default connector allow query to database provided with Websql * use {@link WebsqlConnectorConfiguration} to override default migrations logics. * See W3C specification and check browser support * * To understand QueryConnectors or ModelMigrations see respectively {@link QueryConnectors}, * {@link ModelMigrations} and configuration default script configuration * * Requirements: Websql support by browser * * @example * ```typescript * const connectorConfig = new WebsqlConnectorConfiguration(); * // configure db name on device * connectorConfig.dbName = app.sqlite; * // add config to connector * const connector = WebsqlConnector(connectorConfig); * // create module config * const config = new NgDbHelperModuleConfig(); * config.queryConnector = connector; * config.modelMigration = connector; * config.version = '1'; * // add config to module with forRoot method * ``` * * @author Olivier Margarit * @since 0.1 */ var WebsqlConnector = (function () { /** * @constructor * @throws {UnsatisfiedRequirementError} thrown if Websql is not supported * connector start logic after 'deviceready' signal firing. * * @param {WebsqlConnectorConfiguration} config configuration of the connector */ function WebsqlConnector(config) { this.config = config; /** * @private * @property {boolean} ready, flag updated with connector state to indicate that connector can query */ this.ready = true; this.supportRowid = true; this.ready = !!window.openDatabase; if (!this.ready) { throw (new UnsatisfiedRequirementError('Your browser does not support websql !')); } } Object.defineProperty(WebsqlConnector.prototype, "db", { /** * @private * @property {Database} db getter that open database on demand */ get: function () { if (!this.dbValue) { this.dbValue = window.openDatabase(this.config.dbName, '', this.config.dbName, 10000000); } return this.dbValue; }, enumerable: true, configurable: true }); /** * @public * @method query connector method to fire query * * @param {DbQuery} dbQuery DbQuery object containing query and query params. * * @return {Obsevable<QueryResult<any>>} passing {@link QueryResult<any>} on query success * passing {@link QueryError} on query error */ WebsqlConnector.prototype.query = function (dbQuery) { var _this = this; return Observable.create(function (observer) { var q = dbQuery.query; if (dbQuery.type === 'SELECT' && dbQuery.query.toUpperCase().indexOf('LIMIT') < 0 && dbQuery.query.toUpperCase().indexOf('OFFSET') < 0) { var offset = dbQuery.page * dbQuery.size; q += ' LIMIT ' + (offset + dbQuery.size); q += ' OFFSET ' + offset; } if (_this.db) { var qr_1; _this.db.transaction(function (transaction) { transaction.executeSql(q, dbQuery.params, function (tr, result) { qr_1 = new QueryResultWrapper(result); }, function (tr, err) { observer.error(new QueryError(err.message, q, dbQuery.params ? dbQuery.params.join(', ') : '')); }); }, function (err) { return observer.error(new QueryError(String(err.message), q, dbQuery.params ? dbQuery.params.join(', ') : '')); }, function (res) { observer.next(qr_1); observer.complete(); }); } else { observer.error(new QueryError('no database opened', q, dbQuery.params ? dbQuery.params.join(', ') : '')); } }); }; /** * @public * @method queryBatch connector method to fire many queries in a single transaction * * @param {DbQuery} dbQueries DbQuery object containing query and query params. * * @return {Obsevable<QueryResult<any>>} passing {@link QueryResult<any>} on query success * passing {@link QueryError} on query error */ WebsqlConnector.prototype.queryBatch = function (dbQueries) { var _this = this; return Observable.create(function (observer) { if (_this.db) { _this.db.transaction(function (transaction) { for (var _i = 0, dbQueries_1 = dbQueries; _i < dbQueries_1.length; _i++) { var dbQuery = dbQueries_1[_i]; transaction.executeSql(dbQuery.query, dbQuery.params); } }, function (err) { return observer.error(new QueryError(String(err.message), '', '')); }, function () { observer.next({ insertId: undefined, rowsAffected: 0, rows: { length: 0, item: function (index) { return null; }, toArray: function () { return []; } } }); observer.complete(); }); } else { observer.error(new QueryError('no database opened', '', '')); } }); }; /** * @public * @method isReady to check if module is ready, if not, caller should * subscribe to {@link CordovaSqliteConnector.onReady} * * @return {boolean} should be true if connector can query else false */ WebsqlConnector.prototype.isReady = function () { return this.ready; }; /** * @public * @method onReady no async check is done, capability is checked in constructor. * * @return {Observable<boolean>} passing true if connector is ready * passing false if connector will never be ready */ WebsqlConnector.prototype.onReady = function () { var _this = this; return Observable.create(function (observer) { observer.next(_this.ready); observer.complete(); }); }; /** * @public * @method getDbVersion called to check db version, should be called only if connector * is ready. * * @return {Observable<string>} passing string version after version is checked */ WebsqlConnector.prototype.getDbVersion = function () { var _this = this; return Observable.create(function (observer) { if (_this.db) { observer.next(String(_this.db.version)); } else { observer.next(''); } observer.complete(); }); }; /** * @public * @method initModel is implemented method from ModelMigration, see {@link ModelMigration} to understand usage. * {@link WebsqlConnectorConfiguration.initDataModel} is called * * @param {DataModel} dataModel data model generated with model annotations * * @return {Observable<any>} resolved on initModel finish */ WebsqlConnector.prototype.initModel = function (dataModel) { return this.config.initDataModel(dataModel, this.db); }; /** * @public * @method upgradeModel is implemented method from ModelMigration, see {@link ModelMigration} to understand usage. * directly call {@link WebsqlConnectorConfiguration.upgradeDataModel} * * @param {DataModel} dataModel data model generated with model annotations * @param {string} oldVersion old model version * * @return {Observable<string>} resolved on upgradeModel finish */ WebsqlConnector.prototype.upgradeModel = function (dataModel, oldVersion) { return this.config.upgradeDataModel(dataModel, this.db); }; return WebsqlConnector; }()); export { WebsqlConnector }; //# sourceMappingURL=websql-connector.js.map