Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

p-odm

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-odm - npm Package Compare versions

Comparing version 0.0.18 to 0.0.19

lib/document.js

4

lib/cache.js

@@ -5,4 +5,4 @@ 'use strict';

this.store = {};
options = options || {cacheSize: 262144, ttl: 30000};
this.size = options.cacheSize || 262144;
options = options || {cacheSize: 256, ttl: 30000};
this.size = options.cacheSize || 256;
this.ttl = options.ttl || 30000;

@@ -9,0 +9,0 @@ this.keys = 0;

@@ -7,4 +7,2 @@ 'use strict';

var Cache = require('./cache');
// lazy connector

@@ -34,6 +32,2 @@ var _collections = {};

db: null,
/**
* @memberOf ODM
*/
queryCache: null,

@@ -58,8 +52,2 @@ /**

if (this.queryCache === null) {
this.queryCache = new Cache();
} else {
this.queryCache.reset();
}
if (callback !== undefined) {

@@ -125,9 +113,17 @@ mongodb.connect(_url, _options, function (err, db) {

var self = this;
this.db.collectionNames(function(err, collectionNames) {
this.db.collectionNames(function (err, collectionNames) {
if (err) {
return callback(err);
}
var counter = collectionNames.length;
collectionNames.forEach(function(collectionName, i) {
self.db.dropCollection(
collectionName.name.substr(collectionName.name.lastIndexOf('.')+1), function(err, result) {
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();
callback(null);
}

@@ -203,2 +199,47 @@ });

findOne: function (collection_name, query, fields, options, callback) {
this.collection(collection_name, options, function (err, collection) {
if (err) {
return callback(err);
}
collection.findOne(query, fields, options, callback);
});
},
find: function (collection_name, query, fields, options, callback) {
this.collection(collection_name, options, function (err, collection) {
if (err) {
return callback(err);
}
collection.find(query, fields, options, function (err, cursor) {
if (err) {
return callback(err);
}
cursor.toArray(callback);
});
});
},
save: function (collection_name, document, options, callback) {
this.collection(collection_name, options, function (err, collection) {
if (err) {
return callback(err);
}
collection.save(document, options, callback);
});
},
remove: function (collection_name, query, options, callback) {
this.collection(collection_name, options, function (err, collection) {
if (err) {
return callback(err);
}
collection.remove(query, options, callback);
});
},
/**

@@ -223,116 +264,5 @@ * Creates a new Document Model class

*/
document: function (mongoCollection, l2cache) {
var odm = this;
var cache = l2cache ? new Cache() : undefined;
var Model = function () {
throw new Error('Documents are read only and cannot be instantiated');
};
Model.findOne = function (query, fields, options, callback) {
if (callback === undefined) {
callback = options;
options = {};
}
odm.collection(mongoCollection, options, function (err, collection) {
if (err) {
return callback(err);
}
collection.findOne(query, options, function (err, documentLoaded) {
if (err) {
return callback(err);
}
return callback(null, documentLoaded);
});
});
};
Model.find = function (query, fields, options, callback) {
if (callback === undefined) {
if (options === undefined) {
callback = fields;
options = {};
fields = {};
} else {
callback = options;
options = {};
}
}
odm.collection(mongoCollection, options, function (err, collection) {
if (err) {
return callback(err);
}
collection.find(query, fields, options, function (err, cursor) {
if (err) {
return callback(err);
}
cursor.toArray(function (err, documentsLoaded) {
if (err) {
return callback(err);
}
return callback(null, documentsLoaded);
});
});
});
};
Model.findAll = function (fields, options, callback) {
if (callback === undefined) {
if (options === undefined) {
callback = fields;
options = {};
fields = {};
} else {
callback = options;
options = {};
}
}
// verify if it is in cache
if (l2cache) {
var cachedDocuments = cache.get('all');
if (cachedDocuments !== undefined) {
return callback(null, cachedDocuments);
}
}
odm.collection(mongoCollection, options, function (err, collection) {
if (err) {
return callback(err);
}
collection.find({}, fields, options, function (err, cursor) {
if (err) {
return callback(err);
}
cursor.toArray(function (err, documentsLoaded) {
if (err) {
return callback(err);
}
if (l2cache) {
cache.set('all', documentsLoaded);
}
return callback(null, documentsLoaded);
});
});
});
};
return Model;
}
document: require('./document.js')
};
module.exports = ODM;

