Git Product home page Git Product logo

node-activerecord's Introduction

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()

node-activerecord's People

Contributors

meltingice avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-activerecord's Issues

Implement model relations

Relations are currently totally unimplemented. Will need to support setting relations, retrieving relations, validating relations, etc.

Rewrite/fix tests

The tests are currently for the old version of the library. They will need to be updated to make sure they are relevant with the refactor.

Implement rest of CRUD

Currently only read and create are supported. Also need to implement:

  • update
  • delete
  • touch

This needs to be done in querying.coffee and query.coffee.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.