Required :id field

11 views
Skip to first unread message

Meikel Brandmeyer

unread,
Feb 9, 2010, 4:08:00 AM2/9/10
to FleetDB
Hello Mark,

another issue troubling me with fleetdb is the requirement for an :id.
I have a lot of collections where each row is per definition unique.
However there is no special :id coming from the data itself. Now, I'm
in trouble to invent such a field. Couldn't fleetdb generate such a
field with the desired properties (ie. being unique)? It think couchdb
does that. If there is no :id, make one. Otherwise use it.

Or is there another preferred way? Some UUID library or so?

Sincerely
Meikel

Mark McGranaghan

unread,
Feb 9, 2010, 10:18:39 AM2/9/10
to FleetDB
The "if there is no :id, make one, otherwise use it" semantics are
desirable in some cases but should be implemented in the client layer.
The reason is that in general you may need the id of an object before
it is inserted into the database. For example if you're inserting a
child and a parent object in a single operation and the child points
the parent.

If you need to generate a UUID, you can use java.util.UUID:

(defn- uuid []
(str (UUID/randomUUID)))

You might also consider integer ids, because they are much more
efficient that strings (e.g. UUIDs). If you have a modest number of
records, the following will suffice:

(defn rand-id [#^Random rand]
(Math/abs (.nextLong rand)))

If you'de like to see auto-id functionality in the Clojure client
library I would consider a patch.

HTH,
- Mark

Reply all
Reply to author
Forward
0 new messages