I am using YDN DB for IndexedDB and I want to delete a record from the object store using its id. Here is my schema:
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);
Now, I have function that will delete the record:
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);
}
This function gives me this error:
Uncaught ydn.error.ArgumentException: Second argument must be cursor range iterator.
Thank you for responses.
It should be:
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')
});
}
But since id
is primary key, you don't need to be indexed, it should simply be:
function deleteEntry(id){
var id = parseInt(id, 10);
var keys = db.remove("persons", id);
}
If id is not primary key, this following code run a bit more efficient, since it does not retrieve record values, unnecessarily.
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);
};
}
I have corrected the api doc example.