PouchDB Plugins Best Practice

I’m using pouchDB in an app and I’m using the pouchdd-find plugin as a means of generating better performing queries.

I initially used used require to the include the libraries, like so:

var PouchDB = require('pouchdb');
PouchDB.plugin(require('pouchdb-find'));

But I see best practice in Ionic2 is to use the import statement :

import * as PouchDB from ‘pouchdb’;

What I’m having trouble with is how do I add the pouchdb-find plugin using this method?

I’m fine using the require method, I just figure if I’m spending all this time moving to Ionic2 I should start using best practice.

Hi codiqa

I couldn’t fint any way of using plugins with pouchDB

import * as PouchDB from ‘pouchdb’;
PouchDB.plugin(require(‘pouchdb-authentication’));

I’ve tried few other ways to implement plugin function on top of PouchDB typings but it doesn’t work.

Can you share with me your first solution ?

I used this a long time ago by importing their JS file in my index.html, then adding this line to the top of my database provider:

declare var PouchDB: any;

However, now I think there is a better way to do it, which is by installing the npm package and installing typings.

npm i --save pouchdb
typings install pouchdb

Then you will be able to import the library by doing: (without adding any files to index.html)

import * as PouchDB from 'pouchdb';
// or
import PouchDB = require('pouchdb');

Note that I haven’t tested this yet, but it seems like the best way to use other libraries with TypeScript projects.

I just installed PouchDB, requireJS and the typings library and all worked OK.

npm install pouchdb --save
npm install pouchdb-find --save
npm install requirejs --save
npm install -g typings
typings install dt~pouchdb/pouch --save

Then the above code worked

2 Likes

I don’t think requirejs is needed here.

1 Like

When I tried without it wouldn’t work. I haven’t looked at it for a little while as I was building a proof of concept app which was completed.

2 Likes

Ok ! I’ve found the way to install any PouchDB plugin to angular2/Ionic2 by installing require typings by following this technique.

If you have not installed typings globally yet :
npm install typings --global

And

typings install dt~require --save --global

Then you can initialise any plugin in your angular2/Ionic2 projects like this:

var PouchDB = require("pouchdb");
PouchDB.plugin(require('pouchdb-authentication'));

Now I’ve got no more internal error from using the signup plugin method :

Object {ok: true, id: “org.couchdb.user:robin”, rev: “1-434cd15f54ea2ee85042f4e95828cf8c”}
Hope it helps !