UNPKG

@browser-storage/websql-driver

Version:
219 lines (173 loc) 5.26 kB
import { Defer } from '@browser-storage/core'; function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } class WebsqlSerializer { deserialize(value) { return _asyncToGenerator(function* () { return JSON.parse(value); })(); } serialize(value) { return _asyncToGenerator(function* () { return JSON.stringify(value); })(); } } class WebsqlDriver { constructor(_ref) { let description = _ref.description, size = _ref.size; this._serializer = new WebsqlSerializer(); this._ready = new Defer(); this.description = description; this.size = size; } get isSupported() { return typeof window.openDatabase === 'function'; } clear() { var _this = this; return _asyncToGenerator(function* () { yield _this._executeSql("DELETE FROM ".concat(_this.options.storeName)); })(); } // tslint:disable-next-line destroy() { return _asyncToGenerator(function* () {})(); } getItem(key) { var _this2 = this; return _asyncToGenerator(function* () { const result = yield _this2._executeSql("SELECT * FROM ".concat(_this2.options.storeName, " WHERE key = ? LIMIT 1"), [key]); return _this2._serializer.deserialize(result.rows.item(0).value); })(); } hasItem(key) { var _this3 = this; return _asyncToGenerator(function* () { return (yield _this3.keys()).indexOf(key) > -1; })(); } init(dbOptions) { var _this4 = this; return _asyncToGenerator(function* () { _this4.options = dbOptions; _this4.db = _this4._createDB(); yield _this4._executeSql("CREATE TABLE IF NOT EXISTS ".concat(_this4.options.storeName, " (id INTEGER PRIMARY KEY, key unique, value)")); _this4._ready.resolve(true); })(); } iterate(iterator) { var _this5 = this; return _asyncToGenerator(function* () { const keys = yield _this5.keys(); return keys.reduceRight( /*#__PURE__*/ function () { var _ref2 = _asyncToGenerator(function* (prev, key, index) { yield prev; const value = yield _this5.getItem(key); yield iterator(key, value, index); }); return function (_x, _x2, _x3) { return _ref2.apply(this, arguments); }; }(), Promise.resolve()); })(); } key(index) { var _this6 = this; return _asyncToGenerator(function* () { return (yield _this6.keys())[index]; })(); } keys() { var _this7 = this; return _asyncToGenerator(function* () { const result = yield _this7._executeSql("SELECT key FROM ".concat(_this7.options.storeName, " as a order by id")); const keys = []; for (let i = 0; i < result.rows.length; i++) { keys.push(result.rows.item(i).key); } return keys; })(); } length() { var _this8 = this; return _asyncToGenerator(function* () { const result = yield _this8._executeSql("SELECT COUNT(key) as count FROM ".concat(_this8.options.storeName)); return result.rows.item(0).count; })(); } ready() { return this._ready.promise; } removeItem(key) { var _this9 = this; return _asyncToGenerator(function* () { yield _this9._executeSql("DELETE FROM ".concat(_this9.options.storeName, " WHERE key = ?"), [key]); })(); } setItem(key, item) { var _this10 = this; return _asyncToGenerator(function* () { yield _this10._executeSql("INSERT OR REPLACE INTO ".concat(_this10.options.storeName, " (key, value) VALUES (?, ?)"), [key, yield _this10._serializer.serialize(item)]); return item; })(); } _transaction() { var _this11 = this; return _asyncToGenerator(function* () { return new Promise((resolve, reject) => { _this11.db.transaction(tx => { resolve(tx); }, reject); }); })(); } _executeSql(sql) { var _this12 = this, _arguments = arguments; return _asyncToGenerator(function* () { let args = _arguments.length > 1 && _arguments[1] !== undefined ? _arguments[1] : []; const tx = yield _this12._transaction(); return new Promise((resolve, reject) => { tx.executeSql(sql, args, (t, result) => resolve(result), (t, error) => { reject(error); return true; }); }); })(); } _createDB() { return window.openDatabase(this.options.name, this.options.version.toString(), this.description || '', this.size); } } export { WebsqlDriver, WebsqlSerializer };