在MySQL Loopback Connector上执行原始查询 [英] Execute raw query on MySQL Loopback Connector

查看:78
本文介绍了在MySQL Loopback Connector上执行原始查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How is it possible to execute raw query and expose results through REST API with strongloop?

我已经阅读了有关使用hooksdataSource.connector.query()的内容,但是找不到任何有效的示例.

I've read something about using hooks and dataSource.connector.query() but I cannot find any working examples.

推荐答案

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

Here is a basic example. If you have a Product model (/common/models/product.json), extend the model by adding a /common/models/product.js file:

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

This will create the following endpoint example: GET /Products/byCategory?group=computers

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

这篇关于在MySQL Loopback Connector上执行原始查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