UNPKG

@tanshenghu/web-utils

Version:

web公共方法

189 lines (162 loc) 5.25 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _common = require('../common'); var _common2 = _interopRequireDefault(_common); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * 客户端indexDB 操作 * @param {string} dbName - 数据库名称 * @param {string|array} createTable - 数据库里面的表名称 * @param {number} version - 数据库版本号 */ function database(dbName, createTable) { var version = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1; var G = globalThis; var ODB = G.indexedDB || G.webkitIndexedDB || G.mozIndexedDB || G.msIndexedDB; if (!ODB) { console.error('您的浏览器设备不支持indexDB,请更换或升级浏览器'); return false; } if (_common2.default.isType(createTable, 'undefined')) { createTable = dbName; } this.IDB = null; var readType = 'readwrite'; var getStore = function getStore(IDB, tableName) { var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'readonly'; var transaction = IDB.transaction(tableName, type); return transaction.objectStore(tableName); }; var lotdeal = function lotdeal(data, store, fn) { if (_common2.default.isType(data, 'object')) { data = [data]; } if (_common2.default.isType(data, 'array')) { data.forEach(function (idata) { fn.call(store, idata); }); } }; this.connction = function () { var _this = this; return new Promise(function (resolve, reject) { _this.dbConnect = null; _this.dbConnect = ODB.open(dbName, version); /** * 数据库初始或版本号升级回调 */ _this.dbConnect.onupgradeneeded = function (e) { // keyGenerator, keyPath, autoIncrement _this.IDB = e.target.result; if (_common2.default.isType(createTable, 'string')) { createTable = [createTable]; } createTable.forEach(function (item) { var tname = item.tableName || item; if (!_this.IDB.objectStoreNames.contains(tname)) { var COS = _this.IDB.createObjectStore(tname, item.conf || { autoIncrement: true }); // objectStore.createIndex('name', 'name', { unique: false }); if (_common2.default.isType(_this.getCreateObjectStore, 'function')) { _this.getCreateObjectStore(COS); } } }); }; _this.dbConnect.onsuccess = function (e) { _this.IDB = e.target.result; resolve(_this.IDB); }; _this.dbConnect.onerror = function (e) { console.warn('数据库连接失败'); reject(e.currentTarget.error.message); }; }); }; /** * 数据库插入语句 * @param {string} tableName - 数据库-表名 * @param {any} data - 存入表中的数据 */ this.insert = function (tableName, data) { var store = getStore(this.IDB, tableName, readType); lotdeal(data, store, store.add); return this.IDB; }; /** * 数据库查询语句 * @param {string} tableName - 数据库-表名 * @param {any} column - 主键 * @param {function} cb - 回调函数 */ this.select = function (tableName, column) { var _this2 = this; var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () { return null; }; return new Promise(function (resolve, reject) { var store = getStore(_this2.IDB, tableName); var query = column === '*' ? store.getAll() : store.get(column); query.onsuccess = function (e) { cb(e.target.result, this.IDB); resolve(e.target.result); }; query.onerror = function (e) { reject(e.currentTarget.error.message || '数据查询失败'); }; }); }; /** * 数据库更新语句 * @param {string} tableName - 数据库-表名 * @param {any} data - 表中需要更新的数据 */ this.update = function (tableName, data) { var store = getStore(this.IDB, tableName, readType); lotdeal(data, store, store.put); return this.IDB; }; /** * 数据库删除语句 * @param {string} tableName - 数据库-表名 * @param {any} column - 数据库中的主键 */ this.delete = function (tableName, column) { var store = getStore(this.IDB, tableName, readType); lotdeal(column, store, store.delete); return this.IDB; }; /** * 删除数据库 * @param {string} tableName - 数据库-表名 */ this.truncate = function (tableName) { var store = getStore(this.IDB, tableName, readType); store.clear(); }; /** * 清空数据表 * @param {string} tableName - 数据库-表名 */ this.dropTable = function (tableName) { if (this.IDB.objectStoreNames.contains(tableName)) { this.IDB.deleteObjectStore(tableName); } }; /** * 删除数据库 * @param {string} dbname - 数据库名称 */ this.dropDatabase = function (dbname) { ODB.deleteDatabase(dbname); }; /** * 关闭数据库 */ this.close = function () { this.IDB.close(); }; } exports.default = database; module.exports = exports['default'];