Skip to content

Instantly share code, notes, and snippets.

@jchris
Last active December 29, 2015 11:19
Show Gist options
  • Save jchris/7662666 to your computer and use it in GitHub Desktop.
Save jchris/7662666 to your computer and use it in GitHub Desktop.
db.js
var DATABASE_NAME = 'todo';
var CBLite = require('com.couchbase.cbl');
// globals...
// Ti.API.info('db module');
var Config = {
username : null // todo set on login
// list_id : dynamically configured
};
var myDb;
function database() {
if (myDb) return myDb;
var manager = CBLite.createManager();
myDb = manager.createDatabase(DATABASE_NAME);
return myDb;
};
exports.createDb = function() {
Ti.API.info('createDb');
setupInitialListDocument(); // for interop with multi-list capable clients like TodoLite
return true;
};
function setupInitialListDocument() {
var db = database();
var listBookmark = db.getLocalDocument("listBookmark");
if (listBookmark) {
var list_id = listBookmark.list_id;
}
if (!list_id) {
var list = db.createUntitledDocument();
Ti.API.info('list doc: ' + list.documentId);
list.update(function(newRev) {
Ti.API.info('CBL: list.update: ' + newRev.document.documentId);
var props = newRev.properties
props.type = "list";
props.created_at = "new Date()";
props.owner = Config.username;
props.title = "Appcelerator Demo List";
newRev.properties = props;
return true;
});
list_id = list.documentId;
db.putLocalDocument({list_id : list_id}, "listBookmark");
}
// this will set the list_id on the task documents
Config.list_id = list_id;
Ti.API.info('has list_id '+list_id);
}
exports.selectItems = function(_done) {
// debug
Ti.API.info('selectItems CBL docs: ' + database().documentCount);
// functionality
var todoItems = database().getView("todoItems");
if (!todoItems.map) {
todoItems.setMap(function(doc, emitter) {
Ti.API.info('CBL: todoItems.run(): ' + doc['_id']);
if (doc.type == 'task') {
Ti.API.info('CBL: todoItems.emit(): ' + doc['_id']);
emitter.emit([doc.list_id, doc.checked, doc.title]);
}
}, "1.1");
}
var todoQuery = todoItems.createQuery();
todoQuery.startKey = [Config.list_id, _done];
todoQuery.endKey = [Config.list_id, _done, {}];
var rows = todoQuery.rows;
var retData = [];
if (!rows) return retData;
var row;
while (row = rows.nextRow) {
retData.push({item : row.key[2], id : row.id});
}
Ti.API.info('selectItems view: ' + retData.length);
return retData;
};
exports.updateItem = function(_id, _done) {
Ti.API.info('updateItem: ' + _id);
var itemDoc = database().getDocument(_id);
var properties = itemDoc.properties;
properties.checked = _done;
properties.updated_at = new Date();
return itemDoc.putProperties(properties);
};
exports.addItem = function(_item) {
var taskDoc = database().createUntitledDocument();
taskDoc.update(function(newRevision) {
Ti.API.info('CBL: Add taskDoc: ' + newRevision.document.documentId);
var properties = newRevision.properties;
properties.type = "task";
properties.created_at = JSON.stringify(new Date()).replace(/"/g,'');
properties.updated_at = properties['created_at'];
properties.title = _item;
properties.list_id = Config.list_id;
properties.checked = false;
newRevision.properties = properties;
return true;
});
Ti.API.info('CBL wrote real new doc: ' + database().documentCount);
return true;
};
exports.deleteItem = function(_id) {
Ti.API.info('deleteItem '+_id);
var itemDoc = database().getDocument(_id);
var properties = itemDoc.properties;
properties._deleted = true;
return itemDoc.putProperties(properties);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment