How to use pouchdb-browser - 10 common examples

To help you get started, we’ve selected a few pouchdb-browser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cozy / cozy-client-js / test / unit / offline.js View on Github external
/* eslint-env mocha */

// eslint-disable-next-line no-unused-vars
import 'isomorphic-fetch'
import should from 'should'
import { Client } from '../../src'
import PouchDB from 'pouchdb-browser'
import pouchdbFind from 'pouchdb-find'
PouchDB.plugin(require('pouchdb-adapter-memory'))

// PouchDB should not be a mandatory dependency as it is only used in mobile
// environment, so we declare it in global scope here.
global.PouchDB = PouchDB
global.pouchdbFind = pouchdbFind

describe('offline', () => {
  const fileDoctype = 'io.cozy.files'
  const otherDoctype = 'io.cozy.others'
  const cozyUrl = 'http://cozy.tools:8080/'
  let offlineParameter = {
    doctypes: [fileDoctype, otherDoctype],
    options: { adapter: 'memory' }
  }
  const cozy = {}
github EragonJ / Kaku / src / modules / Database.js View on Github external
import PouchDB from 'pouchdb-browser';
let opt = {};

if (global && global.IS_TEST === true) {
  PouchDB.plugin(require('pouchdb-adapter-memory'));
  opt.adapter = 'memory';
}
else {
  opt.adapter = 'idb';
}

const KakuDB = new PouchDB('kaku', opt);

// Note:
// Because we add something new in the prototype chain and this is not safe,
// we added some prefix to make it unique
PouchDB.prototype.resetDatabase = function() {
  return this.destroy().catch((error) => {
    console.log('Something goes wrong when dropping database');
    console.log(error);
  });
github EragonJ / Kaku / src / modules / Database.js View on Github external
let opt = {};

if (global && global.IS_TEST === true) {
  PouchDB.plugin(require('pouchdb-adapter-memory'));
  opt.adapter = 'memory';
}
else {
  opt.adapter = 'idb';
}

const KakuDB = new PouchDB('kaku', opt);

// Note:
// Because we add something new in the prototype chain and this is not safe,
// we added some prefix to make it unique
PouchDB.prototype.resetDatabase = function() {
  return this.destroy().catch((error) => {
    console.log('Something goes wrong when dropping database');
    console.log(error);
  });
};

module.exports = KakuDB;
github WorldBrain / Memex / src / pouchdb.js View on Github external
import PouchDBErase from 'pouchdb-erase'
import { blobToBase64String, arrayBufferToBlob } from 'blob-util'
import { pageKeyPrefix, pageDocsSelector } from 'src/page-storage'
import { visitKeyPrefix } from 'src/activity-logger'
import { bookmarkKeyPrefix } from 'src/search/bookmarks'
import { normalizeAndEncode } from 'src/util/encode-url-for-id'

PouchDB.plugin(PouchDBFind)
PouchDB.plugin(PouchDBErase)

const pouchdbOptions = {
    name: 'webmemex',
    auto_compaction: true,
}

const db = PouchDB(pouchdbOptions)
export default db

// DEBUG Expose db for debugging or direct user access.
window.db = db

// The couch/pouch way to match keys with a given prefix (e.g. one type of docs).
export const keyRangeForPrefix = prefix => ({
    startkey: `${prefix}`,
    endkey: `${prefix}\uffff`,
})

// Present db.find results in the same structure as other PouchDB results.
export const normaliseFindResult = result => ({
    rows: result.docs.map(doc => ({
        doc,
        id: doc._id,
github QurateInc / vue-pouch-db / src / index.js View on Github external
// If DB Exists return it
    if (this._dbs[dbname]) {
      return this._dbs[dbname];
    }

    // Init only remote
    if (config.remoteOnly) {
      this._dbs[dbname] = new PouchDB(
        `${config.remote}/${dbname}`,
        config.options
      );
      return this._dbs[dbname];
    }

    // Init DB
    this._dbs[dbname] = new PouchDB(dbname, config.options);

    // Populate state with data
    this._dbs[dbname].allDocs(config.allDocs).then((data) => {
      return Vue.set(this._state, dbname, data.rows.map((row) => row.doc));
    });

    // Sync DB
    PouchDB.sync(
      dbname,
      `${config.remote}/${dbname}`,
      config.sync
    );

    // Start detecting changes
    this._initChanges(dbname, config);
github TheDevPath / Navi / client / src / js / leaflet-tileLayer-pouchdb-cached.js View on Github external
L.TileLayer.addInitHook(function() {

	if (!this.options.useCache) {
		this._db     = null;
		this._canvas = null;
		return;
	}

	this._db = new PouchDB('offline-tiles');
	this._canvas = document.createElement('canvas');
	if (!(this._canvas.getContext && this._canvas.getContext('2d'))) {
		// HTML5 canvas is needed to pack the tiles as base64 data. If
		//   the browser doesn't support canvas, the code will forcefully
		//   skip caching the tiles.
		this._canvas = null;
	}
});
github QurateInc / vue-pouch-db / src / index.js View on Github external
_initDB(dbname, config = {}) {
    // If DB Exists return it
    if (this._dbs[dbname]) {
      return this._dbs[dbname];
    }

    // Init only remote
    if (config.remoteOnly) {
      this._dbs[dbname] = new PouchDB(
        `${config.remote}/${dbname}`,
        config.options
      );
      return this._dbs[dbname];
    }

    // Init DB
    this._dbs[dbname] = new PouchDB(dbname, config.options);

    // Populate state with data
    this._dbs[dbname].allDocs(config.allDocs).then((data) => {
      return Vue.set(this._state, dbname, data.rows.map((row) => row.doc));
    });

    // Sync DB
    PouchDB.sync(
github Aam-Digital / ndb-core / src / app / database / pouch-database-manager.service.ts View on Github external
constructor(private alertService: AlertService) {
    super();

    this._localDatabase = new PouchDB(AppConfig.settings.database.name);
    this._remoteDatabase = new PouchDB(AppConfig.settings.database.remote_url + AppConfig.settings.database.name,
      {
        ajax: {
          rejectUnauthorized: false, timeout: AppConfig.settings.database.timeout,
        },
        // This is a workaround for PouchDB 7.0.0 with pouchdb-authentication 1.1.3:
        // https://github.com/pouchdb-community/pouchdb-authentication/issues/239
        // It is necessary, until this merged PR will be published in PouchDB 7.0.1
        // https://github.com/pouchdb/pouchdb/pull/7395
        fetch(url, opts) {
          opts.credentials = 'include';
          return (PouchDB as any).fetch(url, opts);
        },
        skip_setup: true
      } as PouchDB.Configuration.RemoteDatabaseConfiguration
    );
github QurateInc / vue-pouch-db / src / index.js View on Github external
// Internal State
    this._dbs   = {};
    this._watch = {};
    this._state = {};

    // Throw Error if Global Config not defined
    if (!schema.config) throw new Error('[VuePouchDB]: Global Config is not declared in the upper level!');

    // Referencing Actions to the $bucket
    if (schema.actions) merge(this, schema.actions);

    // Init PouchDB plugins
    if (Array.isArray(schema.plugins) && (schema.plugins.length > 0)) {
      for (let i = 0; i < schema.plugins.length; i += 1) {
        PouchDB.plugin(schema.plugins[i]);
      }
    }

    // Initializing DBs that are declared in the schema{}
    Object.keys(schema).forEach((dbname) => {
      // If is ignored Key, skip!
      if (ignoredKeys.indexOf(dbname) !== -1)  return null;
      // Initialize the DB
      return this._initDB(dbname, merge(
        {},
        schema.config,
        schema[dbname]
      ));
    });
  }
github atomiclabs / hyperdex / app / renderer / swap-db.js View on Github external
import PouchDB from 'pouchdb-browser';
import pouchDBFind from 'pouchdb-find';
import pouchDBUpsert from 'pouchdb-upsert';
import cryptoPouch from 'crypto-pouch';
import Emittery from 'emittery';
import PQueue from 'p-queue';
import roundTo from 'round-to';
import {subDays, isAfter} from 'date-fns';
import appContainer from 'containers/App';
import {appTimeStarted} from '../constants';
import {translate} from './translate';

const t = translate('swap');

PouchDB.plugin(pouchDBFind);
PouchDB.plugin(pouchDBUpsert);
PouchDB.plugin(cryptoPouch);

class SwapDB {
	constructor(portfolioId, seedPhrase) {
		// Using `2` so it won't conflict with HyperDEX versions using marketmaker v1.
		this.db = new PouchDB(`swaps2-${portfolioId}`, {adapter: 'idb'});

		this.db.crypto(seedPhrase);

		const ee = new Emittery();
		this.on = ee.on.bind(ee);
		this.off = ee.off.bind(ee);
		this.once = ee.once.bind(ee);

		this.db.changes({
			since: 'now',