18

How to import/export database from local PouchDB database? I need save my local database and open it in different platform. There is no CouchDB on server side.

Cœur
  • 37,241
  • 25
  • 195
  • 267
pZCZ
  • 183
  • 2
  • 11

3 Answers3

19

There are features coded by the amazing @nolanlawson. Not only you can import/export it, you can do all sorts of things with it. Just awesome.

PouchDB Replication Stream https://github.com/nolanlawson/pouchdb-replication-stream

ReadableStreams and WritableStreams for PouchDB/CouchDB replication. Basically, you can replicate two databases by just attaching the streams together. This has many uses: Dump a database to a file, and then load that same file into another database. Do a quick initial replication by dumping the contents of a CouchDB to an HTTP endpoint, which is then loaded into a PouchDB in the browser. Replicate over web sockets? Over bluetooth? Over NFC? Why not? Since the replication stream is just JSON plaintext, you can send it over any transport mechanism. Periodically backup your database.

And PouchDB.load to import: https://github.com/nolanlawson/pouchdb-load

Client-side tools for loading a dump from a CouchDB/PouchDB database. For dumping, check out pouchdb-dump-cli to dump from the command line, or pouchdb-replication-stream to dump from within your Node.js application. This method is typically much faster than standard replication, because it uses fewer HTTP requests. So it's a great way to quickly load an initial state for your database.

Matías
  • 363
  • 1
  • 10
  • 3
    Thanks for the kudos. :) Also check out this blog post explaining how to dump and load SQLite files: https://pouchdb.com/2016/04/28/prebuilt-databases-with-pouchdb.html – nlawson Jul 13 '16 at 22:40
8

To export, why don't you just load all docs and save them into a file?

db.allDocs({include_docs: true, attachments: true}).then(JSON.stringify);
mrded
  • 4,674
  • 2
  • 34
  • 36
  • it not working blob type data ,it stores just raw data only.do you know any solution for that? – Balaji Jul 23 '20 at 15:13
  • Thank you, i integrated that solution, but i have a problem, documents deleted not sync (i exported documents deleted and imported by bulkDocs(data, {new_edits: true}), but not sync, something idea? – Lud Osorio Sep 22 '20 at 21:16
3

The simplest solution, as @mrded said, is to use batch operations:

<button onClick={handleExport}>Export</button>
<input type="file" onChange={handleImport}/>
function handleExport () {
  db.allDocs({include_docs: true}, (error, doc) => {
    if (error) console.error(error);
    else download(
      JSON.stringify(doc.rows.map(({doc}) => doc)),
      'tracker.db',
      'text/plain'
    );
  });
}

function handleImport ({target: {files: [file]}}) {
  if (file) {
    const reader = new FileReader();
    reader.onload = ({target: {result}}) => {
      db.bulkDocs(
        JSON.parse(result),
        {new_edits: false}, // not change revision
        (...args) => console.log('DONE', args)
      );
    };
    reader.readAsText(file);
  }
}

Also check the download function.

Igor Sukharev
  • 2,467
  • 24
  • 21
  • Thank you, i integrated that solution, but i have a problem, documents deleted not sync (i exported documents deleted and imported by bulkDocs(data, {new_edits: true}), but not sync, something idea? – Lud Osorio Sep 22 '20 at 21:16