louischatriot / nedb

The JavaScript Database, for Node.js, nw.js, electron and the browser
MIT License
13.49k stars 1.03k forks source link

document restoring on remove #594

Open digimbyte opened 5 years ago

digimbyte commented 5 years ago

I think I came across a bug where it restores deleted documents on persistence.compactDatafile();

I use nedb for persistence of objects that expire after a duration, when the item is removed, its data entry shifts to [] but if I close the application and relaunch it, it restores it to the initial state and continues to attempt to inject the document despite it being removed() in the previous session.

ERROR: (node:16876) UnhandledPromiseRejectionWarning: Error: Can't insert key 582158434254716938, it violates the unique constraint at _AVLTree.insert (G:\Discord\the-judge\node_modules\binary-search-tree\lib\avltree.js:273:19) at AVLTree.insert (G:\Discord\the-judge\node_modules\binary-search-tree\lib\avltree.js:307:27) at Index.insert (G:\Discord\the-judge\node_modules\nedb\lib\indexes.js:77:15) at Index.insertMultipleDocs (G:\Discord\the-judge\node_modules\nedb\lib\indexes.js:114:12) at Index.insert (G:\Discord\the-judge\node_modules\nedb\lib\indexes.js:69:33) at Index.updateMultipleDocs (G:\Discord\the-judge\node_modules\nedb\lib\indexes.js:193:12) at Index.update (G:\Discord\the-judge\node_modules\nedb\lib\indexes.js:163:36) at Datastore.updateIndexes (G:\Discord\the-judge\node_modules\nedb\lib\datastore.js:223:29) at G:\Discord\the-judge\node_modules\nedb\lib\datastore.js:632:14 at G:\Discord\the-judge\node_modules\nedb\lib\datastore.js:329:14

the process uses loading all entries in the database, iterate through each of them, counting down its health and them performs delete docs[entry] on the document entry and then update with the complete database document as a replacement. deleted documents are saved as [] and are not cleaned up or auto marked for deletion

digimbyte commented 5 years ago

I've seemed to have tracked it to the persistence.compactDatafile(); that runs on startup it removes any and all changes since last 'collapse' of the data changes.