Giter VIP home page Giter VIP logo

pghstore-clj's Introduction

pghstore-clj

What is it?

pghstore-clj is a small helper library to help you work with PostgresSQL's hstore data type with the postgresql JDBC adapter (I use version 9.1-901.jdbc4) in Clojure. It provides two simple helpers to help you get your data into and out of a hash-map.

Installation

Add pghstore-clj to your project.clj file in leiningen:

    [pghstore-clj "0.1.0"]

Usage

Once installed, you can use it like so:

user> (use '[pghstore-clj.core])
nil
user> (def h (to-hstore {:color "blue" :size "small"}))
#'user/h
user> h
#<PGobject "color"=>"blue", "size"=>"small">
user> (from-hstore h)
{:color "blue", :size "small"}
user>

Korma example

If you have a table named "products" with an hstore column called "attributes", a korma insertion statement might look like this:

    (insert products
      (values {:name "computer"
               :attributes (to-hstore {:color "black" :manufacturer "samsung"})}))

When you pull the row back out again, just call "from-hstore" on the value at attributes.

user> row
{:name "computer", :attributes #<PGobject "color"=>"black", "manufacturer"=>"samsung">}
user> (from-hstore (:attributes row))
{:color "black", :manufacturer "samsung"}
user>

If you are using Korma's defentity function, you can apply the hstore translation at the entity rather than at query-time. For the above products example, your entity might look something like so:

    (defentity products
      (prepare #(update-in % [:attributes] to-hstore))
      (transform #(update-in % [:attributes] from-hstore)))

With this in place, the translation is transparent and you can insert plain data structures:

    (insert products
      (values {:name "computer"
               :attributes {:color "black" :manufacturer "samsung"}}))

Wishlist

Ideally, these transformation functions would be applied at the JDBC level, so that you only have to work with hashes - This is going to take a bit more work.

License

Copyright © 2012 Blake Smith

Distributed under the Eclipse Public License, the same as Clojure.

pghstore-clj's People

Contributors

benburkert avatar blakesmith avatar sparkertime avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.