Simple ORM in JavaScript


Modelize is a simple ORM written in JavaScript.

Apache License (version 2)

Modelize requires version 0.6.x of Node.js or higher. If you want to run the tests or work on model, you'll want Vows.

Installing with NPM

npm install modelize

Modelize currently implements an in-memory store. More adapters will follow, a binding is also planned.

Modelize uses a simple syntax for defining a model. It will look like this:

var UserModel = new Modelize('User', function() {
    this.adapter('memory');'username', 'string', { required: true });'password', 'string', { required: true });'firstName', 'string', { defaultValue: 'John' });'lastName', 'string', { defaultValue: 'Doe' });

The following data types are currently supported:

  • string
  • number
  • array
  • object

Now you can create instances of your new fancy User model. It is easy too:

var user = UserModel.create({
    username: 'jdoe',
    password: 'fancymodels',
    lastName: 'Smith'

Just use the following syntax:

// Getting properties 
// Setting properties 

If you try to get a property which was not initialized yet, it will return the default value or undefined if there is none.

Remember: You must initialize/set all required properties before saving the model. Otherwise Modelize will throw an error.; // Saves the model instance into the store 
user.password('nodejs');; // Updates the model instance in the store 
user.remove(); // Removes the model instance from the store 

Modelize offers a simple API for finding and sorting existing items/instances. Please remember, that an instance can only be found, if was called.

The API is (as you might expect for a NodeJS library) asynchronous and the methods for querying are static methods on each model constructor.

Instances are always shared to save memory usage and keep the usage simple. This means:

var user1 = UserModel.create({
    lastName: 'Doe'
var user2 = UserModel.create({
    lastName: 'Doe'
UserModel.find({lastName: 'Doe'}, function(errdata) {
    // data[0] is the same instance as user1 
    // data[1] is the same instance as user2 
    console.log(user1.lastName() == data[0].lastName()) // Is true 

Use the findOne method to find a single item. You can specify a set of query parameters in the form of an object-literal. In the case of multiple results, it will only return the first one. If no result can be found, the return value is undefined.

UserModel.findOne({username: 'jdoe'}, function(erruser) {
    if(err) throw err;
    if(user === undefined) throw new Error('User not found!');

Use the find method to find lots of items. Pass it a set of query parameters in the form of an object-literal, where each key is a field to compare and the value is a simple value for comparison. (equal to)

UserModel.find({lastName: 'Smith'}, function(errdata) {
    if(err) throw err;
    // Data is now array filled with all results found 

If you want to compare an object, you can also give a set of query parameters like this: {stats: {experience: 37, age: 19}}

Modelize Javascript ORM - © 2012-2013 P. Mathis (