diff --git a/lib/bag.js b/lib/bag.js index ad302a4..75cbe0d 100644 --- a/lib/bag.js +++ b/lib/bag.js @@ -25,6 +25,7 @@ function Bag (serialized) { this._offset = 0; this._current = -1; this._size = null; + this._prefetchedRecords = null; } Bag.BAG_EMBEDDED = 0; @@ -133,7 +134,12 @@ Bag.prototype.next = function () { } this._current++; rid = this._consume(); - this._content.push(rid); + if (this._prefetchedRecords && this._prefetchedRecords[rid]) { + this._content.push(this._prefetchedRecords[rid]); + } + else { + this._content.push(rid); + } return rid; } }; diff --git a/lib/db/record.js b/lib/db/record.js index 947e224..136b589 100644 --- a/lib/db/record.js +++ b/lib/db/record.js @@ -2,6 +2,7 @@ var Promise = require('bluebird'), RID = require('../recordid'), + RIDBag = require('../bag'), errors = require('../errors'); /** @@ -150,6 +151,11 @@ function recordIdResolver () { /*jshint validthis:true */ return obj.map(replaceRecordIds.bind(this, records)); } + else if (obj instanceof RIDBag) { + /*jshint validthis:true */ + obj._prefetchedRecords = records; + return obj; + } else if (obj instanceof RID && records[obj]) { return records[obj]; } diff --git a/test/core/bag-test.js b/test/core/bag-test.js index a90242a..d2e8297 100644 --- a/test/core/bag-test.js +++ b/test/core/bag-test.js @@ -74,7 +74,7 @@ describe("RID Bag", function () { var contents = this.bag.all(); contents.length.should.equal(10); contents.forEach(function (item) { - item.should.be.an.instanceOf(LIB.RID); + item.should.have.property('@rid'); }); }); @@ -83,7 +83,7 @@ describe("RID Bag", function () { decoded = JSON.parse(json); decoded.length.should.equal(10); decoded.forEach(function (item) { - (typeof item).should.equal('string'); + item.should.have.property('@rid'); }); }); });