Function: Basic N1QL SELECT Statment

    +

    Goal: Iterate through a basic N1QL SELECT where Eventing interacts with the Data service via an inline N1QL statement.

    • This function basicN1qlSelectStmt demonstrates how to use an inline N1QL SELECT statement with a passed parameter.

    • Requires the "travel-sample" sample dataset to be loaded.

    • Requires a metadata bucket, a source bucket of "travel-sample".

    • Deploy the Function with a Feed Boundary "From now" (Note you will log 187 lines if you use "Everything").

    • Assuming you deployed "From now" mutate any document in "travel-sample" to generate a log line.

    • For more detail refer to N1QL Statements.

    • basicN1qlSelectStmt

    • Input Data/Mutation (via the following N1QL statement)

    • Output Data/Logged

    // Deploy from now then mutate a document in `travel-sample`
    function OnUpdate(doc, meta) {
        // ignore information we don't care about
        if (doc.type !== 'airline') return;
    
        var route_cnt = 0;       // we want to get the total routes per iata
        var airline = doc.iata;  // need a true variable as a N1QL parameter
    
        var results =
            SELECT COUNT(*) AS cnt
            FROM `travel-sample`
            WHERE type = "route" AND airline = $airline;
    
        for (var item of results) {   // Stream results using 'for' iterator.
            route_cnt = item.cnt;
        }
        results.close();              // End the query and free resources held
    
        // Just log the KEY, AIRLINE and ROUTE_CNT it to the Application log
        log("key: " + meta.id + ", airline: "+doc.iata+", route_cnt: "+route_cnt);
    }
    UPDATE `travel-sample` USE KEYS "airline_24" SET id = 24;
    2020-08-25T14:19:13.589-07:00 [INFO] "key: airline_24, airline: AA, route_cnt: 2354"