-
-
Save AdamBrodzinski/9e667d14e980df7c0fbb to your computer and use it in GitHub Desktop.
Repo pattern for Meteor
This file contains 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
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