Skip to content

Commit

Permalink
Add sample for bulk update with continuation token (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
southpolesteve committed Aug 20, 2019
1 parent f34c4cd commit b8f97a1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions azure-pipelines.yml
Expand Up @@ -82,6 +82,7 @@ jobs:
npm run ContainerManagement --prefix ./samples
npm run ServerSideScripts --prefix ./samples
npm run ChangeFeed --prefix ./samples
npm run BulkUpdateWithSproc --prefix ./samples
displayName: "Run Samples"
# - job: NightlyEmulator
# pool:
Expand Down
87 changes: 87 additions & 0 deletions samples/BulkUpdateWithSproc.ts
@@ -0,0 +1,87 @@
import { logSampleHeader, handleError, finish, logStep } from "./Shared/handleError";
import { CosmosClient } from "../dist";
import { endpoint, key, database as databaseId, container as containerId } from "./Shared/config";
import uuid from "uuid/v4";

logSampleHeader("Bulk Update Using Stored Procedures");
// Only to make TypeScript happy
var getContext: any;

function body(continuation: string) {
var collection = getContext().getCollection();
var response = getContext().getResponse();
var responseBody: any = { updatedDocumentIds: [] }; // Setup Initial Response

// Find all documents that need to be updated
collection.queryDocuments(
collection.getSelfLink(),
"SELECT * FROM root r",
{ pageSize: 2, continuation }, // Setting this low to show how continuation tokens work
function(err: any, feed: any, options: any) {
if (err) throw err;
// Set continuation token on response if we get one
responseBody.continuation = options.continuation;
// Update this batch of documents
updateDocs(feed, responseBody);
}
);

function updateDocs(documents: any, responseBody: any) {
if (documents.length === 0) {
// If no documents are left to update, we are done
response.setBody(responseBody);
} else {
// Grab the next document to update
var document = documents.pop();
document.state = "open";
collection.replaceDocument(document._self, document, {}, function(err: any) {
if (err) throw err;
// If we have successfully updated the document, include it in the returned document ids
responseBody.updatedDocumentIds.push(document.id);
// Call update with remaning documents
updateDocs(documents, responseBody);
});
}
}
}

// Establish a new instance of the CosmosClient to be used throughout this demo
const client = new CosmosClient({ endpoint, key });

async function run() {
//ensuring a database & container exists for us to work with
logStep("Create database '" + databaseId + "' and container '" + containerId + "'");
const { database } = await client.databases.createIfNotExists({ id: databaseId });
const { container } = await database.containers.createIfNotExists({ id: containerId });

logStep("Insert 20 items");

// Create 20 items with state set to "closed"
for (let index = 0; index < 20; index++) {
await container.items.create({ id: uuid(), state: "closed" });
}

logStep("Created stored procedure");
const { storedProcedure } = await container.scripts.storedProcedures.create({
id: "queryAndBulkUpdate",
body
});

logStep("Execute stored procedure and follow continuation tokens");
let continuation: string = undefined;
let totalUpdatedDocuments = 0;
while (true) {
const response = await storedProcedure.execute(undefined, [continuation]);
const result: any = response.resource;
totalUpdatedDocuments = totalUpdatedDocuments + result.updatedDocumentIds.length;
console.log(`Updated Documents: ${result.updatedDocumentIds}`);
continuation = result.continuation;
if (!result.continuation) {
console.log("No continuation token! Updates complete");
console.log(`Total Updated Document Count:`, totalUpdatedDocuments);
break;
}
}
await finish();
}
run().catch(handleError);
3 changes: 2 additions & 1 deletion samples/package.json
Expand Up @@ -12,6 +12,7 @@
"ItemManagement": "npx ts-node ./ItemManagement",
"DatabaseManagement": "npx ts-node ./DatabaseManagement",
"IndexeManagement": "npx ts-node ./IndexeManagement",
"ChangeFeed": "npx ts-node ./ChangeFeed"
"ChangeFeed": "npx ts-node ./ChangeFeed",
"BulkUpdateWithSproc": "npx ts-node ./BulkUpdateWithSproc"
}
}

0 comments on commit b8f97a1

Please sign in to comment.