Mongocore Ruby Database Driver
A new MongoDB ORM implementation on top of the latest MongoDB Ruby driver. Very fast and light weight.
The perfect companion for Rails, Sinatra, Susana or other Rack-based web frameworks.
Features
With Mongocore you can do:
- Insert, update and delete
- Finding, sorting, limit, skip, defaults
- Scopes, associations, validations, pagination
- Read and write access control for each key
- Request cache, counter cache, track changes
- Automatic timestamps, tagged keys, json
The schema is specified with a YAML file which supports default values, data types, and security levels for each key.
Please read the source code to see how it works, it's fully commented and very small, only 8 files, and 519 lines of fully test driven code.
The tests are written using Futest, try it out if you haven't, it makes testing so much fun.
Installation
gem install mongocore
or add to your Gemfile.
Then in your model:
class Model
include Mongocore::Document
end
Settings
Mongocore has a few built in settings you can easily toggle:
Mongocore.schema = File.join(Dir.pwd, 'config', 'db', 'schema')
Mongocore.cache = true
Mongocore.access = true
Mongocore.timestamps = true
Mongocore.sort = {}
Mogocore.per_page = 20
Mongocore.debug = false
Usage
Mongocore.db = Mongo::Client.new(['127.0.0.1:27017'], :database => "dbname_#{ENV['RACK_ENV']}")
Mongo::Logger.logger.level = ::Logger::INFO
Mongo::Logger.logger.level = ::Logger::FATAL
Mongo::Logger.logger = ::Logger.new('./log/mongo.log')
m = Model.new
m.duration = 59
m.save
m = Model.insert(:duration => 45, :goal => 55)
m = Model.create(params, :validate => true)
p = Parent.new(:house => 'Nice')
p.save
p.reload
m.parent = p
m.save
query = Model.find
query = Model.where
m = query.all
a = query.featured.all
c = query.count
l = query.last
f = query.first
m = Model.find.all
m = Model.find.paginate
m = Model.find.paginate(:per_page => 10, :page => 5)
m.total
Model.each do |m|
puts m
end
Model.find(:duration => 50).each{|m| puts m}
m = Model.find(:house => {:$ne => nil, :$eq => 'Nice'}).last
m = Model.find({:duration => {:$gt => 40}}, :sort => {:duration => -1}).all
m = p.models.find(:duration => 10).sort(:duration => -1).first
p = Parent.find.sort(:duration => 1).limit(5).all
p = Parent.limit(1).last
m = p.models.find({}, :sort => {:goal => 1}, :limit => 1).first
m = Model.sort(:goal => 1, :duration => -1).limit(10).all
m = Model.find(:_id => object_id).first
m = Model.find(object_id).first
m = Model.find(string).first
m = Model.find(:duration => 60, :goal => {:$gt => 0}).first
m = Model.last
m = p.models.last
c = Model.count
c = p.models.featured.count
m = Model.find.skip(2).first
m = Model.first
m.attributes
m.attributes(:badge)
m.to_json
m.duration = 33
m.changed?
m.duration_changed?
m.duration_was
m.changes
m.saved?
m.persisted?
m.unsaved?
m.new_record?
m.valid?
m.errors.any?
m.errors
m.update(:duration => 60)
m.delete
q = p.models.all
m = p.models.first
m = p.models.last
q = p.models.featured.all
q = p.models.featured.nested.all
m = Model.featured.first
model = Mongocore::Access.role(:user) do
Model.first
end
class Model
include Mongocore::Document
validate do
errors[:duration] << 'duration must be greater than 0' if duration and duration < 1
errors[:goal] << 'you need a higher goal' if goal and goal < 5
end
before :save, :setup
def setup
puts "Before save"
end
after :delete do
puts "After delete"
end
end
Mongocore.db[:models].find.to_a
Mongocore.db[:models].find({:_id => m._id}).first
Mongocore.db[:models].indexes.create_one(:key => 1)
Mongocore.db[:models].indexes.create_one({:key => 1}, :unique => true)
Schema and models
Each model is defined using a YAML schema file. This is where you define keys, defaults, description, counters, associations, access, tags, scopes and accessors.
The default schema file location is APP_ROOT/config/db/schema/*.yml
, so if you have a model called Parent, create a yml file called parent.yml.
You can change the shema file location like this:
Mongocore.schema = File.join(Dir.pwd, 'your', 'schema', 'path')
Parent example schema, has many Models
meta:
name: parent
type: document
keys:
_id:
desc: Unique id
type: object_id
read: all
write: app
world:
desc: Parent world
type: string
read: all
write: user
models_count:
desc: Models count
type: integer
default: 0
read: all
write: app
models_featured_count:
desc: Models featured count
type: integer
default: 0
read: all
write: app
many:
- models
Model example schema, belongs to Parent
meta:
name: model
type: document
keys:
_id:
desc: Unique id
type: object_id
read: all
write: app
duration:
desc: Model duration in days
type: integer
default: 60
read: dev
write: user
tags:
- badge
expires_at:
desc: Model expiry date
type: time
read: all
write: dev
tags:
- badge
- campaigns
location_data:
desc: Model location data
type: hash
read: all
write: user
votes_count:
desc: Votes count
type: integer
default: 0
read: all
write: dev
tags:
- badge
parent_id:
desc: Parent id
type: object_id
read: all
write: dev
accessor:
- submittable
- update_expires_at
- skip_before_save
scopes:
featured:
duration: 60
nested:
goal: 10
finished:
duration: 60
goal:
$gt: 10
active:
params:
- duration
duration:
$ne: duration
ending:
params:
- user
$or:
- user_id: user.id
- listener: user.id
- listener: user.link
deletors:
$ne: user.id
Contribute
Contributions and feedback are welcome! MIT Licensed.
Issues will be fixed, this library is actively maintained by Fugroup Ltd. We are the creators of CrowdfundHQ.
Thanks!
@authors: Vidar