Skip to content

Feature Request - Handle MongooseModel.method().cursor() streams #25

Description

@paynoattn

The .cursor() method on mongoose model methods returns a readable stream, not a promise. Given how stream-oriented node js, and how you can easilly .pipe your mongoose queries to your server response like so:

const Model = mongoose.model('model', mySchema);

// ...
Model.find().limit(10).cursor().pipe(res);

It would be nice to have a stub method that returned a readable stream like so

MyMock = sinon.stub(Model)
MyMock.expects('find')
      .chain('limit').withArgs(10)
      .chain('cursor')
      .streams([1,2,3])

For now, the only way I can do this is to create a custom readable stream that the mock returns like so.

const Readable = require('stream').readable,
      rs = new Readable({objectMode: true}),
      array = [1,2,3];
array.forEach(x => rs.push(x));
rs.push(null);

MyMock.expects('find')
      .chain('limit').withArgs(10)
      .chain('cursor')
      .returns(rs);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions