Skip to content

Commit cf7c236

Browse files
committed
Initial commit
1 parent 7afbea2 commit cf7c236

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
node_modules

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
pouchdb-adapter-react-native-sqlite
2+
======
3+
4+
PouchDB adapter using ReactNative SQLite as its backing store.
5+
6+
### Prerequisites
7+
8+
- [pouchdb-react-native](https://github.com/stockulus/pouchdb-react-native)
9+
- [react-native-sqlite-storage](https://github.com/andpor/react-native-sqlite-storage)
10+
11+
### Usage
12+
13+
Install from npm:
14+
15+
```bash
16+
npm install pouchdb-react-native pouchdb-adapter-react-native-sqlite --save
17+
react-native install react-native-sqlite-storage
18+
```
19+
20+
Then `import` it, notify PouchDB of the plugin, and initialize a database using the `react-native-sqlite` adapter name:
21+
22+
```js
23+
import PouchDB from 'pouchdb-react-native'
24+
import SQLite from 'react-native-sqlite-storage'
25+
import SQLiteAdapterFactory from 'pouchdb-adapter-react-native-sqlite'
26+
27+
const SQLiteAdapter = SQLiteAdapterFactory(SQLite)
28+
PouchDB.plugin(SQLiteAdapter)
29+
const db = new PouchDB('mydb.db', {adapter: 'react-native-sqlite'});
30+
```
31+
32+
## Changelog
33+
34+
- 1.0.0
35+
- Initial release

lib/index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict'
2+
3+
var WebSqlPouchCore = require('pouchdb-adapter-websql-core')
4+
var pouchUtils = require('pouchdb-utils')
5+
var extend = pouchUtils.jsExtend
6+
var guardedConsole = pouchUtils.guardedConsole
7+
var sqlite = null
8+
9+
function createOpenDBFunction (opts) {
10+
return function (name, version, description, size) {
11+
// The SQLite Plugin started deviating pretty heavily from the
12+
// standard openDatabase() function, as they started adding more features.
13+
// It's better to just use their "new" format and pass in a big ol'
14+
// options object. Also there are many options here that may come from
15+
// the PouchDB constructor, so we have to grab those.
16+
var openOpts = extend({}, opts, {
17+
name: name,
18+
version: version,
19+
description: description,
20+
size: size
21+
})
22+
function onError (err) {
23+
console.error(err)
24+
guardedConsole('error', err)
25+
if (typeof opts.onError === 'function') {
26+
opts.onError(err)
27+
}
28+
}
29+
return sqlite.openDatabase(openOpts.name, openOpts.version, openOpts.description, openOpts.size, null, onError)
30+
}
31+
}
32+
33+
function ReactNativeSQLitePouch (opts, callback) {
34+
var websql = createOpenDBFunction(opts)
35+
var _opts = extend({
36+
websql: websql
37+
}, opts)
38+
39+
WebSqlPouchCore.call(this, _opts, callback)
40+
}
41+
42+
ReactNativeSQLitePouch.valid = function () {
43+
// if you're using ReactNative, we assume you know what you're doing because you control the environment
44+
return true
45+
}
46+
47+
// no need for a prefix in ReactNative (i.e. no need for `_pouch_` prefix
48+
ReactNativeSQLitePouch.use_prefix = false
49+
50+
function reactNativeSqlitePlugin (PouchDB) {
51+
PouchDB.adapter('react-native-sqlite', ReactNativeSQLitePouch, true)
52+
}
53+
54+
function createPlugin (SQLite) {
55+
sqlite = SQLite
56+
return reactNativeSqlitePlugin
57+
}
58+
59+
module.exports = createPlugin

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "pouchdb-adapter-react-native-sqlite",
3+
"version": "1.0.0",
4+
"description": "PouchDB adapter using ReactNative SQLite Plugin as its data store.",
5+
"main": "./lib/index.js",
6+
"keywords": [],
7+
"author": "Takuya Matsuyama <[email protected]>",
8+
"license": "MIT",
9+
"repository": "https://github.com/noradaiko/pouchdb-adapter-react-native-sqlite",
10+
"files": [
11+
"lib",
12+
"dist"
13+
],
14+
"scripts": {
15+
"test": "standard"
16+
},
17+
"dependencies": {
18+
"pouchdb-adapter-websql-core": "6.1.0",
19+
"pouchdb-utils": "6.1.0"
20+
},
21+
"devDependencies": {
22+
"standard": "^7.1.2"
23+
}
24+
}

0 commit comments

Comments
 (0)