Skip to content

Commit

Permalink
allow overriding the DB implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Jan 16, 2018
1 parent 031ad29 commit 2721ad4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
8 changes: 6 additions & 2 deletions docs/api/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ const app = Server({
// The return value of Game().
game: game,

// The number of players.
numPlayers: 2
// Optional, if you want to hook it up to a
// custom storage backend not supported by
// the framework. DbImpl must implement the
// same interface shown in db.js:
// https://github.com/google/boardgame.io/blob/master/src/server/db.js
db: new DbImpl(),
});

app.listen(8000);
Expand Down
7 changes: 5 additions & 2 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ const Redux = require('redux');
import { InMemory } from './db';
import { createGameReducer } from '../core/reducer';

function Server({ games }) {
function Server({ games, db }) {
const app = new Koa();
const io = new IO();
app.context.io = io;
io.attach(app);

const db = new InMemory();
if (db === undefined) {
db = new InMemory();
}

const clientInfo = new Map();
const roomInfo = new Map();

Expand Down
15 changes: 15 additions & 0 deletions src/server/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,18 @@ test('playerView', () => {
}
});
});

test('custom db implementation', () => {
let getId = null;
class Custom {
get(id) { getId = id }
set() {}
}

const game = Game({});
const server = Server({ games: [game], db: new Custom() });
const io = server.context.io;

io.socket.receive('sync', 'gameID');
expect(getId).toBe('gameID');
});

0 comments on commit 2721ad4

Please sign in to comment.