Giter Site home page Giter Site logo

wearegenki / db Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 682 KB

Vue plugin for reactive, offline capable databases.

Home Page: https://npmjs.com/package/@wearegenki/db

License: Apache License 2.0

JavaScript 100.00%
vue vue-plugin vuex-plugin offline-first offline-capable pouchdb couchdb couchbase wearegenki

db's Introduction

WARNING: This project is no longer maintained. We decided to migrate away from Vue to Svelte. See minna-ui, our new web UI framework.


Build status NPM version Licence

@wearegenki/db

Vue plugin for reactive, offline capable databases — npmjs.com/package/@wearegenki/db.

NOTE: This package uses various ES6/ES7 features and needs to be transpiled to suit your browser compatibility policy. We don't handle any transpilation in the package itself, it's just the raw source code. You also need a way to load the web worker script.

Overview

Included PouchDB plugins:

  1. pouchdb-adapter-idb
  2. pouchdb-adapter-http
  3. pouchdb-replication

Usage

NOTE: These instructions assume you're using webpack.

1. Install plugin and dependencies

yarn add @wearegenki/db \
  && yarn add --dev workerize-loader

2. Import the plugin

NOTE: See configuration options below.

PRO TIP: This is also a good place to put any custom database methods too.

Create a file to import the plugin and set your configuration, e.g. services/db.js:

import Vue from 'vue';
import Database from '@wearegenki/db';
import vuex from '../store'; // your vuex store

Vue.use(Database);

const db = new Database.Connection({
  vuex,
  local: 'app',
  remote: 'http://localhost:4984/your_db_name', // optional
  queries: [ // set up any reactive queries, optional
    ['docs', [
      '_local/user',
    ]],
  ],
  // debug: '*', // all logs
});

export default db;

3. Inject into the root vue instance

E.g. main.js:

import Vue from 'vue';
import store from './store';
import db from './services/db';
import App from './App';

new Vue({
  el: '#app',
  store,
  db,
  render: h => h(App),
});

4. Use in vue components or JS

NOTE: Keep in mind most of the plugin methods return a Promise so you need to use await or .then() before you can access the resulting value.

You can now use the plugin in vue components like this:

this.$db.get('your_doc_id').then((res) => {
  console.log(res);
});

Or in any JS file like this:

import db from './services/db';

db.get('your_doc_id').then((res) => {
  console.log(res);
});

Options

Option Default Required Value/s Description
local 'app' No <string> The name for the local database. Names get appended to "_pouch_" so the default turns in to _pouch_app. It's also possible to use a URL to bypass saving any data locally and only communicate with a remote database.
remote undefined No* <string> The URL to your remote database, e.g. https://your-site.com:5984/your_db. *Only required if you want syncing functionality.
filter undefined No A PouchDB replication filter See the filtered replication section in the PouchDB docs.
vuex undefined No* <vuex instance> A reference to your vuex store. *Only required when you want to use reactive queries or the status property.
queries [] No Valid input for Map(), see example below Register reactive queries. See queries section below.
namespace 'db' No <string> Namespace for the vuex module. Determines the path to the vuex properties and methods.
createRemote false No <Boolean> Create remote database if it doesn't already exist. Only works with CouchDB (not Couchbase Sync Gateway). Default = assume remote database already exists.
sync true No <Boolean> Enables replication between the local and remote databases. The sync() method is useful to trigger a manual sync when this is set to false, see API methods below. remote is required for this to have any effect.
debounce 300 No <Number> Amount of milliseconds to debounce between executing queries when document changes are detected. This helps to limit unnecessary CPU usage when a flood of documents are syncing from the remote database, like when a user first syncs, in which case we want to delay running queries until the network traffic settles or the sync is complete.
pushCp 'source' No 'source' | 'target' | <Boolean> Dictates where should checkpoints be saved. Default = only local database will save checkpoints, which results in less network traffic for better performance but at a small risk of data loss if the remote database is ever destroyed. For more information see PouchDB docs and the feature discussion on GitHub.
pullCp 'target' No 'source' | 'target' | <Boolean> Same as pushCp but for docs coming from remote. Default = only local database will save checkpoints.
status false No <Boolean> Expose if the sync status is OK by adding a reactive vuex property <namespace>/ok. Only use this when necessary because it has a performance impact (due to its event listeners).
debug undefined No '*' | 'pouchdb:api' | pouchdb:http Enable PouchDB's debug mode.

