在MySQL Loopback Connector上执行原始查询

 手机用户2502918445 发布于 2022-12-10 18:17

如何通过带有strongloop的REST API执行原始查询并公开结果?

我已经阅读了有关使用的内容hooks,dataSource.connector.query()但我找不到任何有用的例子.

1 个回答
  • 这是一个基本的例子.如果您有产品型号(/common/models/product.json),请通过添加/common/models/product.js文件来扩展模型:

    module.exports = function(Product) {
    
        Product.byCategory = function (category, cb) {
    
            var ds = Product.dataSource;
            var sql = "SELECT * FROM products WHERE category=?";
    
            ds.connector.query(sql, category, function (err, products) {
    
                if (err) console.error(err);
    
                cb(err, products);
    
            });
    
        };
    
        Product.remoteMethod(
            'byCategory',
            {
                http: { verb: 'get' },
                description: 'Get list of products by category',
                accepts: { arg: 'category', type: 'string' },
                returns: { arg: 'data', type: ['Product'], root: true }
            }
        );
    
    };
    

    这将创建以下端点示例:GET/Products/byCategory?group = computers

    http://docs.strongloop.com/display/public/LB/Executing+native+SQL

    2022-12-11 02:08 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有