简体   繁体   中英

Loopback Mysql connector custom query result to field value

I use Loopback 3 with Mysql. We have dynamic datamodel use. For example UserAnswers containing type like 'admin' or 'user'. I want to add count of answers to countOfAnswers field for types.

Like: type 'admin' has 5 answers. type 'user' has 3 answers.

I need to count type of answers before save to get current index for answers. Like 'Select * from UserAnswers where type = "admin"' and insert it to field value.

What is the best way to do counting or custom sql. Operation hook mayby? Or could I use custom field defination in model?

LoopBack provides count endpoint out of the box. Just create UserAnswers model mapped to your database table and then make the following request:

GET /api/UserAnswers/count?where[type]=admin

Learn more in LoopBack docs here: Get instance count .


Regarding custom SQL queries: it is possible to execute arbitrary SQL query by calling myModel.dataSource.connector.execute , see the docs: Executing native SQL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM