Skip to content

Instantly share code, notes, and snippets.

@AdamBrodzinski
Created November 9, 2015 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamBrodzinski/9e667d14e980df7c0fbb to your computer and use it in GitHub Desktop.
Save AdamBrodzinski/9e667d14e980df7c0fbb to your computer and use it in GitHub Desktop.
Repo pattern for Meteor
const root = this;
Repo = {
insert(collection) {
const id = root[collection].insert(params);
if (id) {
return ['ok', root[collection].findOne(id)];
} else {
return ['error', 'Error creating document'];
}
},
all(collection) {
const items = root[collection].find().fetch();
if (items) {
return ['ok', items];
} else {
return ['error', 'Error finding documents'];
}
},
update(collection, id, data) {
const count = root[collection].update({_id: id}, {$set: data});
if (count) {
return ['ok', root[collection].findOne(id)];
} else {
return ['error', 'Error finding documents'];
}
},
$update(collection, id, data) {
const count = root[collection].update({_id: id}, {$set: data});
if (count) {
return root[collection].findOne(id);
} else {
throw new Meteor.Error(400, 'Update failed');
}
},
get(collection, id) {
return root[collection].findOne(id);
},
$get(collection, id) {
const doc = root[collection].findOne(id);
if (doc) {
return doc;
} else {
throw new Meteor.Error(404, 'Record not found');
}
},
$delete(collection, id) {
const count = root[collection].remove(id);
if (count) {
return ['ok'];
} else {
throw new Meteor.Error(404, 'Record not found');
}
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment