Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to backup, and restore database? #99

Closed
devolarium opened this issue May 20, 2015 · 3 comments
Closed

Is it possible to backup, and restore database? #99

devolarium opened this issue May 20, 2015 · 3 comments
Labels

Comments

@devolarium
Copy link

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?

@dfahlander
Copy link
Collaborator

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

@dmarcelino
Copy link

@nponiros, @dfahlander it's probably better to close this issue in favour of #391 since the latter has more comments / participants.

@dfahlander
Copy link
Collaborator

Addon for this: https://www.npmjs.com/package/dexie-export-import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants