Hi Sander and All,

Hope your weekend is doing well. Can someone help me please with pouchdb 
please. It is driving me nuts. You can see my code here 
https://stackblitz.com/edit/angular-akrcz9

What happens is I can have any number of app settings so I might update 
just one field of the document. And the update can happen from two, three 
places at the same time since the app have differnet activities that happen 
at the back. My prefered option was to use localStorage but the client 
wants all to be stored in pouchdb. I run into 409 error but do see the 
changes in the revisions, just not in the document itself. 


import { Injectable } from "@angular/core";


import PouchDB from "pouchdb";

@Injectable()
export class AppService {
  private settingsDb; //to store app state

  constructor() {
    // indicates if we are in the process of syncing or not.
    this.settingsDb = new PouchDB("field-support-app-state", {
      auto_compaction: true
    });
  }

  updateAppState(fields) {
    /*
      Create a document if it doesnt exist else update it
      if creating a new, set all to default then update as
      necessary.

      Fields is dictionary of fields to update
      e.g.
        {
            'field1':'field1 value'
        }
    
      we can update the fields we want only
        */

    let vm = this;

    const docId = "_local/appState";

    this.settingsDb.get(docId, function(err, doc) {
      if (err) {
        // cant get the info so create the document now with all the fields
        console.log("config read error; possibly not found ", err);
        let data = { _id: docId };
        data["field1"] = "";
        data["field2"] = "";
        data["field3"] = "";

        data["lastNewDataDownloadedDisplayedOn"] = "";
        //replace as necessary now
        Object.keys(fields).forEach(function(key) {
          data[key] = fields[key];
        });
        vm.settingsDb.put(data);
      } else {
        // document exists already
        // so make changes only then put back the whole document

        Object.keys(fields).forEach(function(key) {
          doc[key] = fields[key];
        });

        vm.settingsDb.put(doc);
      }
    });
  }
}


The method would be called from number of places with the field needed for 
an update. To illustrate, this is from component.ts:

import { Component } from '@angular/core';
import {AppService} from './app.service';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
    constructor(
    public appSerivce: AppService
  ) {

            this.appSerivce.updateAppState({
          lastSyncErrorDisplayedon: ""
        })

  this.randomUpdates();
  }



  randomUpdates(){
    let fields = ['field1', 'lastUpdatedLocationOn','lastItemMovedOn'];
    const vm : any = this;
    fields.forEach(field=>{

      setInterval(
        vm.appSerivce.updateAppState({
        field : new Date().toLocaleString()
      }) , 
        
        3000);

      
    });
  }




}


What I want is to be able to update fields of the document in a flexible 
way. As the app grows, there is a chance a specific omponent or feature 
might want to save its own setting and i don't want to create document for 
each one tho that is also possible if absolutellly necessary.

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/angular/1904ece0-7497-4686-85c7-3f066f71cd61%40googlegroups.com.

Reply via email to