Comparing version 0.0.19 to 0.0.20
@@ -103,31 +103,2 @@ 'use strict'; | ||
/** | ||
* Removes all the collections from the database | ||
* | ||
* @memberOf ODM | ||
* @param {Function} [callback] Callback to call once done | ||
*/ | ||
removeAllCollections: function(callback) { | ||
var self = this; | ||
this.db.collectionNames(function (err, collectionNames) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var counter = collectionNames.length; | ||
collectionNames.forEach(function (collectionName) { | ||
self.db.dropCollection(collectionName.name.substr(collectionName.name.lastIndexOf('.') + 1), function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (--counter === 0) { | ||
callback(null); | ||
} | ||
}); | ||
}); | ||
}); | ||
}, | ||
/** | ||
* Gets a collection object from the native driver | ||
@@ -134,0 +105,0 @@ * |
336
lib/model.js
@@ -33,3 +33,3 @@ 'use strict'; | ||
newObj[i] = obj[i]; | ||
} else if (typeof obj[i] === 'object') { | ||
} else if (typeof obj[i] === 'object') { | ||
newObj[i] = clone(obj[i]); | ||
@@ -326,15 +326,20 @@ } else { | ||
if (item !== undefined && item !== null) { | ||
var embeddedModel; | ||
var embeddedModel = model['_' + key]; | ||
if (Type.prototype !== undefined && Type.prototype !== null) { | ||
embeddedModel = Object.create(Type.prototype); | ||
} else { | ||
embeddedModel = Object.create(submodelproto); | ||
if (embeddedModel === undefined) { | ||
if (Type.prototype !== undefined && Type.prototype !== null) { | ||
embeddedModel = Object.create(Type.prototype); | ||
} else { | ||
embeddedModel = Object.create(submodelproto); | ||
} | ||
// define a read only ref to the parent | ||
Object.defineProperty(embeddedModel, '$parent', {value: model}); | ||
// build the getter/setter | ||
buildValidator(Type.internalSchema, embeddedModel); | ||
// save the computed object | ||
Object.defineProperty(model, '_' + key, {value: embeddedModel}); | ||
} | ||
// define the internal document | ||
Object.defineProperty(embeddedModel, '_internalDocument', {value: model._internalDocument[key][index]}); | ||
// define a read only ref to the parent | ||
Object.defineProperty(embeddedModel, '$parent', {value: model}); | ||
buildValidator(Type.internalSchema, embeddedModel); | ||
Object.defineProperty(embeddedModel, '_internalDocument', {value: model._internalDocument[key][index], configurable: true}); | ||
return embeddedModel; | ||
@@ -634,5 +639,5 @@ } | ||
get: function () { | ||
if (model._internalDocument[key] === undefined) { | ||
if (this._internalDocument[key] === undefined) { | ||
if (schema[key].$push) { | ||
model._internalDocument[key] = []; | ||
this._internalDocument[key] = []; | ||
} | ||
@@ -642,11 +647,11 @@ } | ||
if (schema[key].$push) { | ||
if (model._internalDocument[key] !== null) { | ||
odmArray(model._internalDocument[key], schema, model, key); | ||
if (this._internalDocument[key] !== null) { | ||
odmArray(this._internalDocument[key], schema, this, key); | ||
} | ||
} | ||
return model._internalDocument[key]; | ||
return this._internalDocument[key]; | ||
}, | ||
set: function (value) { | ||
model._internalDocument[key] = schema[key].$set.call(model, value); | ||
this._internalDocument[key] = schema[key].$set.call(this, value); | ||
}, | ||
@@ -660,27 +665,30 @@ enumerable: true | ||
get: function () { | ||
if (model._internalDocument[key] === undefined) { | ||
model._internalDocument[key] = {}; | ||
} | ||
// null is a valid value | ||
if (model._internalDocument[key] === null) { | ||
if (this._internalDocument[key] === null) { | ||
return null; | ||
} | ||
var submodel; | ||
var Type = schema[key].$type; | ||
var submodel = model['_' + key]; | ||
if (submodel === undefined) { | ||
var Type = schema[key].$type; | ||
if (Type !== undefined && Type.prototype !== undefined && Type.prototype !== null) { | ||
submodel = Object.create(Type.prototype); | ||
} else { | ||
submodel = Object.create(submodelproto); | ||
if (Type !== undefined && Type.prototype !== undefined && Type.prototype !== null) { | ||
submodel = Object.create(Type.prototype); | ||
} else { | ||
submodel = Object.create(submodelproto); | ||
} | ||
// define a read only ref to the parent | ||
Object.defineProperty(submodel, '$parent', {value: this}); | ||
// build getter | ||
buildValidator(schema[key], submodel); | ||
// save the computed object | ||
Object.defineProperty(model, '_' + key, {value: submodel}); | ||
} | ||
if (this._internalDocument[key] === undefined) { | ||
this._internalDocument[key] = {}; | ||
} | ||
// define the internal document | ||
Object.defineProperty(submodel, '_internalDocument', {value: model._internalDocument[key]}); | ||
// define a read only ref to the parent | ||
Object.defineProperty(submodel, '$parent', {value: model}); | ||
// build getter | ||
buildValidator(schema[key], submodel); | ||
Object.defineProperty(submodel, '_internalDocument', {value: this._internalDocument[key], configurable: true}); | ||
return submodel; | ||
@@ -691,11 +699,11 @@ }, | ||
if (value === undefined) { | ||
delete model._internalDocument[key]; | ||
delete this._internalDocument[key]; | ||
} else if (value === null) { | ||
// null (mongodb and json understand null so allow it) | ||
model._internalDocument[key] = null; | ||
this._internalDocument[key] = null; | ||
} else if (typeof value === 'object') { | ||
// object (if we pass a object the whole previous object will be replaced) | ||
model._internalDocument[key] = {}; | ||
this._internalDocument[key] = {}; | ||
// get a reference to the getter | ||
var submodel = model[key]; | ||
var submodel = this[key]; | ||
// var sub schema | ||
@@ -938,5 +946,3 @@ var subschema = schema[key]; | ||
function compactObject(obj) { | ||
if (obj === undefined || obj === null || | ||
obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj instanceof Date || | ||
obj instanceof ObjectID || obj instanceof Binary) { | ||
if (obj === undefined || obj === null || obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj instanceof Date || obj instanceof ObjectID || obj instanceof Binary) { | ||
return obj; | ||
@@ -958,5 +964,3 @@ } | ||
// special cases (skip) | ||
if (v === null || | ||
v instanceof String || v instanceof Number || v instanceof Boolean || v instanceof Date || | ||
v instanceof ObjectID || v instanceof Binary) { | ||
if (v === null || v instanceof String || v instanceof Number || v instanceof Boolean || v instanceof Date || v instanceof ObjectID || v instanceof Binary) { | ||
continue; | ||
@@ -989,3 +993,2 @@ } | ||
* @param {Object} cache The Cache Manager Objects | ||
* @param {String} mongoCol Mongo Collection name | ||
* @param {String} field field used to index the cache | ||
@@ -995,7 +998,10 @@ * @param {String} value the value for the index key | ||
*/ | ||
function isCached(cache, mongoCol, field, value) { | ||
if (value instanceof ObjectID) { | ||
return cache.get(mongoCol + ':' + field + ':' + value.toHexString()); | ||
function isCached(cache, field, value) { | ||
if (cache !== undefined) { | ||
if (value instanceof ObjectID) { | ||
return cache.get(field + ':' + value.toHexString()); | ||
} | ||
return cache.get(field + ':' + value); | ||
} | ||
return cache.get(mongoCol + ':' + field + ':' + value); | ||
return undefined; | ||
} | ||
@@ -1007,3 +1013,2 @@ | ||
* @param {Object} cache The Cache Manager Objects | ||
* @param {String} mongoCol Mongo Collection name | ||
* @param {String} field field used to index the cache | ||
@@ -1013,9 +1018,11 @@ * @param {String} value the value for the index key | ||
*/ | ||
function putToCache(cache, mongoCol, field, value, doc) { | ||
if (value !== undefined && value !== null) { | ||
if (value instanceof ObjectID) { | ||
cache.set(mongoCol + ':' + field + ':' + value.toHexString(), doc); | ||
function putToCache(cache, field, value, 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(mongoCol + ':' + field + ':' + value, doc); | ||
} | ||
@@ -1026,20 +1033,21 @@ | ||
* @param {Object} cache The Cache Manager Objects | ||
* @param {String} mongoCol Mongo Collection name | ||
* @param {String[]} indexes for the model | ||
* @param {Model} model Model that triggered the cleanup | ||
*/ | ||
function purgeCache(cache, mongoCol, indexes, model) { | ||
var _id = model._id; | ||
if (_id !== undefined && _id !== null) { | ||
if (_id instanceof ObjectID) { | ||
cache.del(mongoCol + ':' + '_id:' + _id.toHexString()); | ||
function purgeCache(cache, indexes, model) { | ||
if (cache !== undefined) { | ||
var _id = model._id; | ||
if (_id !== undefined && _id !== null) { | ||
if (_id instanceof ObjectID) { | ||
cache.del('_id:' + _id.toHexString()); | ||
} | ||
} | ||
} | ||
if (indexes !== undefined && indexes !== null) { | ||
var i; | ||
for (i = 0; i < indexes.length; i++) { | ||
cache.del(mongoCol + ':' + indexes[i] + ':' + model[indexes[i]]); | ||
if (indexes !== undefined && indexes !== null) { | ||
var i; | ||
for (i = 0; i < indexes.length; i++) { | ||
cache.del(indexes[i] + ':' + model[indexes[i]]); | ||
} | ||
} | ||
cache.del('::all'); | ||
} | ||
cache.del(mongoCol + '::all'); | ||
} | ||
@@ -1074,3 +1082,3 @@ | ||
var cache = l2cache ? new Cache({cacheSize: 256, ttl: 30000}) : undefined; | ||
var cache = l2cache ? new Cache({cacheSize: 1024, ttl: 300000}) : new Cache({cacheSize: 256, ttl: 30000}); | ||
@@ -1126,11 +1134,17 @@ /** | ||
* @param {Object} query Query object as in mongodb documentation | ||
* @param {Object|Function} [fields] filter fields | ||
* @param {Object|Function} [options] Query options, such as skip, limit, etc | ||
* @param {Function} callback Callback function (error, model) with the result of the operation | ||
*/ | ||
Model.findOne = function (query, options, callback) { | ||
// TODO: fix signature | ||
var fields = {}; | ||
Model.findOne = function (query, fields, options, callback) { | ||
if (callback === undefined) { | ||
callback = options; | ||
options = {}; | ||
if (options === undefined) { | ||
callback = fields; | ||
options = {}; | ||
fields = {}; | ||
} else { | ||
callback = options; | ||
options = fields; | ||
fields = {}; | ||
} | ||
} | ||
@@ -1168,11 +1182,17 @@ | ||
* @param {ObjectID|String} id Either a ObjectId instance or, the function will try to cast it to ObjectId. | ||
* @param {Object|Function} [fields] filter fields | ||
* @param {Object|Function} [options] Query options, such as skip, limit, etc | ||
* @param {Function} callback Callback function (error, model) with the result of the operation | ||
*/ | ||
Model.findById = function (id, options, callback) { | ||
// TODO: add right signature | ||
var fields = {}; | ||
Model.findById = function (id, fields, options, callback) { | ||
if (callback === undefined) { | ||
callback = options; | ||
options = {}; | ||
if (options === undefined) { | ||
callback = fields; | ||
options = {}; | ||
fields = {}; | ||
} else { | ||
callback = options; | ||
options = fields; | ||
fields = {}; | ||
} | ||
} | ||
@@ -1202,25 +1222,22 @@ | ||
// verify if it is in cache | ||
if (l2cache) { | ||
var cachedDocument = isCached(cache, mongoCollection, '_id', _id); | ||
var cachedDocument = isCached(cache, '_id', _id); | ||
if (cachedDocument !== undefined) { | ||
if (cachedDocument !== undefined) { | ||
// if we search for an Id and get null return right away | ||
if (cachedDocument === null) { | ||
if (includeNotFound) { | ||
return callback(null, null); | ||
} else { | ||
return callback(mongoCollection + ' ' + _id.toHexString() + ' not found'); | ||
} | ||
// if we search for an Id and get null return right away | ||
if (cachedDocument === null) { | ||
if (includeNotFound) { | ||
return callback(null, null); | ||
} else { | ||
return callback(mongoCollection + ' ' + _id.toHexString() + ' not found'); | ||
} | ||
} | ||
// special case (return direct document from mongoDB) | ||
if (directObject) { | ||
return callback(null, cachedDocument); | ||
} | ||
// special case (return direct document from mongoDB) | ||
if (directObject) { | ||
return callback(null, cachedDocument); | ||
} | ||
var model = new Model(cachedDocument, { deserialize: true }); | ||
return callback(null, model); | ||
} | ||
var model = new Model(cachedDocument, { deserialize: true }); | ||
return callback(null, model); | ||
} | ||
@@ -1233,5 +1250,3 @@ | ||
if (l2cache) { | ||
putToCache(cache, mongoCollection, '_id', _id, documentLoaded); | ||
} | ||
putToCache(cache, '_id', _id, documentLoaded); | ||
@@ -1353,28 +1368,25 @@ // if we search for an Id and get null it should return right away | ||
// verify if it is in cache | ||
if (l2cache) { | ||
var cachedDocuments = isCached(cache, mongoCollection, '', 'all'); | ||
var cachedDocuments = isCached(cache, '', 'all'); | ||
if (cachedDocuments !== undefined) { | ||
if (cachedDocuments !== undefined) { | ||
var documentsLoaded = []; | ||
var i; | ||
var documentsLoaded = []; | ||
var i; | ||
// special case (return direct document from mongoDB) | ||
if (directObject) { | ||
if (pluck !== undefined) { | ||
for (i = 0; i < cachedDocuments.length; i++) { | ||
documentsLoaded[i] = cachedDocuments[i][pluck]; | ||
} | ||
return callback(null, documentsLoaded); | ||
} else { | ||
return callback(null, cachedDocuments); | ||
// special case (return direct document from mongoDB) | ||
if (directObject) { | ||
if (pluck !== undefined) { | ||
for (i = 0; i < cachedDocuments.length; i++) { | ||
documentsLoaded[i] = cachedDocuments[i][pluck]; | ||
} | ||
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); | ||
for (i = 0; i < cachedDocuments.length; i++) { | ||
documentsLoaded[i] = new Model(cachedDocuments[i], { deserialize: true }); | ||
} | ||
return callback(null, documentsLoaded); | ||
} | ||
@@ -1386,4 +1398,4 @@ | ||
} | ||
if (l2cache && pluck === undefined) { | ||
putToCache(cache, mongoCollection, '', 'all', documentsLoaded); | ||
if (pluck === undefined) { | ||
putToCache(cache, '', 'all', documentsLoaded); | ||
} | ||
@@ -1455,15 +1467,5 @@ | ||
if (l2cache) { | ||
var cachedDoc = isCached(cache, mongoCollection, '_id', ids[i]); | ||
if (cachedDoc !== undefined) { | ||
result[i] = new Model(cachedDoc, { deserialize: true }); | ||
} else { | ||
idsToFind.push(ids[i]); | ||
// build index for the missing data | ||
if (index[ids[i].toHexString()] === undefined) { | ||
index[ids[i].toHexString()] = [i]; | ||
} else { | ||
index[ids[i].toHexString()].push(i); | ||
} | ||
} | ||
var cachedDoc = isCached(cache, '_id', ids[i]); | ||
if (cachedDoc !== undefined) { | ||
result[i] = new Model(cachedDoc, { deserialize: true }); | ||
} else { | ||
@@ -1494,5 +1496,3 @@ idsToFind.push(ids[i]); | ||
for (i = 0; i < models.length; i++) { | ||
if (l2cache) { | ||
putToCache(cache, mongoCollection, '_id', models[i]._id, models[i]._internalDocument); | ||
} | ||
putToCache(cache, '_id', models[i]._id, models[i]._internalDocument); | ||
@@ -1540,8 +1540,13 @@ var indexes = index[models[i]._id.toHexString()]; | ||
Model[methodName] = function (id, options, callback) { | ||
// TODO: add right signature | ||
var fields = {}; | ||
Model[methodName] = function (id, fields, options, callback) { | ||
if (callback === undefined) { | ||
callback = options; | ||
options = {}; | ||
if (options === undefined) { | ||
callback = fields; | ||
options = {}; | ||
fields = {}; | ||
} else { | ||
callback = options; | ||
options = fields; | ||
fields = {}; | ||
} | ||
} | ||
@@ -1572,25 +1577,22 @@ | ||
// verify if it is in cache | ||
if (l2cache) { | ||
var cachedDocument = isCached(cache, mongoCollection, field, _id); | ||
var cachedDocument = isCached(cache, field, _id); | ||
if (cachedDocument !== undefined) { | ||
if (cachedDocument !== undefined) { | ||
// if we search for an Id and get null return right away | ||
if (cachedDocument === null) { | ||
if (includeNotFound) { | ||
return callback(null, null); | ||
} else { | ||
return callback(mongoCollection + ' ' + _id + ' not found'); | ||
} | ||
// if we search for an Id and get null return right away | ||
if (cachedDocument === null) { | ||
if (includeNotFound) { | ||
return callback(null, null); | ||
} else { | ||
return callback(mongoCollection + ' ' + _id + ' not found'); | ||
} | ||
} | ||
// special case (return direct document from mongoDB) | ||
if (directObject) { | ||
return callback(null, cachedDocument); | ||
} | ||
// special case (return direct document from mongoDB) | ||
if (directObject) { | ||
return callback(null, cachedDocument); | ||
} | ||
var model = new Model(cachedDocument, { deserialize: true }); | ||
return callback(null, model); | ||
} | ||
var model = new Model(cachedDocument, { deserialize: true }); | ||
return callback(null, model); | ||
} | ||
@@ -1606,5 +1608,3 @@ | ||
if (l2cache) { | ||
putToCache(cache, mongoCollection, field, _id, documentLoaded); | ||
} | ||
putToCache(cache, field, _id, documentLoaded); | ||
@@ -1708,5 +1708,3 @@ // if we search for an Id and get null it should return right away | ||
// document updated delete from cache since it is not valid anymore | ||
if (l2cache) { | ||
purgeCache(cache, mongoCollection, Model.IndexKeys, self); | ||
} | ||
purgeCache(cache, Model.IndexKeys, self); | ||
callback(null, self._internalDocument._id); | ||
@@ -1735,7 +1733,5 @@ }); | ||
odm.remove(mongoCollection, { _id: self._id }, options, function (err) { | ||
odm.remove(mongoCollection, {_id: self._id}, options, function (err) { | ||
// document deleted, delete from cache since it is not valid anymore | ||
if (l2cache) { | ||
purgeCache(cache, mongoCollection, Model.IndexKeys, self); | ||
} | ||
purgeCache(cache, Model.IndexKeys, self); | ||
callback(err); | ||
@@ -1768,5 +1764,3 @@ }); | ||
if (l2cache) { | ||
putToCache(cache, mongoCollection, '_id', _id, documentLoaded); | ||
} | ||
putToCache(cache, '_id', _id, documentLoaded); | ||
@@ -1773,0 +1767,0 @@ // if we search for an Id and get null it should return right away with error |
@@ -8,3 +8,3 @@ { | ||
], | ||
"version": "0.0.19", | ||
"version": "0.0.20", | ||
"engines": { | ||
@@ -11,0 +11,0 @@ "node": ">=0.4.12" |
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
98086
2020