Created
September 10, 2018 09:55
-
-
Save dimitardanailov/96a36b073b7a4e385f82b206d6862726 to your computer and use it in GitHub Desktop.
Example: Working with MongoDB Streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Get('/hello-world-streams') | |
async streams() { | |
// Get Mongodb cursor | |
const cursor = this.repository.getCursorToAllRecords(); | |
const promise = new Promise((resolve, reject) => { | |
cursor.on('data', doc => { | |
console.log(doc); | |
}); | |
cursor.on('close', () => { | |
resolve(); | |
}); | |
cursor.on('error', error => { | |
reject(error); | |
}); | |
}); | |
return await promise.then(() => { | |
return 'Stream was closed'; | |
}).catch(_error => { | |
return 'Ops, something is wrong'; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment