How to import / export database from PouchDB

How to import / export a database from a local PouchDB database? I need to save my local database and open it on another platform. On the server side there is no CouchDB.

+12
javascript pouchdb
source share
3 answers

There are functions encoded by awesome @nolanlawson. Not only can you import / export it, you can do all kinds of things with it. Just awesome.

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

ReadableStreams and WritableStreams for PouchDB / CouchDB Replication. Basically, you can copy two databases simply by joining the threads together. This has many uses: dump the database into a file and then upload the same file to another database. Make a quick initial replication by dumping the contents of CouchDB to the HTTP endpoint, which then loads into PouchDB in the browser. Replication Over Internet Outlets? For bluetooth? Over NFC? Why not? Since the replication stream is just plain JSON text, you can send it to any transport mechanism. Back up your database periodically.

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

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

+14
source share

To export, why don't you just download all the documents and save them to a file?

db.allDocs({include_docs: true, attachments: true}).then(JSON.stringify); 
+4
source share

As @mrded said, the simplest solution 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.

0
source share

All Articles