loopback: How to use transactions in remote hooks?

328 views
Skip to first unread message

107821...@gmail.com

unread,
Jan 10, 2017, 4:17:25 AM1/10/17
to loopb...@googlegroups.com
I create a Project by  "POST /projects", and need add some related instance after create like project members;

So I create members like this : 

Project.afterRemote('create', function(){
 
Member.create(...)
});

But the " insert Project " and " insert Member " is not in same transaction (MySql) . How could I add transactions on it.

Thanks.

Mark Johnson

unread,
Jan 10, 2017, 7:14:25 PM1/10/17
to LoopbackJS
The concept of transactions does not really work well with this type of model.

You would need to access the MySQL driver directly and create your own version of the endpoint that rolls everything into a transaction.

Wesley

unread,
Jan 11, 2017, 2:33:59 AM1/11/17
to LoopbackJS


在 2017年1月11日星期三 UTC+8上午8:14:25,Mark Johnson写道:
The concept of transactions does not really work well with this type of model.

You would need to access the MySQL driver directly and create your own version of the endpoint that rolls everything into a transaction.


Thank you very much. now I see.

But, May I ask Why? Why loopback do not support that ? One Api request make two or more database operation is a very common scene, isn't it ? It's a TODO features? Or should I do that in some other why or other framework?

Thanks.

Mark Johnson

unread,
Jan 11, 2017, 5:25:26 PM1/11/17
to LoopbackJS
An ORM/RESTful API service really doesn't suit transactions.  Each endpoint is a separate call.  How would you expect to tell the ORM which API calls are part of a transaction, and how would you implement the commit/rollback procedure?  What if they're in different databases or using a Microservice configuration where the data source is a remote service (loopback allows this)?  I don't see it happening as there are too many complications.

Another framework (ORM/ODM based at least) really isn't going to help.  If you need transactions you will need to access the raw database API in any framework.
 
Reply all
Reply to author
Forward
0 new messages