Advertisement
cbigot

Untitled

Feb 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { TodoImmutable, Todo } from '../entities/Todo';
  2. import PouchDB from 'pouchdb';
  3. import pouchDBUpsert from 'pouchdb-upsert';
  4. import { fromJS } from 'immutable';
  5. import { CodeStatus, Action } from '../constants/constants';
  6.  
  7. // Injection du plugin upsert dans l'api PouchDB
  8. PouchDB.plugin(pouchDBUpsert);
  9.  
  10. // Déclaration de la variable de base de données
  11. let todoDatabase: PouchDB.Database;
  12.  
  13. // Déclaration de la méthode d'instanciation de la base
  14. const getDB = () => new Promise((resolve) => {
  15.         // 1 - Si la base n'existe pas, on la créée
  16.         if (todoDatabase === null || todoDatabase === undefined) {
  17.             todoDatabase = new PouchDB('TodoDB', { revs_limit: 1, auto_compaction: true });
  18.             return resolve(todoDatabase);
  19.         }
  20.         else {
  21.             // 2 - Si il y a un problème de communication avec la base, on refait l'instanciation
  22.             todoDatabase.info().catch((err) => {
  23.                 todoDatabase = new PouchDB('TodoDB', { revs_limit: 1, auto_compaction: true });
  24.                 return Promise.resolve();
  25.             }).then(() => {
  26.                 return resolve(todoDatabase);
  27.             });
  28.         }
  29. });
  30.  
  31. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement