seancorfield / next-jdbc

A modern low-level Clojure wrapper for JDBC-based access to databases.
https://cljdoc.org/d/com.github.seancorfield/next.jdbc/
Eclipse Public License 1.0
750 stars 89 forks source link

Allow using suffixes other than `id`/`_id` for datafying results #267

Closed sheluchin closed 7 months ago

sheluchin commented 7 months ago

Is your feature request related to a problem? Please describe.

By default, next.jdbc assumes that a column named id or _id is a foreign key into a table called with a primary key called id. https://github.com/seancorfield/next-jdbc/blob/d6fe4c15777080b1ee25f0da6ec8d30345f92303/doc/datafy-nav-and-schema.md#identifying-foreign-keys

The id/_id suffix is hardcoded and cannot be changed. My schema uses did instead of id. I could set each relationship up in the :schema key, but being able to globally set a different suffix would be more convenient.

Describe the solution you'd like Allow users to set a different suffix to use for datafying results in order to get lazy navigation support.

In my case, the foreign key foo_did does point to a foo.did column in the referenced table, but it's conceivable that users may have schemas where the mapping is not as predictable, like foo_fk might point to foo.id. Being able to define suffix relationships that support this kind of thing would be helpful.

Describe alternatives you've considered Using :schema to define each relationship works, but it's less convenient.

Additional context Discussed in https://clojurians.slack.com/archives/C1Q164V29/p1701705842770289.

seancorfield commented 7 months ago

I'm thinking of adding :schema-opts as a hash map option to specify the FK suffix and the PK column, but perhaps also a :pk-fn that can be used instead of :pk (column name) to turn <table> into <table>id or whatever since I know some folks use weird naming like that.

seancorfield commented 7 months ago

There's a snapshot on Clojars with :schema-opts as well as the latest develop branch.

:schema-opts {:fk-suffix "did" :pk "did"} would address your needs.