|
| 1 | +--- |
| 2 | +layout: post |
| 3 | + |
| 4 | +title: PouchDB has a new adapter - nodesqlite |
| 5 | +author: Alba Herrerías |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +Hello everyone! PouchDB has a new available adapter for you to use, `nodesqlite`, that internally uses Node.js's native SQLite module as its persistence layer. This will be, in PouchDB's release version 11.0.0, the default adapter for node environments, replacing the deprecated LevelDB ecosystem we currently depend on. You can read more about the details, discussion and timeline in this [issue](https://github.com/apache/pouchdb/issues/9163), and look at its implementation in this [pull request](https://github.com/apache/pouchdb/pull/9223). |
| 10 | + |
| 11 | +## Migration guide |
| 12 | + |
| 13 | +We suggest you to create a replication from your databases to new ones using the `nodesqlite` adapter. We have drafted a snippet you can copy and modify according to your needs. |
| 14 | + |
| 15 | +```js |
| 16 | +async function getDb (name) { |
| 17 | + const oldDb = new PouchDB(name, { adapter: 'leveldb' }) |
| 18 | + |
| 19 | + // create a new database with new nodesqlite adapter |
| 20 | + const newDb = new PouchDB(name, { adapter: 'nodesqlite' }) |
| 21 | + |
| 22 | + // set up promise wrapped around replication |
| 23 | + return new Promise((resolve, reject) => { |
| 24 | + console.log('Started migrating to nodesqlite...') |
| 25 | + |
| 26 | + PouchDB.replicate(oldDb, newDb).on('complete', async () => { |
| 27 | + // Do you want to remove the old database? |
| 28 | + // If so, uncomment the following line: |
| 29 | + // await oldDb.destroy() |
| 30 | + |
| 31 | + console.log('All done!') |
| 32 | + resolve(newDb) |
| 33 | + }).on('change', (info) => { |
| 34 | + console.log(`Docs written: ${info.docs_written}` ) |
| 35 | + }).on('denied', reject) |
| 36 | + .on('error', reject) |
| 37 | + }) |
| 38 | +} |
| 39 | +``` |
| 40 | +Now, instead of getting your database like: |
| 41 | +```js |
| 42 | +const db = new PouchDB('my-db-name', { adapter: 'leveldb' }) |
| 43 | + |
| 44 | +// or like this, since `leveldb` is the default adapter in the Node.js environment |
| 45 | +const db = new PouchDB('my-db-name') |
| 46 | +``` |
| 47 | + |
| 48 | +Do it like this: |
| 49 | +```js |
| 50 | +const db = await getDb('my-db-name') |
| 51 | +``` |
| 52 | + |
| 53 | +If you encounter a bug in this migration, please [file an issue](https://github.com/pouchdb/pouchdb/issues) and, ideally, modify this post for the benefit of others. Thanks! |
| 54 | + |
| 55 | +## Get in touch |
| 56 | + |
| 57 | +As always, we welcome feedback from the community. Please don't hesitate to [file issues](https://github.com/pouchdb/pouchdb/issues), [open discussions](https://github.com/pouchdb/pouchdb/discussions) or [get in touch](https://github.com/pouchdb/pouchdb/blob/master/CONTRIBUTING.md#get-in-touch). And of course, a big thanks to all of our [new and existing contributors](https://github.com/pouchdb/pouchdb/graphs/contributors)! |
0 commit comments