UNPKG

ngx-dexie-observable

Version:
242 lines (241 loc) 8.51 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var dexie_database_1 = require("./dexie.database"); var DexieService = /** @class */ (function () { function DexieService(db) { this.db = db; } /** * Adds the entry ```object``` to the dexie table ```table```. * Returns a promise which when resolved gives the key of the object added, when rejected gives the error that occured. * * @param table table name * @param object object to add */ DexieService.prototype.addOne = function (table, object) { return rxjs_1.from(this.db.table(String(table)).add(object)); }; /** * Adds multiple ```objects``` to the dexie table ```table``` * Returns a promise which works similar to Dexie ```bulkAdd()``` * * @param table table name * @param objects objects to add */ DexieService.prototype.addMultiple = function (table, objects) { return rxjs_1.from(this.db.table(String(table)).bulkAdd(objects)); }; /** * Returns a promise which when resolved gives the number of objects in the table ```table``` * * @param table table name */ DexieService.prototype.count = function (table) { return rxjs_1.from(this.db.table(String(table)).count()); }; /** * Works similar to Dexie ```put()``` * * @param table table name * @param object * @param key */ DexieService.prototype.addOrUpdateOne = function (table, object, key) { if (key) { return rxjs_1.from(this.db.table(String(table)).put(object, key)); } else { return rxjs_1.from(this.db.table(String(table)).put(object)); } }; /** * Works similar to Dexie ```bulkPut()``` * * @param table table name * @param objects * @param keys */ DexieService.prototype.addOrUpdateMultiple = function (table, objects, keys) { if (keys) { return rxjs_1.from(this.db.table(String(table)).bulkPut(objects, keys)); } else { return rxjs_1.from(this.db.table(String(table)).bulkPut(objects)); } }; /** * Works similar to Dexie ```delete()``` * * @param table table name * @param primaryKey */ DexieService.prototype.deleteOne = function (table, primaryKey) { return rxjs_1.from(this.db.table(String(table)).delete(primaryKey)); }; /** * Works similar to Dexie ```bulkDelete()``` * * @param table table name * @param primaryKeys */ DexieService.prototype.deleteMultiple = function (table, primaryKeys) { return rxjs_1.from(this.db.table(String(table)).bulkDelete(primaryKeys)); }; /** * Works similar to Dexie ```clear()``` * * @param table table name */ DexieService.prototype.clearAll = function (table) { return rxjs_1.from(this.db.table(String(table)).clear()); }; // check /** * Works similar to Dexie ```each()``` * * @param table table name * @param callback */ DexieService.prototype.operateOnEach = function (table, callback) { return rxjs_1.from(this.db.table(String(table)).each(callback)); }; /** * Works similar to Dexie ```filter()``` * * @param table table name * @param filterFunction */ DexieService.prototype.filter = function (table, filterFunction) { return this.db.table(String(table)).filter(filterFunction); }; // check /** * Works similar to Dexie ```get()``` * * @param table table name * @param primaryKey * @param callback */ DexieService.prototype.getByPrimaryKey = function (table, primaryKey, callback) { if (callback) { return rxjs_1.from(this.db.table(String(table)).get(primaryKey, callback)); } else { return rxjs_1.from(this.db.table(String(table)).get(primaryKey)); } }; // check /** * Works similar to Dexie ```get()``` * * @param table table name * @param keyValueMap * @param callback */ DexieService.prototype.getByKeyToValueMap = function (table, keyValueMap, callback) { if (callback) { return rxjs_1.from(this.db.table(String(table)).get(keyValueMap, callback)); } else { return rxjs_1.from(this.db.table(String(table)).get(keyValueMap)); } }; /** * Works similar to Dexie ```limit()``` * * @param table table name * @param num */ DexieService.prototype.getFirstNItemsOfTable = function (table, num) { return this.db.table(String(table)).limit(num); }; /** * Works similar to Dexie ```orderBy()``` * * @param table table name * @param index */ DexieService.prototype.orderBy = function (table, index) { return this.db.table(String(table)).orderBy(index); }; /** * Works similar to Dexie ```offset()``` * * @param table table name * @param ignoreUpto */ DexieService.prototype.offset = function (table, ignoreUpto) { return this.db.table(String(table)).offset(ignoreUpto); }; /** * Works similar to Dexie ```reverse()``` * * @param table table name */ DexieService.prototype.reverse = function (table) { return this.db.table(String(table)).reverse(); }; DexieService.prototype.toArray = function (table, callback) { if (callback) { return rxjs_1.from(this.db.table(String(table)).toArray(callback)); } else { return rxjs_1.from(this.db.table(String(table)).toArray()); } }; /** * Works similar to Dexie ```toCollection()``` * * @param table table name */ DexieService.prototype.toCollection = function (table) { return this.db.table(String(table)).toCollection(); }; // check /** * Works similar to Dexie ```update()``` * * @param table table name * @param key * @param changes */ DexieService.prototype.update = function (table, key, changes) { return rxjs_1.from(this.db.table(String(table)).update(key, changes)); }; /** * List to database change events * * @param table table name or empty to listen to all tables */ DexieService.prototype.onChanges = function (table) { var _this = this; return rxjs_1.fromEventPattern(function (handler) { return _this.db.on('changes', handler); }).pipe(operators_1.map(function (data) { return data[0]; }), operators_1.map(function (changes) { return changes.filter(function (x) { return !table || x.table === table; }); })); }; /** * Return a observable that emits the current values on every data change * * @param table table name */ DexieService.prototype.valueChanges = function (table) { var _this = this; return rxjs_1.merge(rxjs_1.of(null), this.onChanges(table)).pipe(operators_1.mergeMap(function () { return _this.toArray(table); })); }; DexieService = __decorate([ core_1.Injectable(), __metadata("design:paramtypes", [dexie_database_1.DexieDatabase]) ], DexieService); return DexieService; }()); exports.DexieService = DexieService;