Comparing version 0.4.0 to 0.4.1
@@ -63,6 +63,6 @@ // Generated by CoffeeScript 1.4.0 | ||
AdapterBase.prototype._convertToModelInstance = function(model, data) { | ||
AdapterBase.prototype._convertToModelInstance = function(model, data, selected_columns) { | ||
var modelClass; | ||
modelClass = this._connection.models[model]; | ||
return new modelClass(data, this._getModelID(data)); | ||
return new modelClass(data, this._getModelID(data), selected_columns); | ||
}; | ||
@@ -69,0 +69,0 @@ |
@@ -426,3 +426,3 @@ // Generated by CoffeeScript 1.4.0 | ||
MongoDBAdapter.prototype.findById = function(model, id, options, callback) { | ||
var fields, | ||
var client_options, fields, | ||
_this = this; | ||
@@ -440,9 +440,9 @@ if (options.select) { | ||
} | ||
options = {}; | ||
client_options = {}; | ||
if (fields) { | ||
options.fields = fields; | ||
client_options.fields = fields; | ||
} | ||
return this._collection(model).findOne({ | ||
_id: id | ||
}, options, function(error, result) { | ||
}, client_options, function(error, result) { | ||
if (error) { | ||
@@ -454,3 +454,3 @@ return callback(MongoDBAdapter.wrapError('unknown error', error)); | ||
} | ||
return callback(null, _this._convertToModelInstance(model, result)); | ||
return callback(null, _this._convertToModelInstance(model, result, options.select)); | ||
}); | ||
@@ -460,3 +460,3 @@ }; | ||
MongoDBAdapter.prototype.find = function(model, conditions, options, callback) { | ||
var field, fields, obj, orders, | ||
var client_options, field, fields, obj, orders, | ||
_this = this; | ||
@@ -497,12 +497,12 @@ if (options.select) { | ||
} | ||
options = { | ||
client_options = { | ||
limit: options.limit | ||
}; | ||
if (fields) { | ||
options.fields = fields; | ||
client_options.fields = fields; | ||
} | ||
if (orders) { | ||
options.sort = orders; | ||
client_options.sort = orders; | ||
} | ||
return this._collection(model).find(conditions, options, function(error, cursor) { | ||
return this._collection(model).find(conditions, client_options, function(error, cursor) { | ||
if (error || !cursor) { | ||
@@ -516,3 +516,3 @@ return callback(MongoDBAdapter.wrapError('unknown error', error)); | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record); | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
@@ -519,0 +519,0 @@ }); |
@@ -318,3 +318,3 @@ // Generated by CoffeeScript 1.4.0 | ||
if ((result != null ? result.length : void 0) === 1) { | ||
return callback(null, _this._convertToModelInstance(model, result[0])); | ||
return callback(null, _this._convertToModelInstance(model, result[0], options.select)); | ||
} else if ((result != null ? result.length : void 0) > 1) { | ||
@@ -371,3 +371,3 @@ return callback(new Error('unknown error')); | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record); | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
@@ -374,0 +374,0 @@ }); |
@@ -315,3 +315,3 @@ // Generated by CoffeeScript 1.4.0 | ||
if ((rows != null ? rows.length : void 0) === 1) { | ||
return callback(null, _this._convertToModelInstance(model, rows[0])); | ||
return callback(null, _this._convertToModelInstance(model, rows[0], options.select)); | ||
} else if ((rows != null ? rows.length : void 0) > 1) { | ||
@@ -362,3 +362,3 @@ return callback(new Error('unknown error')); | ||
return callback(null, rows.map(function(record) { | ||
return _this._convertToModelInstance(model, record); | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
@@ -365,0 +365,0 @@ }); |
@@ -195,3 +195,3 @@ // Generated by CoffeeScript 1.4.0 | ||
result.id = id; | ||
return callback(null, _this._convertToModelInstance(model, result)); | ||
return callback(null, _this._convertToModelInstance(model, result, options.select)); | ||
} else { | ||
@@ -220,3 +220,3 @@ return callback(new Error('not found')); | ||
return callback(null, records.map(function(record) { | ||
return _this._convertToModelInstance(model, record); | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
@@ -223,0 +223,0 @@ }); |
@@ -265,3 +265,3 @@ // Generated by CoffeeScript 1.4.0 | ||
if ((result != null ? result.length : void 0) === 1) { | ||
return callback(null, _this._convertToModelInstance(model, result[0])); | ||
return callback(null, _this._convertToModelInstance(model, result[0], options.select)); | ||
} else if ((result != null ? result.length : void 0) > 1) { | ||
@@ -310,3 +310,3 @@ return callback(new Error('unknown error')); | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record); | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
@@ -313,0 +313,0 @@ }); |
@@ -34,2 +34,4 @@ // Generated by CoffeeScript 1.4.0 | ||
Model.eliminate_null = false; | ||
Model.newModel = function(connection, name, schema) { | ||
@@ -155,3 +157,3 @@ var NewModel, property; | ||
for (i = _i = 0, _ref = parts.length - 1; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { | ||
this._intermediate_paths[parts.slice(0, +i + 1 || 9e9)] = 1; | ||
this._intermediate_paths[parts.slice(0, +i + 1 || 9e9).join('.')] = 1; | ||
} | ||
@@ -195,3 +197,3 @@ property.type = type; | ||
function Model(data) { | ||
var adapter, column, ctor, id, last, obj, parts, path, property, schema, support_nested, value, _i, _len, _ref, _ref1, _ref2; | ||
var adapter, column, ctor, dont_eliminate_null, id, last, obj, parts, path, property, schema, selected_columns, support_nested, value, _i, _len, _ref, _ref1, _ref2; | ||
data = data || {}; | ||
@@ -201,2 +203,3 @@ ctor = this.constructor; | ||
adapter = ctor._adapter; | ||
dont_eliminate_null = !ctor.eliminate_null; | ||
Object.defineProperty(this, '_prev_attributes', { | ||
@@ -254,5 +257,9 @@ writable: true, | ||
if (id = arguments[1]) { | ||
selected_columns = arguments[2]; | ||
support_nested = adapter.support_nested; | ||
for (column in schema) { | ||
property = schema[column]; | ||
if (selected_columns && selected_columns.indexOf(column) === -1) { | ||
continue; | ||
} | ||
parts = property._parts; | ||
@@ -262,5 +269,12 @@ value = support_nested ? util.getPropertyOfPath(data, parts) : data[property._dbname]; | ||
value = adapter.valueToModel(value, column, property); | ||
} else { | ||
value = null; | ||
} | ||
if ((value != null) || dont_eliminate_null) { | ||
util.setPropertyOfPath(this, parts, value); | ||
} | ||
} | ||
if (dont_eliminate_null) { | ||
this._collapseNestedNulls(selected_columns); | ||
} | ||
Object.defineProperty(this, 'id', { | ||
@@ -278,6 +292,12 @@ configurable: false, | ||
value = util.getPropertyOfPath(data, parts); | ||
if (value != null) { | ||
if (value === void 0) { | ||
value = null; | ||
} | ||
if ((value != null) || dont_eliminate_null) { | ||
util.setPropertyOfPath(this, parts, value); | ||
} | ||
} | ||
if (dont_eliminate_null) { | ||
this._collapseNestedNulls(); | ||
} | ||
Object.defineProperty(this, 'id', { | ||
@@ -287,3 +307,3 @@ configurable: true, | ||
writable: false, | ||
value: void 0 | ||
value: null | ||
}); | ||
@@ -294,2 +314,35 @@ } | ||
Model.prototype._collapseNestedNulls = function(selected_columns) { | ||
var ctor, has_non_null, key, last, obj, path, value, _i, _len, _ref, _ref1, _ref2, _results; | ||
ctor = this.constructor; | ||
_ref = Object.keys(ctor._intermediate_paths); | ||
_results = []; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
path = _ref[_i]; | ||
if (selected_columns && selected_columns.indexOf(path) === -1) { | ||
continue; | ||
} | ||
if (ctor.dirty_tracking) { | ||
obj = this._intermediates; | ||
last = path; | ||
} else { | ||
_ref1 = util.getLeafOfPath(this, path), obj = _ref1[0], last = _ref1[1]; | ||
} | ||
has_non_null = false; | ||
_ref2 = obj[last]; | ||
for (key in _ref2) { | ||
value = _ref2[key]; | ||
if (value != null) { | ||
has_non_null = true; | ||
} | ||
} | ||
if (!has_non_null) { | ||
_results.push(obj[last] = null); | ||
} else { | ||
_results.push(void 0); | ||
} | ||
} | ||
return _results; | ||
}; | ||
Model.prototype._defineProperty = function(object, key, path, enumerable) { | ||
@@ -330,3 +383,4 @@ var _this = this; | ||
Model.prototype.set = function(path, value) { | ||
var k, last, obj, parts, v, _ref, _ref1, _results, _results1; | ||
var eliminate_null, k, last, obj, parts, prev_value, v, _ref, _ref1, _results, _results1; | ||
eliminate_null = this.constructor.eliminate_null; | ||
if (this._intermediates.hasOwnProperty(path)) { | ||
@@ -345,7 +399,17 @@ obj = this._intermediates[path]; | ||
parts = path.split('.'); | ||
prev_value = util.getPropertyOfPath(this._attributes, parts); | ||
if (prev_value === value) { | ||
return; | ||
} | ||
if (!this._prev_attributes.hasOwnProperty(path)) { | ||
this._prev_attributes[path] = util.getPropertyOfPath(this._attributes, parts); | ||
this._prev_attributes[path] = prev_value; | ||
} | ||
_ref = util.getLeafOfPath(this, parts), obj = _ref[0], last = _ref[1]; | ||
this._defineProperty(obj, last, path, value != null); | ||
if (eliminate_null) { | ||
if (eliminate_null || prev_value === void 0) { | ||
this._defineProperty(obj, last, path, value != null); | ||
} | ||
} else if (prev_value === void 0) { | ||
this._defineProperty(obj, last, path, true); | ||
} | ||
util.setPropertyOfPath(this._attributes, parts, value); | ||
@@ -352,0 +416,0 @@ _results1 = []; |
{ | ||
"name": "cormo", | ||
"description": "ORM framework for Node.js", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"keywords": [ | ||
@@ -27,4 +27,6 @@ "orm", | ||
"devDependencies": { | ||
"benchmark": "", | ||
"coffee-script": "", | ||
"crojsdoc": "", | ||
"microtime": "", | ||
"mocha": "", | ||
@@ -31,0 +33,0 @@ "mongodb": ">=1.1.11", |
156503
30
4102
11