Skip to content

Instantly share code, notes, and snippets.

@devjourney
Last active 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/3993db636d9670d6fb7fd9b6657a2924 to your computer and use it in GitHub Desktop.
Save devjourney/3993db636d9670d6fb7fd9b6657a2924 to your computer and use it in GitHub Desktop.
A Node.js function to fetch weather station data.
let documentClient = require('documentdb').DocumentClient;
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;
}
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