node-orm2 — Node.js Object Relational Mapping

ORM Package for Node.js. Works with MySQL, PostgreSQL and SQLite.

var orm = require('orm');

orm.connect("mysql://username:password@host/database", function (err, db) {
    if (err) throw err;

    var Person = db.define('person', {
        name      : String,
        surname   : String,
        age       : Number,
        male      : Boolean,
        continent : [ 'Europe', 'America', 'Asia', 'Africa', 'Australia', 'Antartica' ], // ENUM type
        photo     : Buffer, // BLOB/BINARY
        data      : Object // JSON encoded
    }, {
        methods: {
            fullName: function () {
                return this.name + ' ' + this.surname;
            }
        },
        validations: {
            age: orm.validators.rangeNumber(18, undefined, 'under-age')
        }
    });

    Person.find({ surname: "Doe" }, function (err, people) {
        // SQL: "SELECT * FROM person WHERE surname = 'Doe'"

        console.log("People found: %d", people.length);
        console.log("First person: %s, age %d", people[0].fullName(), people[0].age);

        people[0].age = 16;
        people[0].save(function (err) {
            // err.msg = 'under-age';
        });
    });
});

Don’t let the alpha tag fool you; I’ve used (and have contributed to) node-orm2 in a project before (along with express, mysql and when — killer combination) and must say it’s really stable.

node-orm2 →

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.