How to specify database? i.e. use same models with multiple databases
Hello,
I am reading up on thinky at the moment and can't figure out how I specify a database name for save, query operations, etc. For instance, I have the following database structure:
database "company_1" with their own tables for "staff", "posts", "comments", "attachments", etc
database "company_2" with their own tables for "staff", "posts", "comments", "attachments", etc
Then I have a separate database called "system" to help me coordinate which staff/user belongs to which database and provide access to documents on for that particular database. I would prefer not to mix the multi tenant info in one large database.
In the official driver, the way to do that would be r.db('company_1').table('staff').insert({ ... }).run(). Is there a way to do that with thinky?
I see that a db name can be passed at time of initialization so should I create multiple thinky instances like this:
function createThinky (name) {
var db = {};
var thinky = db.thinky = require('thinky')({ db: name });
db.Staff = thinky.createModel('staff', { ... schema ... }, { ... options ... });
db.Posts = thinky.createModel('posts', { ... schema ... }, { ... options ... });
db.Comments = thinky.createModel('comments', { ... schema ... }, { ... options ... });
db.Attachments = thinky.createModel('attachments', { ... schema ... }, { ... options ... });
// Then do the relations here for each model
return db;
}
var company_1 = createThinky('company_1');
var company_2 = createThinky('company_2');
I see that the ruby ORM for rethink called NoBrainer has an option to create the models and schemas only once and then specify the database to be used as Posts.with_database('company_1'). Is something like that possible in thinky? Perhaps through extending the Model prototypes, etc.
Thank you.
+1
I forgot to answer but the short answer is that it's not possible.
Adding this feature would require some work to properly initialize the new database. It's doable though, just a little cucumbersome to do.