Using multiple databases where choice of database is dynamic dependent on user.

299 views
Skip to first unread message

Wille Faler

unread,
Apr 26, 2013, 3:05:26 PM4/26/13
to sqlk...@googlegroups.com
Hi,
Quite new to Korma and Clojure, so apologies if this is basic:

Is there any way with Korma to choose the database for which queries are executed dynamically/provide a db for queries?
Basically, I'm building a webapp where dependent on which user is making a request I want them to have their queries directed at their personal schema. Effectively each user will have their own set of the same tables, containing only their data.

Firstly, is there a way to achieve this,secondly a way to do it in a relatively non-invasive way?

Regards
Wille Faler

Jason

unread,
Jun 27, 2013, 10:40:52 AM6/27/13
to sqlk...@googlegroups.com
I have the same issue, is this possible?

Jason

unread,
Jun 27, 2013, 11:40:29 AM6/27/13
to sqlk...@googlegroups.com
I got this to work with:

(defmacro defdynamicentity
  [ent & body]
  `(defn ~ent [] (-> (create-entity ~(name ent)) ~@body)))

(defdynamicentity users
  (table :users)
  (database user_db))

Tony

unread,
Feb 12, 2014, 8:34:44 PM2/12/14
to sqlk...@googlegroups.com
Hi,

I know this is an old post (and not even sure if the solution I'm proposing wasn't implemented after this post), but since I was having the same problem and now know the answer, I guess I'd share it.

It's possible to use the "database" function http://sqlkorma.com/api/0.3.0/korma.core.html#var-database in any query
The docs show the function being used when specifying an entity http://sqlkorma.com/docs#entities but it can also be used in the following way:

  (select source-table
    (fields :a :b)
    (database db1))

Easy and non-invasive.
Reply all
Reply to author
Forward
0 new messages