Created
November 17, 2011 20:55
-
-
Save Raynos/1374487 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var Model = require("./model.js"), | |
error = require("error"), | |
pd = require("pd"); | |
var UserModel = pd.make(Model,{ | |
get: function _get(id, cb) { | |
this.nano.get(id, | |
error.whitelist(function _errors(err) { | |
if (err.syscall === 'getaddrinfo') { | |
UserModel.get(id, cb); | |
} else { | |
return false; | |
} | |
}, function _success(err, body, headers) { | |
cb(err, body, headers); | |
}) | |
); | |
}, | |
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; | |
} | |
}, function _success(err, body, headers) { | |
cb(err, body, headers); | |
}) | |
); | |
}, | |
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; | |
} | |
}, function _success(err, body, headers) { | |
cb(err, body, headers); | |
}) | |
); | |
}); | |
} | |
}); | |
UserModel.on("start", function () { | |
UserModel.constructor(); | |
UserModel.nano = UserModel.nano.use("_users"); | |
UserModel.emit("loaded"); | |
}); | |
module.exports = UserModel; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment