Skip to content

Commit 4c66fcb

Browse files
authored
Merge pull request #14052 from Automattic/vkarpov15/gh-14049
fix(ChangeStream): correctly handle `hydrate` option when using change stream as stream instead of iterator
2 parents cc75c7b + fd781c1 commit 4c66fcb

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

lib/cursor/ChangeStream.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class ChangeStream extends EventEmitter {
8383
if (ev === 'error' && this.closed) {
8484
return;
8585
}
86+
if (data != null && data.fullDocument != null && this.options && this.options.hydrate) {
87+
data.fullDocument = this.options.model.hydrate(data.fullDocument);
88+
}
8689
this.emit(ev, data);
8790
});
8891
});

test/model.populate.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11017,9 +11017,9 @@ describe('model: populate:', function() {
1101711017
const peopleList = await Person.find().
1101811018
sort({ firstName: 1 }).
1101911019
populate({ path: 'nationality', match: { desc: 'Spain' } });
11020-
assert.deepStrictEqual(peopleList.map(
11021-
p => p.nationality ? p.nationality.key : undefined),
11022-
[undefined, 'ES', undefined]
11020+
assert.deepStrictEqual(
11021+
peopleList.map(p => p.nationality ? p.nationality.key : undefined),
11022+
[undefined, 'ES', undefined]
1102311023
);
1102411024
});
1102511025

test/model.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5364,6 +5364,30 @@ describe('Model', function() {
53645364
assert.equal(changeData.fullDocument.get('name'), 'Tony Stark');
53655365
});
53665366

5367+
it('fullDocument with immediate watcher and hydrate (gh-14049)', async function() {
5368+
const MyModel = db.model('Test', new Schema({ name: String }));
5369+
5370+
const doc = await MyModel.create({ name: 'Ned Stark' });
5371+
5372+
const p = new Promise((resolve) => {
5373+
MyModel.watch([], {
5374+
fullDocument: 'updateLookup',
5375+
hydrate: true
5376+
}).on('change', change => {
5377+
resolve(change);
5378+
});
5379+
});
5380+
5381+
await MyModel.updateOne({ _id: doc._id }, { name: 'Tony Stark' });
5382+
5383+
const changeData = await p;
5384+
assert.equal(changeData.operationType, 'update');
5385+
assert.equal(changeData.fullDocument._id.toHexString(),
5386+
doc._id.toHexString());
5387+
assert.ok(changeData.fullDocument.$__);
5388+
assert.equal(changeData.fullDocument.get('name'), 'Tony Stark');
5389+
});
5390+
53675391
it('respects discriminators (gh-11007)', async function() {
53685392
const BaseModel = db.model('Test', new Schema({ name: String }));
53695393
const ChildModel = BaseModel.discriminator('Test1', new Schema({ email: String }));

0 commit comments

Comments
 (0)