Skip to content

Commit

Permalink
Add insert(), insertOne()
Browse files Browse the repository at this point in the history
  • Loading branch information
baumandm committed May 23, 2017
1 parent db00d8e commit c4fe378
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 0.7.0

- Add db.collection.insert() and db.collection.insertOne() functions

## 0.6.0

- Add Date() and ISODate() functions
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject monglorious "0.6.0"
(defproject monglorious "0.7.0"
:author "Dave Bauman"
:description "Query MongoDB using strings!"
:url "https://github.com/baumandm/monglorious"
Expand Down
10 changes: 10 additions & 0 deletions src/monglorious/transforms.clj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@
"aggregate"
(fn [_ db] (apply (partial mg-coll/aggregate db collection-name) args))

"insert"
(let [document-or-documents (first args)]
(if (sequential? document-or-documents)
(fn [_ db] (mg-coll/insert-batch db collection-name document-or-documents))
(fn [_ db] (apply (partial mg-coll/insert-and-return db collection-name) args))
))

"insertone"
(fn [_ db] (apply (partial mg-coll/insert-and-return db collection-name) args))

(throw (Exception. (format "Unsupported function: %s." function-name)))))

;; More than one function
Expand Down
38 changes: 37 additions & 1 deletion test/monglorious/transforms_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[clj-time.format :as f]
[clj-time.coerce :as c])
(:use midje.sweet)
(:import (com.mongodb MongoQueryException)))
(:import (com.mongodb MongoQueryException WriteResult)))

;; Test run-command-transform()
(against-background
Expand Down Expand Up @@ -285,3 +285,39 @@
(execute {} "testdb" "db.timeDocs.find({ time: { $lt: new ISODate('2017-02-15T00:00:00.000Z') } }).count()") => 1
(execute {} "testdb" "db.timeDocs.find({ time: { $lte: new ISODate('2017-02-15T00:00:00.000Z') } }).count()") => 2
(execute {} "testdb" "db.timeDocs.find({ time: { $lt: new ISODate('2017-02-17') } }).count()") => 2))

;; Test collect-command-transform() with insert()
(against-background
[(before :contents
(let [conn (mg/connect)
db (mg/get-db conn "testdb")]
(if db (mc/drop db "documents"))))
(after :contents
(let [conn (mg/connect)]
(mg/drop-db conn "testdb")))]

(fact "Monglorious inserts a document"
(execute {} "testdb" "db.documents.insert({name: 'TEST'})") => #(= "TEST" (get % "name"))
(execute {} "testdb" "db.documents.insert({name: 'DOCUMENT'})") => (fn [_]
(let [conn (mg/connect)
db (mg/get-db conn "testdb")
docs (mc/find-maps db "documents")]
(and (= 2 (count docs))
(= #{"TEST" "DOCUMENT"} (set (map :name docs)))))))

(fact "Monglorious inserts several documents"
(execute {} "testdb" "db.documents.insert([{name: 'BATCH 1'}, {name: 'BATCH 2'}])") => (fn [_]
(let [conn (mg/connect)
db (mg/get-db conn "testdb")
docs (mc/find-maps db "documents")]
(and (= 4 (count docs))
(= #{"TEST" "DOCUMENT" "BATCH 1" "BATCH 2"} (set (map :name docs)))))))

(fact "Monglorious insertOnes a document"
(execute {} "testdb" "db.documents.insertOne({name: 'ONE'})") => #(= "ONE" (get % "name"))
(execute {} "testdb" "db.documents.insertOne({name: 'TWO'})") => (fn [_]
(let [conn (mg/connect)
db (mg/get-db conn "testdb")
docs (mc/find-maps db "documents")]
(and (= 6 (count docs))
(= #{"TEST" "DOCUMENT" "BATCH 1" "BATCH 2" "ONE" "TWO"} (set (map :name docs))))))))

0 comments on commit c4fe378

Please sign in to comment.