Skip to content

Commit d636bcd

Browse files
committed
docs(readme): Update usage
1 parent 045e2e8 commit d636bcd

File tree

1 file changed

+76
-15
lines changed

1 file changed

+76
-15
lines changed

README.md

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pouchdb-adapter-react-native-sqlite
33

44
PouchDB adapter using ReactNative SQLite as its backing store.
55

6-
### Why?
6+
## Why?
77

88
SQLite storage performs much faster than AsyncStorage, especially with secondary index.
99
Here is benchmark results:
@@ -24,7 +24,7 @@ Here is benchmark results:
2424
* Iterations: 100
2525
* Used options: `{ include_docs: true }`
2626

27-
#### On Simulator
27+
### On Simulator
2828

2929
* Device: iPad Pro 9.7" (Simulator) - iOS 10.3.2
3030
* Documents: 5000
@@ -42,32 +42,93 @@ Here is benchmark results:
4242
* `allDocs` options: `{ include_docs: true, attachments: true }`
4343
* Using this test [script](https://gist.github.com/hnq90/972f6597a0927f45d9075b8627892783)
4444

45-
### Prerequisites
45+
## How to use it
4646

47-
- [pouchdb-react-native](https://github.com/stockulus/pouchdb-react-native)
48-
- A SQLite module
49-
- [react-native-sqlite-2 (recommended)](https://github.com/noradaiko/react-native-sqlite-2)
50-
- [react-native-sqlite-storage](https://github.com/andpor/react-native-sqlite-storage)
47+
Read [this blogpost](https://dev.to/craftzdog/hacking-pouchdb-to-use-on-react-native-1gjh) for the complete description.
48+
Here is [a working demo app](https://github.com/craftzdog/pouchdb-react-native-demo).
5149

52-
### Usage
50+
### Install deps
5351

54-
Install from npm:
52+
Install PouchDB core packages:
5553

5654
```bash
57-
npm install pouchdb-react-native pouchdb-adapter-react-native-sqlite --save
58-
react-native install react-native-sqlite-2
55+
npm i pouchdb-adapter-http pouchdb-mapreduce
5956
```
6057

61-
Then `import` it, notify PouchDB of the plugin, and initialize a database using the `react-native-sqlite` adapter name:
58+
And install hacked packages for React Native:
59+
60+
```bash
61+
npm i @craftzdog/pouchdb-core-react-native @craftzdog/pouchdb-replication-react-native
62+
```
63+
64+
Next, install SQLite3 engine modules:
65+
66+
```bash
67+
npm i pouchdb-adapter-react-native-sqlite react-native-sqlite-2
68+
react-native link react-native-sqlite-2
69+
```
70+
71+
Then, install some packages to polyfill functions that PouchDB needs:
72+
73+
```bash
74+
npm i base-64 events
75+
```
76+
77+
### Create polyfills
78+
79+
Make a js file to polyfill some functions that PouchDB needs:
6280

6381
```js
64-
import PouchDB from 'pouchdb-react-native'
82+
import {decode, encode} from 'base-64'
83+
84+
if (!global.btoa) {
85+
global.btoa = encode;
86+
}
87+
88+
if (!global.atob) {
89+
global.atob = decode;
90+
}
91+
92+
// Avoid using node dependent modules
93+
process.browser = true
94+
```
95+
96+
Import it at the first line of your `index.js`.
97+
98+
### Load PouchDB
99+
100+
Make `pouchdb.js` like so:
101+
102+
```js
103+
import PouchDB from '@craftzdog/pouchdb-core-react-native'
104+
import HttpPouch from 'pouchdb-adapter-http'
105+
import replication from '@craftzdog/pouchdb-replication-react-native'
106+
import mapreduce from 'pouchdb-mapreduce'
107+
65108
import SQLite from 'react-native-sqlite-2'
66109
import SQLiteAdapterFactory from 'pouchdb-adapter-react-native-sqlite'
67110

68111
const SQLiteAdapter = SQLiteAdapterFactory(SQLite)
69-
PouchDB.plugin(SQLiteAdapter)
70-
const db = new PouchDB('mydb.db', {adapter: 'react-native-sqlite'});
112+
113+
export default PouchDB
114+
.plugin(HttpPouch)
115+
.plugin(replication)
116+
.plugin(mapreduce)
117+
.plugin(SQLiteAdapter)
118+
```
119+
120+
If you need other plugins like `pouchdb-find`, just add them to it.
121+
122+
### Use PouchDB
123+
124+
Then, use it as usual:
125+
126+
```js
127+
import PouchDB from './pouchdb'
128+
129+
function loadDB () {
130+
return new PouchDB('mydb.db', { adapter: 'react-native-sqlite' })
131+
}
71132
```
72133

73134
## Changelog

0 commit comments

Comments
 (0)