Mongorm

Mongorm is a micro ODM for ruby. The difference between mongorm and every other Mongo ODM is that mongorm strongly believes in plain Ruby objects (PROs) and separation of domain logic (Models) from persistance logic (Repositories). With Mongorm, you will never include anything into your models.

Getting Started

If you want to start using Mongorm, do the following steps:

  1. Include the following two gems (will be one once origin releases 1.0) gem 'origin', :git => 'http://github.com/mongoid/origin.git' gem 'mongorm'
  2. In your application, set the Mongorm environment via Mongorm.environment = Rails.env or Mongorm.environment = ENV["RACK_ENV"]
  3. Create a repository for the model you want to persist. For example, for User you'll need a UserRepository.
  4. In your repository, include Mongorm::Repository
  5. Done. Start using it and persisting to mongo.

Example app

Practices

Views -> Controllers -> Repositories -> Models

Controllers should talk to repositories UserRepository.new.find(params[:id]) Models should be PROs Repositories should be data access only. Use services for manipulation of multiple repositories and models, if needed. Simple over Easy. Do not load Rails unless you need it (Hint: Models do not need it!)

Background

This is a repository that is currently showing my attempt to create a purely DataMapper-backed ORM that is as easy to use as Mongoid. It will start with me persisting and pulling of a User object out of a mongo db.

Where I'm at now:

  • This will autowire things, based on these conventions:

    • DomainModal has a DomainModalRepository
    • DomainModelRepository includes a Mongorm::Repository
    • DomainModel has a hash-based constructor (do not like this but not sure of a way around it)
    • Needs a config/mongo.yml
    • All 'fields' need to be attr_accessible in DomainModel
    • DomainModel needs id

API

#find(id) where id is a string. It will return the found DomainModel object. If not found, it will throw an error.

#delete(object)

#delete_all will delete all documents in the collection

#save(object) will create the object and set the id

#where(origin_hash) will search using origin query generation

#collection will give direct access to 10gen's Mongo Ruby Driver