@@ -13,2 +13,4 @@ 'use strict';

var Cache = require('./cache');
var objectIdRegExp = new RegExp("^[0-9a-fA-F]{24}$");

@@ -19,2 +21,29 @@

*/
function clone(obj) {
var newObj = Array.isArray(obj) ? [] : {};
var i;
for (i in obj) {
if (obj.hasOwnProperty(i)) {
var v = obj[i];
if (v === undefined) {
continue;
}
if (v === null || v instanceof String || v instanceof Number || v instanceof Boolean || v instanceof Date || v instanceof ObjectID || v instanceof Binary) {
newObj[i] = obj[i];
} else if (typeof obj[i] === 'object') {
newObj[i] = clone(obj[i]);
} else {
newObj[i] = obj[i];
}
}
}
return newObj;
}
/**
* @private
*/
function isSpecial(obj) {

@@ -131,3 +160,3 @@ var key;

toJSON: function () {
return compactObject(this._internalDocument);
return clone(compactObject(this._internalDocument));
},

@@ -961,9 +990,6 @@ toString: function () {

function isCached(cache, mongoCol, field, value) {
if (cache !== undefined && cache !== null) {
if (value instanceof ObjectID) {
return cache.get(mongoCol + ':' + field + ':' + value.toHexString());
}
return cache.get(mongoCol + ':' + field + ':' + value);
if (value instanceof ObjectID) {
return cache.get(mongoCol + ':' + field + ':' + value.toHexString());
}
return undefined;
return cache.get(mongoCol + ':' + field + ':' + value);
}

@@ -981,10 +1007,8 @@

function putToCache(cache, mongoCol, field, value, doc) {
if (cache !== undefined && cache !== null) {
if (value !== undefined && value !== null) {
if (value instanceof ObjectID) {
cache.set(mongoCol + ':' + field + ':' + value.toHexString(), doc);
}
if (value !== undefined && value !== null) {
if (value instanceof ObjectID) {
cache.set(mongoCol + ':' + field + ':' + value.toHexString(), doc);
}
cache.set(mongoCol + ':' + field + ':' + value, doc);
}
cache.set(mongoCol + ':' + field + ':' + value, doc);
}

@@ -1000,17 +1024,15 @@

function purgeCache(cache, mongoCol, indexes, model) {
if (cache !== undefined && cache !== null) {
var _id = model._id;
if (_id !== undefined && _id !== null) {
if (_id instanceof ObjectID) {
cache.del(mongoCol + ':' + '_id:' + _id.toHexString());
}
var _id = model._id;
if (_id !== undefined && _id !== null) {
if (_id instanceof ObjectID) {
cache.del(mongoCol + ':' + '_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(mongoCol + ':' + indexes[i] + ':' + model[indexes[i]]);
}
cache.del(mongoCol + '::all');
}
cache.del(mongoCol + '::all');
}

@@ -1045,2 +1067,4 @@

var cache = l2cache ? new Cache({cacheSize: 256, ttl: 30000}) : undefined;
/**

@@ -1099,2 +1123,4 @@ * @name Model

Model.findOne = function (query, options, callback) {
// TODO: fix signature
var fields = {};
if (callback === undefined) {

@@ -1111,3 +1137,3 @@ callback = options;

odm.collection(mongoCollection, options, function (err, collection) {
odm.findOne(mongoCollection, query, fields, options, function (err, documentLoaded) {
if (err) {

@@ -1117,19 +1143,13 @@ return callback(err);

collection.findOne(query, options, function (err, documentLoaded) {
if (err) {
return callback(err);
}
if (documentLoaded === null) {
return callback(null, null);
}
if (documentLoaded === null) {
return callback(null, null);
}
// special case (return direct document from mongoDB)
if (directObject) {
return callback(null, documentLoaded);
}
// special case (return direct document from mongoDB)
if (directObject) {
return callback(null, documentLoaded);
}
var model = new Model(documentLoaded, { deserialize: true });
callback(null, model);
});
var model = new Model(documentLoaded, { deserialize: true });
callback(null, model);
});

@@ -1147,2 +1167,4 @@ };

Model.findById = function (id, options, callback) {
// TODO: add right signature
var fields = {};
if (callback === undefined) {

@@ -1177,3 +1199,3 @@ callback = options;

if (l2cache) {
var cachedDocument = isCached(odm.queryCache, mongoCollection, '_id', _id);
var cachedDocument = isCached(cache, mongoCollection, '_id', _id);

@@ -1201,3 +1223,3 @@ if (cachedDocument !== undefined) {

odm.collection(mongoCollection, options, function (err, collection) {
odm.findOne(mongoCollection, {_id: _id}, fields, options, function (err, documentLoaded) {
if (err) {

@@ -1207,28 +1229,22 @@ return callback(err);

collection.findOne({_id: _id}, options, function (err, documentLoaded) {
if (err) {
return callback(err);
}
if (l2cache) {
putToCache(cache, mongoCollection, '_id', _id, documentLoaded);
}
if (l2cache) {
putToCache(odm.queryCache, mongoCollection, '_id', _id, documentLoaded);
// if we search for an Id and get null it should return right away
if (documentLoaded === null) {
if (includeNotFound) {
return callback(null, null);
} else {
return callback(mongoCollection + ' ' + _id.toHexString() + ' not found');
}
}
// if we search for an Id and get null it should return right away
if (documentLoaded === 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, documentLoaded);
}
// special case (return direct document from mongoDB)
if (directObject) {
return callback(null, documentLoaded);
}
var model = new Model(documentLoaded, { deserialize: true });
callback(null, model);
});
var model = new Model(documentLoaded, { deserialize: true });
callback(null, model);
});

@@ -1266,43 +1282,31 @@ };

odm.collection(mongoCollection, options, function (err, collection) {
var pluck = extractOption('pluck', options);
if (pluck !== undefined) {
directObject = true;
// state that we only care about the plucked field
fields[pluck] = true;
}
odm.find(mongoCollection, query, fields, options, function (err, documentsLoaded) {
if (err) {
return callback(err);
}
var i;
// special case (return direct document from mongoDB)
if (directObject) {
if (pluck !== undefined) {
for (i = 0; i < documentsLoaded.length; i++) {
documentsLoaded[i] = documentsLoaded[i][pluck];
}
return callback(null, documentsLoaded);
} else {
return callback(null, documentsLoaded);
}
}
var pluck = extractOption('pluck', options);
if (pluck !== undefined) {
directObject = true;
// state that we only care about the plucked field
fields[pluck] = true;
for (i = 0; i < documentsLoaded.length; i++) {
documentsLoaded[i] = new Model(documentsLoaded[i], { deserialize: true });
}
collection.find(query, fields, options, function (err, cursor) {
if (err) {
return callback(err);
}
cursor.toArray(function (err, documentsLoaded) {
if (err) {
return callback(err);
}
var i;
// special case (return direct document from mongoDB)
if (directObject) {
if (pluck !== undefined) {
for (i = 0; i < documentsLoaded.length; i++) {
documentsLoaded[i] = documentsLoaded[i][pluck];
}
return callback(null, documentsLoaded);
} else {
return callback(null, documentsLoaded);
}
}
for (i = 0; i < documentsLoaded.length; i++) {
documentsLoaded[i] = new Model(documentsLoaded[i], { deserialize: true });
}
callback(null, documentsLoaded);
});
});
callback(null, documentsLoaded);
});

@@ -1348,3 +1352,3 @@ };

if (l2cache) {
var cachedDocuments = isCached(odm.queryCache, mongoCollection, '', 'all');
var cachedDocuments = isCached(cache, mongoCollection, '', 'all');

@@ -1375,41 +1379,29 @@ if (cachedDocuments !== undefined) {

odm.collection(mongoCollection, options, function (err, collection) {
odm.find(mongoCollection, {}, fields, options, function (err, documentsLoaded) {
if (err) {
return callback(err);
}
if (l2cache && pluck === undefined) {
putToCache(cache, mongoCollection, '', 'all', documentsLoaded);
}
collection.find({}, fields, options, function (err, cursor) {
if (err) {
return callback(err);
// special case (return direct document from mongoDB)
if (directObject) {
if (pluck !== undefined) {
for (i = 0; i < documentsLoaded.length; i++) {
documentsLoaded[i] = documentsLoaded[i][pluck];
}
return callback(null, documentsLoaded);
} else {
return callback(null, documentsLoaded);
}
cursor.toArray(function (err, documentsLoaded) {
if (err) {
return callback(err);
}
}
if (l2cache && pluck === undefined) {
putToCache(odm.queryCache, mongoCollection, '', 'all', documentsLoaded);
}
// special case (return direct document from mongoDB)
if (directObject) {
if (pluck !== undefined) {
for (i = 0; i < documentsLoaded.length; i++) {
documentsLoaded[i] = documentsLoaded[i][pluck];
}
return callback(null, documentsLoaded);
} else {
return callback(null, documentsLoaded);
}
}
// do not reuse the variable documentsLoaded since it will mess with the cache
var returnDocuments = [];
var i;
for (i = 0; i < documentsLoaded.length; i++) {
returnDocuments[i] = new Model(documentsLoaded[i], { deserialize: true });
}
callback(null, returnDocuments);
});
});
// do not reuse the variable documentsLoaded since it will mess with the cache
var returnDocuments = [];
var i;
for (i = 0; i < documentsLoaded.length; i++) {
returnDocuments[i] = new Model(documentsLoaded[i], { deserialize: true });
}
callback(null, returnDocuments);
});

@@ -1461,3 +1453,3 @@ };

if (l2cache) {
var cachedDoc = isCached(odm.queryCache, mongoCollection, '_id', ids[i]);
var cachedDoc = isCached(cache, mongoCollection, '_id', ids[i]);
if (cachedDoc !== undefined) {

@@ -1500,3 +1492,3 @@ result[i] = new Model(cachedDoc, { deserialize: true });

if (l2cache) {
putToCache(odm.queryCache, mongoCollection, '_id', models[i]._id, models[i]._internalDocument);
putToCache(cache, mongoCollection, '_id', models[i]._id, models[i]._internalDocument);
}

@@ -1546,3 +1538,4 @@

Model[methodName] = function (id, options, callback) {
// TODO: add right signature
var fields = {};
if (callback === undefined) {

@@ -1578,3 +1571,3 @@ callback = options;

if (l2cache) {
var cachedDocument = isCached(odm.queryCache, mongoCollection, field, _id);
var cachedDocument = isCached(cache, mongoCollection, field, _id);

@@ -1602,3 +1595,6 @@ if (cachedDocument !== undefined) {

odm.collection(mongoCollection, options, function (err, collection) {
var query = {};
query[field] = _id;
odm.findOne(mongoCollection, query, fields, options, function (err, documentLoaded) {
if (err) {

@@ -1608,31 +1604,22 @@ return callback(err);

var query = {};
query[field] = _id;
if (l2cache) {
putToCache(cache, mongoCollection, field, _id, documentLoaded);
}
collection.findOne(query, options, function (err, documentLoaded) {
if (err) {
return callback(err);
// if we search for an Id and get null it should return right away
if (documentLoaded === null) {
if (includeNotFound) {
return callback(null, null);
} else {
return callback(mongoCollection + ' ' + _id + ' not found');
}
}
if (l2cache) {
putToCache(odm.queryCache, mongoCollection, field, _id, documentLoaded);
}
// special case (return direct document from mongoDB)
if (directObject) {
return callback(null, documentLoaded);
}
// if we search for an Id and get null it should return right away
if (documentLoaded === 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, documentLoaded);
}
var model = new Model(documentLoaded, { deserialize: true });
callback(null, model);
});
var model = new Model(documentLoaded, { deserialize: true });
callback(null, model);
});

@@ -1661,3 +1648,3 @@ };

Model.prototype.toJSON = function () {
return compactObject(this._internalDocument);
return clone(compactObject(this._internalDocument));
};

@@ -1711,22 +1698,15 @@

odm.collection(mongoCollection, options, function (err, collection) {
odm.save(mongoCollection, compactObject(self._internalDocument), options, function (err, savedDocument) {
if (err) {
return callback(err);
}
// compact the internal document so we save on DB storage and network latency
collection.save(compactObject(self._internalDocument), options, function (err, savedDocument) {
if (err) {
return callback(err);
}
// only inserts have savedDocument
if (savedDocument) {
self._internalDocument._id = savedDocument._id;
}
// document updated delete from cache since it is not valid anymore
if (l2cache) {
purgeCache(odm.queryCache, mongoCollection, Model.IndexKeys, self);
}
callback(null, self._internalDocument._id);
});
// only inserts have savedDocument
if (savedDocument) {
self._internalDocument._id = savedDocument._id;
}
// document updated delete from cache since it is not valid anymore
if (l2cache) {
purgeCache(cache, mongoCollection, Model.IndexKeys, self);
}
callback(null, self._internalDocument._id);
});

@@ -1754,14 +1734,8 @@ };

odm.collection(mongoCollection, {}, function (err, collection) {
if (err) {
return callback(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);
}
collection.remove({ _id: self._id }, options, function (err) {
// document deleted, delete from cache since it is not valid anymore
if (l2cache) {
purgeCache(odm.queryCache, mongoCollection, Model.IndexKeys, self);
}
callback(err);
});
callback(err);
});

@@ -1788,3 +1762,3 @@ };

odm.collection(mongoCollection, {}, function (err, collection) {
odm.findOne(mongoCollection, {_id: _id}, {}, {}, function (err, documentLoaded) {
if (err) {

@@ -1794,23 +1768,17 @@ return callback(err);

collection.findOne({_id: _id}, {}, function (err, documentLoaded) {
if (err) {
return callback(err);
}
if (l2cache) {
putToCache(cache, mongoCollection, '_id', _id, documentLoaded);
}
if (l2cache) {
putToCache(odm.queryCache, mongoCollection, '_id', _id, documentLoaded);
}
// if we search for an Id and get null it should return right away with error
if (documentLoaded === null) {
return callback('id not found');
}
// if we search for an Id and get null it should return right away with error
if (documentLoaded === null) {
return callback('id not found');
}
Object.defineProperty(self, '_internalDocument', {
configurable: true,
value: documentLoaded
});
Object.defineProperty(self, '_internalDocument', {
configurable: true,
value: documentLoaded
});
callback(null);
});
callback(null);
});

@@ -1837,9 +1805,3 @@ };

odm.collection(mongoCollection, {}, function (err, collection) {
if (err) {
return callback(err);
}
collection.remove(query, options, callback);
});
odm.remove(mongoCollection, query, options, callback);
};

@@ -1846,0 +1808,0 @@

@@ -8,3 +8,3 @@ {

],
"version": "0.0.18",
"version": "0.0.19",
"engines": {

@@ -17,6 +17,2 @@ "node": ">=0.4.12"

"devDependencies": {
"nodelint": "0.6.1"
},
"main" : "./lib",

@@ -23,0 +19,0 @@ "repository": {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc