@fvlab/firebasertdbconfigurationstore
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

FirebaseRTDBConfigurationStore

Simple application settings or document storage using your Firebase project's Realtime Database. Global configurations will be stored under /internal/global/ and user-specific configurations will be stored under /internal/user/<userId>. The paths can be configured by passing optional arguments to the constructor.

NPM Package

Prerequisite

This module can be used within a Firebase project since it can implicitly initialize the Firebase app to access the project's Firebase services. It can also be used in a standalone project, such as with Express.

Usage

Initialization

Before anything can be read or written to the Realtime database the library must be initialized. This is a necessary step to setup the Firebase configuration. There are two types of initialization available.

Initialize with Options

The first type of initialization available allows the library to be initialized with the default Firebase Admin instance. If there are no other Firebase Admin instances being used in the project then this is the quickest, and easiest option available.

It also provides an optional options parameter that allows custom Firebase Admin AppOptions to be used. This is required when using the library outside of Firebase Functions.

public init(options?: admin.AppOptions): this

Initialize with an Existing Firebase Admin Instance

The second type of initialization available allows the library to be initialized with an existing instance of Firebase Admin. The Admin library does not prefer multiple initializations so this allows this library to reuse one that has already been created. This option also works outside of Firebase functions.

public initWithAdminInstance(firebaseAdminInstance: typeof admin): this

Create and initialize the object.

const currentUserId = '1234';
const settings = new FirebaseRTDBConfigurationStore(currentUserId, '/custom/pathToGlobalConfig/', '/custom/pathToUserConfig');
return settings.init();

Retrieve values by key or get it's default value if a value doesn't exist for the key.

return settings.getGlobalData('someKey', 'default value')
.then(globalValue => ...);
return settings.getUserData('someOtherKey', 'default value')
.then(userValue => ...);

Set the key-value pair.

return settings.setGlobalData('someKey', 'some value')
.then(globalValue => ...);
return settings.setUserData('someOtherKey', 'some value')
.then(userValue => ...);

Update the Data Stored in the Specified Path

return settings.updateGlobalData('somePath', 'some value')
.then(globalValue => ...);
return settings.updateGlobalData('someOtherPath', 'some value')
.then(userValue => ...);

Supported operations

  1. Forward-slash separated path

    return settings.setGlobalData('some/other/path', 'some value')
    .then(globalValue => ...);
  2. Object / Array value

    return settings
     .updateGlobalData('somePath', { child1: "value1", child2: 42})
     .then(globalValue => ...);
    return settings
     .setGlobalData('somePath', [4, 2])
     .then(globalValue => ...);
  3. set operations overwrite existing data at the path, update operations do not overwrite

    // Current Config Store
    "apiKeys": {
      "someService": "some-api-key"
    }
    settings.updateGlobalData('apiKeys', { someOtherService: 'some-other-api-key' });
    
    // Expected Config Store
    "apiKeys": {
      "someService": "some-api-key",
      "someOtherService": "some-other-api-key"
    }
    settings.setGlobalData('apiKeys', { anotherService: 'another-api-key' });
    
    // Expected Config Store
    "apiKeys": {
      "anotherService": "another-api-key"
    }

Generate Documentation

Documentation within this project is generated via Typedoc.

npm run docs

Readme

Keywords

none

Package Sidebar

Install

npm i @fvlab/firebasertdbconfigurationstore

Weekly Downloads

0

Version

1.0.7

License

Apache-2.0

Unpacked Size

25.3 kB

Total Files

5

Last publish

Collaborators

  • fvllam
  • smorrisfv