Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade to version 4.2.3, add authorization, and distinct-vals function #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(defproject helpshift/mongrove "0.2.0"
(defproject helpshift/mongrove "0.2.1"
:description "An idiomatic Clojure wrapper for MongoDB java-driver 4.x"
:url "https://medium.com/helpshift-engineering"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:signing {:gpg-key "helpshift-clojars-admin"}
:dependencies [[org.clojure/clojure "1.10.0"]
[org.mongodb/mongodb-driver-sync "4.1.1"]
[org.mongodb/mongodb-driver-sync "4.2.3"]
[org.clojure/tools.logging "1.1.0"]]
:repl-options {:init-ns mongrove.core}
:jvm-opts ^:replace ["-Duser.timezone=UTC"
Expand Down
24 changes: 23 additions & 1 deletion src/mongrove/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ClientSessionOptions$Builder
MongoClientSettings
MongoClientSettings$Builder
MongoCredential
ReadConcern
ReadPreference
ServerAddress
Expand All @@ -20,6 +21,7 @@
(com.mongodb.client
ClientSession
FindIterable
DistinctIterable
MongoClient
MongoClients
MongoCollection
Expand Down Expand Up @@ -167,7 +169,8 @@
"Initialize a ConnectionPoolSettings object from given options map.
Available options : :read-preference :read-concern :write-concern
:retry-reads :retry-writes"
[{:keys [read-preference read-concern write-concern
[{{:keys [user-name password source]} :credential
:keys [read-preference read-concern write-concern
retry-reads retry-writes] :as opts}]
{:pre [(or (nil? read-preference)
(read-preference-map read-preference))
Expand All @@ -178,6 +181,7 @@
(let [opts (merge default-opts opts)
{:keys [read-preference read-concern write-concern
retry-reads retry-writes]} opts
credential (MongoCredential/createCredential user-name source (char-array password))
builder (doto (MongoClientSettings/builder)
(socket-settings opts)
(cluster-settings opts)
Expand All @@ -186,6 +190,7 @@
(.writeConcern (get write-concern-map write-concern))
(.readPreference (get read-preference-map read-preference))
(.retryWrites retry-writes)
(.credential credential)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The credential needs to be conditionally handled. Initialization without a username and password is failing.

;; @TODO : Documentation states that this method
;; exists ! yet we get method not found exception
#_(.retryReads retry-reads))]
Expand Down Expand Up @@ -367,6 +372,23 @@
(.countDocuments collection session bson-query)
(.countDocuments collection bson-query)))))

(defn ^:public-api distinct-vals
"Gets the distinct values for a field name that matches the filter."
([^MongoDatabase db
^String coll
^String field-name
^Class class-type
filter & {:keys [only exclude session]
:or {only [] exclude []}}]
(let [collection (get-collection db coll)
bson-filter (conversion/to-bson-document filter)
^DistinctIterable iterator
(if session
(.distinct ^MongoCollection collection session field-name bson-filter class-type)
(.distinct ^MongoCollection collection field-name bson-filter class-type))
cursor (.cursor iterator)]
(iterator-seq cursor))))


(defn ^:public-api delete
"Delete a document from the collection that matches `query`.
Expand Down