本地数据存储之 IndexedDB 实践
- - SegmentFault 最新的文章本地存储主要有以下几种:. 允许用户对数据进行增删改操作. 数据存储在本地,不依赖后端. 由于 WebSQL 在标准上还存在争议,而 localStorage 实现数据分页、查询比较复杂,最终考虑使用了 IndexedDB 来满足需求. IndexedDB 数据库的使用. ps: 实现细节部分待有时间进行补充完善~.
本地存储主要有以下几种:
由于 WebSQL 在标准上还存在争议,而 localStorage 实现数据分页、查询比较复杂,最终考虑使用了 IndexedDB 来满足需求。
var wxbDB = {
name : "wxb",
version: 1,
db : null
};
function initDB(dbObj) {
dbObj.version = dbObj.version || 1;
var request = indexedDB.open(dbObj.name, dbObj.version);
request.onerror = function (e) {
console.log(e.currentTarget.error.message);
};
request.onsuccess = function (e) {
dbObj.db = e.target.result;
};
request.onupgradeneeded = function (e) {
var thisDB = e.target.result;
if (!thisDB.objectStoreNames.contains("material")) {
var objStore = thisDB.createObjectStore("material", {keyPath: "id", autoIncrement: true});
objStore.createIndex("wxid", "wxid", {unique: true});
}
if (!thisDB.objectStoreNames.contains("account")) {
var objStore = thisDB.createObjectStore("account", {keyPath: "id", autoIncrement: true});
objStore.createIndex("wxid", "wxid", {unique: true});
objStore.createIndex("nickName", "nickName", {unique: false});
}
};
}
function closeDB(dbObj) {
dbObj.db.close();
}
function deleteDB(dbObj) {
indexedDB.deleteDatabase(dbObj.name);
}
function addData(dbObj, tableName, data, cb) {
var transaction = dbObj.db.transaction(tableName, 'readwrite');
transaction.oncomplete = function () {
console.log("transaction complete");
};
transaction.onerror = function (event) {
console.dir(event)
};
var objectStore = transaction.objectStore(tableName);
var request = objectStore.add(data);
request.onsuccess = function (e) {
if (cb) {
cb({
error: 0,
data : data
})
}
};
request.onerror = function (e) {
if (cb) {
cb({
error: 1
})
}
}
}
function deleteData(dbObj, tableName, id, cb) {
var transaction = dbObj.db.transaction(tableName, 'readwrite');
transaction.oncomplete = function () {
console.log("transaction complete");
};
transaction.onerror = function (event) {
console.dir(event)
};
var objectStore = transaction.objectStore(tableName);
var request = objectStore.delete(parseInt(id));
request.onsuccess = function (e) {
if (cb) {
cb({
error: 0,
data : parseInt(id)
})
}
};
request.onerror = function (e) {
if (cb) {
cb({
error: 1
})
}
}
}
function getDataAll(dbObj, tableName, cb) {
var transaction = dbObj.db.transaction(tableName, 'readonly');
transaction.oncomplete = function () {
console.log("transaction complete");
};
transaction.onerror = function (event) {
console.dir(event)
};
var objectStore = transaction.objectStore(tableName);
var rowData = [];
objectStore.openCursor(IDBKeyRange.lowerBound(0)).onsuccess = function (event) {
var cursor = event.target.result;
if (!cursor && cb) {
cb({
error: 0,
data : rowData
});
return;
}
rowData.push(cursor.value);
cursor.continue();
};
}
function getDataById(dbObj, tableName, id, cb) {
var transaction = dbObj.db.transaction(tableName, 'readwrite');
transaction.oncomplete = function () {
console.log("transaction complete");
};
transaction.onerror = function (event) {
console.dir(event)
};
var objectStore = transaction.objectStore(tableName);
var request = objectStore.get(id);
request.onsuccess = function (e) {
if (cb) {
cb({
error: 0,
data : e.target.result
})
}
};
request.onerror = function (e) {
if (cb) {
cb({
error: 1
})
}
}
}
function getDataBySearch(dbObj, tableName, keywords, cb) {
var transaction = dbObj.db.transaction(tableName, 'readwrite');
transaction.oncomplete = function () {
console.log("transaction complete");
};
transaction.onerror = function (event) {
console.dir(event)
};
var objectStore = transaction.objectStore(tableName);
var boundKeyRange = IDBKeyRange.only(keywords);
var rowData;
objectStore.index("nickName").openCursor(boundKeyRange).onsuccess = function (event) {
var cursor = event.target.result;
if (!cursor) {
if (cb) {
cb({
error: 0,
data : rowData
})
}
return;
}
rowData = cursor.value;
cursor.continue();
};
}
function getDataByPager(dbObj, tableName, start, end, cb) {
var transaction = dbObj.db.transaction(tableName, 'readwrite');
transaction.oncomplete = function () {
console.log("transaction complete");
};
transaction.onerror = function (event) {
console.dir(event)
};
var objectStore = transaction.objectStore(tableName);
var boundKeyRange = IDBKeyRange.bound(start, end, false, true);
var rowData = [];
objectStore.openCursor(boundKeyRange).onsuccess = function (event) {
var cursor = event.target.result;
if (!cursor && cb) {
cb({
error: 0,
data : rowData
});
return;
}
rowData.push(cursor.value);
cursor.continue();
};
}
function updateData(dbObj, tableName, id, updateData, cb) {
var transaction = dbObj.db.transaction(tableName, 'readwrite');
transaction.oncomplete = function () {
console.log("transaction complete");
};
transaction.onerror = function (event) {
console.dir(event)
};
var objectStore = transaction.objectStore(tableName);
var request = objectStore.get(id);
request.onsuccess = function (e) {
var thisDB = e.target.result;
for (key in updateData) {
thisDB[key] = updateData[key];
}
objectStore.put(thisDB);
if (cb) {
cb({
error: 0,
data : thisDB
})
}
};
request.onerror = function (e) {
if (cb) {
cb({
error: 1
})
}
}
}
ps: 实现细节部分待有时间进行补充完善~