Created
November 19, 2015 21:37
-
-
Save DanailMinchev/6b9cce7260919b6f3e2c to your computer and use it in GitHub Desktop.
"PouchDB 5.x + SQLite" AngularJS service for use with "Couchbase Sync Gateway" and "Apache Cordova"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
var pouchDbConfiguration = { | |
localDbName: 'main', | |
remoteDbUrl: 'http://localhost:4984/main' | |
}; | |
angular | |
.module('components.databasePouchDb') | |
.constant('pouchDbConfiguration', pouchDbConfiguration); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
angular.module('components.databasePouchDb', []); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
angular | |
.module('components.databasePouchDb') | |
.factory('databasePouchDbService', databasePouchDbService); | |
databasePouchDbService.$inject = ['$window', '$rootScope', '$q', 'pouchDbConfiguration']; | |
function databasePouchDbService($window, $rootScope, $q, pouchDbConfiguration) { | |
var db = null; | |
var changeListener = null; | |
var syncListener = null; | |
var service = { | |
openDb: function() { | |
openDb(); | |
}, | |
deleteDb: function() { | |
return deleteDb(); | |
}, | |
dbInfo: function() { | |
return dbInfo(); | |
}, | |
save: function(doc) { | |
return save(doc); | |
}, | |
get: function(docId, options) { | |
return get(docId, options); | |
}, | |
remove: function(docId, docRev) { | |
return remove(docId, docRev); | |
}, | |
startListening: function() { | |
startListening(); | |
}, | |
stopListening: function() { | |
stopListening(); | |
}, | |
startSyncing: function() { | |
startSyncing(); | |
}, | |
stopSyncing: function() { | |
stopSyncing(); | |
} | |
}; | |
// ========== Private | |
function openDb() { | |
db = new $window.PouchDB(pouchDbConfiguration.localDbName, { | |
auto_compaction: true, | |
adapter: 'websql', | |
location: 2 | |
}); | |
if (!db.adapter) { | |
// websql/sqlite not supported (e.g. FirefoxOS) | |
db = new $window.PouchDB(pouchDbConfiguration.localDbName, { | |
auto_compaction: true | |
}); | |
} | |
} | |
function deleteDb() { | |
return $q.when(db.destroy()); | |
} | |
function dbInfo() { | |
return $q.when(db.info()); | |
} | |
function save(doc) { | |
if (!doc._id) { | |
doc._id = $window.uuid.v4(); | |
} | |
return $q.when(db.put(doc)); | |
} | |
function get(docId, options) { | |
if (options) { | |
return $q.when(db.get(docId, options)); | |
} else { | |
return $q.when(db.get(docId)); | |
} | |
} | |
function remove(docId, docRev) { | |
return $q.when(db.remove(docId, docRev)); | |
} | |
function startListening() { | |
changeListener = db.changes({ | |
live: true, | |
include_docs: true | |
}).on('change', function(change) { | |
if (!change.deleted) { | |
$rootScope.$broadcast('databasePouchDbService:change', change); | |
} else { | |
$rootScope.$broadcast('databasePouchDbService:delete', change); | |
} | |
}).on('error', function(error) { | |
$rootScope.$broadcast('databasePouchDbService:error', error); | |
}); | |
} | |
function stopListening() { | |
changeListener.cancel(); | |
} | |
function startSyncing() { | |
syncListener = db.sync(pouchDbConfiguration.remoteDbUrl, { | |
live: true, | |
retry: true | |
}); | |
} | |
function stopSyncing() { | |
syncListener.cancel(); | |
} | |
return service; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment