Skip to content

Instantly share code, notes, and snippets.

@abellion
Created April 28, 2016 12:58
Show Gist options
  • Save abellion/ab1c71118b7988c16165c02c97c99bad to your computer and use it in GitHub Desktop.
Save abellion/ab1c71118b7988c16165c02c97c99bad to your computer and use it in GitHub Desktop.
PouchDB base class
(function() {
angular
.module('app')
.factory('my_visits', my_visits);
my_visits.$inject = ['$rootScope', '$q', 'my_pouchdb'];
function my_visits( $rootScope, $q, my_pouchdb) {
var self = angular.extend(this, my_pouchdb);
self.db = new PouchDB('public');
self.Type = 'visits';
self.views = {
'all': {
'map': function(doc) {
doc['Type'] === 'visits' && emit(doc['_id']);
}
}
};
/**
* Add a new visit
* @param {object} data Visit data
* @return {promise} Success / Error
*/
self.post = function(data) {
data['creationDate'] = new Date().toJSON();
data['updateDate'] = data['creationDate'];
return my_pouchdb.post.call(self, data);
};
/**
* Update a visit
* @param {object} data Visit data
* @return {promise} Success / Error
*/
self.update = function(data) {
data['updateDate'] = new Date().toJSON();
return my_pouchdb.update.call(self, data);
};
return self;
}
}());
(function() {
angular
.module('app')
.factory('my_pouchdb', my_pouchdb);
my_pouchdb.$inject = ['$rootScope', '$q'];
function my_pouchdb( $rootScope, $q) {
var self = this;
/**
* Post data (create uniq ID)
* @param {object} data Data to insert
* @return {promise} Success : the id / error
*/
self.post = function(data) {
var deferred = $q.defer();
this.db.post(angular.extend(data, {
'Type': this.Type
})).then(function(response) {
return deferred.resolve(response['id']);
}, deferred.reject);
return deferred.promise;
};
/**
* Put data
* @param {object} data The object to put
* @return {promise} Success / Error
*/
self.put = function(data) {
return this.db.put(angular.extend(data, {
'Type': this.Type
}));
};
/**
* Get a doc
* @param {string} id Document id
* @return {promise} Success : doc object / Error
*/
self.get = function(id) {
var deferred = $q.defer();
this.db.get(id).then(function(response) {
return deferred.resolve(response);
}, deferred.reject);
return deferred.promise;
};
/**
* Update a doc, merge the new and old one (prevent conflicts)
* @param {object} data Document to update (must contain the id)
* @return {promise} Success / Error
*/
self.update = function(data) {
var that = this;
var deferred = $q.defer();
this.get(data['_id']).then(function(res) {
var rev = res['_rev'];
res = angular.extend(res, data);
res['_rev'] = rev;
return deferred.resolve(that.put(res));
}, deferred.reject);
return deferred.promise;
};
/**
* Call the view 'all' and return the docs
* @return {promise} Success : array / Error
*/
self.getAll = function() {
var deferred = $q.defer();
var Type = this.Type;
this.db.query(Type+ '/all', { 'include_docs': true }).then(function(docs) {
return deferred.resolve(docs.rows.map(function(item) {
return item['doc'];
}));
}, deferred.reject);
return deferred.promise;
};
/**
* Remove the doc
* @param {object} doc The doc to remove (must contain _id and _rev)
* @return {[type]} [description]
*/
self.remove = function(doc) {
doc['_deleted'] = true;
return self.put.call(this, doc);
};
/**
* Convert map / reduce function into string and remove white spaces / tabs, ...
* @param {object} views Pouchdb views object
* @return {object} A new object
*/
var _formatViews = function(views) {
var ret = {};
for (var viewName in views) {
ret[viewName] = {};
for (var fnName in views[viewName]) {
ret[viewName][fnName] = views[viewName][fnName].toString().replace(/\r?\n|\r|\t/g, "");
}
}
return ret;
};
/**
* Create or update design doc
* @return {promise} Success / Error
*/
self.createViews = function() {
var that = this;
var deferred = $q.defer();
var views = _formatViews(this.views);
self.get.call(that, '_design/' +that.Type).then(function(doc) {
return deferred.resolve(!window.cordova && self.put.call(that, {
'_id': '_design/' +that.Type,
'_rev': doc['_rev'],
'views': views
}) || true);
}, function() {
return deferred.resolve(self.put.call(that, {
'_id': '_design/' +that.Type,
'views': views
}));
});
return deferred.promise;
};
/**
* Build the pouchdb Btree
* @return {promise} success / error
*/
self.buildViewsIndexes = function() {
return this.db.query(this.Type+ '/all', { 'limit': 0 });
};
/**
* Dispatch change event on the 'all' design doc within all scopes
*/
self.dispatchEvents = function() {
var that = this;
that.db.changes({
'since': 'now',
'live': true,
'view': that.Type+ '/all',
'filter': '_view',
}).on('change', function() {
$rootScope.$broadcast(that.Type+ 'Changes');
});
};
return self;
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment