Module to automaticly generate fastapi routes for using react admin
Project description
FastAPI React Admin
This module provides a class ReactAdmin that allows for the automatic generation of routes for React Admin in a FastAPI application.
Installation
pip install fastapi-react-admin
Base usage
FatstAPI part:
from fastapi import FastAPI
from schemas import MyTableModel
from fastapi_react_admin import ReactAdmin
app = FastAPI()
router = app.router
Session = async_sessionmaker(AsyncSession)
ReactAdmin(
table=MyTableModel,
session=Session
).mount(router)
React Admin data provider:
export const sendPost = async (resource: string, method: string, body?: any) => {
const response = await axios({
method: 'post',
url: resource + "/ra" + method,
data: body,
headers: {
"Admin-Token": sessionStorage.getItem('token')
}
})
return response.data
}
const dataProvider: DataProvider = {
getList: (resource: string, params: any) => {
const { page, perPage } = params.pagination
const { field, order } = params.sort
const query = {
sort: JSON.stringify([field, order]),
range_: JSON.stringify([(page - 1) * perPage, page * perPage - 1]),
filter: JSON.stringify(params.filter),
}
return sendPost(resource, `/getList?${queryString.stringify(query)}`)
},
getOne: async (resource: string, params: any) => {
return await sendPost(resource, '/getOne/' + params.id)
},
getMany: (resource: string, params: any) => {
const query = {
filter: JSON.stringify({ id: params.ids}),
}
return sendPost(resource, `/getMany?${queryString.stringify(query)}`)
},
getManyReference: (resource: string, params: any): any => {
const { page, perPage } = params.pagination
const { field, order } = params.sort
const query = {
sort: JSON.stringify([field, order]),
range_: JSON.stringify([(page - 1) * perPage, page * perPage - 1]),
filter: JSON.stringify(params.filter),
}
return sendPost(params.target, `/getList?${queryString.stringify(query)}`)
},
create: async (resource: string, params: any) => {
return await sendPost(resource, '/create', {
...params.data
})
},
update: async (resource: string, params: any) => {
return await sendPost(resource, '/update/' + params.id, params.data)
},
updateMany: async (resource: string, params: any) => {
const query = {
filter: JSON.stringify({ id: params.ids}),
}
return await sendPost(resource, `/updateMany?${queryString.stringify(query)}`, params.data)
},
delete: (resource: string, params: any) => {
return sendPost(resource, '/delete/' + params.id)
},
deleteMany: (resource: string, params: any) => {
const query = {
filter: JSON.stringify({ id: params.ids}),
}
return sendPost(resource, `/deleteMany?${queryString.stringify(query)}` )
},
}
ReactAdmin class params
-
table (required): The SQLAlchemy model representing the database table.
-
session (required): The async_sessionmaker[AsyncSession] for the database session.
-
deleted_field (optional): The name of the field of the table to mark deleted fields (e.g., 'is_deleted'). Default is None.
-
exclude_deleted (optional): Whether to exclude deleted records. Default is True.
-
options (optional): The list of ExecutableOption of the sqlalchemy. Default is [].
-
include_in_schema (optional): Whether to include the routes in the generated schema. Default is False.
ReactAdmin mount params
-
router (required): The APIRouter instance to mount the routes.
-
depends (optional): The sequence of the dependencies
-
prefix (optional): The URL prefix for the React Admin routes. Default is '/ra'.
-
tags (optional): The FastAPI tags.
-
include_* (optional): The params to include/exclude specific route. (For example: include_create=false)
Route Endpoints
The following routes are automatically generated by the ReactAdmin
class:
- POST /ra/getList: Get a list of records from the table. Accepts JSON payload with sort, filter, and range parameters.
- POST /ra/getOne/{id}: Get a single record by ID.
- POST /ra/getMany/{id}: Get multiple records by ID. Accepts JSON payload with id parameter.
- POST /ra/create: Create a new record. Accepts JSON payload with the record data.
- POST /ra/update/{id}: Update a record by ID. Accepts JSON payload with the updated data.
- POST /ra/updateMany: Update multiple records. Accepts JSON payload with id parameter and updated data.
- POST /ra/delete/{id}: Delete a record by ID.
- POST /ra/deleteMany: Delete multiple records. Accepts JSON payload with id parameter.
Response Format
The response format for all routes is a JSON object with a data field. The data field contains the response data.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for fastapi_react_admin-2.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc41763d5146acb9f3b94f657dddd8488617c76b444535246c22b9980c22d5c5 |
|
MD5 | 1074dce43a01a35925ad1c9a93107f60 |
|
BLAKE2b-256 | 35e9f0e4e3e51f0c5a1c4f482dc0a7de8668659b964d9ea9d7f42ac06f2b1bc9 |
Hashes for fastapi_react_admin-2.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62411f926fde80b4e054b98d85177cb54803d4593c8f1097b17ee62886a081a1 |
|
MD5 | e458dbb67e21b590b23ab8e166351733 |
|
BLAKE2b-256 | d0a7a662420de2a2abd4285eec526955cb3c882e107624352c12ef046e74242f |