craftzdog / pouchdb-adapter-react-native-sqlite

PouchDB adapter using ReactNative SQLite as its backing store
https://github.com/craftzdog/pouchdb-react-native
MIT License
131 stars 25 forks source link

PouchDB and SQLite not working on iOS #13

Closed amirduran closed 4 years ago

amirduran commented 4 years ago

Hi @craftzdog,

I have issue with PouchDb react native sqlite adapter when using it on iOS device. I'm developing small React Native application for my SmartHome and using PouchDB to save some data locally on the phone.

The code works good when executing it on the Android device, but on the iOS device I get the following error:

WebSQL threw an error, [TypeError: sqlite.openDatabase is not a function. (In 'sqlite.openDatabase(openOpts.name, openOpts.version, openOpts.description, openOpts.size, null, onError)', 'sqlite.openDatabase' is undefined)]
- node_modules/expo/build/environment/muteWarnings.fx.js:26:24 in error
- node_modules/@sentry/utils/dist/instrument.js:104:50 in <unknown>
- node_modules/pouchdb-adapter-websql-core/node_modules/pouchdb-utils/lib/index-browser.js:457:26 in guardedConsole
- node_modules/pouchdb-adapter-websql-core/lib/index.js:239:32 in <unknown>
- node_modules/pouchdb-adapter-react-native-sqlite/lib/index.js:35:23 in ReactNativeSQLitePouch
- node_modules/pouchdb-react-native/node_modules/pouchdb-core/lib/index.js:1274:38 in PouchDB

I'm using expo project together with "expo-sqlite": "^8.0.0".

const SQLiteAdapter = SQLiteAdapterFactory(SQLite)
PouchDB.plugin(SQLiteAdapter)
PouchDB.plugin(require('pouchdb-find'));
PouchDB.plugin(require('pouchdb-upsert'));
return new PouchDB(dbName, {adapter: 'react-native-sqlite', revs_limit: 1, auto_compaction: true});

When I console log the last line or new PouchDB(...) object here is what I get:

PouchDB {
  "__opts": Object {
    "adapter": "react-native-sqlite",
    "auto_compaction": true,
    "name": "afa31ab81267925e8bd75233177a5988.db",
    "revs_limit": 1,
  },
  "_adapter": "react-native-sqlite",
  "_events": Object {},
  "_maxListeners": undefined,
  "_name": "afa31ab81267925e8bd75233177a5988.db",
  "adapter": "react-native-sqlite",
  "auto_compaction": true,
  "name": "afa31ab81267925e8bd75233177a5988.db",
  "prefix": "_pouch_",
  "taskqueue": TaskQueue$1 {
    "failed": [web_sql_went_bad: unknown],
    "isReady": false,
    "queue": Array [],
  },
}

Any idea what might be the problem?

amirduran commented 4 years ago

It seems like the bug is on this line.

On Android device sqlite is object that has attribute openDatabase which is a function. On iOS device sqlite is an object which has an object SQLite and that object has a function openDatabase.

So if I change the line 25 with the following one on the iOS device, it works normally.

return sqlite.SQLite.openDatabase(openOpts.name, openOpts.version, openOpts.description, openOpts.size, null, onError)

Is this a bug or I have misconfigured something?

craftzdog commented 4 years ago

I don't know how expo-sqlite works because I made it to work with react-native-sqlite-2 as the README suggests.

amirduran commented 4 years ago

Ok I, just saw that line in Readme. I decided to go away from SQLite, PouchDB because of the issues in RN. Thx for your comment.