|
| 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 |
0 commit comments