Skip to content

Instantly share code, notes, and snippets.

@eveiga
Created July 23, 2012 17:11
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 eveiga/3164782 to your computer and use it in GitHub Desktop.
Save eveiga/3164782 to your computer and use it in GitHub Desktop.
DAOFactory.prototype.get_or_create = function(params, defaults){
var self = this;
return new Utils.CustomEventEmitter(function(emitter) {
self.find({ where: params })
.success(
function(instance){
if (instance === null) {
for (var attrname in defaults) {
params[attrname] = defaults[attrname];
}
self.create(params).success(
function(instance) {
emitter.emit('success', instance)
}
)
} else {
emitter.emit('success', instance)
}
}
);
}).run()
}
@pkoch
Copy link

pkoch commented Jul 24, 2012

Re-implementing Django's ORM? :P

I'm not really into node but, don't you also have to emit the failures?

@eveiga
Copy link
Author

eveiga commented Jul 24, 2012

Yup, this gist is incomplete :P

https://github.com/sdepold/sequelize/pull/225

This is the correct version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment