将 knex SELECT 查询结果用于另一个 SELECT 查询

Using knex SELECT query results for another SELECT query

我正在尝试 运行 使用 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 文件,因为我无法让 Promises 工作。 JSON 稍后用于为小型 OpenLayers 应用程序提供动力,因此在获得结果后进行 JSON 化。

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

你似乎缺少的只是一堆returns。

这里是两个函数的骨架版本,包括必要的 returns :

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 突出显示的其他问题。