Skip to content

gtebbutt/massive-cljs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

massive-cljs

Thin Clojurescript wrapper for MassiveJS

Massive's goal is to help you get data from your database. This is not an ORM, it's a bit more than a query tool - our goal is to do just enough, then get out of your way.

If you're wondering why you might want to use Massive over a traditional ORM in general, this blog post is a good place to start.

For Clojure(script) specifically, the drawbacks of using an ORM multiply: often the abstractions rely on extracting data into mutable objects, which are then edited and saved back to the database. Not an insurmountable problem, but a conflict of styles which rapidly becomes clear in development.

NB: Version 0.2.0 is compatible with Massive >=3.0.0; version 0.1.0 targets Massive 2.x

Usage

Require:

(ns example.core
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [massive-cljs.core :as massive]
            [massive-cljs.query :as query]
            [cljs.core.async :refer [<!]]))

Connect:

(massive/init! "http://localhost:5432")

Query:

Query functions return a channel, which will receive a response of the format:

{:error? false :content [{:id 1 :name "Steve"} {:id 2 :name "Bill"}]}

If :error? is true, the content key will be absent and the error message will be present as :msg.

In general query/db-fn mirrors the syntax of Massive's database functions:

(go
  (let [response (<! (query/db-fn :users :find {:city "London"}))]
    (if (:error? response)
      (println (:msg response))

      (for [row (:content response)]
        (println row)))))
(query/db-fn :users :save {:email "new@example.com" :city "Paris"}) ;Insert a new user
(query/db-fn :users :save {:id 4 :city "New York"}) ;Update an existing user by including the PK as a parameter
(query/db-fn :users :find-one {:email "email@example.com"}) ;Returns a single result in :content, rather than a list
(query/db-fn :my-special-function [arg1 arg2]) ;Looks for db/mySpecialFunction.sql in project root

Raw SQL can be executed directly using query/run, with a list of parameters as the optional second argument:

(query/run "SELECT * FROM users WHERE id > $1" [min-id])

About

Thin Clojurescript wrapper for massive.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published