Skip to content

Instantly share code, notes, and snippets.

@kanreisa
Last active May 18, 2016 08:11
Show Gist options
  • Save kanreisa/2ba3e9bf93b777380d46d7ece23b33cf to your computer and use it in GitHub Desktop.
Save kanreisa/2ba3e9bf93b777380d46d7ece23b33cf to your computer and use it in GitHub Desktop.
"use strict";
const http = require("http");
const DocumentClient = require('documentdb-q-promises').DocumentClientWrapper;
const host = "https://<name>.documents.azure.com:443/";// Add your endpoint
const masterKey = "...";// Add the masterkey of the endpoint
const client = new DocumentClient(host, { masterKey: masterKey });
// DB, Collection, Document 生成コードは面倒なので省きました。
// counter document's _SELF
const documentId = "dbs/.../colls/.../docs/.../";
const server = http.createServer((req, res) => {
res.setHeader("Content-Type", "text/plain");
res.writeHead(200);
let startAt = Date.now();
let readTime = -1;
client.readDocumentAsync(documentId)
.then(doc => {
readTime = Date.now() - startAt;
++doc.resource.count;
startAt = Date.now();
return client.replaceDocumentAsync(doc.resource._self, doc.resource);
})
.then(doc => {
res.end(`This is DocumentDB Read/Update Demo. -> count=${doc.resource.count} (r/w: ${readTime}/${Date.now() - startAt} ms)`);
})
.fail(err => {
console.error("error!", err);
res.end(JSON.stringify(err));
});
});
server.listen(process.env.PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment