https://github.com/ShaneKilkelly/clj-jdbc-pg-sanity.git
git clone 'https://github.com/ShaneKilkelly/clj-jdbc-pg-sanity.git'
(ql:quickload :ShaneKilkelly.clj-jdbc-pg-sanity)
Eases interoperability between clojure/java datatypes and postgres datatypes. No more boilerplate!
Handles the following:
DATE
↔ java.time.LocalDate
TIMESTAMP/TIMESTAMPTZ
↔ java.time.ZonedDateTime
JSON/JSONB
↔ clojure map/vectorARRAY
(e.g. int[]
) ↔ clojure vectorBYTEA
↔ byte arrayHSTORE
↔ clojure map (limited support - jdbc stringifies all contents)Can also insert (but not retrieve) the following types:
java.util.Date
→ DATE/TIMESTAMP/TIMESTAMPTZ
java.sql.Timestamp
→ DATE/TIMESTAMP/TIMESTAMPTZ
java.nio.ByteBuffer
→ BYTEA
Note: this library was once called jdbc-pg-sanity, mpg
is the new version.
Add mpg
as a leiningen or boot dependency:
[mpg "1.3.0"]
Just require the mpg.core
namespace and call patch
(ns whatever.db
(require [clojure.java.jdbc :as j]
[mpg.core :as mpg]]))
(mpg/patch) ;; take the default settings
(mpg/patch {:default-map :hstore}) ;; custom settings are merged with the defaults
;; valid settings:
:data - boolean, default true. auto-map maps and vectors?
:datetime - boolean, default true. auto-map java.time.{LocalDate, ZonedDateTime} ?
:default-map - keyword. one of :json, :jsonb, :hstore. Default :jsonb
The current clojure.java.jdbc interface imposes some limitations on us.
You need a database and a user on it with which we can run tests.
You can provide information about these with environment variables:
bash
MPG_TEST_DB_URI # default is '//127.0.0.1:5432/mpg_test'
MPG_TEST_DB_USER
MPG_TEST_DB_PASS
You can create a postgres database to test with createdb
and give your
user permissions with GRANT
as per normal postgres.
Running the tests is the same as any leiningen-based project:
boot test
Contributions and improvements welcome, just open an issue! :)
If this library should do something, and it doesn't currently do it, please fork and open a pull request. All reasonable contributions will be accepted.
Please run the tests before opening a pull request :)
This library was originally extracted from luminus boilerplate which hails from code floating around the internet generally. James Laver basically rewrote it.
Copyright © 2016 Shane Kilkelly, James Laver
Distributed under the MIT license.