Created
September 2, 2018 16:35
-
-
Save devjourney/35451c0eb50f48f58f2b10780bc06a53 to your computer and use it in GitHub Desktop.
A Node.js function to fetch weather station data then filter and shape it with JMESPath.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let documentClient = require('documentdb').DocumentClient; | |
let jmespath = require('jmespath'); | |
let cosmos_uri = process.env["STATION_COSMOS_URI"]; | |
let cosmos_key = process.env["STATION_COSMOS_READONLY_KEY"]; | |
let databaseId = process.env["STATION_COSMOS_DATABASE_NAME"]; | |
let collectionId = process.env["STATION_COSMOS_COLLECTION_NAME"]; | |
let client = new documentClient(cosmos_uri, { 'masterKey': cosmos_key }); | |
let collectionLink = "/dbs/" + databaseId + "/colls/" + collectionId + "/"; | |
module.exports = function (context, req) { | |
let filterQuery = `SELECT * FROM c WHERE c.State = "${req.params.state}"`; | |
try { | |
let queryIterator = client.queryDocuments(collectionLink, filterQuery); | |
queryIterator.toArray(function (err, matchingDocuments) { | |
if (err) { | |
context.done(err); | |
return; | |
} | |
if (req.query && req.query.query) { | |
try { | |
matchingDocuments = jmespath.search(matchingDocuments, req.query.query); | |
} | |
catch (ex) { | |
context.done(ex); | |
return; | |
} | |
} | |
context.done(null, { status: 200, body: matchingDocuments, headers: { 'Content-Type': 'application/json' }}); | |
}); | |
} catch (ex) { | |
context.done(ex); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment