Comparing version 0.0.23 to 0.0.24
101
lib/model.js
@@ -73,3 +73,17 @@ 'use strict'; | ||
/** | ||
* Extracts one option from the object and returns it. | ||
* @private | ||
*/ | ||
function hasOption(name, options) { | ||
var option; | ||
if (options) { | ||
if (options.hasOwnProperty(name)) { | ||
option = options[name]; | ||
} | ||
} | ||
return option; | ||
} | ||
/** | ||
* @private | ||
* Initialize the model object with the data either from db or from user | ||
@@ -792,2 +806,3 @@ */ | ||
// other cases are not allowed | ||
// TODO: this is wrong we should use the callbacks | ||
throw new Error(key + ' must be an Object'); | ||
@@ -1062,10 +1077,8 @@ } | ||
* @param {String} value the value for the index key | ||
* @param {Boolean} isModel is this a js object or model | ||
* @return {Object} | ||
*/ | ||
function isCached(cache, field, value) { | ||
function isCached(cache, field, value, isModel) { | ||
if (cache !== undefined) { | ||
if (value instanceof ObjectID) { | ||
return cache.get(field + ':' + value.toHexString()); | ||
} | ||
return cache.get(field + ':' + value); | ||
return cache.get(field + ':' + value + (isModel ? ':model' : '')); | ||
} | ||
@@ -1081,12 +1094,8 @@ return undefined; | ||
* @param {String} value the value for the index key | ||
* @param {Boolean} isModel is this a js object or model | ||
* @param {Object} doc Document to store | ||
*/ | ||
function putToCache(cache, field, value, doc) { | ||
function putToCache(cache, field, value, isModel, doc) { | ||
if (cache !== undefined) { | ||
if (value !== undefined && value !== null) { | ||
if (value instanceof ObjectID) { | ||
cache.set(field + ':' + value.toHexString(), doc); | ||
} | ||
} | ||
cache.set(field + ':' + value, doc); | ||
cache.set(field + ':' + value + (isModel ? ':model' : ''), doc); | ||
} | ||
@@ -1107,2 +1116,3 @@ } | ||
cache.del('_id:' + _id.toHexString()); | ||
cache.del('_id:' + _id.toHexString() + ':model'); | ||
} | ||
@@ -1114,5 +1124,7 @@ } | ||
cache.del(indexes[i] + ':' + model[indexes[i]]); | ||
cache.del(indexes[i] + ':' + model[indexes[i]] + ':model'); | ||
} | ||
} | ||
cache.del('::all'); | ||
cache.del('::all:model'); | ||
} | ||
@@ -1285,6 +1297,5 @@ } | ||
var cachedDocument = isCached(cache, '_id', _id); | ||
var cachedDocument = isCached(cache, '_id', _id.toHexString(), directObject); | ||
if (cachedDocument !== undefined) { | ||
// if we search for an Id and get null return right away | ||
@@ -1298,10 +1309,3 @@ if (cachedDocument === null) { | ||
} | ||
// special case (return direct document from mongoDB) | ||
if (directObject) { | ||
return callback(null, cachedDocument); | ||
} | ||
var model = new Model(cachedDocument, { deserialize: true }); | ||
return callback(null, model); | ||
return callback(null, cachedDocument); | ||
} | ||
@@ -1314,6 +1318,6 @@ | ||
putToCache(cache, '_id', _id, documentLoaded); | ||
// if we search for an Id and get null it should return right away | ||
if (documentLoaded === null) { | ||
putToCache(cache, '_id', _id.toHexString(), directObject, null); | ||
if (includeNotFound) { | ||
@@ -1328,2 +1332,3 @@ return callback(null, null); | ||
if (directObject) { | ||
putToCache(cache, '_id', _id.toHexString(), directObject, documentLoaded); | ||
return callback(null, documentLoaded); | ||
@@ -1333,2 +1338,3 @@ } | ||
var model = new Model(documentLoaded, { deserialize: true }); | ||
putToCache(cache, '_id', _id.toHexString(), directObject, model); | ||
callback(null, model); | ||
@@ -1434,3 +1440,3 @@ }); | ||
var cachedDocuments = isCached(cache, '', 'all'); | ||
var cachedDocuments = isCached(cache, '', 'all', directObject); | ||
@@ -1449,11 +1455,6 @@ if (cachedDocuments !== undefined) { | ||
return callback(null, documentsLoaded); | ||
} else { | ||
return callback(null, cachedDocuments); | ||
} | ||
} | ||
for (i = 0; i < cachedDocuments.length; i++) { | ||
documentsLoaded[i] = new Model(cachedDocuments[i], { deserialize: true }); | ||
} | ||
return callback(null, documentsLoaded); | ||
return callback(null, cachedDocuments); | ||
} | ||
@@ -1465,5 +1466,2 @@ | ||
} | ||
if (pluck === undefined) { | ||
putToCache(cache, '', 'all', documentsLoaded); | ||
} | ||
@@ -1478,2 +1476,3 @@ // special case (return direct document from mongoDB) | ||
} else { | ||
putToCache(cache, '', 'all', directObject, documentsLoaded); | ||
return callback(null, documentsLoaded); | ||
@@ -1489,2 +1488,3 @@ } | ||
} | ||
putToCache(cache, '', 'all', directObject, returnDocuments); | ||
callback(null, returnDocuments); | ||
@@ -1531,2 +1531,4 @@ }); | ||
var directObject = hasOption('directObject', options); | ||
for (i = 0; i < ids.length; i++) { | ||
@@ -1537,5 +1539,5 @@ if (!(ids[i] instanceof ObjectID)) { | ||
var cachedDoc = isCached(cache, '_id', ids[i]); | ||
var cachedDoc = isCached(cache, '_id', ids[i].toHexString(), directObject); | ||
if (cachedDoc !== undefined) { | ||
result[i] = new Model(cachedDoc, { deserialize: true }); | ||
result[i] = cachedDoc; | ||
} else { | ||
@@ -1566,3 +1568,3 @@ idsToFind.push(ids[i]); | ||
for (i = 0; i < models.length; i++) { | ||
putToCache(cache, '_id', models[i]._id, models[i]._internalDocument); | ||
putToCache(cache, '_id', models[i]._id.toHexString(), directObject, models[i]); | ||
@@ -1638,2 +1640,3 @@ var indexes = index[models[i]._id.toHexString()]; | ||
var _id; | ||
var _idString; | ||
var directObject = extractOption('directObject', options); | ||
@@ -1643,2 +1646,7 @@ | ||
_id = valid(id); | ||
if (_id instanceof ObjectID) { | ||
_idString = _id.toHexString(); | ||
} else { | ||
_idString = _id.toString(); | ||
} | ||
} catch (ex) { | ||
@@ -1648,3 +1656,3 @@ return callback(ex); | ||
var cachedDocument = isCached(cache, field, _id); | ||
var cachedDocument = isCached(cache, field, _idString, directObject); | ||
@@ -1661,10 +1669,3 @@ if (cachedDocument !== undefined) { | ||
} | ||
// special case (return direct document from mongoDB) | ||
if (directObject) { | ||
return callback(null, cachedDocument); | ||
} | ||
var model = new Model(cachedDocument, { deserialize: true }); | ||
return callback(null, model); | ||
return callback(null, cachedDocument); | ||
} | ||
@@ -1680,6 +1681,5 @@ | ||
putToCache(cache, field, _id, documentLoaded); | ||
// if we search for an Id and get null it should return right away | ||
if (documentLoaded === null) { | ||
putToCache(cache, field, _idString, directObject, null); | ||
if (includeNotFound) { | ||
@@ -1694,2 +1694,3 @@ return callback(null, null); | ||
if (directObject) { | ||
putToCache(cache, field, _idString, directObject, documentLoaded); | ||
return callback(null, documentLoaded); | ||
@@ -1699,2 +1700,3 @@ } | ||
var model = new Model(documentLoaded, { deserialize: true }); | ||
putToCache(cache, field, _idString, directObject, model); | ||
callback(null, model); | ||
@@ -1865,4 +1867,2 @@ }); | ||
putToCache(cache, '_id', _id, documentLoaded); | ||
// if we search for an Id and get null it should return right away with error | ||
@@ -1878,2 +1878,3 @@ if (documentLoaded === null) { | ||
putToCache(cache, '_id', _id.toHexString(), true, self); | ||
callback(null); | ||
@@ -1880,0 +1881,0 @@ }); |
@@ -8,3 +8,3 @@ { | ||
], | ||
"version": "0.0.23", | ||
"version": "0.0.24", | ||
"engines": { | ||
@@ -11,0 +11,0 @@ "node": ">=0.4.12" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2294
99916
17