I would like to serialize the database and save it every day once as a text file, so later if it's needed I could unserialize and restore the database.
Does dexie.js has a built in function, to convert the database to string, and restore from a given string?
Nope, you'll have to build it. But it would be a nice thing to have. Either to string or to a Blob so you could handle large backups.
A sample of how to write an export of all tables:
(Just simple export to string. Not for large databases. Would have to be done differently if we must write to a Blob though...)
function exportDatabase(db) {
return db.transaction('r', db.tables, function() {
// Map to transaction-bound table instances because instances in db.tables are not bound
// to current transaction by default (may change in future versions of Dexie)
var tables = db.tables.map(function (t) {
return Dexie.currentTransaction.tables[t.name];
});
// Prepare a result: An array of {tableName: "name", contents: [objects...]}
var result = [];
// Recursively export each table:
return exportNextTable ();
function exportNextTable () {
var table = tables.shift();
return table.toArray().then(function(a) {
result.push({
tableName: table.name,
contents: a
});
return tables.length > 0 ?
exportNextTable() :
result;
});
}
});
});
// Usage:
exportDatabase(db).then(function (dbObj) {
var json = JSON.stringify(dbObj);
alert (json);
});
@nponiros, @dfahlander it's probably better to close this issue in favour of #391 since the latter has more comments / participants.
Addon for this: https://www.npmjs.com/package/dexie-export-import
Most helpful comment
Nope, you'll have to build it. But it would be a nice thing to have. Either to string or to a Blob so you could handle large backups.
A sample of how to write an export of all tables:
(Just simple export to string. Not for large databases. Would have to be done differently if we must write to a Blob though...)