All Projects → elbywan → moongoon

elbywan / moongoon

Licence: MIT license
An object-document mapper for MongoDB. 🌙

Programming Languages

crystal
512 projects

Projects that are alternatives of or similar to moongoon

Mgm
Mongo Go Models (mgm) is a fast and simple MongoDB ODM for Go (based on official Mongo Go Driver)
Stars: ✭ 265 (+546.34%)
Mutual labels:  mongo, odm
php-mongo-migrator
Migrations of MongoDB. Part of @PHPMongoKit
Stars: ✭ 29 (-29.27%)
Mutual labels:  mongo, odm
Php Mongo
MongoDB ODM. Part of @PHPMongoKit
Stars: ✭ 228 (+456.1%)
Mutual labels:  mongo, odm
DoctrineMongoODMModule
Laminas Module for Doctrine MongoDB ODM
Stars: ✭ 83 (+102.44%)
Mutual labels:  mongo, odm
Mongoengine
MongoEngine is a Python Object-Document Mapper for working with MongoDB. Documentation is available at https://mongoengine-odm.readthedocs.io - there is currently a tutorial, a user guide, and an API reference.
Stars: ✭ 3,632 (+8758.54%)
Mutual labels:  mongo, odm
Odmantic
Async ODM (Object Document Mapper) for MongoDB based on python type hints
Stars: ✭ 240 (+485.37%)
Mutual labels:  mongo, odm
derivejs
DeriveJS is a reactive ODM - Object Document Mapper - framework, a "wrapper" around a database, that removes all the hassle of data-persistence by handling it transparently in the background, in a DRY manner.
Stars: ✭ 54 (+31.71%)
Mutual labels:  mongo, odm
koa-session-mongoose
Mongoose store for Koa sessions
Stars: ✭ 29 (-29.27%)
Mutual labels:  mongo
create-mern-ts-app
Create a Mongo-Express-React-Node Application written in TypeScript out of the box.
Stars: ✭ 30 (-26.83%)
Mutual labels:  mongo
petstore
A simple skeleton to build api's based on the chubbyphp-framework, mezzio (former zend-expressive) or slim.
Stars: ✭ 34 (-17.07%)
Mutual labels:  odm
Registration-and-Login-using-MERN-stack
Simple Registration and Login component with MERN stack
Stars: ✭ 210 (+412.2%)
Mutual labels:  mongo
discord-economy-super
Easy and customizable economy module for your Discord bot.
Stars: ✭ 28 (-31.71%)
Mutual labels:  mongo
pytest-mock-resources
Pytest Fixtures that let you actually test against external resource (Postgres, Mongo, Redshift...) dependent code.
Stars: ✭ 84 (+104.88%)
Mutual labels:  mongo
jsonOdm
A JSON ODM (object document mapper) for JavaScript to use on the server or in the browser.
Stars: ✭ 95 (+131.71%)
Mutual labels:  odm
eReports-open-source
Sistema de envio e agendamento de relatórios
Stars: ✭ 30 (-26.83%)
Mutual labels:  mongo
tutorials
Collection of tutorials for various libraries and technologies
Stars: ✭ 98 (+139.02%)
Mutual labels:  mongo
deploy shard mongodb
This repository has a set of scripts and resources required for deploying MongoDB replicated sharded cluster.
Stars: ✭ 17 (-58.54%)
Mutual labels:  mongo
product-catalog-spring-mvc-demo
A Product Catalog Demonstration Project Using Spring MVC and Mongo DB
Stars: ✭ 16 (-60.98%)
Mutual labels:  mongo
toutiao
模仿今日头条,实现 APP 端,Server 端, Web 管理端
Stars: ✭ 17 (-58.54%)
Mutual labels:  mongo
mongo-dot-notation
Transform objects to MongoDB update instructions
Stars: ✭ 35 (-14.63%)
Mutual labels:  mongo

moongoon

A MongoDB ODM written in Crystal.

travis-badge GitHub tag (latest SemVer) GitHub

An object-document mapper (ODM) library written in Crystal which makes interacting with MongoDB a breeze.

