3.0.1 • Published 1 year ago

@universal-health-chain/pouchdb-adapter-react-native-sqlite-ts v3.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

logo

@universal-health-chain/pouchdb-adapter-react-native-sqlite-ts (typescript)

PouchDB adapter using ReactNative SQLite as its backing store. Forked from https://github.com/craftzdog/pouchdb-adapter-react-native-sqlite, but using Typescript and @universal-health-chain/pouchdb-adapter-websql-core-ts (typescript).

Why?

SQLite storage performs much faster than AsyncStorage, especially with secondary index. Here is benchmark results:

1) allDocs speedminmaxmean
AsyncStorage72ms94ms77ms
SQLite27ms39ms28ms
2) query speedminmaxmean
AsyncStorage1,075ms1,117ms1,092ms
SQLite33ms39ms35ms
  • Device: iPhone 6s
  • Documents: 434
  • Update seq: 453
  • Iterations: 100
  • Used options: { include_docs: true }

On Simulator

  • Device: iPad Pro 9.7" (Simulator) - iOS 10.3.2
  • Documents: 5000
3) bulkDocs speedtotalmean
AsyncStorage25.821ms5.16ms
SQLite22.213ms4.44ms
4) allDocs speedtotalmean
AsyncStorage189,379ms37.87ms
SQLite30,527ms6.10ms
  • allDocs options: { include_docs: true, attachments: true }
  • Using this test script

How to use it

(based on https://dev.to/craftzdog/a-performant-way-to-use-pouchdb7-on-react-native-in-2022-24ej)

  1. Install these packages (use expo install, npm i or yarn add):
  • expo install events react-native-get-random-values react-native-quick-base64
  • expo install pouchdb-core pouchdb-replication pouchdb-mapreduce
  • expo install pouchdb-adapter-http react-native-quick-sqlite
  1. Add the new UHC packages, refactored to typescript to solve both jest and import problems in web (for expo):
  • expo install @universal-health-chain/pouchdb-adapter-react-native-sqlite-ts
  • expo install @universal-health-chain/react-native-quick-websql-ts
  1. Then:
  • npx pod-install
  1. Create the shim.js file (see the link) and import it in the first line in the index.js file of the project (as specified in the above link).

    import {shim} from 'react-native-quick-base64'
    
    shim()
    
    // Avoid using node dependent modules
    process.browser = true
  2. Edit your babel.config.js like so:

    module.exports = {
      presets: ['module:metro-react-native-babel-preset'],
      plugins: [
        [
          'module-resolver',
          {
            alias: {
              'pouchdb-collate': '@craftzdog/pouchdb-collate-react-native',
            },
          },
        ],
      ],
    }
  3. To initialize, create pouchdb.ts like so:

    import 'react-native-get-random-values'
    
    // PouchDB required imports
    import PouchDB from 'pouchdb-core'
    import HttpPouch from 'pouchdb-adapter-http'
    import replication from 'pouchdb-replication'
    import mapreduce from 'pouchdb-mapreduce'
    
    // using PouchDB with the "@universal-health-chain" packages to avoid problems with web in expo
    import { createPlugin } from '@universal-health-chain/pouchdb-adapter-react-native-sqlite-ts'
    
    const dbConfigNative: PouchDB.Configuration.DatabaseConfiguration = {
      adapter: 'react-native-sqlite'
    };
    
    // you can check if the platform is "web" or not (native)
    export const db = await openPouchDBWithNativeAdapterSQLite("dbName", dbConfigNative);
    
    async function openPouchDBWithNativeAdapterSQLite(
      dbName: string,
      dbConfig: PouchDB.Configuration.DatabaseConfiguration
    ): Promise<PouchDB.Database> {
    
      // importing the class for react native
      const nativeSQLite = require('@universal-health-chain/react-native-quick-websql-ts');
      
      // instantiate the plugin for react native
      const pluginSQLite = new nativeSQLite.SQLitePlugin();
      
      // create the plugin for react native
      const adapterSQLite = createPlugin(pluginSQLite);
      
      PouchDB.plugin(HttpPouch)
        .plugin(replication)
        .plugin(mapreduce)
        .plugin(adapterSQLite)
    
      // creating the database client (it opens/creates the database)
      const pouchDB = new PouchDB(dbName, dbConfig);
      return pouchDB
    };
  4. Note - do not use these packages to avoid jest and web problems when importing them:

  • pouchdb-adapter-react-native-sqlite
  • react-native-quick-websql

Changelog