cormo
Advanced tools
Comparing version 0.5.5 to 0.5.6
@@ -64,7 +64,21 @@ // Generated by CoffeeScript 1.6.2 | ||
AdapterBase.prototype._refineRawInstance = function(model, data, selected_columns) { | ||
var id; | ||
id = this._getModelID(data); | ||
Object.defineProperty(data, 'id', { | ||
configurable: false, | ||
enumerable: true, | ||
writable: false, | ||
value: id | ||
}); | ||
return data; | ||
}; | ||
AdapterBase.prototype._convertToModelInstance = function(model, data, selected_columns) { | ||
var modelClass; | ||
var id, modelClass; | ||
id = this._getModelID(data); | ||
modelClass = this._connection.models[model]; | ||
return new modelClass(data, this._getModelID(data), selected_columns); | ||
return new modelClass(data, id, selected_columns); | ||
}; | ||
@@ -71,0 +85,0 @@ |
// Generated by CoffeeScript 1.6.2 | ||
(function() { | ||
var AdapterBase, MongoDBAdapter, ObjectID, async, error, mongodb, tableize, types, _, _buildGroupFields, _buildWhere, _buildWhereSingle, _convertValueToObjectID, | ||
var AdapterBase, MongoDBAdapter, ObjectID, async, error, mongodb, tableize, types, _, _buildGroupFields, _buildWhere, _buildWhereSingle, _convertValueToObjectID, _objectIdToString, | ||
__hasProp = {}.hasOwnProperty, | ||
@@ -28,12 +28,135 @@ __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; }; | ||
_convertValueToObjectID = function(value, key) { | ||
var e; | ||
var len, number, oid; | ||
try { | ||
return new ObjectID(value); | ||
} catch (_error) { | ||
e = _error; | ||
throw new Error("'" + key + "' is not a valid id"); | ||
if (value != null) { | ||
len = value.length; | ||
if (len === 24) { | ||
oid = ''; | ||
number = parseInt(value.substr(0, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
number = parseInt(value.substr(2, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
number = parseInt(value.substr(4, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
number = parseInt(value.substr(6, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
number = parseInt(value.substr(8, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
number = parseInt(value.substr(10, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
number = parseInt(value.substr(12, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
number = parseInt(value.substr(14, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
number = parseInt(value.substr(16, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
number = parseInt(value.substr(18, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
number = parseInt(value.substr(20, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
number = parseInt(value.substr(22, 2), 16); | ||
if (number >= 0 && number < 256) { | ||
oid += String.fromCharCode(number); | ||
} | ||
if (oid.length === 12) { | ||
return { | ||
_bsontype: 'ObjectID', | ||
id: oid | ||
}; | ||
} | ||
} | ||
} | ||
throw new Error("'" + key + "' is not a valid id"); | ||
}; | ||
_objectIdToString = function(oid) { | ||
var str, value; | ||
oid = oid.id; | ||
str = ''; | ||
value = oid.charCodeAt(0); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
value = oid.charCodeAt(1); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
value = oid.charCodeAt(2); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
value = oid.charCodeAt(3); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
value = oid.charCodeAt(4); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
value = oid.charCodeAt(5); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
value = oid.charCodeAt(6); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
value = oid.charCodeAt(7); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
value = oid.charCodeAt(8); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
value = oid.charCodeAt(9); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
value = oid.charCodeAt(10); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
value = oid.charCodeAt(11); | ||
if (value < 16) { | ||
str += '0'; | ||
} | ||
str += value.toString(16); | ||
return str; | ||
}; | ||
_buildWhereSingle = function(property, key, value, not_op) { | ||
@@ -315,15 +438,6 @@ var is_objectid, keys, obj, property_type, sub_key, sub_value; | ||
MongoDBAdapter.prototype.idToDB = function(value) { | ||
var e; | ||
try { | ||
return new ObjectID(value); | ||
} catch (_error) { | ||
e = _error; | ||
throw new Error("'id' is not a valid ID"); | ||
} | ||
return _convertValueToObjectID(value, 'id'); | ||
}; | ||
MongoDBAdapter.prototype.valueToDB = function(value, column, property) { | ||
var e; | ||
if (value == null) { | ||
@@ -333,8 +447,3 @@ return; | ||
if (column === 'id' || property.type === 'objectid') { | ||
try { | ||
return new ObjectID(value); | ||
} catch (_error) { | ||
e = _error; | ||
throw new Error("'" + column + "' is not a valid ID"); | ||
} | ||
return _convertValueToObjectID(value, column); | ||
} | ||
@@ -345,3 +454,3 @@ return value; | ||
MongoDBAdapter.prototype._getModelID = function(data) { | ||
return data._id.toString(); | ||
return _objectIdToString(data._id); | ||
}; | ||
@@ -351,3 +460,3 @@ | ||
if (property.type === 'objectid') { | ||
return value.toString(); | ||
return _objectIdToString(value); | ||
} else { | ||
@@ -358,2 +467,34 @@ return value; | ||
MongoDBAdapter.prototype._refineRawInstance = function(model, data, selected_columns) { | ||
var column, id, value, _i, _j, _len, _len1; | ||
if (!selected_columns) { | ||
selected_columns = Object.keys(this._connection.models[model]._schema); | ||
} | ||
id = _objectIdToString(data._id); | ||
delete data._id; | ||
for (_i = 0, _len = selected_columns.length; _i < _len; _i++) { | ||
column = selected_columns[_i]; | ||
value = data[column]; | ||
if (value instanceof ObjectID) { | ||
data[column] = _objectIdToString(value); | ||
} | ||
} | ||
Object.defineProperty(data, 'id', { | ||
configurable: false, | ||
enumerable: true, | ||
writable: false, | ||
value: id | ||
}); | ||
if (!this._connection.models[model].eliminate_null) { | ||
for (_j = 0, _len1 = selected_columns.length; _j < _len1; _j++) { | ||
column = selected_columns[_j]; | ||
if (!data.hasOwnProperty(column)) { | ||
data[column] = null; | ||
} | ||
} | ||
} | ||
return data; | ||
}; | ||
MongoDBAdapter.prototype.create = function(model, data, callback) { | ||
@@ -381,3 +522,3 @@ return this._collection(model).insert(data, { | ||
} | ||
id = result != null ? (_ref = result[0]) != null ? _ref._id.toString() : void 0 : void 0; | ||
id = _objectIdToString(result != null ? (_ref = result[0]) != null ? _ref._id : void 0 : void 0); | ||
if (id) { | ||
@@ -409,3 +550,3 @@ delete data._id; | ||
id = doc._id.toString(); | ||
id = _objectIdToString(doc._id); | ||
if (id) { | ||
@@ -516,3 +657,3 @@ delete data._id; | ||
try { | ||
id = new ObjectID(id); | ||
id = _convertValueToObjectID(id, 'id'); | ||
} catch (_error) { | ||
@@ -535,3 +676,7 @@ e = _error; | ||
} | ||
return callback(null, _this._convertToModelInstance(model, result, options.select)); | ||
if (options.return_raw_instance) { | ||
return callback(null, _this._refineRawInstance(model, result, options.select)); | ||
} else { | ||
return callback(null, _this._convertToModelInstance(model, result, options.select)); | ||
} | ||
}); | ||
@@ -629,5 +774,11 @@ }; | ||
} | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
if (options.return_raw_instance) { | ||
return callback(null, result.map(function(record) { | ||
return _this._refineRawInstance(model, record, options.select); | ||
})); | ||
} else { | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
} | ||
}); | ||
@@ -634,0 +785,0 @@ }); |
@@ -346,3 +346,7 @@ // Generated by CoffeeScript 1.6.2 | ||
if ((result != null ? result.length : void 0) === 1) { | ||
return callback(null, _this._convertToModelInstance(model, result[0], options.select)); | ||
if (options.return_raw_instance) { | ||
return callback(null, _this._refineRawInstance(model, result[0], options.select)); | ||
} else { | ||
return callback(null, _this._convertToModelInstance(model, result[0], options.select)); | ||
} | ||
} else if ((result != null ? result.length : void 0) > 1) { | ||
@@ -408,5 +412,11 @@ return callback(new Error('unknown error')); | ||
} else { | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
if (options.return_raw_instance) { | ||
return callback(null, result.map(function(record) { | ||
return _this._refineRawInstance(model, record, options.select); | ||
})); | ||
} else { | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
} | ||
} | ||
@@ -413,0 +423,0 @@ }); |
@@ -346,3 +346,7 @@ // Generated by CoffeeScript 1.6.2 | ||
if ((rows != null ? rows.length : void 0) === 1) { | ||
return callback(null, _this._convertToModelInstance(model, rows[0], options.select)); | ||
if (options.return_raw_instance) { | ||
return callback(null, _this._refineRawInstance(model, rows[0], options.select)); | ||
} else { | ||
return callback(null, _this._convertToModelInstance(model, rows[0], options.select)); | ||
} | ||
} else if ((rows != null ? rows.length : void 0) > 1) { | ||
@@ -403,5 +407,11 @@ return callback(new Error('unknown error')); | ||
} else { | ||
return callback(null, rows.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
if (options.return_raw_instance) { | ||
return callback(null, rows.map(function(record) { | ||
return _this._refineRawInstance(model, record, options.select); | ||
})); | ||
} else { | ||
return callback(null, rows.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
} | ||
} | ||
@@ -408,0 +418,0 @@ }); |
@@ -201,3 +201,7 @@ // Generated by CoffeeScript 1.6.2 | ||
result.id = id; | ||
return callback(null, _this._convertToModelInstance(model, result, options.select)); | ||
if (options.return_raw_instance) { | ||
return callback(null, _this._refineRawInstance(model, result, options.select)); | ||
} else { | ||
return callback(null, _this._convertToModelInstance(model, result, options.select)); | ||
} | ||
} else { | ||
@@ -226,5 +230,11 @@ return callback(new Error('not found')); | ||
}); | ||
return callback(null, records.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
if (options.return_raw_instance) { | ||
return callback(null, records.map(function(record) { | ||
return _this._refineRawInstance(model, record, options.select); | ||
})); | ||
} else { | ||
return callback(null, records.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
} | ||
}); | ||
@@ -231,0 +241,0 @@ }); |
@@ -177,2 +177,22 @@ // Generated by CoffeeScript 1.6.2 | ||
SQLAdapterBase.prototype._refineRawInstance = function(model, data, selected_columns) { | ||
var column, id; | ||
id = this._getModelID(data); | ||
Object.defineProperty(data, 'id', { | ||
configurable: false, | ||
enumerable: true, | ||
writable: false, | ||
value: id | ||
}); | ||
if (this._connection.models[model].eliminate_null) { | ||
for (column in data) { | ||
if (data[column] == null) { | ||
delete data[column]; | ||
} | ||
} | ||
} | ||
return data; | ||
}; | ||
return SQLAdapterBase; | ||
@@ -179,0 +199,0 @@ |
@@ -288,3 +288,7 @@ // Generated by CoffeeScript 1.6.2 | ||
if ((result != null ? result.length : void 0) === 1) { | ||
return callback(null, _this._convertToModelInstance(model, result[0], options.select)); | ||
if (options.return_raw_instance) { | ||
return callback(null, _this._refineRawInstance(model, result[0], options.select)); | ||
} else { | ||
return callback(null, _this._convertToModelInstance(model, result[0], options.select)); | ||
} | ||
} else if ((result != null ? result.length : void 0) > 1) { | ||
@@ -342,5 +346,11 @@ return callback(new Error('unknown error')); | ||
} else { | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
if (options.return_raw_instance) { | ||
return callback(null, result.map(function(record) { | ||
return _this._refineRawInstance(model, record, options.select); | ||
})); | ||
} else { | ||
return callback(null, result.map(function(record) { | ||
return _this._convertToModelInstance(model, record, options.select); | ||
})); | ||
} | ||
} | ||
@@ -347,0 +357,0 @@ }); |
// Generated by CoffeeScript 1.6.2 | ||
(function() { | ||
var Connection, EventEmitter, Model, async, _, _bindDomain, _use, | ||
var Connection, EventEmitter, Model, async, redis, _, _bindDomain, _use, | ||
__hasProp = {}.hasOwnProperty, | ||
@@ -15,2 +15,6 @@ __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; }; | ||
try { | ||
redis = require('redis'); | ||
} catch (_error) {} | ||
_bindDomain = function(fn) { | ||
@@ -30,3 +34,4 @@ var d; | ||
function Connection(adapter_name, settings) { | ||
var _this = this; | ||
var redis_cache, | ||
_this = this; | ||
@@ -36,2 +41,7 @@ if (settings.is_default !== false) { | ||
} | ||
redis_cache = settings.redis_cache || {}; | ||
redis_cache.host || (redis_cache.host = '127.0.0.1'); | ||
redis_cache.port || (redis_cache.port = 6379); | ||
redis_cache.database || (redis_cache.database = 0); | ||
this._redis_cache_settings = redis_cache; | ||
this.connected = false; | ||
@@ -157,2 +167,31 @@ this.models = {}; | ||
Connection.prototype._connectRedisCache = function(callback) { | ||
var client, settings, | ||
_this = this; | ||
if (this._connecting_redis_cache) { | ||
this.once('redis_cache_connected', callback); | ||
return; | ||
} | ||
if (this._redis_cache_client) { | ||
return callback(null, this._redis_cache_client); | ||
} else if (!redis) { | ||
throw new Error('cache needs Redis'); | ||
} else { | ||
this._connecting_redis_cache = true; | ||
settings = this._redis_cache_settings; | ||
client = redis.createClient(settings.port || 6379, settings.host || '127.0.0.1'); | ||
return client.on('connect', function() { | ||
return client.select(settings.database || 0, function(error) { | ||
_this._connecting_redis_cache = false; | ||
if (!error) { | ||
_this._redis_cache_client = client; | ||
} | ||
_this.emit('redis_cache_connected', error, client); | ||
return callback(error, client); | ||
}); | ||
}); | ||
} | ||
}; | ||
return Connection; | ||
@@ -159,0 +198,0 @@ |
@@ -519,4 +519,6 @@ // Generated by CoffeeScript 1.6.2 | ||
_use('cache'); | ||
module.exports = Model; | ||
}).call(this); |
@@ -130,13 +130,16 @@ // Generated by CoffeeScript 1.6.2 | ||
Query.prototype.exec = function(options, callback) { | ||
Query.prototype.return_raw_instance = function() { | ||
this._options.return_raw_instance = true; | ||
return this; | ||
}; | ||
Query.prototype.cache = function(options) { | ||
this._options.cache = options; | ||
return this; | ||
}; | ||
Query.prototype._exec = function(options, callback) { | ||
var expected_count, | ||
_this = this; | ||
if (this._model._waitingForReady(this, this.exec, arguments)) { | ||
return; | ||
} | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
if (this._find_single_id && this._conditions.length === 0) { | ||
@@ -208,2 +211,32 @@ if (!(options != null ? options.skip_log : void 0)) { | ||
Query.prototype.exec = function(options, callback) { | ||
var cache_key, cache_options, | ||
_this = this; | ||
if (this._model._waitingForReady(this, this.exec, arguments)) { | ||
return; | ||
} | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
if ((cache_options = this._options.cache) && (cache_key = cache_options.key)) { | ||
return this._model._loadFromCache(cache_key, cache_options.refresh, function(error, records) { | ||
if (!error) { | ||
return callback(null, records); | ||
} | ||
return _this._exec(options, function(error, records) { | ||
if (error) { | ||
return callback(error); | ||
} | ||
return _this._model._saveToCache(cache_key, cache_options.ttl, records, function(error) { | ||
return callback(error, records); | ||
}); | ||
}); | ||
}); | ||
} else { | ||
return this._exec(options, callback); | ||
} | ||
}; | ||
Query.prototype.count = function(callback) { | ||
@@ -210,0 +243,0 @@ if (this._model._waitingForReady(this, this.count, arguments)) { |
{ | ||
"name": "cormo", | ||
"description": "ORM framework for Node.js", | ||
"version": "0.5.5", | ||
"version": "0.5.6", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "orm", |
Sorry, the diff of this file is not supported yet
203458
33
5041