This library relies on:

  • cryomongo as the underlying MongoDB driver.
  • bson.cr as the BSON implementation.

For the moongoon version relying on the mongo.cr driver, please check the mongo.cr branch.

Installation

  1. Add the dependency to your shard.yml:
dependencies:
  moongoon:
    github: elbywan/moongoon
  1. Run shards install

  2. Profit! 💰

Usage

Minimal working example

require "moongoon"

# A Model inherits from `Moongoon::Collection`
class User < Moongoon::Collection
  collection "users"

  index keys: { name: 1, age: 1 }, options: { unique: true }

  property name : String
  property age : Int32
  property pets : Array(Pet)

  # Nested models inherit from `Moongoon::Document`
  class Pet < Moongoon::Document
    property pet_name : String
  end
end

# Connect to the mongodb instance.
Moongoon.connect("mongodb://localhost:27017", database_name: "my_database")

# Initialize a model from arguments…
user = User.new(name: "Eric", age: 10, pets: [
  User::Pet.new(pet_name: "Mr. Kitty"),
  User::Pet.new(pet_name: "Fluffy")
])
# …or JSON data…
user = User.from_json(%(
  "name": "Eric",
  "age": 10,
  "pets": [
    { "pet_name": "Mr. Kitty" },
    { "pet_name": "Fluffy" }
  ]
))
# …or from querying the database.
user = User.find_one!({ name: "Eric" })

# Insert a model in the database.
user.insert

# Modify it.
user.name = "Kyle"
user.update

# Delete it.
user.remove

Connecting

API documentation

require "moongoon"

Moongoon.before_connect {
  puts "Connecting…"
}
Moongoon.after_connect {
  puts "Connected!"
}

# … #

Moongoon.connect(
  database_url: "mongodb://address:27017",
  database_name: "my_database"
)

# In case you need to perform a low level query, use `Moongoon.client` or `Moongoon.database`.
# Here, *db* is a `cryomongo` Mongo::Database. (For more details, check the `cryomongo` documentation)
db = Moongoon.database
cursor = db["my_collection"].list_indexes
puts cursor.to_a.to_json

Models

API documentation

require "moongoon"

class MyModel < Moongoon::Collection
  collection "models"

  # Note: the database can be changed - if different from the default one
  # database "database_name"

  # Define indexes
  index keys: { name: 1 }

  # Specify agregation pipeline stages that will automatically be used for queries.
  aggregation_pipeline(
    {
      "$addFields": {
        count: {
          "$size": "$array"
        }
      }
    },
    {
      "$project": {
        array: 0
      }
    }
  )

  # Collection fields
  property name : String
  property count : Int32?
  property array : Array(Int32)? = [1, 2, 3]
end

# …assuming moongoon is connected… #

MyModel.clear

model = MyModel.new(
  name: "hello"
).insert
model_id = model.id!

puts MyModel.find_by_id(model_id).to_json
# => "{\"_id\":\"5ea052ce85ed2a2e1d0c87a2\",\"name\":\"hello\",\"count\":3}"

model.name = "good night"
model.update

puts MyModel.find_by_id(model_id).to_json
# => "{\"_id\":\"5ea052ce85ed2a2e1d0c87a2\",\"name\":\"good night\",\"count\":3}"

model.remove
puts MyModel.count
# => 0

Running scripts

API documentation

# A script must inherit from `Moongoon::Database::Scripts::Base`
# Requiring the script before connecting to the database should be all it takes to register it.
#
# Scripts are then processed automatically.
class Moongoon::Database::Scripts::Test < Moongoon::Database::Scripts::Base
  # Scripts run in ascending order.
  # Default order if not specified is 1.
  order Time.utc(2020, 3, 11).to_unix

  def process(db : Mongo::Database)
    # Dummy code that will add a ban flag for users that are called 'John'.
    # This code uses the `cryomongo` syntax, but Models could
    # be used for convenience despite a small performance overhead.
    db["users"].update_many(
      filter: {name: "John"},
      update: {"$set": {"banned": true}}
    )
  end
end

Contributing

  1. Fork it (https://github.com/elbywan/moongoon/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

See the contributors page.

Credit

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].