MCPcopy Index your code
hub / github.com/benwinding/react-admin-firebase

github.com/benwinding/react-admin-firebase @v5.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v5.0.1 ↗ · + Follow
270 symbols 610 edges 80 files 13 documented · 5%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

react-admin-firebase

NPM Version License Downloads/week Github Issues

A firebase data provider for the React-Admin framework. It maps collections from the Firebase database (Firestore) to your react-admin application. It's an npm package!


Features

  • [x] Firestore Dataprovider (details below)
  • [x] Firebase AuthProvider (email, password)
  • [x] Login with: Google, Facebook, Github etc... (Example Here)
  • [x] Forgot password button... (Example Here)
  • [x] Firebase storage upload functionality, with upload monitoring... (Example Here)

Pull requests welcome!!

Firestore Dataprovider Features

  • [x] Dynamic caching of resources
  • [x] All methods implemented; (GET, POST, GET_LIST ect...)
  • [x] Filtering, sorting etc...
  • [x] Ability to manage sub collections through app configuration
  • [x] Ability to use externally initialized firebaseApp instance
  • [x] Override firestore random id by using "id" as a field in the Create part of the resource
  • [x] Upload to the firebase storage bucket using the standard <FileInput /> field
  • [ ] Realtime updates, using ra-realtime
    • Optional watch collection array or dontwatch collection array

Get Started

yarn add react-admin-firebase firebase

or

npm install --save react-admin-firebase firebase

Demos Basic

A simple example based on the React Admin Tutorial.

Prerequisits

  • Create a posts collection in the firebase firestore database
  • Get config credentials using the dashboard

Options

import {
  FirebaseAuthProvider,
  FirebaseDataProvider,
  FirebaseRealTimeSaga
} from 'react-admin-firebase';

const config = {
  apiKey: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  authDomain: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  databaseURL: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  projectId: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  storageBucket: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  messagingSenderId: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
};

// All options are optional
const options = {
  // Use a different root document to set your resource collections, by default it uses the root collections of firestore
  rootRef: 'root-collection/some-doc' | () => 'root-collection/some-doc',
  // Your own, previously initialized firebase app instance
  app: firebaseAppInstance,
  // Enable logging of react-admin-firebase
  logging: true,
  // Resources to watch for realtime updates, will implicitly watch all resources by default, if not set.
  watch: ['posts'],
  // Resources you explicitly dont want realtime updates for
  dontwatch: ['comments'],
  // Authentication persistence, defaults to 'session', options are 'session' | 'local' | 'none'
  persistence: 'session',
  // Disable the metadata; 'createdate', 'lastupdate', 'createdby', 'updatedby'
  disableMeta: false,
  // Have custom metadata field names instead of: 'createdate', 'lastupdate', 'createdby', 'updatedby'
  renameMetaFields: {
    created_at: 'my_created_at', // default: 'createdate'
    created_by: 'my_created_by', // default: 'createdby'
    updated_at: 'my_updated_at', // default: 'lastupdate'
    updated_by: 'my_updated_by', // default: 'updatedby'
  },
  // Prevents document from getting the ID field added as a property
  dontAddIdFieldToDoc: false,
  // Adds 'deleted' meta field for non-destructive deleting functionality
  // NOTE: Hides 'deleted' records from list views unless overridden by filtering for {deleted: true} 
  softDelete: false,
  // Changes meta fields like 'createdby' and 'updatedby' to store user IDs instead of email addresses
  associateUsersById: false,
  // Casing for meta fields like 'createdby' and 'updatedby', defaults to 'lower', options are 'lower' | 'camel' | 'snake' | 'pascal' | 'kebab'
  metaFieldCasing: 'lower',
  // Instead of saving full download url for file, save just relative path and then get download url
  // when getting docs - main use case is handling multiple firebase projects (environments)
  // and moving/copying documents/storage files between them - with relativeFilePaths, download url
  // always point to project own storage
  relativeFilePaths: false, 
  // Add file name to storage path, when set to true the file name is included in the path
  useFileNamesInStorage: false,
  // Use firebase sdk queries for pagination, filtering and sorting
  lazyLoading: {
    enabled: false
  },
  // Logging of all reads performed by app (additional feature, for lazy-loading testing)
  firestoreCostsLogger: {
    enabled: false,
    localStoragePrefix // optional
  },
  // Function to transform documentData before they are written to Firestore
  transformToDb: (resourceName, documentData, documentId) => documentDataTransformed
}

const dataProvider = FirebaseDataProvider(config, options);
const authProvider = FirebaseAuthProvider(config, options);
const firebaseRealtime = FirebaseRealTimeSaga(dataProvider, options);

Data Provider

import * as React from 'react';
import { Admin, Resource } from 'react-admin';

import { PostList, PostShow, PostCreate, PostEdit } from "./posts";
import {
  FirebaseAuthProvider,
  FirebaseDataProvider,
  FirebaseRealTimeSaga
} from 'react-admin-firebase';

const config = {
  apiKey: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  authDomain: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  databaseURL: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  projectId: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  storageBucket: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
  messagingSenderId: "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
};

const options = {};

