bgio-firebase
v0.5.0
Published
🔌 A Firebase database connector for boardgame.io
Downloads
9
Readme
bgio-firebase
🔌 A Firestore database connector for boardgame.io
This package provides a database connector that allows you to use a Firebase Cloud Firestore instance to store boardgame.io metadata and game state.
Installation
npm install --save bgio-firebase
Usage
This example shows one way to use the database connector when running your server on most Google infrastructure. For more details on configuring Firebase on your server, see the Firebase documentation.
const admin = require('firebase-admin');
const { Server } = require('boardgame.io/server');
const { Firestore } = require('bgio-firebase');
const { MyGame } = require('./game');
const database = new Firestore({
config: {
credential: admin.credential.applicationDefault(),
databaseURL: 'https://<MY-PROJECT>.firebaseio.com',
},
});
const server = Server({
games: [MyGame],
db: database,
});
server.run(8000);
Options
The Firestore
class can be configured with an options object with the following properties.
app
- type:
string
If you are using multiple Firebase apps on your server, pass in the name of the app the Firestore connector should use.
config
- type:
admin.AppOptions
An options object to pass to the Firebase Admin SDK's initializeApp
method. This configures your connection with Firebase. See the Firebase docs for details.
dbPrefix
- type:
string
- default:
'bgio_'
Prefix for the boardgame.io collections within your Firebase project.
ignoreUndefinedProperties
- type:
boolean
- default:
true
By default, the Firestore instance’s settings
method is called
internally to avoid errors from undefined
values in data from boardgame.io.
settings
can only be called once, so if you want to call it with your own
custom options, you can pass false
here to disable the internal call.
useCompositeIndexes
- type:
boolean
- default:
false
This connector tries to be as efficient as possible with minimal set-up, but due to Firestore’s query limitations, it cannot combine a date range query with other queries when listing matches without a composite index. By default, it will fall back to less efficient server-side filtering in these cases, potentially resulting in more database reads than necessary. You may wish to enable composite indexes if you use the boardgame.io Lobby API’s updatedAfter
or updatedBefore
queries when listing matches. See the Firestore docs for details on managing indexes.
Database structure
collection ID | document ID | contents
-------------------------|-------------|-------------------
{dbPrefix}metadata
| {matchID}
| match metadata
{dbPrefix}state
| {matchID}
| game state
{dbPrefix}initialState
| {matchID}
| initial game state
{dbPrefix}log
| {matchID}
| game action logs
Contributing
Bug reports and pull requests are very welcome! I’m not a database expert or a Firebase expert or any kind of expert, so it’s very possible that there could be improvements to how the connector interfaces with Firebase. If you run into any problems or have questions, please open an issue.
Please also note the code of conduct and be kind to each other.
License
The code in this repository is provided under the MIT License.