Skip to content

Commit

Permalink
Logging in mysql and redis
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Nov 11, 2011
1 parent 7a94f84 commit fc99247
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
29 changes: 21 additions & 8 deletions lib/adapters/mysql.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@ MySQL.prototype.define = function (descr) {
this._models[descr.model.modelName] = descr; this._models[descr.model.modelName] = descr;
}; };


MySQL.prototype.query = function (sql, callback) {
var time = Date.now();
var log = this.log;
this.client.query(sql, function (err, data) {
log(sql, time);
callback(err, data);
});
};

MySQL.prototype.save = function (model, data, callback) { MySQL.prototype.save = function (model, data, callback) {
var sql = 'UPDATE ' + model + ' SET ' + this.toFields(model, data) + var sql = 'UPDATE ' + model + ' SET ' + this.toFields(model, data) +
' WHERE id = ' + data.id; ' WHERE id = ' + data.id;


this.client.query(sql, function (err) { this.query(sql, function (err) {
callback(err); callback(err);
}); });
}; };
Expand All @@ -47,7 +56,7 @@ MySQL.prototype.create = function (model, data, callback) {
} else { } else {
sql += ' VALUES ()'; sql += ' VALUES ()';
} }
this.client.query(sql, function (err, info) { this.query(sql, function (err, info) {
callback(err, info && info.insertId); callback(err, info && info.insertId);
}); });
}; };
Expand Down Expand Up @@ -98,15 +107,15 @@ MySQL.prototype.fromDatabase = function (model, data) {


MySQL.prototype.exists = function (model, id, callback) { MySQL.prototype.exists = function (model, id, callback) {
var sql = 'SELECT 1 FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1'; var sql = 'SELECT 1 FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1';
this.client.query(sql, function (err, data) { this.query(sql, function (err, data) {
if (err) return callback(err); if (err) return callback(err);
callback(null, data.length === 1); callback(null, data.length === 1);
}); });
}; };


MySQL.prototype.find = function find(model, id, callback) { MySQL.prototype.find = function find(model, id, callback) {
var sql = 'SELECT * FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1'; var sql = 'SELECT * FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1';
this.client.query(sql, function (err, data) { this.query(sql, function (err, data) {
if (data && data.length === 1) { if (data && data.length === 1) {
data[0].id = id; data[0].id = id;
} else { } else {
Expand All @@ -118,14 +127,14 @@ MySQL.prototype.find = function find(model, id, callback) {


MySQL.prototype.destroy = function destroy(model, id, callback) { MySQL.prototype.destroy = function destroy(model, id, callback) {
var sql = 'DELETE FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1'; var sql = 'DELETE FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1';
this.client.query(sql, function (err) { this.query(sql, function (err) {
callback(err); callback(err);
}); });
}; };


// TODO: hook up where, order, limit and offset conditions // TODO: hook up where, order, limit and offset conditions
MySQL.prototype.all = function all(model, filter, callback) { MySQL.prototype.all = function all(model, filter, callback) {
this.client.query('SELECT * FROM ' + model, function (err, data) { this.query('SELECT * FROM ' + model, function (err, data) {
if (err) { if (err) {
return callback(err, []); return callback(err, []);
} }
Expand Down Expand Up @@ -158,7 +167,7 @@ function applyFilter(filter) {
} }


MySQL.prototype.destroyAll = function destroyAll(model, callback) { MySQL.prototype.destroyAll = function destroyAll(model, callback) {
this.client.query('DELETE FROM ' + model, function (err) { this.query('DELETE FROM ' + model, function (err) {
if (err) { if (err) {
return callback(err, []); return callback(err, []);
} }
Expand All @@ -167,7 +176,7 @@ MySQL.prototype.destroyAll = function destroyAll(model, callback) {
}; };


MySQL.prototype.count = function count(model, callback) { MySQL.prototype.count = function count(model, callback) {
this.client.query('SELECT count(*) as cnt FROM ' + model, function (err, res) { this.query('SELECT count(*) as cnt FROM ' + model, function (err, res) {
callback(err, err ? null : res[0].cnt); callback(err, err ? null : res[0].cnt);
}); });
}; };
Expand All @@ -177,3 +186,7 @@ MySQL.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
this.save(model, data, cb); this.save(model, data, cb);
}; };


MySQL.prototype.disconnect = function disconnect() {
this.client.end();
};

41 changes: 35 additions & 6 deletions lib/adapters/redis.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ BridgeToRedis.prototype.defineForeignKey = function (model, key, cb) {
}; };


BridgeToRedis.prototype.save = function (model, data, callback) { BridgeToRedis.prototype.save = function (model, data, callback) {
var log = this.logger('HMSET ' + model + ':' + data.id + ' ...');
this.client.hmset(model + ':' + data.id, data, function (err) { this.client.hmset(model + ':' + data.id, data, function (err) {
log();
if (err) return callback(err); if (err) return callback(err);
this.updateIndexes(model, data.id, data, callback); this.updateIndexes(model, data.id, data, callback);
}.bind(this)); }.bind(this));
Expand Down Expand Up @@ -68,7 +70,9 @@ BridgeToRedis.prototype.updateIndexes = function (model, id, data, callback) {
}; };


BridgeToRedis.prototype.create = function (model, data, callback) { BridgeToRedis.prototype.create = function (model, data, callback) {
var log = this.logger('INCR id:' + model);
this.client.incr('id:' + model, function (err, id) { this.client.incr('id:' + model, function (err, id) {
log();
data.id = id; data.id = id;
this.save(model, data, function (err) { this.save(model, data, function (err) {
if (callback) { if (callback) {
Expand All @@ -79,28 +83,34 @@ BridgeToRedis.prototype.create = function (model, data, callback) {
}; };


BridgeToRedis.prototype.exists = function (model, id, callback) { BridgeToRedis.prototype.exists = function (model, id, callback) {
var log = this.logger('EXISTS ' + model + ':' + id);
this.client.exists(model + ':' + id, function (err, exists) { this.client.exists(model + ':' + id, function (err, exists) {
log();
if (callback) { if (callback) {
callback(err, exists); callback(err, exists);
} }
}); });
}; };


BridgeToRedis.prototype.find = function find(model, id, callback) { BridgeToRedis.prototype.find = function find(model, id, callback) {
var t1 = Date.now();
this.client.hgetall(model + ':' + id, function (err, data) { this.client.hgetall(model + ':' + id, function (err, data) {
this.log('HGETALL ' + model + ':' + id, t1);
if (data && data.id) { if (data && data.id) {
data.id = id; data.id = id;
} else { } else {
data = null; data = null;
} }
callback(err, data); callback(err, data);
}); }.bind(this));
}; };


BridgeToRedis.prototype.destroy = function destroy(model, id, callback) { BridgeToRedis.prototype.destroy = function destroy(model, id, callback) {
var t1 = Date.now();
this.client.del(model + ':' + id, function (err) { this.client.del(model + ':' + id, function (err) {
this.log('DEL ' + model + ':' + id, t1);
callback(err); callback(err);
}); }.bind(this));
}; };


BridgeToRedis.prototype.possibleIndexes = function (model, filter) { BridgeToRedis.prototype.possibleIndexes = function (model, filter) {
Expand All @@ -119,23 +129,31 @@ BridgeToRedis.prototype.possibleIndexes = function (model, filter) {
BridgeToRedis.prototype.all = function all(model, filter, callback) { BridgeToRedis.prototype.all = function all(model, filter, callback) {
var ts = Date.now(); var ts = Date.now();
var client = this.client; var client = this.client;
var log = this.log;
var t1 = Date.now();
var cmd;


var indexes = this.possibleIndexes(model, filter); var indexes = this.possibleIndexes(model, filter);
if (indexes.length) { if (indexes.length) {
cmd = 'SINTER "' + indexes.join('" "') + '"';
indexes.push(handleKeys); indexes.push(handleKeys);
client.sinter.apply(client, indexes); client.sinter.apply(client, indexes);
} else { } else {
cmd = 'KEYS ' + model + ':*';
client.keys(model + ':*', handleKeys); client.keys(model + ':*', handleKeys);
} }


function handleKeys(err, keys) { function handleKeys(err, keys) {
log(cmd, t1);
var t2 = Date.now();
if (err) { if (err) {
return callback(err, []); return callback(err, []);
} }
var query = keys.map(function (key) { var query = keys.map(function (key) {
return ['hgetall', key]; return ['hgetall', key];
}); });
client.multi(query).exec(function (err, replies) { client.multi(query).exec(function (err, replies) {
log(query, t2);
// console.log('Redis time: %dms', Date.now() - ts); // console.log('Redis time: %dms', Date.now() - ts);
callback(err, filter ? replies.filter(applyFilter(filter)) : replies); callback(err, filter ? replies.filter(applyFilter(filter)) : replies);
}); });
Expand Down Expand Up @@ -167,32 +185,43 @@ function applyFilter(filter) {
} }


BridgeToRedis.prototype.destroyAll = function destroyAll(model, callback) { BridgeToRedis.prototype.destroyAll = function destroyAll(model, callback) {
this.client.keys(model + ':*', function (err, keys) { var keysQuery = model + ':*';
var t1 = Date.now();
this.client.keys(keysQuery, function (err, keys) {
this.log('KEYS ' + keysQuery, t1);
if (err) { if (err) {
return callback(err, []); return callback(err, []);
} }
var query = keys.map(function (key) { var query = keys.map(function (key) {
return ['del', key]; return ['del', key];
}); });
var t2 = Date.now();
this.client.multi(query).exec(function (err, replies) { this.client.multi(query).exec(function (err, replies) {
this.log(query, t2);
callback(err); callback(err);
}); }.bind(this));
}.bind(this)); }.bind(this));
}; };


BridgeToRedis.prototype.count = function count(model, callback) { BridgeToRedis.prototype.count = function count(model, callback) {
this.client.keys(model + ':*', function (err, keys) { var keysQuery = model + ':*';
var t1 = Date.now();
this.client.keys(keysQuery, function (err, keys) {
this.log('KEYS ' + keysQuery, t1);
callback(err, err ? null : keys.length); callback(err, err ? null : keys.length);
}); }.bind(this));
}; };


BridgeToRedis.prototype.updateAttributes = function updateAttrs(model, id, data, cb) { BridgeToRedis.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
var t1 = Date.now();
this.client.hmset(model + ':' + id, data, function () { this.client.hmset(model + ':' + id, data, function () {
this.log('HMSET ' + model + ':' + id, t1);
this.updateIndexes(model, id, data, cb); this.updateIndexes(model, id, data, cb);
}.bind(this)); }.bind(this));
}; };


BridgeToRedis.prototype.disconnect = function disconnect() { BridgeToRedis.prototype.disconnect = function disconnect() {
this.log('QUIT', Date.now());
this.client.quit(); this.client.quit();
}; };


16 changes: 16 additions & 0 deletions lib/schema.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var slice = Array.prototype.slice;
* establish connection (of course it depends on specific adapter) * establish connection (of course it depends on specific adapter)
*/ */
function Schema(name, settings) { function Schema(name, settings) {
var schema = this;
// just save everything we get // just save everything we get
this.name = name; this.name = name;
this.settings = settings; this.settings = settings;
Expand Down Expand Up @@ -54,6 +55,18 @@ function Schema(name, settings) {
if (!this.adapter) { if (!this.adapter) {
throw new Error('Adapter is not defined correctly: it should create `adapter` member of schema'); throw new Error('Adapter is not defined correctly: it should create `adapter` member of schema');
} }

this.adapter.log = function (query, start) {
schema.log(query, start);
};

this.adapter.logger = function (query) {
var t1 = Date.now();
var log = this.log;
return function (q) {
log(q || query, t1);
};
};
}; };


util.inherits(Schema, process.EventEmitter); util.inherits(Schema, process.EventEmitter);
Expand All @@ -71,6 +84,9 @@ Schema.prototype.automigrate = function (cb) {
} }
}; };


Schema.prototype.log = function () {
};

Schema.prototype.freeze = function freeze() { Schema.prototype.freeze = function freeze() {
if (this.adapter.freezeSchema) { if (this.adapter.freezeSchema) {
this.adapter.freezeSchema(); this.adapter.freezeSchema();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Anatoliy Chakkaev", "author": "Anatoliy Chakkaev",
"name": "jugglingdb", "name": "jugglingdb",
"description": "ORM for every database: redis, mysql, neo4j, mongodb", "description": "ORM for every database: redis, mysql, neo4j, mongodb",
"version": "0.0.4", "version": "0.0.5",
"repository": { "repository": {
"url": "https://github.com/1602/jugglingdb" "url": "https://github.com/1602/jugglingdb"
}, },
Expand Down

0 comments on commit fc99247

Please sign in to comment.