Skip to content

Commit

Permalink
changes for couchdb/cloudant
Browse files Browse the repository at this point in the history
  • Loading branch information
mariobriggs committed Aug 27, 2015
1 parent b595720 commit 5bb7764
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions simple-todos.js
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,11 @@
Tasks = new Mongo.Collection("tasks"); Tasks = new CouchDB.Database("tasks");


if (Meteor.isServer) { if (Meteor.isServer) {
// This code only runs on the server // This code only runs on the server
// CouchDB/Cloudant Query requires an index to use a selector other than _id.
// A index that starts with $or requires a text index
// so lets create a text index (needs Cloudant DBaas) on fields private & owner
Tasks._ensureIndex({ type:'text', index:{ fields:[{name: 'private', type: 'boolean'},{name: 'owner',type: 'string'}]}});
// Only publish tasks that are public or belong to the current user // Only publish tasks that are public or belong to the current user
Meteor.publish("tasks", function () { Meteor.publish("tasks", function () {
return Tasks.find({ return Tasks.find({
Expand All @@ -21,10 +25,10 @@ if (Meteor.isClient) {
tasks: function () { tasks: function () {
if (Session.get("hideCompleted")) { if (Session.get("hideCompleted")) {
// If hide completed is checked, filter tasks // If hide completed is checked, filter tasks
return Tasks.find({checked: {$ne: true}}, {sort: {createdAt: -1}}); return Tasks.find({checked: {$ne: true}}, {sort: {createdAt: 'desc'}});
} else { } else {
// Otherwise, return all of the tasks // Otherwise, return all of the tasks
return Tasks.find({}, {sort: {createdAt: -1}}); return Tasks.find({}, {sort: {createdAt: 'desc'}});
} }
}, },
hideCompleted: function () { hideCompleted: function () {
Expand Down Expand Up @@ -84,13 +88,15 @@ Meteor.methods({
if (! Meteor.userId()) { if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized"); throw new Meteor.Error("not-authorized");
} }

Tasks.insert({ Tasks.insert({
text: text, text: text,
createdAt: new Date(), createdAt: new Date(),
owner: Meteor.userId(), owner: Meteor.userId(),
username: Meteor.user().username username: Meteor.user().username,
}); private: false,
checked:false
});
}, },
deleteTask: function (taskId) { deleteTask: function (taskId) {
var task = Tasks.findOne(taskId); var task = Tasks.findOne(taskId);
Expand All @@ -108,7 +114,9 @@ Meteor.methods({
throw new Meteor.Error("not-authorized"); throw new Meteor.Error("not-authorized");
} }


Tasks.update(taskId, { $set: { checked: setChecked} }); task.checked = setChecked;
Tasks.update(task);

}, },
setPrivate: function (taskId, setToPrivate) { setPrivate: function (taskId, setToPrivate) {
var task = Tasks.findOne(taskId); var task = Tasks.findOne(taskId);
Expand All @@ -117,7 +125,8 @@ Meteor.methods({
if (task.owner !== Meteor.userId()) { if (task.owner !== Meteor.userId()) {
throw new Meteor.Error("not-authorized"); throw new Meteor.Error("not-authorized");
} }


Tasks.update(taskId, { $set: { private: setToPrivate } }); task.private = setToPrivate;
Tasks.update(task);
} }
}); });

0 comments on commit 5bb7764

Please sign in to comment.