I'm using the nodejs sqlite library (https://github.com/mapbox/node-sqlite3). How can i persist an in-memory database to disk? Since in the c/c++ implementation there are the backup api how can i persist in nodejs with this library? (or with another library/mechanism)
Asked
Active
Viewed 5,637 times
1
-
Why are you using an in-memory database when you do not want to have it in memory? – CL. Oct 17 '16 at 10:44
-
1I want to have an-in memory database for performance reasons and i want to save it (in a check point) every x minutes – user2957271 Oct 17 '16 at 11:50
2 Answers
3
It looks like node-sqlite3 now supports the backup api (see this test file). Unfortunately, the API Documentation hasn't been updated since 2017, so it's easy to miss.
It looks like the simplest way to do it would be:
var backup = db.backup('test/tmp/backup.db');
backup.step(-1, function(err) {
if (err) throw err;
backup.finish(function(err) {
if (err) throw err;
// It finished!
});
});
Note: I'd recommend better-sqlite3 these days
And better-sqlite3 has documentation for its backup api!
As simple as
db.backup(`my-backup.db`)
.then(() => {
console.log('backup complete!');
})
.catch((err) => {
console.log('backup failed:', err);
});

Jared Forsyth
- 12,808
- 7
- 45
- 54
2
In 2016, using the backup API was the only way. And Node.js did not implement this API. But now, see Jared's answer.
Anyway, just use a normal, on-disk database. You can make it as unsafe as an in-memory database with PRAGMA synchronous = OFF.

CL.
- 173,858
- 17
- 217
- 259