Application Data Management
1. Overview
Connecting to additonal databases can be done by creating an additional dataprovider for your database if it doesn't exist or using an existing one, example - Hasura can be connected to PSQL and has a dataprovider. Dataproviders can be built easily and for an example we have built one for FusionAuth which you can check out here.
For a detailed view on how to build a dataprovider go to next section.
2. Terminology and Setup
2.1 Data Providers
Whenever react-admin needs to communicate with the API, it calls methods on the Data Provider object.
It's the Data Provider's job to turns these method calls into HTTP requests, and transform the HTTP responses to the data format expected by react-admin. In technical terms, a Data Provider is an adapter for an API.
And to inject a Data Provider in a react-admin application, pass it as the dataProvider
prop of the <Admin>
component, as follows:
Thanks to this adapter injection system, react-admin can communicate with any API, whether it uses REST, GraphQL, RPC, or even SOAP, regardless of the dialect it uses. The Data Provider is also the ideal place to add custom HTTP headers, authentication, etc.
A Data Provider must have the following methods:
You can find an example Data Provider implementation at the end of this chapter.
Tip: In react-admin v2, Data Providers used to be functions, not objects. React-admin v3 can detect a legacy Data Provider and wrap an object around it. So Data Providers developed for react-admin v2 still work with react-admin v3.
2.2 Available Providers
The react-admin project includes 4 Data Providers:
- Simple REST: marmelab/ra-data-simple-rest (read more below). It serves mostly as an example. Incidentally, it is compatible with the FakeRest API.
- JSON server: marmelab/ra-data-json-server. Great for prototyping an admin over a yet-to-be-developed REST API.
- Graphcool: marmelab/ra-data-graphcool. A provider for GraphQL servers following the Graphcool convention. Incidentally, this package builds up on marmelab/ra-data-graphql, which lets you develop providers for other GraphQL conventions.
- Local JSON: marmelab/ra-data-fakerest. Based on a local object, it doesn't even use HTTP. Use it for testing purposes.
Developers from the react-admin community have open-sourced Data Providers for many more backends:
- Django Rest Framework: synaptic-cl/ra-data-drf
- Express & Sequelize: express-sequelize-crud
- Feathersjs: josx/ra-data-feathers
- Firebase: aymendhaya/ra-data-firebase-client.
- Firestore: rafalzawadzki/ra-data-firestore-client.
- GraphCool: marmelab/ra-data-graphcool (uses Apollo)
- GraphQL: marmelab/ra-data-graphql (uses Apollo)
- HAL: b-social/ra-data-hal
- Hasura: hasura/ra-data-hasura
- Hydra / JSON-LD: api-platform/admin/hydra
- IndexedDB: tykoth/ra-data-dexie
- JSON API: henvo/ra-jsonapi-client
- JSON HAL: ra-data-json-hal
- JSON server: marmelab/ra-data-json-server.
- Loopback: darthwesker/react-admin-loopback
- Moleculer Microservices: RancaguaInnova/moleculer-data-provider
- NestJS CRUD: FusionWorks/react-admin-nestjsx-crud-dataprovider
- Parse: almahdi/ra-data-parse
- PostgREST: raphiniert-com/ra-data-postgrest
- Prisma: weakky/ra-data-prisma
- OpenCRUD: weakky/ra-data-opencrud
- REST-HAPI: ra-data-rest-hapi
- Sails.js: mpampin/ra-data-json-sails
- Spring Boot: vishpat/ra-data-springboot-rest
- Strapi: nazirov91/ra-strapi-rest
If you've written a Data Provider for another backend, and open-sourced it, please help complete this list with your package.
Tip: In version 1, react-admin was called admin-on-rest (AOR), and developers shared Data Providers for even more backends. Due to breaking changes in v2, these providers are no longer working. Fortunately, Data Providers aren't complex pieces of code, and using legacy Data Provider with a recent react-admin version requires minimal changes. If you are a maintainer of one of these projects, we would warmly welcome an upgrade.
- DynamoDb: abiglobalhealth/aor-dynamodb-client
- Epilogue: dunghuynh/aor-epilogue-client
- Parse Server: leperone/aor-parseserver-client
- Xmysql: soaserele/aor-xmysql
2.3 Usage
As an example, let's focus on the Simple REST data provider. It fits REST APIs using simple GET parameters for filters and sorting.
Install the ra-data-simple-rest
package to use this provider.
Then, initialize the provider with the REST backend URL, and pass the result to the dataProvider
prop of the <Admin>
component:
Here is how this Data Provider maps react-admin calls to API calls:
Method name | API call |
---|---|
getList | GET http://my.api.url/posts?sort=["title","ASC"]&range=[0, 24]&filter={"title":"bar"} |
getOne | GET http://my.api.url/posts/123 |
getMany | GET http://my.api.url/posts?filter={"id":[123,456,789]} |
getManyReference | GET http://my.api.url/posts?filter={"author_id":345} |
create | POST http://my.api.url/posts/123 |
update | PUT http://my.api.url/posts/123 |
updateMany | Multiple calls to PUT http://my.api.url/posts/123 |
delete | DELETE http://my.api.url/posts/123 |
deteleMany | Multiple calls to DELETE http://my.api.url/posts/123 |
Note: The simple REST client expects the API to include a Content-Range
header in the response to getList
calls. The value must be the total number of resources in the collection. This allows react-admin to know how many pages of resources there are in total, and build the pagination controls.
If your API is on another domain as the JS code, you'll need to whitelist this header with an Access-Control-Expose-Headers
CORS header.
2.4 Handling multiple databases
A way of handling multiple dataproviders is to create a master dataprovider
which would then call the individual dataproviders to get he the data for individual resources. A folder with the following structure should do
While doing that, you'll have to put conditions on which resource you want to use which data provider shown as follows.
here, you'll have to populate dataProviderResources object with resources as key and dataprovider as value.
default is mendatory as a data provider in this, as everytime when any the resource is not assigned explicitly in this configuration, it will automatically fallback to the default data provider.
Note that this works only for dataproviders built with the new dataprovider scheme which ensures it be an object
rather than a function. If you have an older dataprovider, you can convert it in the following way, (Example for hasura)
After this all you need to do is import this master dataprovider
to the app like this import dataProvider from "./react-admin-base/dataProviders";
3. FAQs
Feel free to write into tech@samagragovernance.in in case you have questions, feedback or want to know more!
4. Coming Soon
Please review the following section to get information about planned updates to this module.