- Notifications
You must be signed in to change notification settings - Fork9
A fully featured, data-driven database library for Clojure.
License
kwrooijen/gungnir
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A fully featured, data-driven database library for Clojure.
It is said that Gungnir could strike any target, regardless of the wielder'sskill.
- Developer, speaking to the database admin.
Dutch Clojure Meetup - Gungnir
(gungnir.database/make-datasource! {:adapter"postgresql":username"postgres":password"postgres":database-name"postgres":server-name"localhost":port-number5432})(defaccount-model [:map [:account/id {:primary-keytrue} uuid?] [:account/email {:before-save [:string/lower-case]:before-read [:string/lower-case]} [:re {:error/message"Invalid email"}#".+@.+\..+"]] [:account/password {:before-save [:bcrypt]} [:string {:min6}]] [:account/password-confirmation {:virtualtrue} [:string {:min6}]] [:account/created-at {:autotrue} inst?] [:account/updated-at {:autotrue} inst?]])(gungnir.model/register! {:account account-model})(defnpassword-match? [m] (= (:account/password m) (:account/password-confirmation m)))(defmethodgungnir.model/validator:account/password-match? [_] {:validator/key:account/password-confirmation:validator/fn password-match?:validator/message"Passwords don't match"})(defmethodgungnir.model/before-save:bcrypt [_k v] (buddy.hashers/derive v))(defnattempt-register-account [request] (-> (:form-params request) (gungnir.changeset/cast:account) (gungnir.changeset/create [:account/password-match?]) (gungnir.query/save!)))(gungnir.query/find-by!:account/email"some@email.com");; => {:account/email "some@email.com",,,}(-> (gungnir.query/limit5) (gungnir.query/select:account/id:account/email) (gungnir.query/all!:account));; => [{:account/email "..." :account/id "..."},,,]
Gungnir is still in its design phase and can result in breaking changes while onthe SNAPSHOT version. Any breaking changes will be reflected in the updateddocumentation.
Add the following dependencies to yourproject.clj
:dependencies [[kwrooijen/gungnir"0.0.2-xxxxxxxx.yyyyyy-z"];; Optionally for frontend validation [kwrooijen/gungnir.ui"0.0.2-xxxxxxxx.yyyyyy-z"] ,,,]
The Clojure community tends to lean towards the "pick the libraries that youneed" method rather than using a "framework" when building an application. Thiscan make it challenging for new users. Once you're familiar with the Clojureecosystem you'll know which libraries you prefer and create your own setup. Andthat's exactly what I've done.
If you want complete control over your database stack, then this is probably notfor you. If you're a beginner and are overwhelmed with all the necessarylibraries and configuration, or if you're looking for a Clojure database librarythat aims to provide a quality of life experience similar to Ruby's ActiveRecordor Elixir's Ecto, then stick around.
I cannot stress this enough, I really dislike macros. Clojure and a large partof it's community have taught me the beauty of writing data driven code. Withgreat libraries such as HoneySQL, Hiccup, Integrant, Reitit, Malli, I think thisis the Golden age of Data Driven Clojure. I never want to see macros in my APIagain.
Include Gungnir in your project, and off you go! Gungnir includes everything foryour database needs.
Gungnir uses models to provide data validation and seamless translation betweenClojure and SQL.Read more
Inspired by Elixir Ecto's Changesets. Validate your data before inserting orupdating it in your database. View the actual changes being made, and aggregateany error messages.Readmore
Gungnir isn't here to reinvent the wheel. Even though we have an interface forquerying the database, we can still make use of HoneySQL syntax. This allows usto expand our queries, or write more complex ones for the edge cases.Readmore
Define your migrations using Clojure data structures and extend them asneeded. You can also fallback to raw SQL if necessary.Readmore
Relations are easily accessed with Gungnir. Records with relations will haveaccess torelational atoms
which can be dereffed to query any relatedrows.Readmore,and more
Gungnir also provides an extra package,gungnir.ui. Which provides somevalidation in the frontend.Readmore
Read the guide for a fulloverview of all the features and how to use them.
TheGungnir code playgroundis a repository with an "interactive tutorial". Clone the repository and executethe code in the core namespace step by step.
In order to run the tests you'll needdocker-compose. Make sure thisis an up to date version. Inside of the root directory you can setup the testingdatabases with the following command.
docker-compose up -d
Then run the tests withlein
leintest
Released under theMIT License byKevin William van Rooijen.
About
A fully featured, data-driven database library for Clojure.