-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
change_streams.js
197 lines (162 loc) · 5.9 KB
/
change_streams.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* eslint no-unused-vars: 0 */
'use strict';
const setupDatabase = require('../functional/shared').setupDatabase;
const expect = require('chai').expect;
describe('examples(change-stream):', function() {
let client;
let db;
before(async function() {
await setupDatabase(this.configuration);
});
beforeEach(async function() {
client = await this.configuration.newClient().connect();
db = client.db(this.configuration.db);
await db.createCollection('inventory');
await db.collection('inventory').deleteMany({});
});
afterEach(async function() {
await client.close();
client = undefined;
db = undefined;
});
class Looper {
constructor(lambda, interval) {
this._run = false;
this._lambda = lambda;
this._interval = interval || 50;
}
async _go() {
this._run = true;
while (this._run) {
await new Promise(r => setTimeout(r, this._interval));
await this._lambda();
}
}
run() {
this._p = this._go().catch(() => {});
}
stop() {
this._run = false;
return this._p;
}
}
it('Open A Change Stream', {
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } },
test: async function() {
const looper = new Looper(() => db.collection('inventory').insertOne({ a: 1 }));
looper.run();
// Start Changestream Example 1
const collection = db.collection('inventory');
const changeStream = collection.watch();
changeStream.on('change', next => {
// process next document
});
// End Changestream Example 1
// Start Changestream Example 1 Alternative
const changeStreamIterator = collection.watch();
const next = await changeStreamIterator.next();
// End Changestream Example 1 Alternative
await changeStream.close();
await changeStreamIterator.close();
await looper.stop();
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 });
const looper = new Looper(() =>
db.collection('inventory').updateOne({ a: 1 }, { $set: { a: 2 } })
);
looper.run();
// Start Changestream Example 2
const collection = db.collection('inventory');
const changeStream = collection.watch({ fullDocument: 'updateLookup' });
changeStream.on('change', next => {
// process next document
});
// End Changestream Example 2
// Start Changestream Example 2 Alternative
const changeStreamIterator = collection.watch({ fullDocument: 'updateLookup' });
const next = await changeStreamIterator.next();
// End Changestream Example 2 Alternative
await changeStream.close();
await changeStreamIterator.close();
await looper.stop();
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() {
const looper = new Looper(async () => {
await db.collection('inventory').insertOne({ a: 1 });
await db.collection('inventory').insertOne({ b: 2 });
});
looper.run();
// Start Changestream Example 3
const collection = db.collection('inventory');
const changeStream = collection.watch();
let newChangeStream;
changeStream.on('change', next => {
const resumeToken = changeStream.resumeToken;
changeStream.close();
newChangeStream = collection.watch({ resumeAfter: resumeToken });
newChangeStream.on('change', next => {
// process next document
});
});
// End Changestream Example 3
// Start Changestream Example 3 Alternative
const changeStreamIterator = collection.watch();
const change1 = await changeStreamIterator.next();
const resumeToken = changeStreamIterator.resumeToken;
changeStreamIterator.close();
const newChangeStreamIterator = collection.watch({ resumeAfter: resumeToken });
const change2 = await newChangeStreamIterator.next();
// End Changestream Example 3 Alternative
await newChangeStreamIterator.close();
await newChangeStream.close();
await looper.stop();
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() {
const looper = new Looper(async () => {
await db.collection('inventory').insertOne({ username: 'alice' });
});
looper.run();
// 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);
changeStream.on('change', next => {
// process next document
});
// End Changestream Example 4
// Start Changestream Example 4 Alternative
const changeStreamIterator = collection.watch(pipeline);
const next = await changeStreamIterator.next();
// End Changestream Example 4 Alternative
await changeStream.close();
await changeStreamIterator.close();
await looper.stop();
expect(next).to.have.nested.property('fullDocument.username', 'alice');
expect(next).to.have.property('newField', 'this is an added field!');
}
});
});