other-languages

Ryan Singer 2021-12-23T14:41:57.126800Z

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 Singer 2021-12-23T14:43:05.127800Z

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.

borkdude 2021-12-23T14:45:03.128200Z

Welcome to this slack @rjs!

borkdude 2021-12-23T14:50:59.132300Z

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.

seancorfield 2021-12-23T18:06:17.159Z

@borkdude 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?

borkdude 2021-12-23T18:13:56.159700Z

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

seancorfield 2021-12-23T18:20:37.159900Z

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

borkdude 2021-12-23T18:24:49.160100Z

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

seancorfield 2021-12-23T18:25:52.160300Z

I would agree with that too 🙂

borkdude 2021-12-23T14:52:07.133200Z

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 Singer 2021-12-23T14:52:55.133900Z

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 Singer 2021-12-23T14:55:59.137100Z

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 Sless 2021-12-23T14:59:01.140800Z

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 Singer 2021-12-23T15:04:11.144700Z

Thanks

Ryan Singer 2021-12-23T15:49:14.147900Z

Wow this talk is amazing.

🎉 1
Ryan Singer 2021-12-23T14:57:07.138600Z

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.

borkdude 2021-12-23T14:58:27.140400Z

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 Singer 2021-12-23T14:59:57.142Z

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?

seancorfield 2021-12-23T18:09:49.159400Z

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
borkdude 2021-12-23T15:00:19.142400Z

No not at all, SQL is often used.

Ryan Singer 2021-12-23T15:00:49.142600Z

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

Ben Sless 2021-12-23T15:01:01.142700Z

But, "it's just data" 🙂

borkdude 2021-12-23T15:01:24.143200Z

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
borkdude 2021-12-23T15:02:38.144400Z

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
borkdude 2021-12-23T15:04:43.145500Z

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 Singer 2021-12-23T15:05:10.145900Z

Ah, looks promising. Thanks

borkdude 2021-12-23T15:08:16.147300Z

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
yogthos 2021-12-23T16:47:31.148700Z

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

borkdude 2021-12-23T16:48:27.149600Z

@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? ;)

yogthos 2021-12-23T16:49:11.150600Z

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

🙇 1
yogthos 2021-12-23T16:50:23.151300Z

I really appreciate the lack of churn in Clojure community

Ryan Singer 2021-12-23T17:03:44.151700Z

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

3
2021-12-23T17:14:14.152Z

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

👆 1
seancorfield 2021-12-23T18:08:30.159200Z

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!).