Skip to content
Integrant methods for connecting to a MongoDB database via Monger
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/duct/database/mongodb
test/duct/database/mongodb
.gitignore
.travis.yml
LICENSE
README.md
project.clj

README.md

Duct database.mongodb.monger

Build Status

Integrant methods for connecting to a MongoDB database via Monger.

Installation

To install, add the following to your project :dependencies

[me.grison/duct-mongodb "0.1.1"]

Usage

This library provides two things:

  • a Boundary record that holds both the Monger connection (:conn) and the selected database (:db).
  • a multimethod for :duct.database.mongodb/monger that initiates the connection based on those options into the Boundary.

When you write functions against the MongoDB database, consider using a protocol and extending the Boundary record. This will allow you to easily mock or stub out the database using a tool like Shrubbery.

Connection settings

URI

{:duct.database.mongodb/monger 
  {:uri "mongodb://127.0.0.1:27017/hello?username=foo&password=bar"}

Additional parameters can be used in the URI format.

Host & port

{:duct.database.mongodb/monger 
  {:host "127.0.0.1", :port 27017, :db-name "hello"}}

Host & port with extended options

{:duct.database.mongodb/monger 
  {:host "127.0.0.1", :port 27017, :db-name "hello"
   :options {:socket-timeout 1234
             :threads-allowed-to-block-for-connection-multiplier 300}}}

See MongoOptions for more information.

Example

Consider a MongoDB database where a users collection exist, containing documents having at least a username field.

The database connection can be extracted from this module Boundary by using the :db key.

If you need access to the whole connection you can do so using the :conn key.

(ns my-project.boundary.user-db
  (:require [duct.database.mongodb.monger]
            [monger.collection :as mc]))
            
(defprotocol UserDatabase
  (get-user [db username]))
  
(extend-protocol UserDatabase
  duct.database.mongodb.monger.Boundary
  (get-user [{:keys [db]} username]
    (mc/find db "users" {:username username})))

For more information using Monger, you can start with their getting started webpage.

Building & testing this library

If you already have a mongodb running on your machine just type lein test

If you don't have mongodb installed on your machine you can type lein embongo test which will download an embedded mongodb and run it during the test phase.

License

Copyright © 2018 Alexandre Grison

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

You can’t perform that action at this time.