Skip to content

Commit

Permalink
docs(changeStream): update changestream examples
Browse files Browse the repository at this point in the history
Updates changeStream examples to use async/await

Fixes NODE-1533
  • Loading branch information
daprahamian committed Jul 6, 2018
1 parent 09c7d8e commit d080143
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 269 deletions.
124 changes: 124 additions & 0 deletions test/examples/change_streams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
'use strict';

const setupDatabase = require('../functional/shared').setupDatabase;
const expect = require('chai').expect;
const MongoClient = require('../../lib/mongo_client');

describe('examples(change-stream):', function() {
let client;
let db;

before(async function() {
await setupDatabase(this.configuration);
});

beforeEach(async function() {
client = await MongoClient.connect(this.configuration.url());
db = client.db(this.configuration.db);

await db.collection('inventory').deleteMany({});
});

afterEach(async function() {
await client.close();
client = undefined;
db = undefined;
});

it('Open A Change Stream', {
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } },
test: async function() {
setTimeout(async function() {
await db.collection('inventory').insertOne({ a: 1 });
});

// Start Changestream Example 1
const collection = db.collection('inventory');
const changeStream = collection.watch();
const next = await changeStream.next();
// End Changestream Example 1

await changeStream.close();

expect(next)
.to.have.property('operationType')
.that.equals('insert');
}
});

it('Lookup Full Document for Update Operations', {
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } },
test: async function() {
await db.collection('inventory').insertOne({ a: 1, b: 2 });
setTimeout(async function() {
await db.collection('inventory').updateOne({ a: 1 }, { $set: { a: 2 } });
});

// Start Changestream Example 2
const collection = db.collection('inventory');
const changeStream = collection.watch({ fullDocument: 'updateLookup' });
const next = await changeStream.next();
// End Changestream Example 2

await changeStream.close();

expect(next)
.to.have.property('operationType')
.that.equals('update');
expect(next)
.to.have.property('fullDocument')
.that.has.all.keys(['_id', 'a', 'b']);
}
});

it('Resume a Change Stream', {
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } },
test: async function() {
setTimeout(async function() {
await db.collection('inventory').insertOne({ a: 1 });
await db.collection('inventory').insertOne({ b: 2 });
});

// Start Changestream Example 3
const collection = db.collection('inventory');
const changeStream = collection.watch();
const change1 = await changeStream.next();

const resumeAfter = change1._id;
changeStream.close();

const newChangeStream = collection.watch({ resumeAfter });
const change2 = await newChangeStream.next();
// End Changestream Example 3

await newChangeStream.close();

expect(change1).to.have.nested.property('fullDocument.a', 1);
expect(change2).to.have.nested.property('fullDocument.b', 2);
}
});

it('Modify Change Stream Output', {
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } },
test: async function() {
setTimeout(async function() {
await db.collection('inventory').insertOne({ username: 'alice' });
});

// Start Changestream Example 4
const pipeline = [
{ $match: { 'fullDocument.username': 'alice' } },
{ $addFields: { newField: 'this is an added field!' } }
];
const collection = db.collection('inventory');
const changeStream = collection.watch(pipeline);
const next = await changeStream.next();
// End Changestream Example 4

await changeStream.close();

expect(next).to.have.nested.property('fullDocument.username', 'alice');
expect(next).to.have.property('newField', 'this is an added field!');
}
});
});
269 changes: 0 additions & 269 deletions test/functional/operation_changestream_example_tests.js

This file was deleted.

0 comments on commit d080143

Please sign in to comment.