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

mongoose-nedb driver compatibility #485

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
22 changes: 20 additions & 2 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ Cursor.prototype.project = function (candidates) {
, keepId, action, keys
;

// Mongoose stores specified projection in a 'fields' property '{ fields: {} }'.
var projectionKeys = Object.keys(this._projection);
if (projectionKeys.length === 1 &&
projectionKeys[0] === 'fields') {
this._projection = this._projection.fields;
}

if (this._projection === undefined || Object.keys(this._projection).length === 0) {
return candidates;
}
Expand Down Expand Up @@ -132,9 +139,15 @@ Cursor.prototype._exec = function(_callback) {
this.db.getCandidates(this.query, function (err, candidates) {
if (err) { return callback(err); }

var queryKeys = Object.keys(self.query);

// If query contains a single field which has an existing index,
// index results can be directly returned without further matching
var matchingNeeded = queryKeys.length > 1 || !self.db.indexes || !self.db.indexes[queryKeys[0]];

try {
for (i = 0; i < candidates.length; i += 1) {
if (model.match(candidates[i], self.query)) {
if (!matchingNeeded || model.match(candidates[i], self.query)) {
// If a sort is defined, wait for the results to be sorted before applying limit and skip
if (!self._sort) {
if (self._skip && self._skip > skipped) {
Expand Down Expand Up @@ -198,7 +211,12 @@ Cursor.prototype.exec = function () {
this.db.executor.push({ this: this, fn: this._exec, arguments: arguments });
};


Cursor.prototype.toArray = function(callback) {
var self = this;
process.nextTick(function() {
callback(null, self.res);
});
};

// Interface
module.exports = Cursor;
14 changes: 13 additions & 1 deletion lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ Datastore.prototype.resetIndexes = function (newData) {
* @param {Function} cb Optional callback, signature: err
*/
Datastore.prototype.ensureIndex = function (options, cb) {
// Add compatibility with Mongoose which calls ensureIndex with 3 arguments.
if (arguments.length == 3) {
cb = arguments[2];
options = arguments[1];
indexFields = arguments[0];
for (var i in indexFields)
options.fieldName = i;
}

var err
, callback = cb || function () {};

Expand Down Expand Up @@ -482,7 +491,8 @@ Datastore.prototype.find = function (query, projection, callback) {
for (i = 0; i < docs.length; i += 1) {
res.push(model.deepCopy(docs[i]));
}
return callback(null, res);
this.res = res;
return callback(null, this);
});

cursor.projection(projection);
Expand Down Expand Up @@ -565,6 +575,8 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
;

if (typeof options === 'function') { cb = options; options = {}; }
if (!options) options = {};

callback = cb || function () {};
multi = options.multi !== undefined ? options.multi : false;
upsert = options.upsert !== undefined ? options.upsert : false;
Expand Down
16 changes: 16 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ function compareThings (a, b, _compareStrings) {
var aKeys, bKeys, comp, i
, compareStrings = _compareStrings || compareNSB;

// Custom type (ex: bson.ObjectID)
if (!!a && !!a.equals && typeof a.equals == 'function') {
return a.equals(b) ? 0 : -1;
}
if (!!b && !!b.equals && typeof b.equals == 'function') {
return b.equals(a) ? 0 : -1;
}

// undefined
if (a === undefined) { return b === undefined ? 0 : -1; }
if (b === undefined) { return a === undefined ? 0 : 1; }
Expand Down Expand Up @@ -528,6 +536,14 @@ function getDotValue (obj, field) {
function areThingsEqual (a, b) {
var aKeys , bKeys , i;

// Custom type (ex: bson.ObjectID)
if (!!a && !!a.equals && typeof a.equals == 'function') {
return a.equals(b);
}
if (!!b && !!b.equals && typeof b.equals == 'function') {
return b.equals(a);
}

// Strings, booleans, numbers, null
if (a === null || typeof a === 'string' || typeof a === 'boolean' || typeof a === 'number' ||
b === null || typeof b === 'string' || typeof b === 'boolean' || typeof b === 'number') { return a === b; }
Expand Down