Multi-tenant application with Salat

106 views
Skip to first unread message

Manuel Bernhardt

unread,
Jul 13, 2012, 12:17:12 PM7/13/12
to scala...@googlegroups.com
Hi,

I'm looking into making our Play 2 application multi-tenant, in such a
way that it would use different connections (to different DBs)
depending on which subdomain / URL is accessed (customer1.foo.org,
customer2.foo.org, ...).

We've been using SalatDAO so far, and I see there's now also a
ModelCompanion trait that adds additional functionality.

I've been trying to think of an easy way to keep the straight-forward
way of accessing the DAO companion object directly ( e.g.
User.find(...) ) and yet switch to the correct connection behind the
scenes.

What I've been thinking about is to make use of implicit parameters, like e.g.

case class User(...)

object User extends ModelCompanion[User, ObjectId] {

def dao(implicit subdomain: String) = { ... }

def findAll()(implicit subdomain: String) = dao.find(MongoDBObject())

}

subdomain could be anything really, the crux is that if it is in
scope, when a method of the User DAO is called, it'd be able to pass
that key along and dao could resolve dynamically to whatever is
suited.


Does anyone think this kind of design is a good idea, and is there
perhaps something more clever (right now you've got to include that
implicit parameter all over, but I can't think of a better way atm)?

I've been thinking of giving this a go by implementing a delegating
DAO trait that'd be used in all cases. Note that I'd probably use that
directly instead of using the ModelCompanion (we're not using it,
since we don't need the JSON capabilities).

Thanks,

Manuel

Manuel Bernhardt

unread,
Oct 29, 2012, 9:38:58 AM10/29/12
to scala...@googlegroups.com
Hi,

for the record, this is the solution I went for in the end:

https://github.com/delving/culture-hub/blob/12.10/web-core/app/models/MultiModel.scala

One example implementation:

- case class definition:
https://github.com/delving/culture-hub/blob/12.10/web-core/app/models/Group.scala#L13
- MultiModel: https://github.com/delving/culture-hub/blob/12.10/web-core/app/models/Group.scala#L24
- DAO implementation:
https://github.com/delving/culture-hub/blob/12.10/web-core/app/models/Group.scala#L38
(the BindingModel / Injectable are just related to using Subcut, not
to the MultiModel per-se)

Manuel
Reply all
Reply to author
Forward
0 new messages