Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Node.js function to fetch weather station data then filter and shape it with JMESPath.
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