node-activerecord
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 |
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 =sqlite:database: "/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 defaultconfig: configfields: 'id''username''name'
Creating a Record
user =user.username = "meltingice"user.name = "Ryan"usersave
Retreiving a Record
# Find by primary IDUserfind 1 consolelog usertoJSON# Find multiple by primary IDUserfindAll 12consolelog usertoJSON for user in users# Find by custom queryUserfind "SELECT * FROM users WHERE id < ?"5consolelog usertoJSON
Updating a Record
Userfind 1user.name = "Bob"usersave consolelog "updated!"
Deleting a Record
Userfind 1userdelete consolelog "deleted!"
Model Relations
config: configfields: 'id''username''name': ->Messageconfig: configfields: 'id''user_id''text': ->UserMessagefind 1messageuserconsolelog usertoJSON
Non-SQL Middleware
config: configidMiddleware: 'redis'idMiddlewareOptions:key: 'users:id'fields: 'id''username''name'
Plugins
messages:# Callback hooks: -> messagespush "Created model for "; true: -> messagespush "Updated model for "; true# Extend the model: -> consolelog msg for msg in messagesconfig: configfields: 'id''username''name': ->'json''logger'user = name: 'foo'username: 'bar'usersave useroutputLog