使用knex SELECT查询结果进行另一个SELECT查询

时间:2017-04-22 19:45:37

标签: node.js postgresql promise openlayers-3 knex.js

我正在尝试使用Knex运行PostgreSQL查询,然后使用结果运行另一个查询。

exports.buildBuoyFeaturesJSON = function (conditionA, conditionB) {
var query = null;

var selectedFields = knex.select
(
    knex.raw('t_record.id AS id'),
    ...
    knex.raw('t_record.latitude AS latitude'),
    knex.raw('t_record.longitude AS longitude')
)
    .from('t_record')
    .then(function (response) {
        var geometry_array = [];
        var rows = response.rows;
        var keys = [];

        for (var key = 0; key <= rows.length - 1; key++) {
            var geometry =
                {
                    "id" : rows[key].id,
                    "type" : "Feature",
                    "geometry" : rows[key].geometry,
                    "properties" : {
                        ...
                        "sensors" : []
                    }
                };
            keys.push(rows[key].id);
            geometry_array.push(geometry);
        }
        getMeasurementsAndSensors(keys, geometry_array);
    });
};

后一个函数使用前一个函数的一些结果。由于Knex的异步特性,我需要在第一个函数的.then()语句中调用第二个函数:

function getMeasurementsAndSensors (keys, geometry_array) {
        var query = knex
            .select
            (
                't_record_id',
                'i_sensor_id',
                'description',
                'i_measurement_id',
                't_sensor_name',
                't_measurement_name',
                'value',
                'units'
            )
            .from('i_record')
            ...
            .whereRaw('i_record.t_record_id IN (' + keys + ')')
            .orderByRaw('t_record_id, i_sensor_id ASC')
            .then(function (response) {

        var rows = response.rows;
        var t_record_id = 0;
        var i_sensor_id = 0;
        var record_counter = -1;
        var sensor_counter = -1;

        for (var records = 0; records <= rows.length -1; records++) {
            if (t_record_id !== rows[records].t_record_id) {
                t_record_id = rows[records].t_record_id;
                record_counter++;
                sensor_counter = -1;
            }

            if (i_sensor_id !== rows[records].i_sensor_id) {
                i_sensor_id = rows[records].i_sensor_id;

                geometry_array[record_counter].properties.sensors[++sensor_counter] =
                {
                    'i_sensor_id' : rows[records].i_sensor_id,
                    't_sensor_name' : rows[records].t_sensor_name,
                    'description' : rows[records].description,
                    'measurements' : []
                };
            }

            geometry_array[record_counter].properties.sensors[sensor_counter].measurements.push
            ({
                    'i_measurement_id': rows[records].i_measurement_id,
                    'measurement_name': rows[records].t_measurement_name,
                    'value': rows[records].value,
                    'units': rows[records].units
            });
        }
        //wrapping features with metadata.
        var feature_collection = GEOGRAPHY_METADATA;
        feature_collection.features = geometry_array;

        JSONToFile(feature_collection, 'buoy_features');
    });

}

目前我将最终结果保存到JSON文件中,因为我无法使Promise工作。 JSON后来用于为小型OpenLayers应用程序提供支持,因此在获得结果后会使用JSON-ification。

我很确定从数据库获取数据,将其保存到文件,然后从另一个进程访问它并将其用于OpenLayers是一种非常冗余的方法,但到目前为止,它是唯一一个作品。 我知道有很多方法可以让这些功能更好地工作,但我是承诺的新手,并且不知道如何在大多数基本功能之外使用它们。欢迎提出如何改进此代码的任何建议。

1 个答案:

答案 0 :(得分:2)

你似乎缺少的只是一堆回报。

这是两个函数的骨架化版本,包括必要的返回:

exports.buildBuoyFeaturesJSON = function(conditionA, conditionB) {
    return knex.select (...)
    ^^^^^^
    .from(...)
    .then(function(response) {
        // synchronous stuff
        // synchronous stuff
        return getMeasurementsAndSensors(keys, geometry_array);
        ^^^^^^
    }).then(function(geometry_array) {
        var feature_collection = GEOGRAPHY_METADATA;
        feature_collection.features = geometry_array;
        return feature_collection;
        ^^^^^^
    });
};

function getMeasurementsAndSensors(keys, geometry_array) {
    return knex.select(...)
    ^^^^^^
    .from(...)
    ...
    .whereRaw(...)
    .orderByRaw(...)
    .then(function(response) {
        // heaps of synchronous stuff
        // heaps of synchronous stuff
        // heaps of synchronous stuff
        return geometry_array;
        ^^^^^^^^^^^^^^^^^^^^^
    });
}

我将feature_collection集合部分移动到buildBuoyFeaturesJSON(),因为它似乎更符合逻辑。如果没有,那么将其移回getMeasurementsAndSensors()将非常简单。

我没有尝试修复@ vitaly-t强调的其他问题。

相关问题