Package: fastapi-react-admin
FastAPI React AdminThis module provides a class ReactAdmin that allows for the automatic generation of routes for React Admin in a FastAPI application.Installationpip install fastapi-react-adminBase usageFatstAPI part:fromfastapiimportFastAPIfromschemasimportMyTableModelfromfastapi_react_adminimportReactAdminapp=FastAPI()router=app.routerSession=async_sessionmaker(AsyncSession)ReactAdmin(table=MyTableModel,session=Session).mount(router)React Admin data provider:exportconstsendPost=async(resource:string,method:string,body?:any)=>{constresponse=awaitaxios({method:'post',url:resource+"/ra"+method,data:body,headers:{"Admin-Token":sessionStorage.getItem('token')}})returnresponse.data}constdataProvider:DataProvider={getList:(resource:string,params:any)=>{const{page,perPage}=params.paginationconst{field,order}=params.sortconstquery={sort:JSON.stringify([field,order]),range_:JSON.stringify([(page-1)*perPage,page*perPage-1]),filter:JSON.stringify(params.filter),}returnsendPost(resource,`/getList?${queryString.stringify(query)}`)},getOne:async(resource:string,params:any)=>{returnawaitsendPost(resource,'/getOne/'+params.id)},getMany:(resource:string,params:any)=>{constquery={filter:JSON.stringify({id:params.ids}),}returnsendPost(resource,`/getMany?${queryString.stringify(query)}`)},getManyReference:(resource:string,params:any):any=>{const{page,perPage}=params.paginationconst{field,order}=params.sortconstquery={sort:JSON.stringify([field,order]),range_:JSON.stringify([(page-1)*perPage,page*perPage-1]),filter:JSON.stringify(params.filter),}returnsendPost(params.target,`/getList?${queryString.stringify(query)}`)},create:async(resource:string,params:any)=>{returnawaitsendPost(resource,'/create',{...params.data})},update:async(resource:string,params:any)=>{returnawaitsendPost(resource,'/update/'+params.id,params.data)},updateMany:async(resource:string,params:any)=>{constquery={filter:JSON.stringify({id:params.ids}),}returnawaitsendPost(resource,`/updateMany?${queryString.stringify(query)}`,params.data)},delete:(resource:string,params:any)=>{returnsendPost(resource,'/delete/'+params.id)},deleteMany:(resource:string,params:any)=>{constquery={filter:JSON.stringify({id:params.ids}),}returnsendPost(resource,`/deleteMany?${queryString.stringify(query)}`)},}ReactAdmin class paramstable (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 paramsrouter (required): The APIRouter instance to mount the routes.depends (optional): The sequence of the dependenciesprefix (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 EndpointsThe following routes are automatically generated by theReactAdminclass: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 FormatThe response format for all routes is a JSON object with a data field. The data field contains the response data.