I am working on a simple application to build a tree out of nodes. I have the following hooks setup so that when a node is deleted from a tree, all of the child nodes are deleted and also all associations for those nodes are deleted. I am using the following code to try and create this behavior:
// delete node hook
db.nodes.hook('deleting', function(key, record, transaction){
// after deleting a node, go delete its associations
this.onsuccess = function(){
console.log(transaction);
transaction.links.where('node').equals(record.id).delete();
};
});
// delete link hook
db.links.hook('deleting', function(key, record, transaction){
// start delete cascade to prevent dead entries
this.onsuccess = function(){
transaction.nodes.where('id').equals(record.child).delete();
};
});
And that is giving me this error "Unhandled rejection: NotFoundError: Tablelinks not part of transaction". I logged the transaction object to check on that and also looked into the source code for where that error might be coming from, and it looks like it is storeNames. My database is structured like this:
db.version(1).stores({
nodes : '++id, title, content',
links : '&[node+child], node, child'
});
but I only see "nodes" when I look into storeNames. I am using Dexie version 2 beta 10 - and that could very well be the problem, but I'm not sure because this is my first indexeddb project and dexie was recommended to me. I went with the beta because of all of the guides using the syntax for 2.x stuff.
Any insight would be greatly appreciated!
-- short delay -- I just tried this again with the latest 1.x release and I got this error now: "Unhandled rejection: Error: Failed to execute 'objectStore' on 'IDBTransaction': The specified object store was not found."
So neither the release nor the beta can do delete hooks? Or more likely, did I just do something incorrectly? I followed this and as you can see, my hook code is based on the example provided there. I tried setting up the hooks right after stores and inside the open promise but nothing seems to be working for me. I'd rather have a sort-of cascade delete than scan the entire database several times for nodes that do not ultimately connect to the root, haha.
--edit: It turns out I could solve this issue by using a transaction block and performing my delete from there after including both tables in the transaction. The issue was that the transaction used in the hook code I wrote here does not know about any tables except for ones involved in the transaction.