lucidquiet
lucidquiet

Reputation: 6558

Is there a Clojure in memory data store?

I program mostly in Node, and like document stores, but I'd like to prototype the data calls between the client and the server first. I've used lowdb and da-base in the past to setup a quick Json data store. Is there something similar for Clojure?

Upvotes: 6

Views: 3136

Answers (2)

M-x
M-x

Reputation: 141

Datascript seems like a perfect (though poorly named) fit for your needs. Basically, it's a lightweight in-memory store designed after Datomic. With a map-in-an-atom approach you very quickly would find yourself writing quirky code for selection, id management, etc. Datascript takes care of such stuff and allows you to write complex queries easily, still being almost as lightweight as a map in an atom.

Upvotes: 4

DanLebrero
DanLebrero

Reputation: 8593

Given that you are just prototype, if you don't need durability, a simple atom will do. If you want durability using simple files have a look at https://github.com/alandipert/enduro

You can have one atom per table or you can have an atom with a map of table->docs, whatever you find simpler. Any query will just be a filter.

For example, to add a document:

(def my-db (atom {}))
(defn add [table doc] (swap! my-db update-in [table] conj doc))
(defn search-by-name [table name] 
    (filter #(= name (:name %)) (get @my-db table)))

Upvotes: 7

Related Questions