More in-depth documentation can be found in README on GitHub.
Tubbs is a feature-rich and database-agnostic model layer for you Node.js applications.
Some of Tubbs’ excellent features are:
- Rails ActiveModel style property validators, like
formatOf,inclusionOf, etc… - Hackable custom async validation
- Model extensibility
- ES5 getter/setter properties with configurable default values (none of that
model.set(...)garbage) - Non-serialized virtual properties
- Can be used with any database as long as an adaptor is provided
- Current built-in support for Riak and in-memory databases
Example: define a User model module:
var Tubbs = require('tubbs');
var Validate = Tubbs.Validate;
var Regex = require('regex');
var User = module.exports = Tubbs.create({
// Persist our data with Riak
dataStore: new Tubbs.RiakStore({ bucket: 'users' }),
// Use the `username` property as the primary key for operations like `find`.
// Defaults to the `id` property.
primaryKey: 'username',
fields: {
username: undefined,
password: undefined,
first: undefined,
last: undefined,
email: undefined
},
// Instances will have a `name` property, but it will not be serialized as
// JSON when saved to the database.
virtual: {
name: function() {
return ((this.first || '') + ' ' + (this.last || '')).trim();
}
},
// Add some validation...
validation: [
Validate.required('username'),
Validate.formatOf('username', {
with: Regex.username
}),
Validate.required('username'),
Validate.formatOf('password', {
with: Regex.passwordHash
}),
Validate.formatOf('email', {
with: Regex.email
})
]
});
Example: the defined User model can pull data from our database:
var user = User.find('dandean');
user.email = 'whatever@stuff.com';
user.save(function(e, result) {
if (e) {
// Could not save, respond with the error messages
res.send({
error: e.message,
errors: user.errors
}, 400);
} else {
// User found, send the user bac to the client.
res.send({ user: user }, 200);
}
});
Tubbs can do a whole lot more, and can make your database code pretty concise and easy to work with. Take a look at the repository for more examples and documentation.
blog comments powered by Disqus