Justin Taft - Home / Posts

Clojure(script) ORM – Design Thought

This post isn’t providing a solution, but thoughts on how to implement something similar in clj(s).

Java is harped on for getter and setter functions for setting simple properties. This abstraction is useful though, as implementation of how the fields are set is hidden. ORMs can leverage this abstraction to track state modification of objects, lazy loading of relationships, etc.

How does this relate to Clojure? Well, clojure is big on using simple collections (maps,sequences, etc.) rather then individual data classes. Under the hood, interfaces are still used to define how getting/setting values works.

Rather then creating utility functions to track updates to collections, why not define a new type that implements the appropriate interface of a collection, that can also track changes? For instance, whenever assoc is called, the type could append a new field to the map for what fields were updated ((or perhaps track in some metadata field that gets copied around as necessary).

This approach has the benefit that you can use ordinary clojure functions, and you don’t have to worry about accidentally using a non-utility functions necessary to keep track of updates.

Just for reference, here is the function definition of assoc for clojurescript:

https://github.com/clojure/clojurescript/blob/105d3febb8cb5e72c1dd27f396f3f363a73e7879/src/main/cljs/cljs/core.cljs#L2038

The same would apply to lazy fetching of sequences.

A couple libraries (that I haven’t used yet) that might be able to be used for this are:
https://github.com/johnmn3/wrap-map
https://github.com/clj-commons/potemkin


Leave a Reply

Your email address will not be published. Required fields are marked *