cormo
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -69,2 +69,25 @@ // Generated by CoffeeScript 1.4.0 | ||
AdapterBase.prototype._convertToGroupInstance = function(model, data, group_by, group_fields) { | ||
var expr, field, instance, op, property, schema, _i, _len; | ||
instance = {}; | ||
if (group_by) { | ||
schema = this._connection.models[model]._schema; | ||
for (_i = 0, _len = group_by.length; _i < _len; _i++) { | ||
field = group_by[_i]; | ||
property = schema[field]; | ||
if (property) { | ||
instance[field] = this.valueToModel(data[field], field, property); | ||
} | ||
} | ||
} | ||
for (field in group_fields) { | ||
expr = group_fields[field]; | ||
op = Object.keys(expr)[0]; | ||
if (op === '$sum') { | ||
instance[field] = Number(data[field]); | ||
} | ||
} | ||
return instance; | ||
}; | ||
AdapterBase.prototype.create = function(model, data, callback) { | ||
@@ -71,0 +94,0 @@ return callback(new Error('not implemented')); |
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
var AdapterBase, MongoDBAdapter, ObjectID, async, mongodb, tableize, types, _buildWhere, _buildWhereSingle, _convertValueToObjectID, | ||
var AdapterBase, MongoDBAdapter, ObjectID, async, mongodb, tableize, types, _buildGroupFields, _buildWhere, _buildWhereSingle, _convertValueToObjectID, | ||
__hasProp = {}.hasOwnProperty, | ||
@@ -54,3 +54,3 @@ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
} | ||
} else if (typeof value === 'object' && (keys = Object.keys(value)).length === 1) { | ||
} else if (typeof value === 'object' && value !== null && (keys = Object.keys(value)).length === 1) { | ||
sub_key = keys[0]; | ||
@@ -97,3 +97,3 @@ switch (sub_key) { | ||
} else { | ||
if (is_objectid) { | ||
if (is_objectid && (value != null)) { | ||
value = _convertValueToObjectID(value, key); | ||
@@ -163,2 +163,24 @@ } | ||
_buildGroupFields = function(group_by, group_fields) { | ||
var expr, field, group; | ||
group = {}; | ||
if (group_by) { | ||
if (group_by.length === 1) { | ||
group._id = '$' + group_by[0]; | ||
} else { | ||
group._id = {}; | ||
group_by.forEach(function(field) { | ||
return group._id[field] = '$' + field; | ||
}); | ||
} | ||
} else { | ||
group._id = null; | ||
} | ||
for (field in group_fields) { | ||
expr = group_fields[field]; | ||
group[field] = expr; | ||
} | ||
return group; | ||
}; | ||
MongoDBAdapter = (function(_super) { | ||
@@ -469,3 +491,3 @@ var _getMongoDBColName; | ||
MongoDBAdapter.prototype.find = function(model, conditions, options, callback) { | ||
var client_options, field, fields, obj, orders, | ||
var client_options, field, fields, obj, orders, pipeline, | ||
_this = this; | ||
@@ -515,7 +537,13 @@ if (options.select) { | ||
} | ||
return this._collection(model).find(conditions, client_options, function(error, cursor) { | ||
if (error || !cursor) { | ||
return callback(MongoDBAdapter.wrapError('unknown error', error)); | ||
if (options.group_by || options.group_fields) { | ||
pipeline = []; | ||
if (conditions) { | ||
pipeline.push({ | ||
$match: conditions | ||
}); | ||
} | ||
return cursor.toArray(function(error, result) { | ||
pipeline.push({ | ||
$group: _buildGroupFields(options.group_by, options.group_fields) | ||
}); | ||
return this._collection(model).aggregate(pipeline, function(error, result) { | ||
if (error) { | ||
@@ -525,6 +553,25 @@ return callback(MongoDBAdapter.wrapError('unknown error', error)); | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
if (options.group_by) { | ||
if (options.group_by.length === 1) { | ||
record[options.group_by[0]] = record._id; | ||
} | ||
} | ||
return _this._convertToGroupInstance(model, record, options.group_by, options.group_fields); | ||
})); | ||
}); | ||
}); | ||
} else { | ||
return this._collection(model).find(conditions, client_options, function(error, cursor) { | ||
if (error || !cursor) { | ||
return callback(MongoDBAdapter.wrapError('unknown error', error)); | ||
} | ||
return cursor.toArray(function(error, result) { | ||
if (error) { | ||
return callback(MongoDBAdapter.wrapError('unknown error', error)); | ||
} | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
}); | ||
}); | ||
} | ||
}; | ||
@@ -531,0 +578,0 @@ |
@@ -344,3 +344,5 @@ // Generated by CoffeeScript 1.4.0 | ||
_this = this; | ||
if (options.select) { | ||
if (options.group_by || options.group_fields) { | ||
selects = this._buildGroupFields(options.group_by, options.group_fields); | ||
} else if (options.select) { | ||
selects = options.select.length > 0 ? 'id,' + options.select.join(',') : 'id'; | ||
@@ -364,2 +366,5 @@ } else { | ||
} | ||
if (options.group_by) { | ||
sql += ' GROUP BY ' + options.group_by.join(','); | ||
} | ||
if ((options != null ? options.orders.length : void 0) > 0 || order_by) { | ||
@@ -385,5 +390,11 @@ orders = options.orders.map(function(order) { | ||
} | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
if (options.group_fields) { | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToGroupInstance(model, record, options.group_by, options.group_fields); | ||
})); | ||
} else { | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
} | ||
}); | ||
@@ -390,0 +401,0 @@ }; |
@@ -341,3 +341,5 @@ // Generated by CoffeeScript 1.4.0 | ||
_this = this; | ||
if (options.select) { | ||
if (options.group_by || options.group_fields) { | ||
selects = this._buildGroupFields(options.group_by, options.group_fields); | ||
} else if (options.select) { | ||
selects = options.select.length > 0 ? 'id,' + options.select.join(',') : 'id'; | ||
@@ -356,2 +358,5 @@ } else { | ||
} | ||
if (options.group_by) { | ||
sql += ' GROUP BY ' + options.group_by.join(','); | ||
} | ||
if ((options != null ? options.limit : void 0) != null) { | ||
@@ -376,5 +381,11 @@ sql += ' LIMIT ' + options.limit; | ||
} | ||
return callback(null, rows.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
if (options.group_fields) { | ||
return callback(null, rows.map(function(record) { | ||
return _this._convertToGroupInstance(model, record, options.group_by, options.group_fields); | ||
})); | ||
} else { | ||
return callback(null, rows.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
} | ||
}); | ||
@@ -381,0 +392,0 @@ }; |
@@ -43,3 +43,8 @@ // Generated by CoffeeScript 1.4.0 | ||
case '$not': | ||
return "(NOT (" + (this._buildWhereSingle(property, key, value[sub_key], params)) + ") OR " + (key.replace('.', '_')) + " IS NULL)"; | ||
if (value[sub_key] === null) { | ||
return "NOT " + (key.replace('.', '_')) + " IS NULL"; | ||
} else { | ||
return "(NOT (" + (this._buildWhereSingle(property, key, value[sub_key], params)) + ") OR " + (key.replace('.', '_')) + " IS NULL)"; | ||
} | ||
break; | ||
case '$in': | ||
@@ -127,2 +132,27 @@ values = value[sub_key]; | ||
SQLAdapterBase.prototype._buildGroupFields = function(group_by, group_fields) { | ||
var expr, field, op, selects, sub_expr; | ||
selects = []; | ||
if (group_by) { | ||
[].push.apply(selects, group_by); | ||
} | ||
for (field in group_fields) { | ||
expr = group_fields[field]; | ||
op = Object.keys(expr)[0]; | ||
if (op === '$sum') { | ||
sub_expr = expr[op]; | ||
if (sub_expr === 1) { | ||
selects.push("COUNT(*) as " + field); | ||
} else if (sub_expr.substr(0, 1) === '$') { | ||
selects.push("SUM(" + (sub_expr.substr(1)) + ") as " + field); | ||
} else { | ||
throw new Error("unknown expression '" + (JSON.stringify(op)) + "'"); | ||
} | ||
} else { | ||
throw new Error("unknown expression '" + (JSON.stringify(op)) + "'"); | ||
} | ||
} | ||
return selects.join(','); | ||
}; | ||
return SQLAdapterBase; | ||
@@ -129,0 +159,0 @@ |
@@ -291,3 +291,5 @@ // Generated by CoffeeScript 1.4.0 | ||
_this = this; | ||
if (options.select) { | ||
if (options.group_by || options.group_fields) { | ||
selects = this._buildGroupFields(options.group_by, options.group_fields); | ||
} else if (options.select) { | ||
selects = options.select.length > 0 ? 'id,' + options.select.join(',') : 'id'; | ||
@@ -306,2 +308,5 @@ } else { | ||
} | ||
if (options.group_by) { | ||
sql += ' GROUP BY ' + options.group_by.join(','); | ||
} | ||
if ((options != null ? options.limit : void 0) != null) { | ||
@@ -324,5 +329,11 @@ sql += ' LIMIT ' + options.limit; | ||
} | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
if (options.group_fields) { | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToGroupInstance(model, record, options.group_by, options.group_fields); | ||
})); | ||
} else { | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
} | ||
}); | ||
@@ -329,0 +340,0 @@ }; |
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
var ConnectionAssociation, inflector, types, _; | ||
var ConnectionAssociation, async, inflector, types, _; | ||
_ = require('underscore'); | ||
async = require('async'); | ||
inflector = require('../inflector'); | ||
@@ -106,3 +108,4 @@ | ||
type: types.RecordID, | ||
connection: target_model._connection | ||
connection: target_model._connection, | ||
required: options != null ? options.required : void 0 | ||
}); | ||
@@ -189,2 +192,5 @@ column = (options != null ? options.as : void 0) || inflector.underscore(target_model._name); | ||
_this = this; | ||
if (this._waitingForApplyingSchemas(this, this.getInconsistencies, arguments)) { | ||
return; | ||
} | ||
result = {}; | ||
@@ -204,3 +210,3 @@ return async.forEach(Object.keys(this.models), function(model, callback) { | ||
return async.forEach(integrities, function(integrity, callback) { | ||
var conditions, query; | ||
var conditions, property, query; | ||
query = integrity.child.select(''); | ||
@@ -214,3 +220,4 @@ conditions = {}; | ||
query.where(conditions); | ||
if (integrity.type === 'parent_nullify') { | ||
property = integrity.child._schema[integrity.column]; | ||
if (!property.required) { | ||
conditions = {}; | ||
@@ -217,0 +224,0 @@ conditions[integrity.column] = { |
@@ -140,2 +140,5 @@ // Generated by CoffeeScript 1.4.0 | ||
if (this._schema.hasOwnProperty(path)) { | ||
if ((property != null ? property.required : void 0) != null) { | ||
this._schema[path].required = property.required; | ||
} | ||
return; | ||
@@ -142,0 +145,0 @@ } |
@@ -49,2 +49,12 @@ // Generated by CoffeeScript 1.4.0 | ||
ModelQuery.group = function(group_by, fields, callback) { | ||
var query; | ||
query = new Query(this); | ||
query.group(group_by, fields); | ||
if (typeof callback === 'function') { | ||
query.exec(callback); | ||
} | ||
return query; | ||
}; | ||
ModelQuery.count = function(condition, callback) { | ||
@@ -51,0 +61,0 @@ return this._createOptionalQueryAndRun('where', condition, 'count', callback); |
@@ -96,2 +96,16 @@ // Generated by CoffeeScript 1.4.0 | ||
Query.prototype.group = function(group_by, fields) { | ||
var columns, schema_columns; | ||
this._options.group_by = null; | ||
schema_columns = Object.keys(this._model._schema); | ||
if (typeof group_by === 'string') { | ||
columns = group_by.split(/\s+/).filter(function(column) { | ||
return schema_columns.indexOf(column) >= 0; | ||
}); | ||
this._options.group_by = columns; | ||
} | ||
this._options.group_fields = fields; | ||
return this; | ||
}; | ||
Query.prototype.limit = function(limit) { | ||
@@ -301,3 +315,5 @@ this._options.limit = limit; | ||
} | ||
return query.exec(callback); | ||
return query.exec({ | ||
skip_log: options != null ? options.skip_log : void 0 | ||
}, callback); | ||
}, function(records, callback) { | ||
@@ -325,2 +341,5 @@ var archive_records; | ||
} | ||
if (records.length === 0) { | ||
return callback(null); | ||
} | ||
ids = records.map(function(record) { | ||
@@ -327,0 +346,0 @@ return record.id; |
{ | ||
"name": "cormo", | ||
"description": "ORM framework for Node.js", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "orm", |
174488
4539