我正在使用YDN DB for IndexedDB,我想使用其id从对象存储中删除记录。 这是我的架构:
var personsSchema = {
name: "persons",
keyPath: "id",
autoIncrement: true,
indexes: [{
name: "id",
unique: true
}, {
name: "firstname",
unique: false,
multiEntry: false
}, {
name: "lastname",
unique: false,
multiEntry: false
}]
};
schema = {
stores: [personsSchema]
};
var db = new ydn.db.Storage("xdb", schema);
现在,我有删除记录的功能:
function deleteEntry(id){
var id = parseInt(id);
var objectStore = "persons";
var iterator = new ydn.db.ValueCursors(objectStore, "id", ydn.db.KeyRange.only(id));
var mode = "readwrite";
request = db.open(iterator, 1).then(function(cursor){
cursor.clear();
}, mode);
}
这个函数给了我这个错误:
Uncaught ydn.error.ArgumentException: Second argument must be cursor range iterator.
感谢您的回复。
答案 0 :(得分:2)
应该是:
function deleteEntry(id){
var id = parseInt(id);
var objectStore = "persons";
var iterator = new ydn.db.IndexValueCursors(objectStore, "id", ydn.db.KeyRange.only(id));
var mode = "readwrite";
request = db.open(function(cursor){
cursor.clear();
}, iterator, mode).then(function() {
console.log('cleared')
});
}
但由于id
是主键,因此您无需编入索引,它应该只是:
function deleteEntry(id){
var id = parseInt(id, 10);
var keys = db.remove("persons", id);
}
如果id不是主键,则以下代码运行效率更高,因为它不会不必要地检索记录值。
function deleteEntry(id){
var id = parseInt(id);
var objectStore = "persons";
db.keys(objectStore, "id", ydn.db.KeyRange.only(id)).done(function(keys) {
db.remove(objectStore, keys);
};
}
我更正了api doc example。