Skip to content

Instantly share code, notes, and snippets.

@christopheranderson
Created March 14, 2019 18:38
Show Gist options
  • Save christopheranderson/4b444b1090c1147dfe9ce676df4001b4 to your computer and use it in GitHub Desktop.
Save christopheranderson/4b444b1090c1147dfe9ce676df4001b4 to your computer and use it in GitHub Desktop.
ChangeFeed hasMoreResults issue
//@ts-check
const cosmos = require("@azure/cosmos");
const {items} = require("./sampleItems");
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
const {CosmosClient} = cosmos;
const endpoint = process.env.COSMOS_ENDPOINT;
const key = process.env.COSMOS_KEY;
const client = new CosmosClient({endpoint, key});
async function main() {
const container = await init();
const cfquery = container.items.readChangeFeed({ startFromBeginning: true});
const maxpages = 20;
let currentpage = 0;
do {
const page = await cfquery.executeNext();
console.log(`Read ${page.count} items`);
if(++currentpage >= maxpages) {
throw new Error("Infinite loop detected");
}
} while(cfquery.hasMoreResults)
return "done";
}
async function init() {
const {database: db} = await client.databases.createIfNotExists({
id: "change-feed-test"
});
const {container} = await db.containers.createIfNotExists({
id: "chrange-feed-test"
});
for(const item of items) {
await container.items.upsert(item);
}
return container;
}
main().then(console.log).catch(console.error);
{
"name": "sample-js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@azure/cosmos": "^2.1.3"
}
}
module.exports = {
items: [
{
id: "1",
pk: "bar"
},
{
id: "2",
pk: "bar"
},
{
id: "3",
pk: "bar"
},
{
id: "4",
pk: "bar"
},
{
id: "5",
pk: "bar"
},
{
id: "6",
pk: "bar"
},
{
id: "7",
pk: "bar"
},
{
id: "8",
pk: "bar"
},
{
id: "9",
pk: "bar"
},
{
id: "10",
pk: "bar"
},
{
id: "11",
pk: "bar"
},
{
id: "12",
pk: "bar"
},
{
id: "13",
pk: "bar"
},
{
id: "14",
pk: "bar"
},
{
id: "15",
pk: "bar"
},
{
id: "16",
pk: "bar"
},
{
id: "17",
pk: "bar"
},
{
id: "18",
pk: "bar"
},
{
id: "19",
pk: "bar"
},
{
id: "20",
pk: "bar"
},
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment