cormo
Advanced tools
Comparing version 0.3.9 to 0.4.0
@@ -260,3 +260,3 @@ // Generated by CoffeeScript 1.4.0 | ||
} catch (e) { | ||
return callback(new Error("'id' is not a valid ID")); | ||
throw new Error("'id' is not a valid ID"); | ||
} | ||
@@ -273,3 +273,3 @@ }; | ||
} catch (e) { | ||
callback(new Error("'" + column + "' is not a valid ID")); | ||
throw new Error("'" + column + "' is not a valid ID"); | ||
} | ||
@@ -276,0 +276,0 @@ } |
@@ -30,2 +30,5 @@ // Generated by CoffeeScript 1.4.0 | ||
var _this = this; | ||
if (settings.is_default !== false) { | ||
Connection.defaultConnection = this; | ||
} | ||
this.connected = false; | ||
@@ -129,4 +132,6 @@ this.models = {}; | ||
Model._Connection = Connection; | ||
module.exports = Connection; | ||
}).call(this); |
@@ -70,4 +70,17 @@ // Generated by CoffeeScript 1.4.0 | ||
ConnectionManipulate.prototype._manipulateFind = function(model, data, callback) { | ||
model = inflector.camelize(inflector.singularize(model)); | ||
if (!this.models[model]) { | ||
return callback(new Error("model " + model + " does not exist")); | ||
} | ||
model = this.models[model]; | ||
return model.where(data).exec({ | ||
skip_log: true | ||
}, function(error, records) { | ||
return callback(error, records); | ||
}); | ||
}; | ||
ConnectionManipulate.prototype._manipulateConvertIds = function(id_to_record_map, model, data) { | ||
var column, property, _ref, _results; | ||
var column, property, record, _ref, _results; | ||
model = inflector.camelize(model); | ||
@@ -83,3 +96,8 @@ if (!this.models[model]) { | ||
if (property.record_id && data.hasOwnProperty(column)) { | ||
_results.push(data[column] = id_to_record_map[data[column]].id); | ||
record = id_to_record_map[data[column]]; | ||
if (record) { | ||
_results.push(data[column] = record.id); | ||
} else { | ||
_results.push(void 0); | ||
} | ||
} else { | ||
@@ -140,2 +158,16 @@ _results.push(void 0); | ||
return _this._manipulateDropAllModels(callback); | ||
} else if (key.substr(0, 5) === 'find_') { | ||
model = key.substr(5); | ||
id = data.id; | ||
delete data.id; | ||
if (!id) { | ||
return callback(null); | ||
} | ||
return _this._manipulateFind(model, data, function(error, records) { | ||
if (error) { | ||
return callback(error); | ||
} | ||
id_to_record_map[id] = records; | ||
return callback(null); | ||
}); | ||
} else { | ||
@@ -142,0 +174,0 @@ return callback(new Error('unknown command: ' + key)); |
@@ -56,2 +56,5 @@ // Generated by CoffeeScript 1.4.0 | ||
Model.connection = function(connection, name) { | ||
if (this.hasOwnProperty('_connection')) { | ||
throw new Error('Model::connection was called twice'); | ||
} | ||
if (!name) { | ||
@@ -61,2 +64,3 @@ name = this.name; | ||
connection.models[name] = this; | ||
connection[name] = this; | ||
Object.defineProperty(this, '_connection', { | ||
@@ -92,2 +96,12 @@ value: connection | ||
Model._checkConnection = function() { | ||
if (this.hasOwnProperty('_connection')) { | ||
return; | ||
} | ||
if (!(Model._Connection.defaultConnection != null)) { | ||
throw new Error('Create a Connection before creating a Model'); | ||
} | ||
return this.connection(Model._Connection.defaultConnection); | ||
}; | ||
Model._waitingForReady = function(object, method, args) { | ||
@@ -113,2 +127,3 @@ if (this._connection._waitingForApplyingSchemas(object, method, args)) { | ||
var i, parts, subcolumn, subproperty, type, _i, _ref; | ||
this._checkConnection(); | ||
if (typeof property === 'object' && !Array.isArray(property) && (!property.type || property.type.type)) { | ||
@@ -154,2 +169,3 @@ for (subcolumn in property) { | ||
Model.index = function(columns, options) { | ||
this._checkConnection(); | ||
options || (options = {}); | ||
@@ -377,2 +393,3 @@ if (!options.name) { | ||
Model.hasMany = function(target_model_or_column, options) { | ||
this._checkConnection(); | ||
return this._connection.addAssociation({ | ||
@@ -387,2 +404,3 @@ type: 'hasMany', | ||
Model.belongsTo = function(target_model_or_column, options) { | ||
this._checkConnection(); | ||
return this._connection.addAssociation({ | ||
@@ -389,0 +407,0 @@ type: 'belongsTo', |
@@ -193,4 +193,8 @@ // Generated by CoffeeScript 1.4.0 | ||
schema = ctor._schema; | ||
for (path in this._prev_attributes) { | ||
ctor._buildSaveDataColumn(data, this._attributes, path, schema[path], true); | ||
try { | ||
for (path in this._prev_attributes) { | ||
ctor._buildSaveDataColumn(data, this._attributes, path, schema[path], true); | ||
} | ||
} catch (e) { | ||
return callback(e, this); | ||
} | ||
@@ -197,0 +201,0 @@ if (!(options != null ? options.skip_log : void 0)) { |
@@ -99,3 +99,3 @@ // Generated by CoffeeScript 1.4.0 | ||
Query.prototype.exec = function(callback) { | ||
Query.prototype.exec = function(options, callback) { | ||
var expected_count, | ||
@@ -106,7 +106,13 @@ _this = this; | ||
} | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
if (this._find_single_id && this._conditions.length === 0) { | ||
this._connection.log(this._name, 'find by id', { | ||
id: this._id, | ||
options: this._options | ||
}); | ||
if (!(options != null ? options.skip_log : void 0)) { | ||
this._connection.log(this._name, 'find by id', { | ||
id: this._id, | ||
options: this._options | ||
}); | ||
} | ||
this._adapter.findById(this._name, this._id, this._options, _bindDomain(function(error, record) { | ||
@@ -139,6 +145,8 @@ if (error || !record) { | ||
} | ||
this._connection.log(this._name, 'find', { | ||
conditions: this._conditions, | ||
options: this._options | ||
}); | ||
if (!(options != null ? options.skip_log : void 0)) { | ||
this._connection.log(this._name, 'find', { | ||
conditions: this._conditions, | ||
options: this._options | ||
}); | ||
} | ||
return this._adapter.find(this._name, this._conditions, this._options, _bindDomain(function(error, records) { | ||
@@ -145,0 +153,0 @@ if (error) { |
{ | ||
"name": "cormo", | ||
"description": "ORM framework for Node.js", | ||
"version": "0.3.9", | ||
"version": "0.4.0", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "orm", |
@@ -46,6 +46,7 @@ # About | ||
class Post extends cormo.Model | ||
@connection connection # must be first | ||
@connection connection # if omitted, Connection.defaultConnection will be used instead | ||
@column 'title', String # 'String' is the same as '{ type: String }' | ||
@column 'body', 'string' # you can also use 'string' to specify a string type | ||
# apply defined models to Database. This can be omitted | ||
connection.applySchemas (error) -> | ||
@@ -211,4 +212,2 @@ console.log error | ||
connection.applySchemas() | ||
User.create { name: 'Bill Smith', age: 45, email: 'bill@foo.org' }, (error, user1) -> | ||
@@ -244,4 +243,2 @@ User.create { name: 'Bill Simpson', age: 38, email: 'bill@foo.org' }, (error, user2) -> | ||
connection.applySchemas() | ||
User.create { name: 'John Doe', age: 10, email: 'invalid' }, (error, user) -> | ||
@@ -282,3 +279,2 @@ # error.message will be 'invalid email,too young' or 'too young,invalid email' | ||
class Post extends cormo.Model | ||
@connection connection # must be first | ||
@column 'title', String # 'String' is the same as '{ type: String }' | ||
@@ -313,5 +309,2 @@ @column 'body', 'string' # you can also use 'string' to specify a string type | ||
# you must call applySchemas after defining association | ||
connection.applySchemas() | ||
# get associated objects | ||
@@ -351,4 +344,2 @@ user.posts (error, records) -> | ||
connection.applySchemas() | ||
# create | ||
@@ -355,0 +346,0 @@ Place.create name: 'Carrier Dome', location: [-76.136131, 43.036240] |
150369
4040
363