Skip to content

Commit

Permalink
Redis sync with flatten data
Browse files Browse the repository at this point in the history
  • Loading branch information
dawee committed May 10, 2014
1 parent aedc38e commit a086701
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/model/game.js
Expand Up @@ -17,10 +17,13 @@ var Stone = require('./stone');


var Game = module.exports = Mei.Model.extend({ var Game = module.exports = Mei.Model.extend({
defaults: { defaults: {
type: 'game',
turn: 'black', turn: 'black',
goban: new Goban() goban: new Goban()
}, },
schema: { schema: {
id: String,
type: String,
turn: String, turn: String,
goban: Goban goban: Goban
} }
Expand Down
68 changes: 68 additions & 0 deletions lib/sync/redis.js
@@ -0,0 +1,68 @@
/*
* Module dependencies
*/

var _ = require('underscore');
var async = require('async');
var assert = require('assert');
var redis = require("redis");

/*
* Register sync methods
*/

var sync = module.exports = function (method, model, opts) {
if (method === 'create' || method === 'update') sync.save(model, opts);
};

/* Check if model has an id */

sync.checkId = function (data, done) {
done(!data.id? 'Model has no id' : null);
};

/* Check if model has a type */

sync.checkType = function (data, done) {
done(!data.id? 'Model has no type' : null);
};

/* Save all data keys on Redis */

sync.saveKeys = function (data, done) {
var client = redis.createClient();

async.each(
_.keys(data),
function saveKey(key, nextKey) {
var hashKey = data.type + ':' + data.id;
client.hset(hashKey, key, data[key], nextKey);
},
function endClient(err) {
client.end();
done(err);
}
);
};

/* Forward error or success when process end */

sync.end = function (opts, err) {
if (err) {
opts.error(new Error(err));
} else {
opts.success();
}
};

/* Save model */

sync.save = function (model, opts) {
var data = model.flatten();

async.series([
_.bind(sync.checkId, sync, data),
_.bind(sync.checkType, sync, data),
_.bind(sync.saveKeys, sync, data),
], _.bind(sync.end, sync, opts));
};
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -9,7 +9,7 @@
"author": "David Corticchiato", "author": "David Corticchiato",
"license": "BSD", "license": "BSD",
"dependencies": { "dependencies": {
"async": "~0.2.10", "async": "^0.2.10",
"backbone": "^1.1.2", "backbone": "^1.1.2",
"backbone.schema": "^0.4.8", "backbone.schema": "^0.4.8",
"browserify": "^3.32.1", "browserify": "^3.32.1",
Expand All @@ -20,6 +20,7 @@
"flat": "^1.2.1", "flat": "^1.2.1",
"jade": "^1.3.0", "jade": "^1.3.0",
"nconf": "^0.6.9", "nconf": "^0.6.9",
"redis": "^0.10.1",
"request": "~2.34.0", "request": "~2.34.0",
"socket.io": "^0.9.16", "socket.io": "^0.9.16",
"sockjs-client": "^0.1.3", "sockjs-client": "^0.1.3",
Expand Down
21 changes: 21 additions & 0 deletions test/game.test.js
Expand Up @@ -2,6 +2,7 @@ var assert = require('assert');
var _ = require('underscore'); var _ = require('underscore');
var unflatten = require('flat').unflatten; var unflatten = require('flat').unflatten;
var Game = require('../lib/model/game'); var Game = require('../lib/model/game');
var syncRedis = require('../lib/sync/redis');
var fixtures = { var fixtures = {
's19bCenter': require('./fixtures/game-19x19-center-black-group') 's19bCenter': require('./fixtures/game-19x19-center-black-group')
}; };
Expand Down Expand Up @@ -74,4 +75,24 @@ describe('Game', function () {
}); });


}); });

describe('sync.redis', function () {

it('should save flatten data on Redis', function (done) {
var game = new Game(_.extend({
id: 'my-game'
}, fixtures.s19bCenter));
game.sync = syncRedis;
game.save(null, {
success: function () {
done();
},
error: function (model, err) {
throw err;
}
});
});

});

}); });

0 comments on commit a086701

Please sign in to comment.