# transform-pouch

> Transform Pouch - a PouchDB plugin for transforming documents

Latest version **2.0.0** (published 2021-08-04) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install transform-pouch
pnpm add transform-pouch
yarn add transform-pouch
bun add transform-pouch
```

## Health

**Score 23/100 (F)** — status: abandoned.

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2021-08-04 |
| First published | 2015-03-04 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | separate (@types/transform-pouch) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 23.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 101 |
| Author | Nolan Lawson <nolan@nolanlawson.com> |
| Maintainers | daleharvey, nolanlawson, cwmma, commandoline, jcoglan, gr2m, garbados |
| Keywords | pouch, pouchdb, plugin, transform, couch, couchdb |

## Links

- npm: https://www.npmjs.com/package/transform-pouch
- Repository: https://github.com/pouchdb-community/transform-pouch
- Issues: https://github.com/pouchdb-community/transform-pouch/issues
- npm.io page: https://npm.io/package/transform-pouch

## Dependencies (1)

- [pouchdb-wrappers](https://npm.io/package/pouchdb-wrappers.md) ^5.0.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2021-08-04
- 1.1.5 — 2019-12-13
- 1.1.4 — 2017-10-16
- 1.1.3 — 2016-06-27
- 1.1.2 — 2016-06-17
- 1.1.1 — 2016-04-23
- 1.1.0 — 2016-04-14
- 1.0.3 — 2016-04-07
- 1.0.2 — 2015-05-28
- 1.0.1 — 2015-03-04

## README

Transform Pouch
=====

[![.github/workflows/transform-pouch.yml](https://github.com/pouchdb-community/transform-pouch/actions/workflows/transform-pouch.yml/badge.svg)](https://github.com/pouchdb-community/transform-pouch/actions/workflows/transform-pouch.yml)

Apply a *transform function* to documents before and after they are stored in the database. These functions are triggered invisibly for every `get()`, `put()`, `post()`, `bulkDocs()`, `bulkGet()`, `allDocs()`, `changes()`, and also to documents added via replication.

This allows you to:

* Encrypt and decrypt sensitive document fields
* Compress and uncompress large content (e.g. to avoid hitting [browser storage limits](http://pouchdb.com/faq.html#data_limits))
* Remove or modify documents before storage (e.g. to massage data from CouchDB)

*__Note:__ This plugin was formerly known as `filter-pouch`, but was renamed to be less confusing. The `filter()` API is still supported, but deprecated.*

## Usage

Just npm install it:

```
npm install transform-pouch
```

And then attach it to the `PouchDB` object:

```js
var PouchDB = require('pouchdb');
PouchDB.plugin(require('transform-pouch'));
```

You can also use `npm run build` to compile browser-ready bundles.

## API

When you create a new PouchDB, you need to configure the transform functions:

```js
var pouch = new PouchDB('mydb');
pouch.transform({
  incoming: function (doc) {
    // do something to the document before storage
    return doc;
  },
  outgoing: function (doc) {
    // do something to the document after retrieval
    return doc;
  }
});
```

You can also use Promises:

```js
var pouch = new PouchDB('mydb');
pouch.transform({
  incoming: function (doc) {
    return Promise.resolve(doc);
  },
  outgoing: function (doc) {
    return Promise.resolve(doc);
  }
});
```

Notes:

* You can provide an `incoming` function, an `outgoing` function, or both.
* Your transform function **must** return the document itself, or a new document (or a promise for such).
* `incoming` functions apply to `put()`, `post()`, `bulkDocs()`, and incoming replications.
* `outgoing` functions apply to `get()`, `allDocs()`, `bulkGet()`, `changes()`, `query()`, and outgoing replications.
* The `incoming`/`outgoing` methods can be async or sync &ndash; just return a Promise for a doc, or the doc itself.

## Example: Encryption

**Update!** Check out [crypto-pouch](https://github.com/calvinmetcalf/crypto-pouch), which is based on this plugin, and runs in both the browser and Node. The instructions below will only work in Node.

Using the Node.js crypto library, let's first set up our encrypt/decrypt functions:

```js
var crypto = require('crypto');

var cipher = crypto.createCipher('aes-256-cbc', 'password');
var decipher = crypto.createDecipher('aes-256-cbc', 'password');

function encrypt(text) {
  var crypted = cipher.update(text, 'utf8', 'base64');
  return crypted + cipher.final('base64');
}

function decrypt(text) {
  var dec = decipher.update(text, 'base64', 'utf8');
  return dec + decipher.final('utf8');
}
```

Obviously you would want to change the `'password'` to be something only the user knows!

Next, let's set up our transforms:

```js
pouch.transform({
  incoming: function (doc) {
    Object.keys(doc).forEach(function (field) {
      if (field !== '_id' && field !== '_rev' && field !== '_revisions') {
        doc[field] = encrypt(doc[field]);
      }
    });
    return doc;
  },
  outgoing: function (doc) {
    Object.keys(doc).forEach(function (field) {
      if (field !== '_id' && field !== '_rev' && field !== '_revisions') {
        doc[field] = decrypt(doc[field]);
      }
    });
    return doc;
  }
});
```

(`transform-pouch` will automatically ignore deleted documents, so you don't need to handle that case.)

Now, the documents are encrypted whenever they're stored in the database. If you want to verify, try opening them with a `Pouch` where you haven't set up any `transforms`.  You'll see documents like:

```js
{
  secret: 'YrAtAEbvp0bPLil8EpbNeA==',
  _id: 'doc',
  _rev: '1-bfc37cd00225f68671fe3187c054f9e3'
}
```

whereas privileged users will see:

```js
{
  secret: 'my super secret text!',
  _id: 'doc',
  _rev: '1-bfc37cd00225f68671fe3187c054f9e3'
}
```

This works for remote CouchDB databases as well.  In fact, only the encrypted data is sent over the wire, so it's ideal for protecting sensitive information.

## Note on query()

Since the remote CouchDB doesn't have accesss to the untransformed document, map/reduce functions that are executed directly against CouchDB will be applied to the untransformed version. PouchDB doesn't have this limitation, because everything is local.

So for instance, if you try to `emit()` an encrypted field in your map function:

```js
function (doc) {
  emit(doc.secret, 'shhhhh');
}
```

... the emitted key will be encrypted when you `query()` the remote database, but decrypted when you `query()` a local database. So be aware that the `query()` functionality is not exactly the same.

## Building

You can build transform-pouch for the browser with `npm run build`:

    npm install
    npm run build

This will place browser bundles, minified and unminified, in the `dist/` folder.

## Testing

You can run the test suite with `npm test`.

To run tests in Node specifically, using LevelDB:

    npm run test:node

You can also run tests in a headless browser with [mochify](https://www.npmjs.com/package/mochify):

    npm run test:browser

You can also check for code coverage using:

    npm run coverage

You can run single test using options from [mocha](https://www.npmjs.com/package/mocha):

```
TEST_DB=local npm run test:node -- --reporter spec --grep search_phrase
```

The `TEST_DB` environment variable specifies the database that PouchDB should use.
You may specify either `local` (which uses LevelDB) or `http` (which uses the `$COUCH_URL` environment variable to connect to a CouchDB installation.)

# License

[Apache-2.0](./LICENSE)

---
_Source: https://npm.io/package/transform-pouch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
