Skip to content

A ORM written in Coffeescript that supports multiple database systems (SQL/NoSQL) and ID generation middleware.

Notifications You must be signed in to change notification settings

meltingice/node-activerecord

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-activerecord

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.

Install

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

Built-In Plugins

Plugin Enabled by Default Description
json true Allows conversion of a model to a simple JS object and offers field protection
logger false A verbose logger that outputs significant logging data to the console

Examples

Configuration

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
  sqlite:
    database: "#{__dirname}/test.db"
  mysql:
    host: 'localhost'
    database: 'test'
    user: 'test'
    password: 'password'
  middleware:
    redis:
      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"
user.name = "Ryan"
user.save()

Retreiving a Record

# Find by primary ID
User.find 1, (err, user) -> console.log user.toJSON()

# Find multiple by primary ID
User.findAll [1, 2], (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) ->
  user.name = "Bob"
  user.save (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: -> [
    Message
  ]

class Message extends ActiveRecord.Model
  config: config
  fields: ['id', 'user_id', 'text']
  belongsTo: -> [
    User
  ]

Message.find 1, (message) ->
  message.user (err, user) ->
    console.log user.toJSON()

Non-SQL Middleware

class User extends ActiveRecord.Model
  config: config
  idMiddleware: 'redis'
  idMiddlewareOptions:
    key: 'users:id'

  fields: ['id', 'username', 'name']

Plugins

class AltLogger 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: -> [
    'json'
    AltLogger
  ]

user = new User name: 'foo', username: 'bar'
user.save (err) -> user.outputLog()

About

A ORM written in Coffeescript that supports multiple database systems (SQL/NoSQL) and ID generation middleware.

Resources

Stars

Watchers

Forks

Packages

No packages published