const dataProvider = FirebaseDataProvider(config, options);
...
      <Admin 
        dataProvider={dataProvider} 
      >
        <Resource name="posts" list={PostList} show={PostShow} create={PostCreate} edit={PostEdit}/>
      </Admin>
...

Auth Provider

Using the FirebaseAuthProvider you can allow authentication in the application.

const dataProvider = FirebaseDataProvider(config);
const authProvider = FirebaseAuthProvider(config);
...
      <Admin 
        dataProvider={dataProvider}
        authProvider={authProvider}
      >
...

Also checkout how to login with: Google, Facebook, Github etc... (Example Here)

And you might want a "Forgot password" button... (Example Here)

Note

To get the currently logged in user run const user = await authProvider.checkAuth(), this will return the firebase user object, or null if there is no currently logged in user.

Realtime Updates!

NOTE: Realtime updates were removed in react-admin v3.x (see https://github.com/marmelab/react-admin/pull/3908). As such, react-admin-firebase v3.x also does not support Realtime Updates. However, much of the original code used for this functionality in react-admin v2.x remains - including the documentation below. For updates on the implementation of realtime please follow these issues: - https://github.com/benwinding/react-admin-firebase/issues/49 - https://github.com/benwinding/react-admin-firebase/issues/57

Get realtime updates from the firebase server instantly on your tables, with minimal overheads, using rxjs observables!

...
import {
  FirebaseRealTimeSaga,
  FirebaseDataProvider
} from 'react-admin-firebase';
...
const dataProvider = FirebaseDataProvider(config);
const firebaseRealtime = FirebaseRealTimeSaga(dataProvider);
...
      <Admin 
        dataProvider={dataProvider} 
        customSagas={[firebaseRealtime]}
      >
...

Realtime Options

Trigger realtime on only some routes using the options object.

...
const dataProvider = FirebaseDataProvider(config);
const options = {
  watch: ['posts', 'comments'],
  dontwatch: ['users']
}
const firebaseRealtime = FirebaseRealTimeSaga(dataProvider, options);
...

Upload Progress

Monitor file upload data using custom React component which listen for following events (CustomEvent):

  • FILE_UPLOAD_WILL_START
  • FILE_UPLOAD_START
  • FILE_UPLOAD_PROGRESS
  • FILE_UPLOAD_PAUSED
  • FILE_UPLOAD_CANCELD
  • FILE_UPLOAD_COMPLETE
  • FILE_SAVED

All events have data passed in details key:

  • fileName: the file anme
  • data: percentage for FILE_UPLOAD_PROGRESS

Events are sent to HTML DOM element with id "eventMonitor". See demo implementation for example at src-demo/src/App.js;

Help Develop react-admin-firebase?

  1. git clone https://github.com/benwinding/react-admin-firebase
  2. yarn
  3. yarn start-demo

Now all local changes in the library source code can be tested immediately in the demo app.

Run tests

To run the tests, either watch for changes or just run all tests.

  • yarn test-watch
  • yarn test

paypal

Extension points exported contracts — how you extend this code

IFirebaseWrapper (Interface)
(no doc) [3 implementers]
src/providers/database/firebase/IFirebaseWrapper.ts
IFirestoreLogger (Interface)
(no doc) [1 implementers]
src/misc/logger/firestore-logger.ts
RAFirebaseOptions (Interface)
(no doc)
src/providers/options.ts
ParsedUpload (Interface)
(no doc)
src/misc/translate-to-firestore.ts
IDataProvider (Interface)
(no doc)
src/providers/DataProvider.ts
ParsedDocRef (Interface)
(no doc)
src/misc/translate-to-firestore.ts
IResource (Interface)
(no doc)
src/providers/database/ResourceManager.ts
ParseResult (Interface)
(no doc)
src/misc/translate-to-firestore.ts

Core symbols most depended-on inside this repo

MakeMockClient
called by 23
tests/integration-tests/utils/test-helpers.ts
dbGetCollection
called by 19
src/providers/database/firebase/IFirebaseWrapper.ts
log
called by 17
src/misc/logger/logger-base.ts
filterArray
called by 11
src/misc/arrayHelpers.ts
translateDocFromFirestore
called by 11
src/misc/translate-from-firestore.ts
getUserLogin
called by 10
src/providers/AuthProvider.ts
TryGetResource
called by 10
src/providers/database/ResourceManager.ts
run
called by 9
src/providers/DataProvider.ts

Shape

Function 125
Method 92
Interface 32
Class 21

Languages

TypeScript100%

Modules by API surface

src/providers/database/firebase/FirebaseWrapper.ts20 symbols
tests/integration-tests/utils/FirebaseWrapperStub.ts18 symbols
src/providers/AuthProvider.ts18 symbols
src/misc/messageTypes.ts18 symbols
src/providers/database/firebase/IFirebaseWrapper.ts17 symbols
src/providers/database/ResourceManager.ts14 symbols
src/providers/DataProvider.ts13 symbols
src/providers/database/FireClient.ts9 symbols
src/misc/logger/logger-base.ts9 symbols
tests/integration-tests/utils/test-helpers.ts8 symbols
src/providers/lazy-loading/paramsToQuery.ts8 symbols
src/misc/logger/firestore-logger.ts8 symbols

For agents

$ claude mcp add react-admin-firebase \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page