[英]Loopback Mysql connector custom query result to field value
I use Loopback 3 with Mysql. 我使用Loopback 3与Mysql。 We have dynamic datamodel use.
我们使用动态数据模型。 For example UserAnswers containing type like 'admin' or 'user'.
例如UserAnswers包含类似'admin'或'user'的类型。 I want to add count of answers to countOfAnswers field for types.
我想为countOfAnswers字段添加类型的答案数。
Like: type 'admin' has 5 answers. 喜欢:类型'admin'有5个答案。 type 'user' has 3 answers.
类型'用户'有3个答案。
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. 比如
'Select * from UserAnswers where type = "admin"'
并将其插入到字段值中。
What is the best way to do counting or custom sql. 什么是计数或自定义SQL的最佳方法。 Operation hook mayby?
操作钩mayby? Or could I use custom field defination in model?
或者我可以在模型中使用自定义场定义吗?
LoopBack provides count
endpoint out of the box. LoopBack提供开箱即用的
count
端点。 Just create UserAnswers
model mapped to your database table and then make the following request: 只需创建映射到数据库表的
UserAnswers
模型,然后发出以下请求:
GET /api/UserAnswers/count?where[type]=admin
Learn more in LoopBack docs here: Get instance count . 在此处了解LoopBack文档中的更多信息: 获取实例计数 。
Regarding custom SQL queries: it is possible to execute arbitrary SQL query by calling myModel.dataSource.connector.execute
, see the docs: Executing native SQL . 关于自定义SQL查询:可以通过调用
myModel.dataSource.connector.execute
来执行任意SQL查询,请参阅文档: 执行本机SQL 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.