Giter Club home page Giter Club logo

ember-pouch's Introduction

Ember Pouch

Build Status

Changelog

Ember Pouch is a PouchDB/CouchDB adapter for Ember Data.

With Ember Pouch, all of your app's data is automatically saved on the client-side using IndexedDB or WebSQL, and you just keep using the regular Ember Data store API. This data may be automatically synced to a remote CouchDB (or compatible servers) using PouchDB replication.

What's the point?

  1. You don't need to write any server-side logic. Just use CouchDB.

  2. Data syncs automatically.

  3. Your app works offline, and requests are super fast, because they don't need the network.

For more on PouchDB, check out pouchdb.com.

Install and setup

ember install ember-pouch

This provides

  • import PouchDB from 'pouchdb'
  • import {Model, Adapter, Serializer} from 'ember-pouch'

Ember-Pouch requires you to add a rev: DS.attr('string') field to all your models. This is for PouchDB/CouchDB to handle revisions:

// app/models/todo.js

import DS from 'ember-data';

export default DS.Model.extend({
  title       : DS.attr('string'),
  isCompleted : DS.attr('boolean'),
  rev         : DS.attr('string')    // <-- Add this to all your models
});

If you like, you can also use Model from Ember-Pouch that ships with the rev attribute:

// app/models/todo.js

import DS from 'ember-data';
import { Model } from 'ember-pouch';

export default Model.extend({
  title       : DS.attr('string'),
  isCompleted : DS.attr('boolean')
});

Configuring /app/adapters/application.js

A local PouchDB that syncs with a remote CouchDB looks like this:

// app/adapters/application.js

import PouchDB from 'pouchdb';
import { Adapter } from 'ember-pouch';

var remote = new PouchDB('http://localhost:5984/my_couch');
var db = new PouchDB('local_pouch');

db.sync(remote, {
   live: true,   // do a live, ongoing sync
   retry: true   // retry if the conection is lost
});

export default Adapter.extend({
  db: db
});

You can also turn on debugging:

import PouchDB from 'pouchdb';

PouchDB.debug.enable('*');

See the PouchDB sync API for full usage instructions.

Sample app

Tom Dale's blog example using Ember CLI and EmberPouch: broerse/ember-cli-blog

Notes

LocalStorage

Currently PouchDB doesn't use LocalStorage unless you include an experimental plugin. Amazingly, this is only necessary to support IE โ‰ค 9.0 and Opera Mini. It's recommended you read more about this, what storage mechanisms modern browsers now support, and using SQLite in Cordova on the PouchDB adapters page.

CouchDB

From day one, CouchDB and its protocol have been designed to be always Available and handle Partitioning over the network well (AP in the CAP theorem). PouchDB/CouchDB gives you a solid way to manage conflicts. It is "eventually consistent," but CouchDB has an API for listening to changes to the database, which can be then pushed down to the client in real-time.

To learn more about how CouchDB sync works, check out the PouchDB guide to replication.

Plugins

With PouchDB, you also get access to a whole host of PouchDB plugins.

Relational Pouch

Ember Pouch is really just a thin layer of Ember-y goodness over Relational Pouch. Before you file an issue, check to see if it's more appropriate to file over there.

Offline first

If you want to go completely offline-first, you'll also need an HTML5 appcache.manifest with broccoli-manifest. This will allow your HTML/CSS/JS assets to load even if the user is offline. Plus your users can "add to homescreen" on a mobile device (iOS/Android).

Security

An easy way to secure your Ember Pouch-using app is to ensure that data can only be fetched from CouchDB โ€“ not from some other sever (e.g. in an XSS attack).

To do so, add a Content Security Policy whitelist entry to /config/environment.js:

ENV.contentSecurityPolicy = {
  "connect-src": "'self' http://your_couch_host.com:5984"
};

Ember CLI includes the content-security-policy plugin by default to ensure that CSP is kept in the forefront of your thoughts. You still have actually to set the CSP HTTP header on your backend in production.

CORS setup (important!)

To automatically set up your remote CouchDB to use CORS, you can use the plugin add-cors-to-couchdb:

npm install -g add-cors-to-couchdb
add-cors-to-couchdb http://your_couch_host.com:5984 -u your_username -p your_password

Multiple models for the same data

Ember-data can be slow to load large numbers of records which have lots of relationships. If you run into this problem, you can define multiple models and have them all point to the same set of records by defining documentType on the model class. Example (in an ember-cli app):

// app/models/post.js

import DS from 'ember-data';
import { Model } from 'ember-pouch';

export default Model.extend({
    title: DS.attr('string'),
    text: DS.attr('string'),

    author: DS.belongsTo('author'),
    comments: DS.hasMany('comments')
});


// app/models/post-summary.js

import DS from 'ember-data';
import { Model } from 'ember-pouch';

var PostSummary = Model.extend({
    title: DS.attr('string'),
});

PostSummary.reopenClass({
  documentType: 'post'
})

export default PostSummary;

The value for documentType is the camelCase version of the primary model name.

For best results, only create/update records using the full model definition. Treat the others as read-only.

Installation

  • git clone this repository
  • npm install
  • bower install

Running

Running Tests

  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit http://www.ember-cli.com/.

Credits

This project was originally based on the ember-data-hal-adapter by @locks, and I really benefited from his guidance during its creation.

And of course thanks to all our wonderful contributors, here and in Relational Pouch!

Changelog

  • 2.0.3
    • Use Ember.get to reference the PouchDB instance property in the adapter (db), allowing it to be injected (#84). Thanks to @jkleinsc!
    • Indicate to ember-data 1.13+ that reloading individual ember-pouch records is never necessary (due to the change watcher that keeps them up to date as they are modified) (#79, #83).
  • 2.0.2 - Use provide findRecord for ember-data 1.13 and later thanks to @OleRoel (#72)
  • 2.0.1 - Fixed #62 thanks to @rsutphin (deprecated typekey in Ember-Data 1.0.0-beta.18)
  • 2.0.0 - Ember CLI support, due to some amazing support by @fsmanuel! Bower and npm support are deprecated now; you are recommended to use Ember CLI instead.
  • 1.2.5 - Last release with regular Bower/npm support via bundle javascript in the dist/ directory.
  • 1.0.0 - First release

ember-pouch's People

Contributors

nolanlawson avatar rsutphin avatar locks avatar fsmanuel avatar oleroel avatar larsjk avatar backspace avatar scandinave avatar broerse avatar brianmbutler avatar devinrhode2 avatar jkleinsc avatar mize avatar

Watchers

James Cloos avatar Steve Jabour avatar

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.