Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/moeriki/mongo-iterable-cursor
Turn your mongodb cursor into an async iterable.
https://github.com/moeriki/mongo-iterable-cursor
async es2017 iterable mongodb
Last synced: 7 days ago
JSON representation
Turn your mongodb cursor into an async iterable.
- Host: GitHub
- URL: https://github.com/moeriki/mongo-iterable-cursor
- Owner: moeriki
- License: mit
- Created: 2017-02-26T15:36:09.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-07-17T07:11:06.000Z (over 5 years ago)
- Last Synced: 2024-12-21T23:36:10.992Z (18 days ago)
- Topics: async, es2017, iterable, mongodb
- Language: JavaScript
- Homepage:
- Size: 197 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mongo-iterable-cursor
Turn your mongodb cursor into an async iterable.
Async iteration is a stage-3 proposal. Use with caution!
## Without babel
You'll need Node.js 4 or above.
```js
const { MongoClient } = require('mongodb');
const iterable = require('mongo-iterable-cursor');MongoClient.connect('mongodb://localhost:27017/test')
.then((db) => {
const users = db.collection('users');for (const pendingUser of iterable(users.find())) {
pendingUser.then((user) => {
// handle user
});
}
})
;
```You can use [for-await](https://www.npmjs.com/package/for-await) to handle your items serially.
```js
const forAwait = require('for-await');
const { MongoClient } = require('mongodb');
const iterable = require('mongo-iterable-cursor');MongoClient.connect('mongodb://localhost:27017/test')
.then((db) => {
const users = db.collection('users');
return forAwait((user) => {
// handle user
}).of(iterable(users.find()));
})
;
```## With babel
Your setup needs to support async generator functions. If you are using Babel you'll need at least the following config.
```js
{
"presets": ["es2017"], // or use Node.js v8 which includes async/await
"plugins": [
"transform-async-generator-functions"
]
}
``````js
const iterable = require('mongo-iterable-cursor');
const { MongoClient } = require('mongodb');(async () => {
const db = await MongoClient.connect('mongodb://localhost:27017/test');
const users = db.collection('users');for await (const user of iterable(users.find())) {
//
}
})();
```## Register
Requiring `mongo-iterable-cursor/register` adds `[Symbol.asyncIterator]` to the [mongodb cursor](http://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html) `Cursor.prototype`.
```js
require('mongo-iterable-cursor/register');const { MongoClient } = require('mongodb');
(async () => {
const db = await MongoClient.connect('mongodb://localhost:27017/test');
const users = db.collection('users');for await (const user of users.find()) {
//
}
})();
```