title: Paging query with NodeJS and ObjectionJs
body: <p>Hi I am relatively new to NodeJS and am using <a href="http://vincit.github.io/objection.js" rel="nofollow noreferrer">ObjectionJS</a> as ORM.
  I want to do a migration script for my users table, in order to change some field of each row. </p>
  
  <p>I make something like this</p>
  
  <pre><code>export default class UserMigration {
  
    constructor(firstId, lastId = 9000000) {
      this.firstId = firstId;
      this.lastId  = lastId;
    }
  
    migrate() {
      let more = true;
      let page = 0;
      let batchSize = 100;
      while(more) {
        UserModel.query().where('id', '&gt;=', this.firstId)
          .where('id', '&lt;=', this.lastId)
          .page(page, batchSize)
          .then((result) =&gt; {
            let users = result.results;
            debug('Page: ', page);
            debug('PageSize: ', users.length)
            users.forEach((user) =&gt; {
              // Do something here
            });
            if(result.results.length &lt; batchSize) {
              more = false
            } else {
              page++;
            }
          })
      }
    }
  
  }
  </code></pre>
  
  <p>But then I realize that the query is being executed asynchronously while the <code>while</code> block is being executed synchronously, is that correct?</p>
  
  <p>How can I achieve the migration without making one big query that returns all the users at once?</p>
  
  <p>Thanks in advance!!</p>