在之前,我写了一个websql的封装类库,代码如下:
View Code
上述代码存在的问题非常明显,由于websql操作都是异步操作,当我们为了获取到websql操作的结果之后再进行后续操作时,往往是通过回调函数来实现的,当回调一多的时候,回调地狱就出现了,为了解决回调地狱问题,我将通过Promise来改写,后续调用时,可以直接通过await和async来调用,或者直接通过Promise链式调用也是可以的。
现在我将通过ES6的语法重写之前的封装类,为了应用ES6中js面向对象的思想,我这里用到了class,最终代码如下:
import { browserVersions } from '@/utils/utils.js'; class SmpWebSql { constructor(options) { options = options || {}; this.database = null; this.DateBaseName = options.DateBaseName || 'RedDB'; this.Version = options.Version || '1.0'; this.Description = options.Description || '智维离线工单数据库'; this.DataBaseSize = options.DataBaseSize || 2 * 1024 * 1024; this.init(); } /** * 初始化数据库 */ init() { this.database = openDatabase( this.DateBaseName, this.Version, this.Description, this.DataBaseSize ); } /** * 批量添加字段 * @param {*} tableName 表名 * @param {*} arr 更新的数据 [{key1:value1 , key2 : value2 ...},{key1:value1 , key2 : value2 ...}] * @param {*} index BLOG字段所在的索引位置 * @param {*} isFirst 是否是第一次创建表 */ addBlob(tableName, arr, index, isFirst = true) { var _db = this.database; var _me = this; // eslint-disable-next-line promise/param-names return new Promise(function(resovle, reject) { if (arr == null) { return this; } var keyC = []; var keyI = []; var _key = ''; arr = arr || []; if (arr && arr.constructor == Array) { for (var i in arr[0]) { keyC.push(i); keyI.push(i); } keyC[0] = keyC[0] + ' unique'; _key = keyI.join(','); index = index == undefined ? 0 : index; keyC[index] = keyC[index] + ' BLOB'; // eslint-disable-next-line promise/param-names _db.transaction(function(tx, result) { // var csql = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(",") + ')'; // console.log('csql:' + csql); if (isFirst == true) { tx.executeSql( 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(',') + ')' ); } // var sql = ""; for (var s = 0, _len = arr.length; s < _len; s++) { var _value = _me.split(arr[s]); // sql += 'INSERT INTO ' + tableName + ' (' + _key + ') VALUES (' + _value + ')'; // console.log("sql:" + sql); tx.executeSql( 'INSERT INTO ' + tableName + ' (' + _key + ') VALUES (' + _value + ')', [], function(tx, result) { resovle(result.rowsAffected); console.log('添加成功' + result.rowsAffected); }, function(tx) { console.error('添加失败'); // eslint-disable-next-line prefer-promise-reject-errors reject(false); } ); } _key = keyI = keyC = null; resovle(arr.length); }); } }).catch(error => { console.log('error :', error); }); } /** * 批量添加字段 * @param {*} tableName 表名 * @param {*} arr 更新的数据 [{key1:value1 , key2 : value2 ...},{key1:value1 , key2 : value2 ...}] * @param {*} index BLOG字段所在的索引位置 * @param {*} firstKey firstKey 第一个字段是否是主键(默认是) * @param {*} isFirst 是否是第一次创建表 */ patchAddBlob(tableName, arr, index, firstKey = true, isFirst = true) { var _db = this.database; var _me = this; // eslint-disable-next-line promise/param-names return new Promise(function(resovle, reject) { if (arr == null) { return this; } var keyC = []; var keyI = []; var _key = ''; arr = arr || []; if (arr && arr.constructor == Array) { for (var i in arr[0]) { keyC.push(i); keyI.push(i); } if (firstKey) { keyC[0] = keyC[0] + ' unique'; } _key = keyI.join(','); index = index == undefined ? 0 : index; keyC[index] = keyC[index] + ' text'; // eslint-disable-next-line promise/param-names _db.transaction(function(tx, result) { // var csql = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(",") + ')'; // console.log('csql:' + csql); if (isFirst == true) { tx.executeSql( 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(',') + ')' ); } var sql = ''; var _values = []; for (var s = 0, _len = arr.length; s < _len; s++) { _values.push('(' + _me.split(arr[s]) + ')'); } sql = 'INSERT INTO ' + tableName + ' (' + _key + ') VALUES ' + _values.join(','); // console.log('sql:' + sql); tx.executeSql( sql, [], function(tx, result) { resovle(result.rowsAffected); // console.log('添加成功'+result.rowsAffected); }, function(tx, error) { console.error('添加失败', tx, error); // eslint-disable-next-line prefer-promise-reject-errors reject(false); } ); _key = keyI = keyC = null; resovle(arr.length); }); } }).catch(error => { console.log('error :', error); }); } /** * 批量添加字段 注 : 数据里面的第一个key 为主键 * @param {*} tableName 表名 * @param {*} arr arr 更新的数据 [{key1:value1 , key2 : value2 ...},{key1:value1 , key2 : value2 ...}] * @param {*} firstKey firstKey 第一个字段是否是主键(默认是) */ add(tableName, arr, firstKey = true) { var _me = this; var _db = this.database; // eslint-disable-next-line promise/param-names return new Promise(function(resovle, reject) { if (arr == null) { return this; } var keyC = []; var keyI = []; var _key = ''; arr = arr || []; if (arr && arr.constructor == Array) { for (var i in arr[0]) { keyC.push(i); keyI.push(i); } if (firstKey) { keyC[0] = keyC[0] + ' unique'; } _key = keyI.join(','); _db.transaction(function(tx) { // /var csql = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(",") + ')'; // console.log('csql:' + csql); tx.executeSql( 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(',') + ')' ); // var sql = ""; for (var s = 0, _len = arr.length; s < _len; s++) { var _value = _me.split(arr[s]); // sql += 'INSERT INTO ' + tableName + ' (' + _key + ') VALUES (' + _value + ')'; // console.log("sql:" + sql); tx.executeSql( 'INSERT INTO ' + tableName + ' (' + _key + ') VALUES (' + _value + ')', [], function(tx, result) { resovle(result.rowsAffected); // console.log('添加成功'+result.rowsAffected); }, function(tx, error) { console.error('添加失败', error); // eslint-disable-next-line prefer-promise-reject-errors reject(false); } ); } _key = keyI = keyC = null; resovle(arr.length); }); } }).catch(error => { console.log('error :', error); }); } /** * 批量添加行记录 注 : 数据里面的第一个key 为主键 * @param {*} tableName 表名 * @param {*} arr arr 更新的数据 [{key1:value1 , key2 : value2 ...},{key1:value1 , key2 : value2 ...}] * @param {*} firstKey firstKey 第一个字段是否是主键(默认是) */ patchAdd(tableName, arr, firstKey = true) { var _me = this; var _db = this.database; // eslint-disable-next-line promise/param-names return new Promise(function(resovle, reject) { if (arr == null) { return this; } var keyC = []; var keyI = []; var _key = ''; arr = arr || []; if (arr && arr.constructor == Array) { for (var i in arr[0]) { keyC.push(i); keyI.push(i); } if (firstKey) { keyC[0] = keyC[0] + ' unique'; } _key = keyI.join(','); _db.transaction(function(tx) { // /var csql = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(",") + ')'; // console.log('csql:' + csql); tx.executeSql( 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(',') + ')' ); var sql = ''; var _values = []; for (var s = 0, _len = arr.length; s < _len; s++) { _values.push('(' + _me.split(arr[s]) + ')'); } sql = 'INSERT INTO ' + tableName + ' (' + _key + ') VALUES ' + _values.join(','); console.log('sql:' + sql); tx.executeSql( sql, [], function(tx, result) { resovle(result.rowsAffected); // console.log('添加成功'+result.rowsAffected); }, function(tx, error) { console.error('添加失败'); // eslint-disable-next-line prefer-promise-reject-errors reject(false); } ); _key = keyI = keyC = null; resovle(arr.length); }); } }).catch(error => { console.log('error :', error); }); }