Skip to content

🔥Firebase Realtime-Database client for Cloudflare Workers.

License

Notifications You must be signed in to change notification settings

Sharks-Interactive/workers-firebase-rtdb-rest-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm version npm downloads npm types

Workers Firebase RTDB Client

Workers Firebase RTDB is a Firebase Realtime Database client library for use specifically with Cloudflare Workers written in TypeScript.

Functionality

  • Easily create, edit, update, and delete json from your database
  • Easy authentication with your database and its rules
  • Supports conditional requests/ETag's
  • Follows Workers guidelines, such as not mutating global state
  • Thorough documentation and easy to understand functions

Usage:

npm i --save @sharks-interactive/workers-firebase-rtdb-rest-client

Quick Examples:

Writing Data

import Database from '@sharks-interactive/workers-firebase-rtdb';

addEventListener('fetch', (event) => {
  const db = new Database({
    databaseUrl: 'https://example-db-default-rtdb.firebaseio.com/',
    authentication: 'bearer ya29.[YOUR-OAUTH-TOKEN-HERE]',
    tokenAuthentication: true,
  });
  event.respondWith(async () => {
    const success = await db.update(
        'user/settings',
        JSON.stringify({theme: 'dark'}),
        true,
        new Headers(), // Optional
    );

    if (success) return new Response('Ok', {status: 200, statusText: 'OK'});
    else return new Response('Something went wrong', {status: 500, statusText: 'Internal Server Error'});
  });
});

Reading Data

import Database from '@sharks-interactive/workers-firebase-rtdb';

addEventListener('fetch', (event) => {
  const db = new Database({
    databaseUrl: 'https://example-db-default-rtdb.firebaseio.com/',
    authentication: 'bearer ya29.[YOUR-OAUTH-TOKEN-HERE]',
    tokenAuthentication: true,
  });
  event.respondWith(async () => {
    const response = await db.read(
        'user/settings',
        true,
        new Headers(), // Optional
    );

    if (response != '') return new Response(response, {status: 200, statusText: 'OK'});
    else return new Response('Something went wrong', {status: 500, statusText: 'Internal Server Error'});
  });
});

Features:

  • .update() updates data with a PATCH request
  • .push() pushes data with a POST request
  • .write() writes data with a PUT request
  • .read() reads data with a GET request
  • .delete() deletes data with a DELETE request
  • .appendGetEtagHeader() appends to a list of headers the header required to get the ETag of data in the database
  • .appendEtagIfHeader() appends to a list of headers the header required to submit a conditional ETag request to the database
  • tokenAuthentication supports OAUTH token authentication as well as Firebase ID authentication

Options (Required)

Option Type Description
databaseUrl string A string containing the base URL of your database. It SHOULD end in a / and it MUST start with https://. See: https://firebase.google.com/docs/database/rest/start
authentication string Your authentication information. This should be a OAUTH token if tokenAuthentication is true, and an ID token if if is false. See: https://firebase.google.com/docs/database/rest/auth
tokenAuthentication boolean Whether the authentication string is an OAUTH token or Firebase ID. See: https://firebase.google.com/docs/database/rest/auth

How it works:

This client library is a simplified layer between your code and the Firebase REST API. In the background it uses the Workers FETCH API to send HTTP requests to your Database.

Read the wiki for extra documentation.

Project created and maintained by Sharks Interactive.

Developing:

  • Commit to staging and pr to prod for changes

Code Style:

  • Continious Integration will handle formatting for you
  • Use ESLINT locally to catch errors pre-pr

Acknowledgements:

README.MD and general SDK structure, styling, practices etc, modelled after and taken from the excellent Toucan-JS