Giter Site home page Giter Site logo

bgio-firebase's Introduction

bgio-firebase

NPM Version Test CI Status Coverage Status

🔌 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.

bgio-firebase's People

Contributors

delucis avatar dependabot-preview[bot] avatar dependabot[bot] avatar stefan-hanke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sort2f

bgio-firebase's Issues

Cannot use "undefined" as a Firestore value

Hello, I was trying to setup Firestore as database with my bgio project.

I got this error:

Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "unlisted").

It happened when calling setMetadata, arguments look like this:

setMetadata gameID: n_lATufQM metadata: { gameName: 'myGame',
  unlisted: undefined,
  players:
   { '0': { id: 0 },
     '1': { id: 1 },
     '2': { id: 2 },
     '3': { id: 3 },
     '4': { id: 4 },
     '5': { id: 5 },
     '6': { id: 6 },
     '7': { id: 7 },
     '8': { id: 8 },
     '9': { id: 9 },
     '10': { id: 10 } },
  setupData: undefined }

There are actually 2 undefined values in metadata - unlisted and setupData. I don't know if I am doing something wrong in server setup of boardgame.io or if its normal to have these as undefined.

I did simple check for undefined values and deleted them.

Object.keys(metadata).map((key) => {
    if (typeof metadata[key] === "undefined") {
        delete metadata[key]
    }
})

Then everything seems working fine.

Should I make PR with fix like this?

"undefined" as a Firestore value found in field "action.payload.playerID"

I basically running the tutorial and am having a little issue with sending payloads to firebase, it says I could turn on ignoreUndefinedProperties is there a way to set that setting?

I tried setting it with:

const database = new Firestore({
  config: {
    credential: admin.credential.applicationDefault(),
    databaseURL: 'https://xxx.firebaseio.com',
  },
});
database.db.settings({...database.db.settings, ignoreUndefinedProperties: true })

But that results in a (node:82194) UnhandledPromiseRejectionWarning: Error: 5 NOT_FOUND: No document to update: projects/boardgameio/databases/(default)/documents/bgio_log/default error.

Let me know if you need anymore information, thanks for the awesome package!

(node:42090) UnhandledPromiseRejectionWarning: Error: Element at index 1 is not a valid array element. Cannot use "undefined" as a Firestore value (found in field "action.payload.playerID"). If you want to ignore undefined values, enable `ignoreUndefinedProperties`.
    at validateUserInput (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/serializer.js:269:19)
    at validateUserInput (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/serializer.js:260:13)
    at validateUserInput (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/serializer.js:260:13)
    at Object.validateUserInput (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/serializer.js:260:13)
    at validateArrayElement (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/field-value.js:428:18)
    at ArrayUnionTransform.validate (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/field-value.js:353:13)
    at /Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/document.js:783:56
    at Map.forEach (<anonymous>)
    at DocumentTransform.validate (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/document.js:783:25)
    at WriteBatch.update (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/write-batch.js:379:19)
    at process.emitWarning (internal/process/warning.js:132:11)
    at process.<anonymous> (/Users/fzf/Projects/game/node_modules/esm/esm.js:1:289642)
    at process.<anonymous> (/Users/fzf/Projects/game/node_modules/esm/esm.js:1:289518)
    at process.<anonymous> (/Users/fzf/Projects/game/node_modules/esm/esm.js:1:284879)
    at Object.apply (/Users/fzf/Projects/game/node_modules/esm/esm.js:1:199341)
    at emitUnhandledRejectionWarning (internal/process/promises.js:149:15)
    at processPromiseRejections (internal/process/promises.js:211:11)
    at processTicksAndRejections (internal/process/task_queues.js:98:32)
(node:42090) Error: Element at index 1 is not a valid array element. Cannot use "undefined" as a Firestore value (found in field "action.payload.playerID"). If you want to ignore undefined values, enable `ignoreUndefinedProperties`.
    at validateUserInput (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/serializer.js:269:19)
    at validateUserInput (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/serializer.js:260:13)
    at validateUserInput (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/serializer.js:260:13)
    at Object.validateUserInput (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/serializer.js:260:13)
    at validateArrayElement (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/field-value.js:428:18)
    at ArrayUnionTransform.validate (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/field-value.js:353:13)
    at /Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/document.js:783:56
    at Map.forEach (<anonymous>)
    at DocumentTransform.validate (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/document.js:783:25)
    at WriteBatch.update (/Users/fzf/Projects/game/node_modules/@google-cloud/firestore/build/src/write-batch.js:379:19)
(node:42090) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    at process.emitWarning (internal/process/warning.js:132:11)
    at process.<anonymous> (/Users/fzf/Projects/game/node_modules/esm/esm.js:1:289642)
    at process.<anonymous> (/Users/fzf/Projects/game/node_modules/esm/esm.js:1:289518)
    at process.<anonymous> (/Users/fzf/Projects/game/node_modules/esm/esm.js:1:284879)
    at Object.apply (/Users/fzf/Projects/game/node_modules/esm/esm.js:1:199341)
    at emitDeprecationWarning (internal/process/promises.js:161:11)
    at processPromiseRejections (internal/process/promises.js:213:13)
    at processTicksAndRejections (internal/process/task_queues.js:98:32)
^C

Can we use Firestore for transport?

Good evening,

I'm using this plugin and it's working beautifully, thanks a lot for that.
I was wondering if it would be feasible to use Firestore real-time capabilities as a replacement for Socket-IO and use it as the game transport. Do you think it's possible? If so, what files do you recommend for me to tinker?

Thanks again!

Best,

juan francisco

Upgrade husky

This repository currently uses husky@^4 to run some checks on git hooks. husky@^6 is now released but requires some updated configuration to work.

Server crashes when creating a new match

If I add this db to my server the app crashes when creating new matches with Error: 3 INVALID_ARGUMENT: Cannot convert an array value in an array value.

> node -r esm src/server.js

INFO: App serving on 8000...

  Error: 3 INVALID_ARGUMENT: Cannot convert an array value in an array value.
      at Object.callErrorFromStatus (/home/*/node_modules/@grpc/grpc-js/build/src/call.js:31:26)
      at Object.onReceiveStatus (/home/*/node_modules/@grpc/grpc-js/build/src/client.js:176:52)
      at Object.onReceiveStatus (/home/*/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:342:141)
      at Object.onReceiveStatus (/home/*/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:305:181)
      at process.nextTick (/home/*/node_modules/@grpc/grpc-js/build/src/call-stream.js:124:78)
      at process._tickCallback (internal/process/next_tick.js:61:11)
  Caused by: Error
      at WriteBatch.commit (/home/*/node_modules/@google-cloud/firestore/build/src/write-batch.js:420:23)
      at Firestore.createMatch (/home/*/node_modules/bgio-firebase/dist/firestore.js:50:14)
      at CreateMatch (/home/*/node_modules/boardgame.io/dist/cjs/server.js:1971:14)
      at router.post (/home/*/node_modules/boardgame.io/dist/cjs/server.js:2027:31)
      at dispatch (/home/*/node_modules/koa-router/node_modules/koa-compose/index.js:44:32)
      at next (/home/*/node_modules/koa-router/node_modules/koa-compose/index.js:45:18)
      at /home/*/node_modules/koa-body/index.js:148:14
      at process._tickCallback (internal/process/next_tick.js:68:7)


Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.