cormo
Advanced tools
Comparing version 0.3.6 to 0.3.7
@@ -294,6 +294,15 @@ // Generated by CoffeeScript 1.4.0 | ||
}, function(error, result) { | ||
var id, key, _ref; | ||
var column, id, key, _ref; | ||
if ((error != null ? error.code : void 0) === 11000) { | ||
key = error.err.match(/index: [\w-.]+\$(\w+)_1/); | ||
return callback(new Error('duplicated ' + (key != null ? key[1] : void 0))); | ||
column = ''; | ||
key = error.err.match(/index: [\w-.]+\$(\w+)/); | ||
if (key != null) { | ||
column = key[1]; | ||
key = column.match(/(\w+)_1/); | ||
if (key != null) { | ||
column = key[1]; | ||
} | ||
column = ' ' + column; | ||
} | ||
return callback(new Error('duplicated' + column)); | ||
} | ||
@@ -300,0 +309,0 @@ if (error) { |
@@ -158,9 +158,17 @@ // Generated by CoffeeScript 1.4.0 | ||
_processSaveError = function(model, error, callback) { | ||
var key, re; | ||
var column, key; | ||
if (error.code === '42P01') { | ||
error = new Error('table does not exist'); | ||
} else if (error.code === '23505') { | ||
re = new RegExp("unique constraint \"" + (tableize(model)) + "_([^']*)_key\""); | ||
key = error.message.match(re); | ||
error = new Error('duplicated ' + (key != null ? key[1] : void 0)); | ||
column = ''; | ||
key = error.message.match(/unique constraint \"(.*)\"/); | ||
if (key != null) { | ||
column = key[1]; | ||
key = column.match(new RegExp("" + (tableize(model)) + "_([^']*)_key")); | ||
if (key != null) { | ||
column = key[1]; | ||
} | ||
column = ' ' + column; | ||
} | ||
error = new Error('duplicated' + column); | ||
} else { | ||
@@ -167,0 +175,0 @@ error = PostgreSQLAdapter.wrapError('unknown error', error); |
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
var ConnectionManipulate, async, inflector; | ||
var ConnectionManipulate, async, inflector, types; | ||
@@ -9,2 +9,4 @@ async = require('async'); | ||
types = require('../types'); | ||
ConnectionManipulate = (function() { | ||
@@ -21,3 +23,3 @@ | ||
return model.create(data, function(error, record) { | ||
return callback(error); | ||
return callback(error, record); | ||
}); | ||
@@ -37,4 +39,36 @@ }; | ||
ConnectionManipulate.prototype._manipulateDeleteAllModels = function(callback) { | ||
var _this = this; | ||
return async.forEach(Object.keys(this.models), function(model, callback) { | ||
model = _this.models[model]; | ||
return model["delete"](function(error, count) { | ||
return callback(error); | ||
}); | ||
}, callback); | ||
}; | ||
ConnectionManipulate.prototype._manipulateConvertIds = function(id_to_record_map, model, data) { | ||
var column, property, _ref, _results; | ||
model = inflector.camelize(model); | ||
if (!this.models[model]) { | ||
return; | ||
} | ||
model = this.models[model]; | ||
_ref = model._schema; | ||
_results = []; | ||
for (column in _ref) { | ||
property = _ref[column]; | ||
if (property.record_id && data.hasOwnProperty(column)) { | ||
_results.push(data[column] = id_to_record_map[data[column]].id); | ||
} else { | ||
_results.push(void 0); | ||
} | ||
} | ||
return _results; | ||
}; | ||
ConnectionManipulate.prototype.manipulate = function(commands, callback) { | ||
var _this = this; | ||
var id_to_record_map, | ||
_this = this; | ||
id_to_record_map = {}; | ||
if (!Array.isArray(commands)) { | ||
@@ -44,3 +78,3 @@ commands = [commands]; | ||
return async.forEachSeries(commands, function(command, callback) { | ||
var data, key, model; | ||
var data, id, key, model; | ||
if (typeof command === 'object') { | ||
@@ -62,6 +96,19 @@ key = Object.keys(command); | ||
model = key.substr(7); | ||
return _this._manipulateCreation(model, data, callback); | ||
id = data.id; | ||
delete data.id; | ||
_this._manipulateConvertIds(id_to_record_map, model, data); | ||
return _this._manipulateCreation(model, data, function(error, record) { | ||
if (error) { | ||
return callback(error); | ||
} | ||
if (id) { | ||
id_to_record_map[id] = record; | ||
} | ||
return callback(null); | ||
}); | ||
} else if (key.substr(0, 7) === 'delete_') { | ||
model = key.substr(7); | ||
return _this._manipulateDeletion(model, data, callback); | ||
} else if (key === 'deleteAll') { | ||
return _this._manipulateDeleteAllModels(callback); | ||
} else { | ||
@@ -71,3 +118,3 @@ return callback(new Error('unknown command: ' + key)); | ||
}, function(error) { | ||
return callback(error); | ||
return callback(error, id_to_record_map); | ||
}); | ||
@@ -74,0 +121,0 @@ }; |
@@ -132,2 +132,3 @@ // Generated by CoffeeScript 1.4.0 | ||
type = this._getKeyType(property.connection); | ||
property.record_id = true; | ||
} | ||
@@ -134,0 +135,0 @@ if (type === types.GeoPoint && !this._adapter.support_geopoint) { |
{ | ||
"name": "cormo", | ||
"description": "ORM framework for Node.js", | ||
"version": "0.3.6", | ||
"version": "0.3.7", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "orm", |
146713
3932