# @universal-health-chain/pouchdb-adapter-react-native-sqlite-ts

> PouchDB adapter using ReactNative SQLite Plugin as its data store.

Latest version **3.0.1** (published 2023-03-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install @universal-health-chain/pouchdb-adapter-react-native-sqlite-ts
pnpm add @universal-health-chain/pouchdb-adapter-react-native-sqlite-ts
yarn add @universal-health-chain/pouchdb-adapter-react-native-sqlite-ts
bun add @universal-health-chain/pouchdb-adapter-react-native-sqlite-ts
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2023-03-07 |
| First published | 2023-03-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 165 |
| Author | Takuya Matsuyama |
| Maintainers | deepikaverma710, flatorre, brunocolm, cehaw15 |

## Links

- npm: https://www.npmjs.com/package/@universal-health-chain/pouchdb-adapter-react-native-sqlite-ts
- Repository: https://github.com/craftzdog/pouchdb-adapter-react-native-sqlite
- Homepage: https://github.com/Universal-Health-Chain/pouchdb-adapter-react-native-sqlite#readme
- Issues: https://github.com/craftzdog/pouchdb-adapter-react-native-sqlite/issues
- npm.io page: https://npm.io/package/@universal-health-chain/pouchdb-adapter-react-native-sqlite-ts

## Dependencies (1)

- [@universal-health-chain/pouchdb-adapter-websql-core-ts](https://npm.io/package/@universal-health-chain/pouchdb-adapter-websql-core-ts.md) ^7.2.3

## Recent versions

- 3.0.1 (latest) — 2023-03-07

## README

![logo](https://avatars.githubusercontent.com/u/57396025?s=200&v=4)

# **@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` speed | min  | max  | mean |
| ------------------ | ---- | ---- | ---- |
| AsyncStorage       | 72ms | 94ms | 77ms |
| SQLite             | 27ms | 39ms | 28ms |

| 2) `query` speed | min     | max     | mean    |
| ---------------- | ------- | ------- | ------- |
| AsyncStorage     | 1,075ms | 1,117ms | 1,092ms |
| SQLite           | 33ms    | 39ms    | 35ms    |

- 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` speed | total    | mean   |
| ------------------- | -------- | ------ |
| AsyncStorage        | 25.821ms | 5.16ms |
| SQLite              | 22.213ms | 4.44ms |

| 4) `allDocs` speed | total     | mean    |
| ------------------ | --------- | ------- |
| AsyncStorage       | 189,379ms | 37.87ms |
| SQLite             | 30,527ms  | 6.10ms  |

- `allDocs` options: `{ include_docs: true, attachments: true }`
- Using this test [script](https://gist.github.com/hnq90/972f6597a0927f45d9075b8627892783)

## 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).

    ```js
    import {shim} from 'react-native-quick-base64'

    shim()

    // Avoid using node dependent modules
    process.browser = true
    ```

2. Edit your `babel.config.js` like so:
    ```js
    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:
    ```js
    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

- 3.0.1
  - Using [@universal-health-chain/pouchdb-adapter-websql-core-ts](https://www.npmjs.com/package/@universal-health-chain/pouchdb-adapter-websql-core-ts)
- 3.0.0
  - Use [@craftzdog/pouchdb-adapter-websql-core](https://github.com/craftzdog/pouchdb-adapter-websql-core) #11
- 2.0.0
  - Upgrade pouchdb-adapter-websql-core to 7.0.0
- 1.0.3
  - Remove `pouchdb-utils` dependency
- 1.0.2
  - Upgrade pouchdb-util & pouchdb-adapter-websql-core to 6.2.0
  - Update benchmark result
- 1.0.1
  - Remove unnecessary console output
- 1.0.0
  - Initial release

---
_Source: https://npm.io/package/@universal-health-chain/pouchdb-adapter-react-native-sqlite-ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
