Dexie.js: Is it possible to backup, and restore database?

Created on 20 May 2015  路  3Comments  路  Source: dfahlander/Dexie.js

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?

idea

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...)

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);
});

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sortegamartin picture sortegamartin  路  3Comments

oviniciuslara picture oviniciuslara  路  4Comments

Script47 picture Script47  路  3Comments

acicali picture acicali  路  4Comments

xavibonell picture xavibonell  路  4Comments