.stream( criteria )

Purpose

This method uses a node write stream to pipe model data as it is retrieved without first having to buffer all of the results to memory.

Overview

Parameters

Description Accepted Data Types Required ?
1 Find Criteria {},[{}], string, int Yes
2 Custom Write/End Methods {} No

Returned

Description Possible Data Types
1 Error Error
2 Stream of Records stream

Example Usage

UsersController.js

  1. module.exports = {
  2. testStream: function(req,res){
  3. if (req.param('startStream') && req.isSocket){
  4. var getSocket = req.socket;
  5. // Start the stream. Pipe it to sockets.
  6. User.stream({name:'Walter'}).pipe(getSocket.emit);
  7. } else {
  8. res.view();
  9. }
  10. }
  11. }
  12. `

views/users/testSocket.ejs

  1. <script type="text/javascript">
  2. window.onload = function startListening(){
  3. socket.on('gotUser',function(data){
  4. console.log(data.name+' number '+data.id+' has joined the party');
  5. });
  6. };
  7. </script>
  8. <div class="addButton" onClick="socket.get('/users/testStream/',{startStream:true})">Stream all the Users !</div>

Notes

This method is useful for piping data from VERY large models straight to res. You can also pipe it other places. See the node stream docs for more info. Only the mongo, mysql, and posgresql adapters support this method. This won’t work with the disk adapter. Any string arguments passed must be the ID of the record.