Created
November 5, 2015 20:26
-
-
Save tambling/b324b5ad44632fe32e3c to your computer and use it in GitHub Desktop.
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
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