Skip to content

Commit a8d62af

Browse files
committed
feat: transform option works differently in mongo v7:
mongodb/node-mongodb-native#4728
1 parent 09e39dc commit a8d62af

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

lib/Collection.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ class Collection {
173173
queryAsStream(query, options = {}, streamOptions = {}) {
174174
if (!$check.instanceStrict(query, MongoQuery)) throw new Error('Invalid query object');
175175
const cursor = this.queryAsCursor(query, options);
176+
const cursorStream = cursor.stream(streamOptions);
177+
178+
if(streamOptions.transform) {
179+
return cursorStream.map(streamOptions.transform);
180+
};
176181

177182
return cursor.stream(streamOptions);
178183
}

test/01. MonogQuery.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ describe('Mongo Query', function () {
2323
const collections = await _db.collections();
2424

2525
for (const collection of collections) {
26-
await collection.deleteMany({});
26+
if (!collection.collectionName.startsWith('system.')) {
27+
await collection.deleteMany({});
28+
}
2729
}
2830
});
2931

test/02. Collection.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const stream = require('stream');
55
const {MongoClient} = require('mongodb');
66
const Collection = require('../lib').Collection;
77
const MongoQuery = require('../lib').MongoQuery;
8-
const {EJSON} = require('bson');
8+
const {EJSON, ObjectId} = require('bson');
99

1010
const config = {
1111
databaseName: 'mongo-magic-tests',
@@ -28,7 +28,9 @@ describe('Collection', function () {
2828
const collections = await _db.collections();
2929

3030
for (const collection of collections) {
31-
await collection.deleteMany({});
31+
if (!collection.collectionName.startsWith('system.')) {
32+
await collection.deleteMany({});
33+
}
3234
}
3335
});
3436

@@ -143,6 +145,27 @@ describe('Collection', function () {
143145
const mQuery = new MongoQuery({limit: 1000});
144146
collection.queryAsStream(mQuery, {transform: (x) => EJSON.serialize(x, {})}).pipe(ws);
145147
});
148+
149+
it('should apply a transform on documents if one is provided', function (done) {
150+
const collection = new Collection(_db.collection('testquery'));
151+
const ws = new stream.Writable({objectMode: true});
152+
let writeCnt = 0 ;
153+
154+
ws._write = function (chunk, encoding, done) {
155+
writeCnt++;
156+
assert.strictEqual(ObjectId.isValid(chunk._id.$oid), true, 'Document was not transformed')
157+
return done();
158+
};
159+
160+
ws.on('finish', function () {
161+
assert.strictEqual(writeCnt, 2, 'Invalid writes');
162+
return done();
163+
});
164+
165+
const mQuery = new MongoQuery({limit: 1000});
166+
collection.queryAsStream(mQuery, {}, {transform: (x) => EJSON.serialize(x, {})}).pipe(ws);
167+
})
168+
146169
});
147170

148171
describe('Stats', function () {

0 commit comments

Comments
 (0)