ng-db-helper
Version:
Simple db helper for typescript like an orm with plugable connectors.
202 lines • 8.17 kB
JavaScript
import { CordovaSqliteConnector } from './cordova-sqlite-connector';
import { WebsqlConnector } from './websql-connector';
import { WebsqlConnectorConfiguration } from './configurations/websql-connector-configuration';
import { Observable } from 'rxjs/Observable';
import { UnsatisfiedRequirementError } from 'ts-db-helper';
/**
* @class MixedCordovaSqliteWebsqlConnector
*
* @description
* 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 cordova-sqlite-storage or Websql
* use {@link CordovaSqliteConnectorConfiguration} to override default migrations logics.
*
* To understand QueryConnectors or ModelMigrations see respectively {@link QueryConnectors},
* {@link ModelMigrations} and configuration default script configuration
*
* Requirements: if crodova is supported, cordova, cordova-plugin-device, cordova-plugin-file, cordova-sqlite-storage
* else Websql
*
* @example
* ```typescript
* const connectorConfig = new CordovaSqliteConnectorConfiguration();
* // configure db name on device
* connectorConfig.dbName = app.sqlite;
* // add config to connector
* const connector = MixedCordovaSqliteWebsqlConnector(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 MixedCordovaSqliteWebsqlConnector = (function () {
/**
* @public
* @constructor
* @throws {UnsatisfiedRequirementError}, thrown if cordova-plugin-device is missing and needed
* connector start logic after 'deviceready' signal firing if cordova is present else directly start,
* assuming that in this case platform should support Websql.
*
* @param {CordovaSqliteConnectorConfiguration} config configuration of the connector
*/
function MixedCordovaSqliteWebsqlConnector(config) {
var _this = this;
this.config = config;
/**
* @private
* @property {boolean} ready flag updated with connector state to indicate that connector can query
*/
this.ready = false;
this.supportRowid = true;
if (window.cordova) {
document.addEventListener('deviceready', function () {
if (!window.device) {
throw (new UnsatisfiedRequirementError('Mixed connector need cordova-plugin-device to be installed'));
}
if (window.device.platform === 'Browser') {
_this.setupWebsqlConnector();
}
else {
_this.setupCordovaConnector();
}
});
}
else {
this.setupWebsqlConnector();
}
}
/**
* @private
* @method setupWebsqlConnector
*/
MixedCordovaSqliteWebsqlConnector.prototype.setupWebsqlConnector = function () {
var connectorConfig = new WebsqlConnectorConfiguration();
connectorConfig.dbName = this.config.dbName;
this.connector = new WebsqlConnector(connectorConfig);
this.observeConnectorReady();
};
/**
* @private
* @method setupCordovaConnector
*/
MixedCordovaSqliteWebsqlConnector.prototype.setupCordovaConnector = function () {
this.connector = new CordovaSqliteConnector(this.config);
this.observeConnectorReady();
};
/**
* @private
* @method observeConnectorReady called on connector setted up
*/
MixedCordovaSqliteWebsqlConnector.prototype.observeConnectorReady = function () {
var _this = this;
this.connector.onReady().subscribe(function (ready) {
_this.ready = ready;
if (_this.onReadyObserver) {
_this.onReadyObserver.next(_this.ready);
_this.onReadyObserver.complete();
}
});
};
/**
* @public
* @method query connector method to fire query
*
* @param {DbQuery} dbQuery DbQuery object containing query and query params.
* see {@link DbQuery}
*
* @return {Observable<QueryResult<any>>} passing {@link QueryResult<any>} on query success
* passing {@link QueryError} on query error
*/
MixedCordovaSqliteWebsqlConnector.prototype.query = function (dbQuery) {
return this.connector.query(dbQuery);
};
/**
* @public
* @method queryBatch make multiple queries in an unique transaction
*
* @param {DbQuery} dbQueries multiple queries to run in the same transaction
*
* @return {Observable<QueryResult<any>>} passing {@link QueryResult<any>} on query success
* passing {@link QueryError} on query error
*/
MixedCordovaSqliteWebsqlConnector.prototype.queryBatch = function (dbQueries) {
return this.connector.queryBatch(dbQueries);
};
/**
* @public
* @method isReady to check if module is ready, if not, caller should
* subscribe to {@link MixedCordovaSqliteWebsqlConnector.onReady}
*
* @return {boolean} should be true if connector can query else false
*/
MixedCordovaSqliteWebsqlConnector.prototype.isReady = function () {
return this.ready;
};
/**
* @public
* @method onReady should be subscribed if connector is not ready
* if connector is ready, observable is immediatly called, else all check will
* be done after 'deviceready' signal
*
* @return {Observable<boolean>} passing true if connector is ready
* passing false if connector will never be ready
*/
MixedCordovaSqliteWebsqlConnector.prototype.onReady = function () {
var _this = this;
return Observable.create(function (observer) {
if (_this.ready) {
observer.next(_this.ready);
observer.complete();
}
else {
_this.onReadyObserver = observer;
}
});
};
/**
* @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
*/
MixedCordovaSqliteWebsqlConnector.prototype.getDbVersion = function () {
return this.connector.getDbVersion();
};
/**
* @public
* @method initModel is implemented method from ModelMigration, see {@link ModelMigration} to understand usage.
* method directly delegated to real connector
*
* @param {DataModel} dataModel data model generated with model annotations
*
* @return {Observable<any>} resolved on initModel finish
*/
MixedCordovaSqliteWebsqlConnector.prototype.initModel = function (dataModel) {
return this.connector.initModel(dataModel);
};
/**
* @public
* @method upgradeModel is implemented method from ModelMigration, see {@link ModelMigration} to understand usage.
* method directly delegated to real connector
*
* @param {DataModel} dataModel data model generated with model annotations
* @param {string} oldVersion old model version
*
* @return {Observable<any>} resolved on upgradeModel finish
*/
MixedCordovaSqliteWebsqlConnector.prototype.upgradeModel = function (dataModel, oldVersion) {
return this.connector.upgradeModel(dataModel, oldVersion);
};
return MixedCordovaSqliteWebsqlConnector;
}());
export { MixedCordovaSqliteWebsqlConnector };
//# sourceMappingURL=mixed-cordova-sqlite-websql-connector.js.map