node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »



Build Status

An ORM written in Coffeescript that supports multiple database systems (SQL, NoSQL, and even REST), as well as ID generation middleware. It is fully extendable to add new database systems and plugins.

Note: this project is new and is still evolving rapidly. A lot is done, but there is still a lot to do.


node-activerecord is available in npm:

npm install activerecord

Installing node-activerecord will not automatically install the required libraries for every adapter since this could easily make the library very bloated and dependent on things you may or may not need.

Adapter Libraries

You can use npm to install the required libraries for each adapter:

Adapter Libraries
sqlite sqlite3
mysql mysql
redis redis
REST restler

ID Middleware Libraries

You can also use npm to install the required libraries for any ID generation middleware:

Middleware Libraries
sql none
redis redis



By default, ActiveRecord assumes SQL ID middleware. This means it checks for the last generated auto-increment ID on the primary key.

ActiveRecord = require 'activerecord'
module.exports = new ActiveRecord.Configuration
    database: "#{__dirname}/test.db"
    host: 'localhost'
    database: 'test'
    user: 'test'
    password: 'password'
      host: 'localhost'

Model Definition

ActiveRecord = require 'activerecord'
config = require __dirname + "/config"
# Note: uses sqlite3 by default 
class User extends ActiveRecord.Model
  config: config
  fields: ['id''username''name']

Creating a Record

user = new User()
user.username = "meltingice" = "Ryan"

Retreiving a Record

# Find by primary ID 
User.find 1(err, user) -> console.log user.toJSON()
# Find multiple by primary ID 
User.findAll [12](err, users) ->
  console.log user.toJSON() for user in users
# Find by custom query 
User.find "SELECT * FROM users WHERE id < ?"5(err, user) ->
  console.log user.toJSON()

Updating a Record

User.find 1(err, user) -> = "Bob" (err) -> console.log "updated!"

Deleting a Record

User.find 1(err, user) ->
  user.delete (err) -> console.log "deleted!"

Model Relations

class User extends ActiveRecord.Model
  config: config
  fields: ['id''username''name']
  hasMany: -> [
class Message extends ActiveRecord.Model
  config: config
  fields: ['id''user_id''text']
  belongsTo: -> [
Message.find 1(message) ->
  message.user (err, user) ->
    console.log user.toJSON()

Non-SQL Middleware

class User extends ActiveRecord.Model
  config: config
  idMiddleware: 'redis'
    key: 'users:id'
  fields: ['id''username''name']


class Logger extends ActiveRecord.Plugin
  messages: []
  # Callback hooks 
  afterCreate: -> messages.push "Created model for #{@tableName()}"; true
  afterUpdate: -> messages.push "Updated model for #{@tableName()}"; true
  # Extend the model 
  outputLog: -> console.log msg for msg in messages
class User extends ActiveRecord.Model
  config: config
  fields: ['id''username''name']
  plugins: -> [
user = new User name: 'foo'username: 'bar' (err) -> user.outputLog()