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

Add Function example for pooling db connections WIP #60

Closed
wants to merge 1 commit into from
Closed
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
Empty file added examples/bb-db/README.adoc
Empty file.
3 changes: 3 additions & 0 deletions examples/bb-db/bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{:paths ["."]
:deps {org.clojure/data.json {:mvn/version "1.2.2"}
monger/monger {:mvn/version "3.1.0"}}}
33 changes: 33 additions & 0 deletions examples/bb-db/handler.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns function.handler
(:require [babashka.process :as process]

Choose a reason for hiding this comment

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

[clj-kondo] reported by reviewdog 🐶
namespace babashka.process is required but never used

[clojure.data.json :as json]
[monger.core :as mg]
[monger.collection :as mc]))

(def clients-db (atom nil))

(defn prepare-db []
(let [url (str "mongodb://" (System/getenv "mongo") ":27017/clients")]
(if @clients-db
@clients-db
(do
(reset! clients-db (mg/get-db (mg/connect url) "clients"))
@clients-db))))

(defn insert-user [event context]
(let [users (prepare-db)
record {:name (:body event)}]
(try
(mc/insert users "users" record)
(context
:status 200
:succeed {:status (str "Insert done of: " (json/write-str (:body event)))})
(catch Exception e
(context :fail (.toString e))))))

(defn handler [event context]
(insert-user event context))

;; ------------------------

(defn handler [s])

Choose a reason for hiding this comment

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

[clj-kondo] reported by reviewdog 🐶
unused binding s

Loading