【问题标题】:Is there a way to query multiple tables at the same time in Sails?有没有办法在 Sails 中同时查询多个表?
【发布时间】:2016-01-29 20:45:58
【问题描述】:

我的任务是向网站添加 Angular Typeahead 搜索字段,并且数据需要来自多个表。它需要是一种“搜索所有事物”类型的查询,可以在一个位置查找人员、服务器和应用程序。

我在想最好的方法是在 Sails 中有一个 API 端点,它可以从同一个数据库上的 3 个表中提取并发送结果,但我不太确定如何去做。

【问题讨论】:

    标签: angularjs sails.js waterline sails-postgresql


    【解决方案1】:

    使用内置的bluebird library,特别是Promise.all()。要处理结果,请使用.spread()。示例控制器代码(根据您的情况进行修改):

    var Promise = require('bluebird');
    
    module.exports = {
    
        searchForStuff: function(req, res) {
            var params = req.allParams();
            // Replace the 'find' criteria with whatever suitable for your case
            var requests = [
                Person.find({name: params.searchString}),
                Server.find({name: params.searchString}),
                Application.find({name: params.searchString})
            ];
            Promise.all(requests)
            .spread(function(people, servers, applications) {
                return res.json({
                    people: people,
                    servers: servers,
                    applications: applications
                })
            })
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 2020-07-05
      • 2020-09-18
      • 1970-01-01
      • 2019-03-13
      • 2013-12-01
      相关资源
      最近更新 更多