Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@devjourney
Created September 2, 2018 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devjourney/35451c0eb50f48f58f2b10780bc06a53 to your computer and use it in GitHub Desktop.
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.
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