UNPKG

tsbase

Version:

Base class libraries for TypeScript

216 lines 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IndexedDb = void 0; var tslib_1 = require("tslib"); var Until_1 = require("../../Utility/Timers/Until"); var module_1 = require("../../Patterns/CommandQuery/module"); var TransactionModes_1 = require("./TransactionModes"); var IndexedDb = /** @class */ (function () { function IndexedDb(Name, Version, migrations, indexedDbFactory) { this.Name = Name; this.Version = Version; this.migrations = migrations; this.indexedDbFactory = indexedDbFactory; this.database = null; } IndexedDb.Instance = function (name, version, migrations, indexedDbFactory) { if (indexedDbFactory === void 0) { indexedDbFactory = indexedDB; } return new IndexedDb(name, version, migrations, indexedDbFactory); }; Object.defineProperty(IndexedDb.prototype, "Connected", { get: function () { return !!this.database; }, enumerable: false, configurable: true }); ; IndexedDb.prototype.Connect = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { return [2 /*return*/, new module_1.AsyncQuery(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var openRequest; var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!this.Connected) return [3 /*break*/, 1]; return [2 /*return*/, this.database]; case 1: openRequest = this.indexedDbFactory.open(this.Name, this.Version); this.setOpenRequestHandlers(openRequest, this.migrations); return [4 /*yield*/, (0, Until_1.Until)(function () { return _this.Connected; })]; case 2: _a.sent(); return [2 /*return*/, this.database]; } }); }); }).Execute()]; }); }); }; IndexedDb.prototype.Insert = function (insertions) { var storeNames = Object.keys(insertions); return this.executeTransaction(storeNames, function (transaction) { storeNames.forEach(function (storeName) { var store = transaction.objectStore(storeName); var objects = insertions[storeName]; objects.forEach(function (object) { store.add(object); }); }); }); }; IndexedDb.prototype.Get = function (storeName, query) { var _this = this; return new module_1.AsyncQuery(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var complete, result, transaction, store, request_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.Connect()]; case 1: _a.sent(); complete = false; result = null; transaction = this.database.transaction([storeName], TransactionModes_1.TransactionMode.ReadOnly); store = transaction.objectStore(storeName); if (typeof query === 'string' || typeof query === 'number') { request_1 = store.get(query); request_1.onsuccess = function () { result = request_1.result || null; complete = true; }; } else { store.openCursor().onsuccess = function (event) { result = result ? result : new Array(); var cursor = event.target.result; if (cursor && query(cursor['value'])) { result.push(cursor.value); cursor.continue(); } else if (cursor) { cursor.continue(); } else { complete = true; } }; } return [4 /*yield*/, (0, Until_1.Until)(function () { return complete; })]; case 2: _a.sent(); return [2 /*return*/, result]; } }); }); }).Execute(); }; IndexedDb.prototype.GetAll = function (storeName) { var _this = this; return new module_1.AsyncQuery(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var complete, result, request; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.Connect()]; case 1: _a.sent(); complete = false; result = new Array(); request = this.database.transaction([storeName], TransactionModes_1.TransactionMode.ReadOnly) .objectStore(storeName) .getAll(); request.onsuccess = function (event) { result = event.target.result; complete = true; }; return [4 /*yield*/, (0, Until_1.Until)(function () { return complete; })]; case 2: _a.sent(); return [2 /*return*/, result]; } }); }); }).Execute(); }; IndexedDb.prototype.Delete = function (deletions) { var storeNames = Object.keys(deletions); return this.executeTransaction(storeNames, function (transaction) { storeNames.forEach(function (storeName) { var store = transaction.objectStore(storeName); var keys = deletions[storeName]; keys.forEach(function (key) { store.delete(key); }); }); }); }; IndexedDb.prototype.Update = function (updates) { var storeNames = Object.keys(updates); return this.executeTransaction(storeNames, function (transaction) { storeNames.forEach(function (storeName) { var store = transaction.objectStore(storeName); var objects = updates[storeName]; objects.forEach(function (object) { store.put(object); }); }); }); }; IndexedDb.prototype.Disconnect = function () { var _a; (_a = this.database) === null || _a === void 0 ? void 0 : _a.close(); this.database = null; }; IndexedDb.prototype.setOpenRequestHandlers = function (openRequest, migrations) { var _this = this; openRequest.onupgradeneeded = function (e) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var db, migrations_1, migrations_1_1, migration; var e_1, _a; return tslib_1.__generator(this, function (_b) { db = openRequest.result; try { for (migrations_1 = tslib_1.__values(migrations), migrations_1_1 = migrations_1.next(); !migrations_1_1.done; migrations_1_1 = migrations_1.next()) { migration = migrations_1_1.value; if (e.oldVersion < migration.version && this.Version >= migration.version) { migration.command(db); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (migrations_1_1 && !migrations_1_1.done && (_a = migrations_1.return)) _a.call(migrations_1); } finally { if (e_1) throw e_1.error; } } return [2 /*return*/]; }); }); }; openRequest.onsuccess = function () { return _this.database = openRequest.result; }; }; IndexedDb.prototype.executeTransaction = function (storeNames, command) { var _this = this; return new module_1.AsyncCommand(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var complete, transaction; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.Connect()]; case 1: _a.sent(); complete = false; transaction = this.database.transaction(storeNames, TransactionModes_1.TransactionMode.ReadWrite); command(transaction); transaction.oncomplete = function () { return complete = true; }; return [4 /*yield*/, (0, Until_1.Until)(function () { return complete; })]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }).Execute(); }; return IndexedDb; }()); exports.IndexedDb = IndexedDb; //# sourceMappingURL=IndexedDb.js.map