Skip to content

MongoDB adapter for ROM.

License

Notifications You must be signed in to change notification settings

bestwebua/rom-mongo

Repository files navigation

MongoDB adapter for Ruby Object Mapper

Maintainability Test Coverage CircleCI Gem Version Downloads GitHub Contributor Covenant

rom-mongodb - MongoDB adapter for ROM. What is ROM? It's a fast ruby persistence library with the goal of providing powerful object mapping capabilities without limiting the full power of the underlying datastore.

Table of Contents

Requirements

Ruby MRI 2.5.0+

Installation

Add this line to your application's Gemfile:

gem 'rom-mongodb'

And then execute:

bundle

Or install it yourself as:

gem install rom-mongodb

Usage

# Define your container with mongo adapter

require 'mongo'
require 'rom'
require 'rom/mongo'

connection = Mongo::Client.new('mongodb://127.0.0.1:27017/your_db_name')
container = ROM.container(:mongo, connection) do |config|
  config.relation(:users) do
    schema(:users) do
      attribute :_id, ROM::Types.Nominal(BSON::ObjectId)
      attribute :email, ROM::Types::String
      attribute :rating, ROM::Types::Integer
      attribute :status, ROM::Types::Bool
      attribute :orders?, ROM::Types::Array # optional attribute, if field does not exists will return nil
    end
  end
end

# Define your repository

require 'rom/repository'

UserRepository = ::Class.new(ROM::Repository[:users]) do
  commands(:create, :delete, update: :by_pk)

  def all
    users.to_a
  end

  def find(**options)
    users.find(options)
  end
end

user_repository = UserRepository.new(container)

# Now you can do some manipulations with your repository

user_repository.create({ email: 'olo@domain.com', rating: 42, status: true })
user_repository.all
user_repository.find(email: 'olo@domain.com')

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bestwebua/rom-mongo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct. Please check the open tickets. Be sure to follow Contributor Code of Conduct below and our Contributing Guidelines.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the rom-mongodb project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Credits

Versioning

rom-mongodb uses Semantic Versioning 2.0.0