vendredi 6 mars 2015

Change connection used on runtime

I hope you can help me (us). I'm working on a API project which have two databases :


Production DB : api.myapp.fr


Testing DB : test.api.myapp.fr


Theses two databases are writable by the user.


When a user call our API, he can set the authorization header whichever he needs. For example :


Authorization: s_0


Will perform operations on api.myapp.fr and


Authorization: s_t_0


Will perform operations on test.api.myapp.fr .


My question is : How can I do that with sails ?


Actually, I have a policie which check if the user is using a production key or a testing key, and I override the default models with the one for testings purposes, like this :



if (!is_production) {
req.session.isProd = false;
req.session.logs.environment = "test";

User = UserTest;
Payment = PaymentTest;
PayzenStatus = PayzenStatusTest;
Transaction = TransactionTest;
Card = CardTest;
Doc = DocTest;
}


But you can see the problem if a user makes a test request and then a production request, the models are still the tests ones...


I use my models in services and policies, therefor I can't do



req.models = {};
// If not in production, use the test models
if (!is_production) {
req.session.isProd = false;
req.session.logs.environment = "test";

req.models.User = UserTest;
req.models.Payment = PaymentTest;
req.models.PayzenStatus = PayzenStatusTest;
req.models.Transaction = TransactionTest;
req.models.Card = CardTest;
req.models.Doc = DocTest;
}
// Otherwise use the production models
else {
req.models.User = User;
req.models.Payment = Payment;
req.models.PayzenStatus = PayzenStatus;
req.models.Transaction = Transaction;
req.models.Card = Card;
req.models.Doc = Doc;
}


If you have any idea on how ton achieve this (whatever the way, we can still perform deep changes in our code), I would be really happy to ear it.


Thanks


Aucun commentaire:

Enregistrer un commentaire