<pre>
var DB = {
scheme: {
//timetag: {key: {keyPath: "name"}},
//blacklist: {key: {keyPath: "account"}},
//mutelist: {key: {keyPath: "account"}},
//friend: {key: {keyPath: "account"}},
//user: {key: {keyPath: "account"}},
//team: {key: {keyPath: "teamId"}},
//teamMember: {key: {keyPath: "id"}, indexes: {teamId: {unique: !1}}},
//msg: {
// key: {autoIncrement: !0},
// indexes: {
// idClient: {unique: !0},
// sessionId: {unique: !1},
// time: {unique: !1},
// type: {unique: !1},
// sessionType: {unique: !1}
// }
//},
//sysMsg: {
// key: {autoIncrement: !0},
// indexes: {idServer: {unique: !0}, category: {unique: !1}, type: {unique: !1}}
//},
//sysMsgUnread: {key: {keyPath: "type"}},
settings: {
key: {keyPath: 'name'}
},
CustomMsg: {
key: {keyPath: "time"}
}
},
init: function (table_name) {
var that = this
that.db_name = 'test-mydb'
that.db_version = 1
that.db = false;
that.table_name = table_name || 'CustomMsg'
that.request = that.request || indexedDB.open(that.db_name, that.db_version);
that.tables = Object.keys(that.scheme)
that.request.onupgradeneeded = function (e) {
console.log("db Upgrading...");
var thisDB = e.target.result;
for (var r in that.scheme) {
var table, setting = that.scheme[r];
table = thisDB.objectStoreNames.contains(r) ? e.currentTarget.transaction.objectStore(r) : thisDB.createObjectStore(r, setting.key);
for (var a in setting.indexes) {
var s = setting.indexes[a];
try {
table.index(a)
} catch (e) {
table.createIndex(a, s.key || a, Object.keys(s).length ? s : {unique: !1})
}
}
}
}
that.request.onsuccess = function (e) {
console.log("db Success!");
that.db = e.target.result;
}
that.request.onerror = function (e) {
console.log("Error");
console.dir(e);
}
return that
},
insert: function (data, callback) {
var request = this.db.transaction([this.table_name], "readwrite").objectStore(this.table_name).add(data);
request.onerror = function (e) {
console.log("Error", e.target.error.name);
}
request.onsuccess = function (e) {
console.log('insert ok')
if (callback != null)
callback.call()
}
},
update: function (data, callback) {
var request = this.db.transaction([this.table_name], "readwrite").objectStore(this.table_name).put(data);
request.onerror = function (e) {
console.log("Error", e.target.error.name);
}
request.onsuccess = function (e) {
if (callback != null)
callback.call()
}
},
read: function (key, callback) {
var request = this.db.transaction([this.table_name], "readonly").objectStore(this.table_name).get(key);
request.onerror = function (e) {
console.log("Error", e.target.error.name);
}
request.onsuccess = function (e) {
if (callback != null)
callback.call()
}
},
read_all: function (callback) {
var cursor = this.db.transaction([this.table_name], "readonly").objectStore(this.table_name).openCursor(),
result = [];
cursor.onsuccess = function (e) {
var res = e.target.result
if (res) {
result.push(res.value);
res.continue();
} else {
if (callback != null)
callback.call(this, result)
}
}
},
delete: function (key, callback) {
var request = this.db.transaction([this.table_name], "readwrite").objectStore(this.table_name).delete(key)
request.onerror = function (e) {
console.log("Error", e.target.error.name);
}
request.onsuccess = function (e) {
if (callback != null)
callback.call()
}
}
}</pre>
我们可以给这段代码复制到浏览器的console里,然后试用一下:
<pre>
//初始化数据
var CustomMsg = DB.init('CustomMsg')
//插入数据
CustomMsg.insert({time:(new Date().getTime()),from:'zhang',to:'wang',content:'你好'})
CustomMsg.insert({time:(new Date().getTime()),from:'wang',to:'zhang',content:'你也好啊'})
//读数据
CustomMsg.read_all(function(result){console.log(result)})
//删除
CustomMsg.delete(1472780909969)
</pre>
使用indexDb有一些需要注意的点:
- 必须增大db_version才能增加表(本篇默认version 1,生成两张表)
- 指定主键以后,update的功效更insert一样了
- 异步,indexdb的东西基本都是异步操作,数据都是在回调里!也就是说刚insert和read同时写上去,read是看不到数据的
- 比较复杂的索引与查询,请参考 http://www.cnblogs.com/dolphinX/p/3416889.html