Skip to content

Commit

Permalink
feat!: remove Cursor#transformStream
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Reggi authored Oct 16, 2020
1 parent ae1fc1a commit a54be7a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 28 deletions.
25 changes: 1 addition & 24 deletions src/cursor/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { emitDeprecatedOptionWarning } from '../utils';
import { ReadPreference, ReadPreferenceLike } from '../read_preference';
import { Transform, PassThrough, Readable } from 'stream';
import { Readable } from 'stream';
import { deprecate } from 'util';
import { MongoError, AnyError } from '../error';
import {
Expand Down Expand Up @@ -886,32 +886,9 @@ export class Cursor<

/** Return a modified Readable stream including a possible transform method. */
stream(options?: CursorStreamOptions): CursorStream {
// TODO: replace this method with transformStream in next major release
return new CursorStream(this, options);
}

/**
* Return a modified Readable stream that applies a given transform function, if supplied. If none supplied,
* returns a stream of unmodified docs.
*/
transformStream(options?: CursorStreamOptions): Transform {
const streamOptions: typeof options = options || {};
if (typeof streamOptions.transform === 'function') {
const stream = new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
if (streamOptions.transform) {
this.push(streamOptions.transform(chunk));
}
callback();
}
});
return this.stream().pipe(stream);
}

return this.stream(options).pipe(new PassThrough({ objectMode: true }));
}

/**
* Execute the explain for the cursor
*
Expand Down
8 changes: 4 additions & 4 deletions test/functional/cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4121,7 +4121,7 @@ describe('Cursor', function () {
.then(() => collection.insertMany(docs))
.then(() => {
cursor = collection.find();
return cursor.transformStream(transformParam);
return cursor.stream(transformParam);
})
.then(stream => {
stream.on('data', function (doc) {
Expand All @@ -4141,7 +4141,7 @@ describe('Cursor', function () {
});
};

it('transformStream should apply the supplied transformation function to each document in the stream', function (done) {
it('stream should apply the supplied transformation function to each document in the stream', function (done) {
const configuration = this.configuration;
const client = configuration.newClient({ w: 1 }, { poolSize: 1, auto_reconnect: false });
const expectedDocs = [
Expand All @@ -4152,15 +4152,15 @@ describe('Cursor', function () {
const config = {
client: client,
configuration: configuration,
collectionName: 'transformStream-test-transform',
collectionName: 'stream-test-transform',
transformFunc: doc => ({ _id: doc._id, b: doc.a.b, c: doc.a.c }),
expectedSet: new Set(expectedDocs)
};

testTransformStream(config, done);
});

it('transformStream should return a stream of unmodified docs if no transform function applied', function (done) {
it('stream should return a stream of unmodified docs if no transform function applied', function (done) {
const configuration = this.configuration;
const client = configuration.newClient({ w: 1 }, { poolSize: 1, auto_reconnect: false });
const expectedDocs = [
Expand Down

0 comments on commit a54be7a

Please sign in to comment.