Skip to content

Instantly share code, notes, and snippets.

@cjroth
Last active August 29, 2015 13:57
Show Gist options
  • Save cjroth/9800025 to your computer and use it in GitHub Desktop.
Save cjroth/9800025 to your computer and use it in GitHub Desktop.
/*
an orm should only take care of mapping objects to a relational database. that's **all**. a lot of my problems with
sequelize come from the fact that i'm using sequelize to define my models. i would much prefer to use plain javascript
objects to define my models - not using sequelize.define or some other sort of "definer". ideally i could follow a more
classic object-oriented approach where my models could extend or implement an orm that takes care of relaying data back and
fourth between the database and client and nothing else.
*/
var ORM = function() {
this._as = {};
};
ORM.prototype.get = function(property) {
// filter based on "as"
return this[property];
}
var User = function() {
// constructor logic here...
}
ORM.extend(User, {
id: {
type: 'uuid',
primaryKey: true,
}
}); // add ORM functionality.
User.prototype.setPassword = ORM.extendMethod(function(password) {
});
User.prototype.checkPassword = function(password) {
};
UserStore
.find(3)
.on('error', function(err) {
throw err;
})
.on('success', function(user) {
var data = user.get({ as: 'self', units: 'metric' });
res.json(data);
});
UserStore
.find(3)
.error(next)
.success(function(user) {
user
.as({ role: 'self', units: 'metric' }) // what's a better name than `as`?
.set({ first_name: 'Chris' })
.save()
.error('validation', function(err) { // should we call this method "error" or "catch"?
res.json(400, err);
})
.error(function(err) {
res.json(500);
})
.success(function() { // no need to pass user object to callback
res.json(this); // could also do `res.json(user)`
});
});
UserStore
.query()
.where({ id: 3 })
.limit(3)
.sort({ first_name: 'asc' })
.execute()
.error(next)
.success(function(user) {
user
.as({ role: 'self', units: 'metric' }) // what's a better name than `as`?
.set({ first_name: 'Chris' })
.save()
.error('validation', function(err) { // should we call this method "error" or "catch"?
res.json(400, err);
})
.error(function(err) {
res.json(500);
})
.success(function() { // no need to pass user object to callback
res.json(this); // could also do `res.json(user)`
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment