Skip to content

Instantly share code, notes, and snippets.

@kanreisa kanreisa/documentdb.js
Last active May 18, 2016

Embed
What would you like to do?
"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
You can’t perform that action at this time.