UNPKG

ionic-database-builder

Version:

Extended library from database-builder to assist in creating and maintaining SQL commands. Allowing integrate execute commands with SQLite ('@ionic-native/sqlite'), Web Sql, etc. Through the interface injection 'DatabaseCreatorContract' returning an imple

16 lines (14 loc) 15.7 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs"),require("database-builder"),require("rxjs/operators"),require("moment")):"function"==typeof define&&define.amd?define("ionic-database-builder",["exports","@angular/core","rxjs","database-builder","rxjs/operators","moment"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["ionic-database-builder"]={},e.ng.core,e.rxjs,e.databaseBuilder,e.rxjs.operators,e.momentNs)}(this,(function(e,t,n,r,o,a){"use strict";function i(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var c=i(a),s=function(e,t){return s=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])},s(e,t)}; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */function u(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}s(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function l(e,t,n,r){return new(n||(n=Promise))((function(o,a){function i(e){try{s(r.next(e))}catch(e){a(e)}}function c(e){try{s(r.throw(e))}catch(e){a(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(i,c)}s((r=r.apply(e,t||[])).next())}))}function p(e,t){var n,r,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function c(a){return function(c){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;i;)try{if(n=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,r=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){i.label=a[1];break}if(6===a[0]&&i.label<o[1]){i.label=o[1],o=a;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(a);break}o[2]&&i.ops.pop(),i.trys.pop();continue}a=t.call(e,i)}catch(e){a=[6,e],r=0}finally{n=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}Object.create;Object.create;var f=function(){},b=new t.InjectionToken("is_available"),d=new t.InjectionToken("is_enable_log"),h=new t.InjectionToken("database_creator"),v=new t.InjectionToken("database_migration"),y=new t.InjectionToken("platform_load"),m=function(e){function t(t,n){var r=e.call(this)||this;return r._isAvailable=t,r._databaseCreator=n,r}return u(t,e),t.prototype.database=function(e){var t=this;return new n.Observable((function(n){t._isAvailable?t._databaseCreator.create({name:e,location:"default"}).then((function(e){n.next(e),n.complete()})).catch((function(e){n.error(e),n.complete()})):(n.next(void 0),n.complete())}))},t}(f);m.decorators=[{type:t.Injectable}],m.ctorParameters=function(){return[{type:Boolean,decorators:[{type:t.Inject,args:[b]}]},{type:void 0,decorators:[{type:t.Inject,args:[h]}]}]};var g=function(){function e(e,t){this.databaseFactory=e,this._platformLoad=t,this._databases=new Map}return e.prototype.cleanDatabaseName=function(e){return e.replace(/([^a-z0-9]+)/gi,"-")},e.prototype.addDatabaseNameExtension=function(e){return e+".db"},e.prototype.databaseInstance=function(e,t){var n=e+t;return this._databases.has(n)?this._databases.get(n):this.setDatabase(n,this.createDatabase(this.databaseNameFile(e),t))},e.prototype.invalidateInstance=function(){this._databases=new Map},e.prototype.setDatabase=function(e,t){if(t)return this._databases.set(e,t).get(e);throw new r.DatabaseBuilderError("Connection with provider of database cannot be created!")},e.prototype.createDatabase=function(e,t){var n=this;return new Promise((function(r,o){n._platformLoad.ready().then((function(){n.databaseFactory.database(e).subscribe((function(e){n.migrationVersion(e,t).subscribe((function(t){r(e)}),(function(e){o(e)}))}),(function(e){n.catchException(e),o(e)}))})).catch((function(e){n.catchException(e),o(e)}))}))},e.prototype.catchException=function(e){console.error(e)},e}(),_=function(e){function t(t,n,r,o,a,i){void 0===i&&(i=!0);var c=e.call(this,t,a)||this;return c._databaseSettings=n,c._injector=r,c._mapper=o,c.enableLog=i,c}return u(t,e),Object.defineProperty(t.prototype,"mapper",{get:function(){return this._mapper},enumerable:!1,configurable:!0}),t.prototype.databaseInstance=function(){var t=e.prototype.databaseInstance.call(this,this.databaseName(),this.version());if(!t)throw new Error("SQLite not avaliable!");return t},t.prototype.managedTransaction=function(){return n.from(this.databaseInstance()).pipe(o.mergeMap((function(e){if(!e.managedTransaction)throw new r.DatabaseBuilderError("Managed Transaction not supported in current middleware!");return n.of(e.managedTransaction())})))},t.prototype.beginTransaction=function(){var e=this;return new n.Observable((function(t){e.sql("BEGIN TRANSACTION").subscribe((function(n){e.crud().subscribe((function(e){t.next(e),t.complete()}),(function(e){t.error(e),t.complete()}))}),(function(e){t.error(e),t.complete()}))}))},t.prototype.commitTransaction=function(){var e=this;return new n.Observable((function(t){e.sql("COMMIT").subscribe((function(e){t.next(!0),t.complete()}),(function(e){t.error(e),t.complete()}))}))},t.prototype.rollbackTransaction=function(){var e=this;return new n.Observable((function(t){e.sql("ROLLBACK").subscribe((function(e){t.next(!0),t.complete()}),(function(e){t.error(e),t.complete()}))}))},t.prototype.crud=function(){var e=this;return new n.Observable((function(t){e.databaseInstance().then((function(n){return l(e,void 0,void 0,(function(){var e,o,a;return p(this,(function(i){switch(i.label){case 0:return o=(e=t).next,a=r.Crud.bind,[4,this._databaseSettings.config(this._injector,n)];case 1:return o.apply(e,[new(a.apply(r.Crud,[void 0,i.sent(),{database:n,getMapper:this._mapper,enableLog:this.enableLog}]))]),t.complete(),[2]}}))}))})).catch((function(e){t.error(e),t.complete()}))}))},t.prototype.sql=function(e,t){var o=this;return void 0===t&&(t=[]),new n.Observable((function(n){o.databaseInstance().then((function(a){new r.ExecutableBuilder(o.enableLog).execute([{query:e,params:t}],a).subscribe((function(e){n.next(e[0]),n.complete()}),(function(e){n.error(e),n.complete()}))})).catch((function(e){n.error(e),n.complete()}))}))},t.prototype.query=function(e,t){var o=this;return void 0===t&&(t=void 0),new n.Observable((function(n){o.databaseInstance().then((function(a){var i=o;n.next(new r.Query(e,{alias:t,getMapper:function(e){return i._mapper.get(e)},mapperTable:o._mapper.get(e).mapperTable,database:a,enableLog:o.enableLog})),n.complete()})).catch((function(e){n.error(e),n.complete()}))}))},t.prototype.ddl=function(){var e=this;return new n.Observable((function(t){e.databaseInstance().then((function(n){t.next(new r.Ddl({database:n,getMapper:e._mapper,enableLog:e.enableLog})),t.complete()})).catch((function(e){t.error(e),t.complete()}))}))},t}(g),w=c,x=function(){function e(){}return e.prototype.version=function(e,t){var r=this;return new n.Observable((function(n){r.checkTableVersion(e).subscribe((function(o){r.checkVersion(e,t).subscribe((function(t){r.migration(e,t).subscribe((function(e){n.next(e),n.complete()}),(function(e){return r.error(e,n)}))}),(function(e){return r.error(e,n)}))}),(function(e){return r.error(e,n)}))}))},e.prototype.error=function(e,t){console.error(e),t.error(e),t.complete()},e.prototype.checkTableVersion=function(e){var t=this;return new n.Observable((function(n){e.executeSql("CREATE TABLE IF NOT EXISTS MigrationVersion(\n id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT\n , data INTEGER\n , version TEXT\n );",{}).then((function(e){n.next(e),n.complete()}),(function(e){return t.error(e,n)}))}))},e.prototype.checkVersion=function(e,t){var r=this;return new n.Observable((function(n){r.getVersion(e).subscribe((function(o){o>0?t>o&&(console.info("Version old: "+o),e.executeSql("UPDATE MigrationVersion\n SET (data, version) = (?, ?);",[w().unix(),t]).then().catch((function(e){return r.error(e,n)}))):e.executeSql("INSERT INTO MigrationVersion (data, version) VALUES (?, ?)",[w().unix(),t]).then().catch((function(e){return r.error(e,n)})),n.next({oldVersion:o,newVersion:t}),n.complete()}))}))},e.prototype.getVersion=function(e){var t=this;return new n.Observable((function(n){e.executeSql("SELECT * FROM MigrationVersion",{}).then((function(e){var t=0;e.rows.length>0&&(t=Number.parseFloat(e.rows.item(0).version)),n.next(t),n.complete()})).catch((function(e){return t.error(e,n)}))}))},e.prototype.migration=function(e,t){var r=this;return new n.Observable((function(n){r.migrationExecute(e,t).subscribe((function(e){n.next(e),n.complete()}),(function(e){return r.error(e,n)}))}))},e}(),I=function(){},O=function(){},j=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return u(t,e),t.prototype.ready=function(){return Promise.resolve()},t}(O);j.decorators=[{type:t.Injectable}];var T=function(e){function t(t,n,r){var o=e.call(this)||this;return Number.isInteger(t)?o._model={version:t,databaseName:n,mapper:r}:o._model=t,o}return u(t,e),t.prototype.databaseName=function(e){return this._model.databaseName},t.prototype.version=function(e){return this._model.version},t.prototype.mapper=function(){return this._model.mapper},t.prototype.config=function(e,t){return Promise.resolve({sqliteLimitVariables:999})},t}(I),E=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return u(t,e),t}(r.DatabaseHelper);E.decorators=[{type:t.Injectable}];var M=function(){function e(){}return e.prototype.onStart=function(){},e.prototype.onFinish=function(){},e.prototype.onProgress=function(){},e}(),S=function(e){function t(t,n){var r=e.call(this)||this;return r._injector=t,r._databaseMigrationContract=n,r._settings=t.get(I),r}return u(t,e),t.prototype.reset=function(e){console.info("database reset");var t=[],n=this._settings.mapper(this._injector),o=new r.Ddl({database:e,getMapper:n,enableLog:!0});return n.forEachMapper((function(e,n){e.readOnly||(t.push(o.drop(e.newable).execute()),t.push(o.create(e.newable).execute()))})),r.forkJoinSafe(t)},t.prototype.migrationExecute=function(e,t){var r=this;return new n.Observable((function(n){var o=[];if(r._databaseMigrationContract){r._databaseMigrationContract.onStart();var a=r._databaseMigrationContract.to(t,e,r._settings.mapper(r._injector),r);a&&a.length>0&&(o=o.concat(a))}0===o.length&&t.oldVersion<1&&o.push(r.reset(e)),o.length>0&&r._databaseMigrationContract.onProgress(),r.callNested(o,0).subscribe((function(e){n.next(e),n.complete()}),(function(e){n.error(e),n.complete()}),(function(){r._databaseMigrationContract.onFinish()}))}))},t.prototype.callNested=function(e,t){var r=this;return new n.Observable((function(n){e.length>t?e[t].subscribe((function(o){r.callNested(e,++t).subscribe((function(e){n.next(!0),n.complete()}),(function(e){n.error(e),n.complete()}))})):(n.next(!0),n.complete())}))},t}(x);S.decorators=[{type:t.Injectable}],S.ctorParameters=function(){return[{type:t.Injector},{type:M,decorators:[{type:t.Optional},{type:t.Inject,args:[v]}]}]};var D=function(){function e(){}return e.prototype.create=function(e){var t=this;return console.log("Database Fake! \\o/"),new Promise((function(e,n){e({executeSql:function(e,n){return t.executeFake(e,n),new Promise((function(e,n){e(t.resultFake())}))},transaction:function(e){return new Promise((function(n,r){e({executeSql:function(e,n,r,o){t.executeFake(e,n),r(void 0,t.resultFake())}}),n(void 0)}))}})}))},e.prototype.executeFake=function(e,t){console.log("Fake - sql: "+e+", params: "+JSON.stringify(t))},e.prototype.resultFake=function(){return{rows:{length:20,item:function(e){return{index:e}}}}},e}();D.decorators=[{type:t.Injectable}];var N=function(e){function t(t,n,r,o,a,i,c){var s=e.call(this,a,r,o,r.mapper(o),c,n)||this;return s._isAvailable=t,s._databaseMigration=i,s}return u(t,e),t.prototype.migrationVersion=function(e,t){return this._isAvailable?this._databaseMigration.version(e,t):new n.Observable((function(e){e.next(!0),e.complete()}))},t.prototype.databaseName=function(){return this._databaseSettings.databaseName(this._injector)},t.prototype.version=function(){return this._databaseSettings.version(this._injector)},t.prototype.databaseNameFile=function(e){return void 0===e&&(e=this.databaseName()),this.addDatabaseNameExtension(this.cleanDatabaseName(e))},t}(_);N.decorators=[{type:t.Injectable}],N.ctorParameters=function(){return[{type:Boolean,decorators:[{type:t.Inject,args:[b]}]},{type:Boolean,decorators:[{type:t.Inject,args:[d]}]},{type:I},{type:t.Injector},{type:f},{type:S},{type:O,decorators:[{type:t.Inject,args:[y]}]}]};var A=function(e){function t(){return e.call(this,window)||this}return u(t,e),t}(r.WebSqlDatabaseAdapter);A.decorators=[{type:t.Injectable}],A.ctorParameters=function(){return[]};var C=function(){function e(e){if(e)throw new Error("IonicDatabaseBuilderModule is already loaded. Import it in the AppModule only")}return e.forSimple=function(t,n,r){return void 0===t&&(t=!1),void 0===n&&(n=!0),void 0===r&&(r=j),{ngModule:e,providers:[{provide:d,useValue:t},{provide:b,useValue:n},{provide:y,useClass:r}]}},e.forRoot=function(t,n,r,o,a,i){return void 0===o&&(o=j),void 0===a&&(a=!1),void 0===i&&(i=!0),{ngModule:e,providers:[{provide:I,useClass:t},{provide:h,useClass:n},{provide:v,useClass:r},{provide:d,useValue:a},{provide:y,useClass:o},{provide:b,useValue:i}]}},e}();C.decorators=[{type:t.NgModule,args:[{providers:[S,N,E,{provide:f,useClass:m},{provide:y,useClass:j}]}]}],C.ctorParameters=function(){return[{type:C,decorators:[{type:t.Optional},{type:t.SkipSelf}]}]},e.BuildableDatabaseManager=_,e.DATABASE_CREATOR=h,e.DATABASE_MIGRATION=v,e.Database=N,e.DatabaseFactoryContract=f,e.DatabaseFactoryDefault=m,e.DatabaseHelperService=E,e.DatabaseManager=g,e.DatabaseMigration=S,e.DatabaseMigrationBase=x,e.DatabaseMigrationContract=M,e.DatabaseMockService=D,e.DatabaseSettingsFactoryContract=I,e.DatabaseSettingsFactoryDefault=T,e.IS_AVAILABLE_DATABASE=b,e.IS_ENABLE_LOG=d,e.IonicDatabaseBuilderModule=C,e.PLATFORM_LOAD=y,e.PlatformLoad=O,e.PlatformLoadDefault=j,e.WebSqlDatabaseService=A,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=ionic-database-builder.umd.min.js.map