Skip to content

Commit

Permalink
Merge pull request #5 from synatic/develop
Browse files Browse the repository at this point in the history
0.3.1
  • Loading branch information
thiren authored Jun 18, 2021
2 parents 16a3b6a + 6b0c355 commit 152102c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ class Collection {
}
}

queryAsStream(query) {
queryAsStream(query, options = {}) {
if (!$check.instanceStrict(query, MongoQuery)) throw new Error('Invalid query object');
const cur = this.queryAsCursor(query);
return cur.stream();

return cur.stream(options);
}

updateStats(options, callback) {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@synatic/mongo-magic",
"version": "0.3.0",
"version": "0.3.1",
"description": "mongo magic utils",
"main": "index.js",
"files": [
Expand Down Expand Up @@ -44,6 +44,7 @@
"node": ">=12"
},
"dependencies": {
"bson": "^4.4.0",
"check-types": "11.1.2",
"deepmerge": "4.2.2",
"moment": "2.29.1",
Expand All @@ -56,8 +57,8 @@
"clone-deep": "^4.0.1",
"eslint": "^7.16.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^7.1.0",
"mocha": "^8.2.1",
"eslint-config-prettier": "^8.1.0",
"mocha": "^9.0.0",
"nyc": "^15.1.0",
"prettier": "^2.2.1"
}
Expand Down
19 changes: 19 additions & 0 deletions test/02. Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const _s = require('underscore.string');
const {MongoClient} = require('mongodb');
const Collection = require('../lib').Collection;
const MongoQuery = require('../lib').MongoQuery;
const {EJSON} = require('bson');

const config = {
databaseName: 'mongo-magic-tests',
Expand Down Expand Up @@ -128,6 +129,24 @@ describe('Collection', function () {
const mQuery = new MongoQuery({limit: 1000});
collection.queryAsStream(mQuery).pipe(ws);
});

it('should stream and transform results', function (done) {
const collection = new Collection(_db.collection('testquery'));
const ws = new stream.Writable({objectMode: true});
let writeCnt = 0;
ws._write = function (chunk, encoding, done) {
writeCnt++;
return done();
};

ws.on('finish', function () {
assert.strictEqual(writeCnt, 2, 'Invalid writes');
return done();
});

const mQuery = new MongoQuery({limit: 1000});
collection.queryAsStream(mQuery, {transform: x => EJSON.serialize(x, {})}).pipe(ws);
});
});

describe('Stats', function () {
Expand Down

0 comments on commit 152102c

Please sign in to comment.