indexDB数据库怎么使用

发表时间:2019-11-04 03:33:34
摘要信息

indexDB数据库怎么使用,封装好的js

定义数据库

let myDB = {

name: "api_db",

version: 1,

db: null,

ojstore: {

name: "cart", //存储空间表的名字

keypath: "id" //主键

}

}


直接上完整代码

let myDB = {

name: "api_db",

version: 1,

db: null,

ojstore: {

name: "cart", //存储空间表的名字

keypath: "id" //主键

}

}

function openDB(myDB, callback) {

//建立或打开数据库,建立对象存储空间(ObjectStore)

var self = this;

var version = myDB.version || 1;

var request = self.indexedDB.open(myDB.name, version);

request.onerror = function (e) {

console.log(e.currentTarget.error.message);

};

request.onsuccess = function (e) {

myDB.db = e.target.result;

console.log('成功建立并打开数据库:' + myDB.name + ' version' + version);

callback();

};

request.onupgradeneeded = function (e) {

var db = e.target.result,

transaction = e.target.transaction,

store;

if (!db.objectStoreNames.contains(myDB.ojstore.name)) {

//没有该对象空间时创建该对象空间

store = db.createObjectStore(myDB.ojstore.name, {

keyPath: myDB.ojstore.keypath

});
store.createIndex('id', 'id', {unique: false})
store.createIndex('value', 'value', {unique: false})

console.log('成功建立对象存储空间:' + myDB.ojstore.name);

}

}

}

openDB(myDB, function (data) {
})
window.INDEXdb = {

indexedDB: window.indexedDB || window.webkitindexedDB,

IDBKeyRange: window.IDBKeyRange || window.webkitIDBKeyRange, //键范围

openDB: function (myDB, callback) {
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;

if (indexedDB) {

} else {
callback(false);
}

//建立或打开数据库,建立对象存储空间(ObjectStore)

var self = this;

var version = myDB.version || 1;

var request = self.indexedDB.open(myDB.name, version);

request.onerror = function (e) {

console.log(e.currentTarget.error.message);

};

request.onsuccess = function (e) {

myDB.db = e.target.result;

console.log('成功建立并打开数据库:' + myDB.name + ' version' + version);

callback();

};

request.onupgradeneeded = function (e) {

var db = e.target.result,

transaction = e.target.transaction,

store;

if (!db.objectStoreNames.contains(myDB.ojstore.name)) {

//没有该对象空间时创建该对象空间

store = db.createObjectStore(myDB.ojstore.name, {

keyPath: myDB.ojstore.keypath

});
store.createIndex('id', 'id', {unique: false})
store.createIndex('value', 'value', {unique: false})
console.log('成功建立对象存储空间:' + myDB.ojstore.name);

}

}

},

deletedb: function (dbname) {

//删除数据库

var self = this;

self.indexedDB.deleteDatabase(dbname);

console.log(dbname + '数据库已删除')

},

closeDB: function () {

//关闭数据库

myDB.db.close();

console.log('数据库已关闭')

},

addData: function (data, path) {
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;

if (indexedDB) {

} else {
callback(false);
}

var self = this;

var store = myDB.db.transaction([myDB.ojstore.name], 'readwrite').objectStore(myDB.ojstore.name)

var request = store.add({id: path, value: JSON.stringify(data)});

request.onerror = function () {

console.log('已经有此数据')

self.putData({id: path, value: JSON.stringify(data)});


};

request.onsuccess = function () {

console.log('存储完成')

};

},

putData: function (data) {
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;

if (indexedDB) {

} else {
callback(false);
}

//添加数据,重复添加会更新原有数据
var modData = data

this.getDataByKey(modData.id, function (res) {

res.totalCount += modData.totalCount;

var store = myDB.db.transaction(myDB.ojstore.name, 'readwrite').objectStore(myDB.ojstore.name),

request;
request = store.put(data);

request.onsuccess = function () {
console.log('数据已经更新')
};

})

},

getAllData(callback) {

//获取所有数据
var store = myDB.db.transaction(myDB.ojstore.name, 'readwrite').objectStore(myDB.ojstore.name);

var allRecords = store.getAll();

allRecords.onsuccess = function () {

console.log(allRecords.result);

if (typeof (callback) === 'function') {

callback(allRecords.result);

}

};

},

getDataByKey: function (key, callback) {
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;

if (indexedDB) {

} else {
callback(false);
}
//根据存储空间的键找到对应数据
var store = myDB.db.transaction(myDB.ojstore.name, 'readwrite').objectStore(myDB.ojstore.name);

var request = store.get(key);
request.onerror = function () {
callback(false);

};

request.onsuccess = function (e) {

var result = e.target.result;
if (typeof (callback) === 'function') {
if (result != undefined || result != null) {
callback(JSON.parse(result.value));
} else {
callback(false);
}


}

};

},

deleteData: function (key) {

//删除某一条记录

var store = store = myDB.db.transaction(myDB.ojstore.name, 'readwrite').objectStore(myDB.ojstore.name);

store.delete(key)

console.log('已删除存储空间' + myDB.ojstore.name + '中' + key + '记录');

},

clearData: function () {

//删除存储空间全部记录

var store = myDB.db.transaction(myDB.ojstore.name, 'readwrite').objectStore(myDB.ojstore.name);

store.clear();

console.log('已删除存储空间' + myDB.ojstore.name + '全部记录');

}

}


40% Complete (success)