user2302244
user2302244

Reputation: 935

How do I use pouchdb with typescript?

I am trying to use pouchdb with Typescript. I cannot link to the pouchdb module.

import { PouchDB } from "pouchdb"

reports that it cannot find module pouchdb, even though it is in node_modules.

I also cannot find the appropriate typings for pouchdb.

Upvotes: 8

Views: 10904

Answers (5)

Danny
Danny

Reputation: 341

import PouchDB from 'pouchdb';

export abstract class PouchDBDatabase {
    private _database: PouchDB.Database<Sheet>;

    constructor(protected DATABASE_URL: string) {
        this._database = new PouchDB(this.DATABASE_URL);
    }
}

auto-completion with typescript

And you're good to go with typescript + pouchDB :)

Upvotes: 2

Jeremy
Jeremy

Reputation: 3809

I had the same problem trying to import into Angular 6.

Your imports seem fine:

import PouchDB from 'pouchdb';
import PouchFind from 'pouchdb-find';
PouchDB.plugin(PouchFind);

What you may be missing is you need to add this to your polyfills.ts file:

(window as any).global = window;
(window as any).process = {};
(window as any).process.nextTick = setTimeout;

Upvotes: 3

Max Kuchenkiller
Max Kuchenkiller

Reputation: 113

Cause i just had this Problem, For Angular 2 + Typescript the correct way to use PouchDB (using angular-cli) is to:

  1. ng new SOMENAME
  2. npm install --save pouchdb
  3. npm install --save-dev @types/pouchdb
  4. In your app.component import PouchDB from 'pouchdb';
  5. In your App Component Class public db: any; and to init this.db = new PouchDB('test'); // , {storage:'persistent'} not working in typescript without updating typings

see https://github.com/nolanlawson/pouchdb-find/issues/201.

If you have problems installing the packages on windows with an EPERM Error use (f.e.) npm install --save pouchdb --no-optional to disable the warning. The installation should still be ok. For more info see https://github.com/npm/npm/issues/17671

Upvotes: 3

leetheguy
leetheguy

Reputation: 872

I'm doing this in Ionic, so I may be missing a step on getting the types file loaded properly.

Make sure your types are installed with:

npm install --save-dev @types/pouchdb

At the top of your data service import pouch like so:

import * as PouchDB from 'pouchdb';

* edit *

I don't have all the facts, but this is my current understanding. Typings is no longer needed in Typescript >2.0 I believe typescript now works automatically with types files installed from DefinitelyTyped. DefinitelyTyped is an official central repository that is kept current like npm. And even if I'm dead wrong about all this, DefinitelyTyped is still better than typings and has a much bigger community.

Upvotes: 4

user2302244
user2302244

Reputation: 935

I managed to get the module recognised by using

declare function require(a)

var PouchDB = require("pouchdb")

I have given up type checking, but at least I can make progress.

Upvotes: 0

Related Questions