Database

database.read(pathOrRef)
Generator

Returns the data at this path in firebase's database.

Arguments

TypeDescription
pathOrRef
Optional

String or Firebase Database Reference

The path or reference to the value to read.

Output

Whatever value is store at this path in the database (number, string, object, etc).

Example

function* getTodo() {
  const firstTodo = yield call(rsf.database.read, 'todos/1');
  yield put(gotTodo(firstTodo));
}

database.create(pathOrRef, data)
Generator

Create a new path in the database and stores the data there.

Arguments

TypeDescription
pathOrRef
Optional

String or Firebase Database Reference

The path or reference to the destination.

data
Optional

Any value

The value to store.

Output

The key newly created (a string).

Example

function* addTodo() {
  const key = yield call(rsf.database.create, 'todos', {
    done: false,
    label: 'Do this',
  });
  // `key` is something like "-Kfn7EyLEoHax0YGoQr0"
}

database.update(pathOrRef, data)
Generator

Replace the value store at path in the database with data.

Arguments

TypeDescription
pathOrRef
Optional

String or Firebase Database Reference

The path or reference to the value to update.

data
Optional

Any value

The value to store.

Example

function* updateTodo() {
  yield call(rsf.database.update, 'todos/-Kfn7EyLEoHax0YGoQr0', {
    done: true, // yay, it's done now!
    label: 'Do this',
  });
}

database.patch(pathOrRef, data)
Generator

Patches the value store at path in the database with data. Like database.update but doesn't remove unmentionned keys.

Arguments

TypeDescription
pathOrRef
Optional

String or Firebase Database Reference

The path or reference to the value to update.

data
Optional

Any value

The value to store.

Example

function* updateTodo() {
  // With this call, no need to re-send the todo label.
  yield call(rsf.database.patch, 'todos/-Kfn7EyLEoHax0YGoQr0', {
    done: true,
  });
}

database.delete(pathOrRef)
Generator

Removes the value at the specified path in the database.

Arguments

TypeDescription
pathOrRef
Optional

String or Firebase Database Reference

The path or reference to the value to delete.

Example

function* deleteTodo() {
  yield call(rsf.database.delete, 'todos/-Kfn7EyLEoHax0YGoQr0');
}

database.channel(pathOrRef, event, buffer)
Function

Returns a redux-saga Channel which emits every change at the specified path in the database.

Arguments

TypeDescription
pathOrRef
Optional

String or Firebase Database Reference

The path or reference to the value to read.

event
Optional

String

Defaults to value. A string describing the type of event to listen for. Options includes value, child_added, child_removed, child_changed and child_moved. See Reference.on documentation for more information.

buffer
Optional

A Buffer

Defaults to buffers.none(). Optional Buffer object to buffer messages on this channel. If not provided, messages will not buffered on this channel. See redux-saga documentation for more information for what options are available.

Output

A redux-saga Channel which emits every change at the specified path in the database. The emitted value is an object with two keys:

Example

function* syncTodosSaga() {
  const channel = yield call(rsf.database.channel, 'todos');

  while(true) {
    const { value: todos } = yield take(channel);
    yield put(syncTodos(todos));
  }
}

database.sync(pathOrRef, options, event)
Generator

Automatically dispatches a redux action every time path changes.

Arguments

TypeDescription
pathOrRef
Optional

String or Firebase Database Reference

The path or reference to the value to synced.

options
Optional

Object

An object to configure how the database should be synchronised. It must contain at least the successActionCreator which must take a single argument being the value read from the firebase reference. The other possible options are failureActionCreator which is called on channel errors and transform which is an optional transformer function to be applied to the value before it's passed to the action creator. When failureActionCreator is not specified, errors are printed to the console. transform defaults to the identity function (x => x).

event
Optional

String

One of the following strings: value, child_added, child_changed, child_removed, or child_moved. Defaults to value. More details on the Reference.on doc.

Example

import { syncTodos } from '../actionCreators/todos';

function* todoRootSaga() {
  yield fork(
    rsf.database.sync,
    'todos',
    { successActionCreator: syncTodos }
  );
}