About today

Waterline: Node.js adapter-based ORM

· by Otto Giron · Read in about 3 min · (438 Words)
javascript nodejs sails waterline

Waterline is an ORM/ODM (Object Document Mapper), part of the Sails Framework.

Waterline is agnostic of the data store so it allows you to map different types of databases to a common model structure, focusing on business logic for CURD operations, querying records and models relations mapping.

Pros

Standardized Data Access Interface

In a project is always necessary to have a set of standards or architectural guidelines that the developers can follow, so they can focus on business logic. As developers thinking one from many ways of doing a something adds mental load and it makes us waste time. Also depending in our experience, we may end up writing a lot of boilerplate code that might not be even modular and reusable.

Waterline provides a common language the developers can agree on, it also reduces the boilerplate code implementing common database operations.

Flexibility

Imagine that you are working in a project using let’s say Mongo. Then you realize that the company has no previous experience with Mongo, they have invested time and resources in MySQL and they want to stick to that. You have already invested a couple of weeks developing a prototype using Waterline. No problem! since you’re using Waterline you just need to change a couple of configuration lines.

Adapters

An adapter is an interface that maps methods like find() and create() to lower level syntax like “SELECT * FROM” and “INSERT INTO”. Sails core team maintains a set of adapters for the most popular databases. A great thing is that you can also contribute with your own adapters, or the existing ones.

Cons

Maturity Or Availability Of Adapters

For some databases adapters might be at early stages, or nonexistent. The good thing is that you can implement your own adapter or contribute existing ones. Right now I’m working on an adapter for Elasticsearch https://github.com/ottogiron/sails-elasticsearch

Example

var waterline = require('waterline');

// Instantiate a new instance of the ORM
var orm = new Waterline();

var config = {
adapters: {
mysql: require('sails-mysql')
},

connections: {
myLocalMySql: {
adapter: 'mysql'
host: 'localhost',
database: 'test'
}
}
};

var User = Waterline.Collection.extend({
identity: 'user',
connection: 'mysql',
attributes: {
first_name: 'string',
last_name: 'string'
}

})

//Load the models
orm.looadCollection(User);

// Start Waterline passing adapters in
orm.initialize(config, function(err, models) {
models.user
.find({first_name: 'something'})
.execute(function(err, result){
// do something
});
})

Find additional examples on how to map models and using them here: http://sailsjs.org/#!/documentation/concepts/ORM

Conclusion

With Waterline you can create an abstraction of different databases, reducing the boilerplate code for common operations, and giving the developers a common data access language they can work with, letting them focus in the business logic.

Comments