2017-04-22 16 views
0

KnexでPostgreSQLクエリを実行し、その結果を使用して別のクエリを実行しようとしています。knex SELECTクエリ結果を別のSELECTクエリに使用する

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); 
    }); 
}; 

後者の関数は、前の関数の結果の一部を使用します。 「私はcouldnので

は現在、私は、JSONファイルに最終結果を保存

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'); 
    }); 

}:Knexの非同期の性質のために、私は最初の関数の.then内側から2番目の関数を呼び出すために()ステートメントを必要とします仕事を約束する。 JSONは、後で小さなOpenLayersアプリケーションに電力を供給するために使用されます。

データベースからデータを取得してファイルに保存し、別のプロセスからアクセスしてOpenLayersに使用することは、非常に冗長な方法ですが、これまでのところ唯一の方法ですそれは動作します。 これらの機能をよりうまく機能させる方法はたくさんありますが、私は約束を新しくしており、最も基本的な機能の外でそれらを使用する方法を知らない。このコードをより良くするための提案は大歓迎です。

+1

'knex'の重要なポイントは、未処理のクエリの実行を避けることです。あなたはそれに対して厳密に従うので、' knex'を使うことのすべての利点を放棄します。生のクエリを実行するには、[pg-promise](https://github.com/vitaly-t/pg-promise)がより適しているでしょう;) –

+0

方法を学ぶときにクエリを少し生かそうとします適切に使用してください。 – jjustas

答えて

2

あなたが欠けているように見えるのは、リターンの束です。ここで

は、必要に応じてリターンを含む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; 
     ^^^^^^^^^^^^^^^^^^^^^ 
    }); 
} 

私はより論理的にそこに座っているようだ基づいてbuildBuoyFeaturesJSON()feature_collectionコレクションの一部を移動しました。そうでない場合は、getMeasurementsAndSensors()に戻すのは非常に簡単です。

@ vitaly-tで強調表示されている追加の問題を修正しようとしていません。

+1

これは素晴らしい仕事でした!私は何かが欠けていたが、それが何であるか把握できなかったことを知っていた。ありがとうございました! – jjustas

関連する問題