Permalink
Browse files

Get all tests passing in Mongo

  • Loading branch information...
1 parent be9bb11 commit 237b3fe711f798835a99e46231f5765d1dddbe22 mde committed Sep 3, 2012
View
@@ -3,10 +3,12 @@ var file = require('utilities').file
, utils = require('utilities')
, operation = require('../../query/operation')
, comparison = require('../../query/comparison')
+ , Query = require('../../query/query').Query
, datatypes = require('../../datatypes')
, request = utils.request
, BaseAdapter = require('../base_adapter').BaseAdapter
, _baseConfig
+ , _comparisonTypeMap
, _collectionizeModelName;
_baseConfig = {
@@ -18,6 +20,15 @@ _baseConfig = {
, port: 27017
};
+_comparisonTypeMap = {
+ 'NotEqualTo': '$ne'
+, 'In': '$in'
+, 'GreaterThan': '$gt'
+, 'LessThan': '$lt'
+, 'GreaterThanOrEqual': '$gte'
+, 'LessThanOrEqual': '$lte'
+};
+
_collectionizeModelName = function (name) {
var collectionName = utils.inflection.pluralize(name);
collectionName = utils.string.snakeize(collectionName);
@@ -43,12 +54,83 @@ Adapter.prototype.constructor = Adapter;
utils.mixin(Adapter.prototype, new (function () {
this._serializeSortOrder = function (sort) {
+ var ret = {};
+ if (sort) {
+ for (var p in sort) {
+ ret[p] = (sort[p] == 'asc') ? 1 : -1;
+ }
+ }
+ return ret;
};
this._serializeConditions = function (conditions) {
+ return this._serializeOperation(conditions);
};
this._serializeOperation = function (op) {
+ var self = this
+ , ops = []
+ , ret = {};
+ if (!op.isEmpty()) {
+ if (op.type == 'not') {
+ ret = {'$nor': [self._serializeOperation(op.operand())]};
+ }
+ else {
+ // 'and' or 'or', ignore 'null' for now
+ ret['$' + op.type] = ops;
+ op.forEach(function (o) {
+ if (o instanceof operation.OperationBase) {
+ ops.push(self._serializeOperation(o));
+ }
+ else {
+ ops.push(self._serializeComparison(o));
+ }
+ });
+ }
+ }
+ return ret;
+ };
+
+ this._serializeComparison = function (comp) {
+ var ret = {}
+ , nocase = comp.opts.nocase
+ , complex
+ , re
+ , val = comp.value;
+
+ //if (comp.datatype == 'date' || comp.datetime == 'datetime') {
+ // val = JSON.stringify(val).replace(/"/g, '');
+ //}
+
+ switch (comp.type) {
+ case 'EqualTo':
+ // Case-insensitive equality via regex
+ if (nocase) {
+ val = val.toLowerCase();
+ re = new RegExp('^' + val + '$', 'i');
+ ret[comp.field] = re;
+ }
+ else {
+ ret[comp.field] = val;
+ }
+ break;
+ // Convert to regex
+ case 'Like':
+ if (nocase) {
+ val = val.toLowerCase();
+ re = new RegExp('^' + val, 'i');
+ }
+ else {
+ re = new RegExp('^' + val);
+ }
+ ret[comp.field] = re;
+ break;
+ default:
+ complex = {};
+ complex[_comparisonTypeMap[comp.type]] = val;
+ ret[comp.field] = complex;
+ }
+ return ret;
};
this.init = function () {
@@ -61,9 +143,6 @@ utils.mixin(Adapter.prototype, new (function () {
this.client = driver.db.apply(driver, args);
};
- this.exec = function (options, callback) {
- };
-
this.load = function (query, callback) {
var collectionName = _collectionizeModelName(query.model.modelName)
, collection = this.client.collection(collectionName)
@@ -104,10 +183,15 @@ utils.mixin(Adapter.prototype, new (function () {
}
// Collection
else {
- conditions = this._serializeConditions(query.rawConditions);
+ conditions = this._serializeConditions(query.conditions);
sort = this._serializeSortOrder(query.opts.sort);
- /*
- this.request(requestOpts, function (err, data) {
+
+ //var util = require('util');
+ //console.log(util.inspect(conditions, false, null));
+
+ collection.find(conditions, {})
+ .sort(sort)
+ .toArray(function (err, data) {
var rows
, res = [];
if (err) {
@@ -118,6 +202,7 @@ utils.mixin(Adapter.prototype, new (function () {
rows.forEach(function (row) {
var inst = query.model.create(row);
inst.id = row.id;
+ inst._id = row._id;
inst._saved = true;
res.push(inst);
});
@@ -129,7 +214,6 @@ utils.mixin(Adapter.prototype, new (function () {
callback(null, res);
}
});
- */
}
};
@@ -138,7 +222,6 @@ utils.mixin(Adapter.prototype, new (function () {
var collectionName = _collectionizeModelName(query.model.modelName)
, collection = this.client.collection(collectionName)
, id = query.byId
- , requestOpts
, item = data;
// Single instance-lookup by id
if (id) {
@@ -171,25 +254,26 @@ utils.mixin(Adapter.prototype, new (function () {
var collectionName = _collectionizeModelName(query.model.modelName)
, collection = this.client.collection(collectionName)
, id = query.byId
- , requestOpts;
+ , conditions;
// Single instance-lookup by id
if (id) {
- collection.remove({id: id}, function (err, data) {
- var inst
- , res = [];
- if (err) {
- callback(err, null);
- }
- else {
- callback(null, true);
- }
- });
+ conditions = {id: id};
}
// Collection
else {
- callback(new Error('Bulk remove is not supported'), null);
+ conditions = this._serializeConditions(query.conditions);
}
+ collection.remove(conditions, function (err, data) {
+ var inst
+ , res = [];
+ if (err) {
+ callback(err, null);
+ }
+ else {
+ callback(null, true);
+ }
+ });
};
this.insert = function (data, opts, callback) {
@@ -165,7 +165,7 @@ utils.mixin(Adapter.prototype, new (function () {
// Use bracket-notation, in case field-name has special chars
// or is a reserved word
var name = 'data[\'' + comp.field + '\']';
- if (comp.opts.lowercase) {
+ if (comp.opts.nocase) {
name += '.toLowerCase()';
}
return name;
View
@@ -64,18 +64,18 @@ utils.mixin(Adapter.prototype, new (function () {
return '';
}
else {
- op.forEach(function (o) {
- if (o instanceof operation.OperationBase) {
- ops.push(self._serializeOperation(o));
- }
- else {
- ops.push(self._serializeComparison(o));
- }
- });
if (op.type == 'not') {
return 'NOT (' + self._serializeOperation(op.operand()) + ')';
}
else {
+ op.forEach(function (o) {
+ if (o instanceof operation.OperationBase) {
+ ops.push(self._serializeOperation(o));
+ }
+ else {
+ ops.push(self._serializeComparison(o));
+ }
+ });
return '(' + ops.join(' ' + op.type.toUpperCase() + ' ') + ')';
}
}
@@ -89,7 +89,7 @@ utils.mixin(Adapter.prototype, new (function () {
this._serializeComparisonFieldName = function (comp) {
var name = this._columnizePropertyName(comp.field);
- if (comp.opts.lowercase) {
+ if (comp.opts.nocase) {
name = 'LOWER(' + name + ')';
}
return name;
View
@@ -12,20 +12,20 @@ Query = function (model, conditions, options) {
this.initialize.apply(this, arguments);
};
-Query.prototype = new (function () {
+Query.comparisonTypes = {
+ 'eql': 'EqualTo'
+, 'ne': 'NotEqualTo'
+, 'in': 'In'
+, 'like': 'Like'
+, 'gt': 'GreaterThan'
+, 'lt': 'LessThan'
+, 'gte': 'GreaterThanOrEqual'
+, 'lte': 'LessThanOrEqual'
+};
- var _comparisonCtorNames = {
- 'eql': 'EqualTo'
- , 'ne': 'NotEqualTo'
- , 'in': 'In'
- , 'like': 'Like'
- , 'gt': 'GreaterThan'
- , 'lt': 'LessThan'
- , 'gte': 'GreaterThanOrEqual'
- , 'lte': 'LessThanOrEqual'
- }
+Query.prototype = new (function () {
- , _operationTypes = {
+ var _operationTypes = {
'and': true
, 'or': true
, 'not': true
@@ -137,15 +137,15 @@ Query.prototype = new (function () {
// TODO: Validate the value for both the particular field
// (e.g., must be a numeric) and the type of comparison
// (e.g., 'IN' must be a collection, etc)
- return comparison.create(_comparisonCtorNames[type], key, val,
+ return comparison.create(Query.comparisonTypes[type], key, val,
datatype, opts);
}
, _createComparisonOpts = function (key) {
var opts = this.opts
- , lowercase = opts.lowercase
+ , nocase = opts.nocase
, ret = {};
- ['lowercase'].forEach(function (k) {
+ ['nocase'].forEach(function (k) {
var opt = opts[k]
, included;
if (opt) {
@@ -14,7 +14,7 @@ var utils = require('utilities')
, Account = require('../../fixtures/account').Account;
tests = {
- 'before': function () {
+ 'before': function (next) {
adapter = new Adapter({
dbname: 'model_test'
});
@@ -25,6 +25,22 @@ tests = {
, 'Profile': adapter
, 'Account': adapter
};
+
+ Zooby.remove({}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ next();
+ });
+ }
+
+, 'after': function (next) {
+ Zooby.remove({}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ next();
+ });
}
, 'test create adapter': function () {
Oops, something went wrong.

0 comments on commit 237b3fe

Please sign in to comment.