typicode / lowdb

Simple and fast JSON database
MIT License
21.35k stars 918 forks source link

Clearing up db file #508

Closed chk06 closed 2 years ago

chk06 commented 2 years ago

Hi, I have initializing the db file once I start my application. But there is a scenario where I need to replace the contents of the db file with new data.

When I do the following with the new data,, which is fileData, nothing happens. My old data remains in the db file.

      db.defaults(fileData)
        .write(); 

So I was thinking maybe need to clear the db file before running the above code. But nothing is working. What am I doing wrong?

typicode commented 2 years ago

Looks like lowdb 1 code. To clear:

db.setState({})
db.write()

With lowdb 3, you can do:

db.data = {} // will clear data
db.write() // data {} will be written to file