code-ninja has asked for the wisdom of the Perl Monks concerning the following question:
ok... apologies for yet another Catalyst doubt but I'm actually learning stuff these days... so, you know.
Anyways, I'm trying to connect to multiple databases depending on the user, for example: if user_1 logs in to my application, db_user_1_id should be automatically selected. Also, if a new user signs-up with my application, I'll create a model on the fly . To do this, I've taken the following approach, is there a better way?
My approach
1. If a new user signs-up, run a script (a bash or Perl script) that creates a MySql database for the new user. I'll call this new database DB_new_user. Now, I run another script that creates a model using the catalyst's helper script like:
$ script/myapp_create.pl model DB_new_user's_id DBIC::Schema myapp::Schema create=static dbi:mysql:DB_new_user '<Mysql username>' '<Mysql password>'
2. If an existing user (I'll call this user, user_n) logs in, a catalyst controller (say, `messages') can then select a specific database by saying:
sub messages :Local { my($self, $c) = @_; # somehow fetch user_n's id $c->stash(messages => [$c->model('DB_user_n::Messages')->all]); }
The codes shown are not necessarily correct, they are just an abstraction of what I'm doing/trying to achieve. :)
Update Just going through the Catalyst book again and it does say: If you need to use a different database, you can change the connection information in one file, restart your application, and the change will take effect everywhere, automatically.
No way... I love the concept of TIMTOWTDI in Perl. Still working on this.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Multiple Databases in Catalyst
by Jenda (Abbot) on May 11, 2013 at 10:54 UTC | |
by code-ninja (Scribe) on May 11, 2013 at 11:19 UTC | |
Re: Multiple Databases in Catalyst
by sundialsvc4 (Abbot) on May 11, 2013 at 17:18 UTC | |
by code-ninja (Scribe) on May 12, 2013 at 03:18 UTC |