Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created November 17, 2011 21:41
Show Gist options
  • Save Raynos/1374623 to your computer and use it in GitHub Desktop.
Save Raynos/1374623 to your computer and use it in GitHub Desktop.
var UserModel = pd.make(Model,{
get: function _get(id, cb) {
// nano is a database client
this.nano.get(id,
// whitelist runs every error through the white list,
// if it fails (false) throw it
// if it succeeds (true) pass it to cb
error.whitelist(function _errors(err) {
if (err.syscall === 'getaddrinfo') {
UserModel.get(id, cb);
} else if (err.error === "not_found") {
return true;
} else {
return false;
}
}, cb)
);
},
insert: function _create(json, cb) {
this.nano.insert(json, json._id,
error.whitelist(function _errors(err) {
if (err.syscall === 'getaddrinfo') {
UserModel.insert(json, cb);
} else if (err.error === "conflict") {
return true;
} else {
return false;
}
}, cb)
);
},
delete: function _delete(name, cb) {
var that = this;
this.get(name, function _getRev(err, body) {
that.nano.destroy(name, body._rev,
error.whitelist(function _errors(err) {
if (err.syscall === 'getaddrinfo') {
UserModel.delete(name, cb);
} else {
return false;
}
}, cb)
);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment