Skip to content

Commit 2721ad4

Browse files
committed
allow overriding the DB implementation
1 parent 031ad29 commit 2721ad4

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

docs/api/Server.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ const app = Server({
2626
// The return value of Game().
2727
game: game,
2828

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

3337
app.listen(8000);

src/server/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ const Redux = require('redux');
1212
import { InMemory } from './db';
1313
import { createGameReducer } from '../core/reducer';
1414

15-
function Server({ games }) {
15+
function Server({ games, db }) {
1616
const app = new Koa();
1717
const io = new IO();
1818
app.context.io = io;
1919
io.attach(app);
2020

21-
const db = new InMemory();
21+
if (db === undefined) {
22+
db = new InMemory();
23+
}
24+
2225
const clientInfo = new Map();
2326
const roomInfo = new Map();
2427

src/server/index.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,18 @@ test('playerView', () => {
155155
}
156156
});
157157
});
158+
159+
test('custom db implementation', () => {
160+
let getId = null;
161+
class Custom {
162+
get(id) { getId = id }
163+
set() {}
164+
}
165+
166+
const game = Game({});
167+
const server = Server({ games: [game], db: new Custom() });
168+
const io = server.context.io;
169+
170+
io.socket.receive('sync', 'gameID');
171+
expect(getId).toBe('gameID');
172+
});

0 commit comments

Comments
 (0)