datalevin 2025-09-19

I've been looking into Datalevin, and I'm unsure of the best way to store insertion order of entries. In SQL I'd use an auto-increment column. In Datalevin (and Datomic), I've considered maintaining a counter field that I update within a transaction, and I've also thought about using a sequential ID algorithm like Flake. Is there a better, more idiomatic option that I'm missing?

Thanks for the advice! That's the conclusion I was coming to as well. I guess what I really want is a deterministic time ordering, so I could sort by timestamp then ID to resolve any potential conflicts.

Yeah that’s what I would do. Honestly I’d do the same thing in SQL too, At least with Postgres, sequences can get out of order or skip due to a variety of reasons.

I'm not sure about datalevin specifically, but in Datomic you'd use a https://docs.datomic.com/transactions/transaction-functions.html#writing that first queries the highest existing id, increments it, then stores it. It seems like Datalevin has https://cljdoc.org/d/datalevin/datalevin/0.9.22/doc/transaction#transaction-functions-in-datalog-store. That said, I would strongly recommend against data models that need to maintain insertion order. I've certainly done this to meet very specific business requirements, but it's a pain and should be avoided if at all possible.

Does datalevin come with the same caveats for long string values like datomic? (Curious about datalog and KV mode)

I store fairly large strings in datalevin with no issues, I do not use the search index though