Louis
Louis

Reputation: 2618

How to properly use pouchdb in Angular2 and Ionic2?

I am trying to convert to Angular 2 (Ionic2) and my app uses pouchDB.

In Angular1 I was using var _usersDatabase = pouchDB(CouchConstants.COUCHDB_USERS_DB_NAME); with a lowercase pouchDB and no new. But the TypeScript compiler is complaining, so I did that:

import { Injectable } from '@angular/core';
import { LocalStorageService } from 'angular-2-local-storage';
import { UtilsService } from '../providers/utils-service';
import { MigrationService } from '../providers/migration-service';
import { CouchConstants } from '../couch-constants';
import PouchDB from 'pouchdb';




@Injectable()
export class UsersDatabase {



    constructor(
        private storageService: LocalStorageService
        , private UtilsService: UtilsService
        , private MigrationService: MigrationService

    ) {


    'use strict';


    var _usersDatabase = new PouchDB(CouchConstants.COUCHDB_USERS_DB_NAME);
    //We attach the function if necessary
    if (_usersDatabase.updateReplication == null) {
        _usersDatabase.updateReplication = function (newDocsIds) {
                    console.info("Updating replications with: ");
                    console.info(newDocsIds);
                    ...

but I get this error:

Typescript Error
Property 'updateReplication' does not exist on type 'Database<{}>'.
src/providers/users-database.ts
    //We attach the function if necessary
    if (_usersDatabase.updateReplication == null) {

Upvotes: 0

Views: 1222

Answers (2)

Max Kuchenkiller
Max Kuchenkiller

Reputation: 113

I just installed PouchDB For Angular 2 + Typescript using the angular-cli. This is what I did and it works for me:

  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: 1

nlawson
nlawson

Reputation: 11620

To import PouchDB in a TypeScript project, you need to do:

// Correct
import * as PouchDB from 'pouchdb';

rather than:

// Incorrect
import PouchDB from 'pouchdb';

Also I don't believe updateReplication is a supported PouchDB API, which is probably why you're seeing the TypeScript warning.

Upvotes: 1

Related Questions