Browse files

Redis sync with flatten data

  • Loading branch information...
1 parent aedc38e commit a086701c1cba9f000bd80d097779d1e517f7262b @dawee committed May 10, 2014
Showing with 94 additions and 1 deletion.
  1. +3 −0 lib/model/game.js
  2. +68 −0 lib/sync/redis.js
  3. +2 −1 package.json
  4. +21 −0 test/game.test.js
View
3 lib/model/game.js
@@ -17,10 +17,13 @@ var Stone = require('./stone');
var Game = module.exports = Mei.Model.extend({
defaults: {
+ type: 'game',
turn: 'black',
goban: new Goban()
},
schema: {
+ id: String,
+ type: String,
turn: String,
goban: Goban
}
View
68 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));
+};
View
3 package.json
@@ -9,7 +9,7 @@
"author": "David Corticchiato",
"license": "BSD",
"dependencies": {
- "async": "~0.2.10",
+ "async": "^0.2.10",
"backbone": "^1.1.2",
"backbone.schema": "^0.4.8",
"browserify": "^3.32.1",
@@ -20,6 +20,7 @@
"flat": "^1.2.1",
"jade": "^1.3.0",
"nconf": "^0.6.9",
+ "redis": "^0.10.1",
"request": "~2.34.0",
"socket.io": "^0.9.16",
"sockjs-client": "^0.1.3",
View
21 test/game.test.js
@@ -2,6 +2,7 @@ var assert = require('assert');
var _ = require('underscore');
var unflatten = require('flat').unflatten;
var Game = require('../lib/model/game');
+var syncRedis = require('../lib/sync/redis');
var fixtures = {
's19bCenter': require('./fixtures/game-19x19-center-black-group')
};
@@ -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.