changing records keys
mderazon opened this issue · 2 comments
I want to be able to update a record key. I could do db.rm(key)
and then db.set(new_key, val)
. The problem is that I lose the order on the elements when I do forEach()
(I assume order is not guaranteed but so far it has has been kind for me and the order is not super critical in my case)
I tried doing the following
var key = 'old'
var new_key = 'new'
var val = 'new val'
db.forEach(function(k, v) {
if (k === key) {
console.log('found key', k);
k = new_key;
v = val;
}
});
It doesn't work. Should it work ? is there any other way to make this work ?
👍
I don't think this should work because you are not calling db.set with the new values. If you want things in ordered either create another dirty db with numeric keys (like an index) or add a sortable field to each entry and then sort them whenever you want them in order (e.g., date stamp or alphabetical on some field).
Simple Answer: No, it should not work.
If you want to achieve ordering, I suggest adding an 'idx' property to each element and then sorting your records when needed.
I wonder if this is a common use-case. If it is, it might be worth adding and .index() function that tells the db to keep an index of certain properties.