Skip to content

saveDatabase() error when invoked too quickly #526

@mromanbcn

Description

@mromanbcn

I am writing an app in Node that uses LokiJS.

I am manually calling saveDatabase() every time I insert an object. If I create multiple objects quickly, the saveDatabase method is invoked multiple times in a short period of time and after the first call it complains because it cannot rename the dbName~ file. The problem is that the fs.writeFile and fs.rename (lokijs.js lines 2020 - 2025) are executed asynchronously so in some cases, when the code tries to rename dbname~ to dbname, dbname~ does not exist any longer because it was already renamed in a previous callback.

I fixed the issue by making sure the temp db name is unique by changing lokijs.js line 2019 from:

var tmpdbname = dbname + '~';

to

var tmpdbname = dbname + Date.now();

The way I am calling saveDatabase is not efficient and should probably use the autoSave feature.
The reason I do it this way is because my code does not write often, so I wanted to avoid having the autosave timer function running periodically (though it may be cleaner to do so).

Would you like me to submit a pull request?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions