This library adds promises to PouchDB API to make PouchDB easier to use with Ember.js.
PouchDB is a CouchDB inspired database designed for the browser. It uses IndexedDB in Firefox and Chrome, WebSQL in Safari and Opera and LevelDB in Node.js.
It is currently tested in:
- Firefox 12+
- Chrome 19+
- Opera 12+
- Safari 5+
- Internet Explorer 10+
- Node.js 0.10+
- Apache Cordova
Note: This library is currently only tested in Chrome. Submit an issue if you find problems with other PouchDB compatible environments.
This library requires PouchDB library to be included in the page before this library.
PouchDB
Ember PouchDB
<script src="//cdn.jsdelivr.net/pouchdb/2.2.3/pouchdb.js"></script>
<script src="https://raw.github.com/taras/ember-pouchdb/master/dist/ember-pouchdb.js"></script>
bower install ember-pouchdb --save
<script src="bower_components/pouchdb-nightly.js/index.js"></script>
<script src="bower_components/ember-pouchdb/dist/ember-pouchdb.js"></script>
You can use the API via an app instance or dependancy injection.
Use this method if you're not using Ember App Kit or ES6 Modules.
var App = Ember.Application.create();
// Create a model
App.Photo = EPDB.Model.extend({
// serialize method returns an object that's stored in the database
serialize: function(){
return this.getProperties(['name','description','image']);
}
});
// Create an instance that you'll use to perform operations
App.pouch = EPDB.Storage.create({
dbName: "your-app",
docTypes: {
photo: App.Photo
}
});
App.Router.map(function(){
this.route('photos');
this.route('photo', {path:'photo/:id'});
});
App.PhotosRoute = Ember.Route.extend({
model: function() {
return App.pouch.findAll('photo');
}
});
App.PhotoRoute = Ember.Route.extend({
model: function(params) {
return App.pouch.GET(params.id);
}
});
Use this method with Ember App Kit or ES6 Modules.
app/app.js
import pouchdb_initializer from 'ember-pouchdb/initializer';
import Photo from 'appkit/models/photo';
import routes from 'appkit/routes;
var App = Ember.Application.create();
App.initializer(pouchdb_initializer({
docTypes: {
photo: App.PhotoModel
}
}));
App.Router.map(routes);
export default App;
app/routes.js
var routes = function() {
this.route('photos');
this.route('photo', {path:'photo/:id'});
}
export default routes;
app/models/photo.js
import Model from 'ember-pouchdb/model';
var Photo = Model.extend({
serialize: function(){
return this.getProperties(['name', 'description', 'image']);
}
});
export default Photo;
app/routes/photos.js
var PhotosRoute = Ember.Route.extend({
model: function() {
return this.pouch.findAll('photo');
}
});
export default PhotosRoute;
app/routes/photo.js
var PhotoRoute = Ember.Route.extend({
model: function(params) {
return this.pouch.GET(params.id);
}
});
export default PhotoRoute;
Method that returns an initializer object that can be passed to App.initializer() method.
docTypes is the only option that you must specify to be able to use this library. docTypes tells the Storage class how to idenfify models that you will later pass to perform operations. Its an object with keys as simple names for the docType and class of the Model as value. 99% of the time, you would want your Model classes to extend from EPDB.Model class.
var options = { // Default options that will be returned if you don't pass an empty options object.
/**
* Name of the initializer
* @type {String}
*/
name: "PouchDB",
/**
* Property that the instance will be injected onto
* @type {String}
*/
propName: "pouch",
/**
* Name of the database
* @type {String}
*/
dbName: "PouchDB",
/**
* fullName of the type to register
* @link https://github.com/emberjs/ember.js/blob/master/packages/container/lib/main.js#L258
* @type {String}
*/
fullName: 'pouch:main',
/**
* Types to inject property into
* @type {Array}
*/
types: [ 'controller', 'route' ],
/**
* Object with docType as key and model class as value
* Example:
* {
* 'photo': App.PhotoModel,
* 'product': App.ProductModel
* }
* @type {Object}
*/
docTypes: {}
}
This is a very simple Model class.
EPDB.Model = Ember.Object.extend( Ember.Copyable, {
copy: function() {
var serialized = this.serialize();
serialized.id = this.get('id');
serialized.rev = this.get('rev');
return this.constructor.create(serialized);
},
serialize: function() {
throw new Error(Ember.String.fmt("%@ must implement serialize() method which returns JSON of this model.", [this]));
}
});
Implements CopyableMixin and defines a serialize()
method. CopyableMixin provides copy()
method, which returns a new instance of model that's created from the same class with properties taken from this.serialize()
.
serialize()
method must return an object with properties and values for this object. This method is used to extract values from this object that are stored in the PouchDB database.
EPDB.Storage class is the Ember PouchDB wrapper class. Creating an instance of this class will automatically open a PouchDB database that you can manipulate with the instance of this class.
- dbName - required, string to identify this database. It an be a simple string (ie. "my-pouch") or a url of a remote CouchDB database
- docTypes - required, object with simple strings as properties and model classes as values.
Retrieve a document with the provided id and return a promise that will resolve to an instance of a model of the retrieved document.
Create a document in the database for the provided model and return a promise that will resolve to a model with id and rev for the created document.
Update a document in the database for the provided model and return a promise that will resolve to a model with updated rev.
Delete document for the provided model. It will not destroy the model instance.
Retrieve from the database all documents of the specified type and return a promise that will resolve with an array of model instances.
Delete the database and return a promise that will resolve after the database.
npm install
bower install
In terminal: grunt test
In browser:
grunt server
- Go to http://localhost:8000/test
$ grunt release[:patch|minor|major|git]