Queries

Queries are a way to get a set of documents which match certain criteria easily. Use the db.query() method to do a one-time query. One-time queries are great, however, a key feature of this library is reactive queries, which can be set as a initial option or any time via the db.register() method. Please note this is different from mango queries — it's much simpler leading to much better performance.

NOTE: To use reactive queries a vuex store is required.

Query format

{
  id: 'doc_id_prefix', // doc _id prefix to search
  filter: [ // optional
    'field_to_filter_by',
    '>', // comparison operator (>, <, >=, <=, or when undefined ===)
    'matching_value',
  ],
  sort: 'field_to_sort_by', // optional
  limit: 100, // optional
  start: 'doc_id_prefix', // optional, doc _id prefix to start search from (and end at `prefix`) for pagination
}

Example queries

One-time query

SimpleComponent.vue:

<script>
export default {
  mounted() {
    this.getFeedbackList().then((res) => {
      console.log(res);
    });
  },
  methods: {
    // simple array of "feedback" docs sorted by score
    getFeedbackList() {
      return this.$db.query({
        id: 'feedback',
        sort: 'score',
      });
    }
  },
};
</script>

Reactive query

ReactiveComponent.vue:

<script>
import { mapState } from 'vuex';

export default {
  computed: mapState('db', [
    'topBooks', // you can use the topBooks array in your vue template
  ]),
  created() {
    this.getTopBooks();
  },
  beforeDestroy() {
    this.$db.unregister('topBooks');
  },
  methods: {
    async getTopBooks() {
      this.$db.register({
        id: 'book',
        filter: ['publishDate', '>=', '2015-03-25'],
        sort: 'title',
        limit: 10,
      }, 'topBooks');
    },
  },
};
</script>

API Methods

PRO TIP: It's recommended to call methods on the local database so to provide "offline first" support for your projects.

Direct database access

local(method, ...opts)

Run a PouchDB method against the local database.

#TODO: Write description and give example.

remote(method, ...opts)

Run a PouchDB method against the remote database.

#TODO: Write description and give example.

Convenience wrapper methods

For developer convenience the most common PouchDB methods are available directly.

get(_id)

#TODO: Write description and give example.

put(doc)

#TODO: Write description and give example.

remove(doc)

#TODO: Write description and give example.

Additional methods

Extra API methods unique to this plugin; not part of PouchDB.

waitFor(_id, newOnly, timeout)

#TODO: Write description and give example.

register(query, key)

#TODO: Write description and give example.

unregister(key, isDoc)

#TODO: Write description and give example.

query({ id, filter, sort, limit, start })

#TODO: Write description and give example.

sync(retry)

#TODO: Write description and give example.

upsert(_id, diff)

#TODO: Write description and give example.

rev()

#TODO: Write description and give example.

md5(string)

#TODO: Write description and give example.

Useful variables

ready

A promise that resolves once the initial remote to local replication has finished. A.K.A. once the initial data has finished downloading.

Licence

@wearegenki/db is an Apache-2.0 licensed open source project. See LICENCE.


© 2018 We Are Genki

db's People

Contributors

maxmilton avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

aral

db's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

File: .renovaterc.json
Error type: The renovate configuration file contains some invalid settings
Message: Invalid configuration option: enable

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper integration’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Authentication strategies?

First off, thank you for authoring and sharing this – it sounds awesome.

I was just wondering how you’re handling authentication for the app(s) in which you’re using this (for the remote sync).

I realise we’re pulling vuex in a direction the Vue folks don’t necessarily want it to go (https://forum.vuejs.org/t/vuejs-vuex-data-synchronization-with-server-backend/16340/17) and so it’s not necessarily realistic to expect issues like authentication to be integrated but it is also a direction that makes perfect sense moving forward and looking towards a decentralised/p2p web. For an example of how a similar project (not offline-first) is handling it, see https://github.com/feathers-plus/feathers-vuex

Thanks again for your great work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.