[Dbix-class] Multi-tennant application/data and dbix-class

Andrew Beverley andy at andybev.com
Wed Jan 3 22:07:49 GMT 2018


On Wed, 3 Jan 2018 20:37:58 +0000 Aaron Trevena wrote:
> I was wondering if anyone can share any success, tips and problems of
> using dbix-class in a multi-tenant application.
> 
> I'm looking at using tenant id in the main tables,

Yes, that's what I do.

> and thinking best approach will be to add logic to a resultset base
> class or role to hook in forcing queries to restrict by tenant id

Yes, same as. I add an accessor in Schema.pm which contains tenant_id.
Each time a request is made to the application, the very first thing I
do is set $schema->tenant_id.

Then, I add a default resultset (also to Schema.pm), which adds this
as a search automatically to all queries, if it exists in the table.

Here's an example:

https://github.com/ctrlo/GADS/blob/master/lib/GADS/Schema.pm

> and any gotchas,

The one gotcha I hit recently was that if you call
$self->result_source->resultset in a Result, then it will not include
the above tenant_id search. I therefore use
$self->result_source->schema->resultset instead.

Andy



More information about the DBIx-Class mailing list