Fork me on GitHub
#other-languages
<
2021-12-23
>
Ryan Singer14:12:57

Thanks @viebel for raising my question about associations here. Appreciate everyone's responses. Are there any public projects or sample code I might look at to better understand how a Clojurian deals with relational associations in practice? Many of the responses here strike me as reflections about how to do it better in the future rather than what is normal practice today.

Ryan Singer14:12:05

Or maybe I'm avoiding the strict answer ... which is just work it out case-by-case because there isn't a packaged/conventional answer.

borkdude14:12:03

Welcome to this slack @rjs!

borkdude14:12:59

I think there is a more fundamental thing at play here. Rails / ORM -like solutions, although they exist, are not popular in the Clojure world. Most people like to write/use libraries and compose their solutions of these libraries. Most libraries consist of "pure" functions that receive data and return data. In the SQL world there is a library called HoneySql which allows you to construct SQL queries from Clojure data. And then you can pass that query to a database connector called clojure.java.jdbc (or next.jdbc) (which is not pure because it has to do some effect to the database). The HoneySQL library does not know about the next.jdbc library but you can compose these things together yourself. This allows for optimal flexibility but does not optimize for optimal initial setup.

seancorfield18:12:17

@U04V15CAJ Careful. clojure.jdbc exists but it has less than auspicious beginnings and it is no longer maintained. I think you meant clojure.java.jdbc, the Contrib library. Could you edit your message please?

borkdude18:12:56

Sure, I was using it as an abbreviation for clojure.java.jdbc ;)

seancorfield18:12:37

Not a good abbreviation, given that it exists and is a very different library 🙂

borkdude18:12:49

Well, you can argue that the library chose a bad name too. But I stand corrected 😨

seancorfield18:12:52

I would agree with that too 🙂

borkdude14:12:07

But perhaps it's good to go back to your original question and get it clear what's the actual problem statement, because that might not be clear at all. Association might refer to something else than ORM/SQL.

Ryan Singer14:12:55

I think in order to ask the question properly, I should try to spike something in code. That'll make the ask more concrete.

Ryan Singer14:12:59

I'm just trying to envision my first steps outside of ActiveRecord. For example, the app I'm working on has lots of relations where a Task is associated with some Container via a Location. And Containers themselves are located within Phases. If I were to port my Rails-thinking directly over, I would keep some kind of map around for Containers, some kind of map for Locations, and use IDs to look up what belongs to what all the time.

Ben Sless14:12:01

You might find this introduction useful. In Clojure world the focus is often on the entities and their attributes, less on the relation. An entity's attribute can itself be a reference to another entity https://www.youtube.com/watch?v=oo-7mN9WXTw

Ryan Singer15:12:14

Wow this talk is amazing.

🎉 1
Ryan Singer14:12:07

I guess I would write custom functions then to look up Tasks given some Container. Again, maybe I have to spike this to understand what the real question is. I guess it's more just some anxiety about bringing the wrong mental models into a new universe.

borkdude14:12:27

If I would make a CRUD-like app, I would do the querying in the database (since that's where it's stored) and then this returns data (immutable) which you can then display or whatever you want. request -> handler -> sql -> data -> (html | json response)

Ryan Singer14:12:57

Is using SQL in the first place somehow "off" in the Clojure world? Is everyone using Datomic, or is that the domain of some special use cases?

seancorfield18:12:49

There's a #sql channel which covers clojure.java.jdbc (the older Contrib library) and next.jdbc (its modern replacement), as well as SQL stuff in general. There's also channels for the #honeysql and #hugsql libraries if you choose to use those @rjs

👍 1
borkdude15:12:19

No not at all, SQL is often used.

Ryan Singer15:12:49

Aha ok so at least I'm not in the wrong universe entirely.

Ben Sless15:12:01

But, "it's just data" 🙂

borkdude15:12:24

If you would like to get started with SQL, I'd start here: https://github.com/seancorfield/next-jdbc/blob/develop/doc/getting-started.md

👍 1
borkdude15:12:38

Perhaps go through the book Web Development with Clojure: https://pragprog.com/titles/dswdcloj3/web-development-with-clojure-third-edition/ This will teach you how to build typical web apps using commonly used libraries and patterns.

👍 1
borkdude15:12:43

There is also a "framework" which is really more like a template composed of libraries by the same author, called luminus. This is a "guestbook" example: https://github.com/luminus-framework/guestbook

Ryan Singer15:12:10

Ah, looks promising. Thanks

borkdude15:12:16

This is a really simplified one page example of a guestbook: https://github.com/kloimhardt/babashka-scittle-guestbook For persistence it uses a file on on disk. It's a toy example really. It runs with babashka which is a scripting environment for Clojure with fast startup time and is interpreted (doesn't support all of Clojure). Conceptually it works the same as what you would do in a production app (barring things like user management, connection pooling, etc, etc, again, toy example).

👍 1
yogthos16:12:31

@rjs and author here if you have any questions about the book 🙂

borkdude16:12:27

@yogthos I noticed the guestbook example hasn't been updated for a couple of years, but this is only because clojure is very stable and doesn't need updating right? ;)

yogthos16:12:11

more or less, I should probably update the library versions, but in terms of structure and API nothing really changed : )

🙇 1
yogthos16:12:23

I really appreciate the lack of churn in Clojure community

Ryan Singer17:12:44

Amazing that a couple year-old example can still hold like that

clojure-spin 3
andy.fingerhut17:12:14

Or perhaps more amazing is that there are so many software development environments where that is NOT true.

👆 1
seancorfield18:12:30

A lot of ten-year-old Clojure still runs just fine 🙂 Our codebase at work spans over a decade and we hardly ever have to make changes when we update the version of Clojure (or any of the other Clojure libraries we use -- but we often find updating Java libraries breaks things!).