Skip to content

Instantly share code, notes, and snippets.

@tambling
Created November 5, 2015 20:26
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 tambling/b324b5ad44632fe32e3c to your computer and use it in GitHub Desktop.
Save tambling/b324b5ad44632fe32e3c to your computer and use it in GitHub Desktop.
function Civet(){
this.data = {};
this.id = 1;
};
Civet.prototype.create = function(entity, callback){
var error = null;
if((entity instanceof Object) && !Array.isArray(entity)) {
entity.id = this.id;
this.data[entity.id] = Object.assign({}, entity);
this.id += 1;
} else {
error = "You need to pass in an object!";
}
callback(error);
}
Civet.prototype.find = function(callback){
var error = null;
if(Object.keys(this.data).length === 0){
error = "No records found!";
}
var entities = [];
for (entity in this.data) {
if(this.data.hasOwnProperty(entity)){
entities.push(this.data[entity])
}
}
callback(error, entities);
}
Civet.prototype.findById = function(id, callback) {
var entity = this.data[id];
var error = entity ? null : "No record found!";
callback(error, entity);
}
Civet.prototype.remove = function(id, callback){
var entity = Object.assign({}, this.data[id]);
var error = null;
if(entity){
delete this.data[id]
} else {
error = "No record found!";
}
callback(error, entity);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment