Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Return _id as ObjectId from cache #66

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/extend-aggregate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const generateKey = require('./generate-key');
const recoverObjectId = require('./recover-objectid');
let hasBeenExtended = false;

module.exports = function(mongoose, cache) {
Expand Down Expand Up @@ -28,7 +29,9 @@ module.exports = function(mongoose, cache) {

return new Promise((resolve, reject) => {
cache.get(key, (err, cachedResults) => { //eslint-disable-line handle-callback-err
if (cachedResults) {
if (cachedResults != null) {
cachedResults = recoverObjectId(mongoose, cachedResults);

callback(null, cachedResults);
return resolve(cachedResults);
}
Expand Down
3 changes: 3 additions & 0 deletions src/extend-query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const generateKey = require('./generate-key');
const recoverObjectId = require('./recover-objectid');

module.exports = function(mongoose, cache) {
const exec = mongoose.Query.prototype.exec;
Expand Down Expand Up @@ -34,6 +35,8 @@ module.exports = function(mongoose, cache) {
cachedResults = Array.isArray(cachedResults) ?
cachedResults.map(hydrateModel(constructor)) :
hydrateModel(constructor)(cachedResults);
} else {
cachedResults = recoverObjectId(mongoose, cachedResults);
}

callback(null, cachedResults);
Expand Down
25 changes: 25 additions & 0 deletions src/recover-objectid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

module.exports = function(mongoose, cachedResults) {
if (!Array.isArray(cachedResults)) {
return recoverObjectId(mongoose, cachedResults);
}

const recoveredResult = [];

for (const doc of cachedResults) {
recoveredResult.push(recoverObjectId(mongoose, doc));
}

return recoveredResult;
};

function recoverObjectId(mongoose, doc) {
if (!doc._id) {
return doc;
}

// eslint-disable-next-line new-cap
doc._id = mongoose.Types.ObjectId(doc._id);
return doc;
}
36 changes: 36 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,30 @@ describe('cachegoose', () => {
const diffSort = await getAllSorted({ num: -1 });
diffSort.length.should.equal(20);
});

it('should return similar _id in cached array result for lean', async () => {
const originalRes = await getAllLean(60);
const cachedRes = await getAllLean(60);
const originalConstructor = originalRes[0]._id.constructor.name.should;
const cachedConstructor = cachedRes[0]._id.constructor.name.should;
originalConstructor.should.deepEqual(cachedConstructor);
});

it('should return similar _id in one cached result for lean', async () => {
const originalRes = await getOneLean(60);
const cachedRes = await getOneLean(60);
const originalConstructor = originalRes._id.constructor.name.should;
const cachedConstructor = cachedRes._id.constructor.name.should;
originalConstructor.should.deepEqual(cachedConstructor);
});

it('should return similar _id in cached array result for aggregate', async () => {
const originalRes = await aggregateAll(60);
const cachedRes = await aggregateAll(60);
const originalConstructor = originalRes[0]._id.constructor.name.should;
const cachedConstructor = cachedRes[0]._id.constructor.name.should;
originalConstructor.should.deepEqual(cachedConstructor);
});
});

function getAll(ttl, cb) {
Expand All @@ -373,6 +397,10 @@ function getOne(ttl, cb) {
return Record.findOne({ num: { $gt: 2 } }).cache(ttl).exec(cb);
}

function getOneLean(ttl, cb) {
return Record.findOne({ num: { $gt: 2 } }).lean().cache(ttl).exec(cb);
}

function getWithSkip(skip, ttl, cb) {
return Record.find({}).skip(skip).cache(ttl).exec(cb);
}
Expand Down Expand Up @@ -432,6 +460,14 @@ function aggregate(ttl, cb) {
.exec(cb);
}

function aggregateAll(ttl, cb) {
return Record.aggregate([
{ $match: {} },
])
.cache(ttl)
.exec(cb);
}

function generate(amount) {
const records = [];
let count = 0;
Expand Down