RobertCraigie / prisma-client-py

Prisma Client Python is an auto-generated and fully type-safe database client designed for ease of use
https://prisma-client-py.readthedocs.io
Apache License 2.0
1.83k stars 77 forks source link

Provide a FastAPI admin panel #287

Open RobertCraigie opened 2 years ago

RobertCraigie commented 2 years ago

Problem

Frameworks like Django provide an admin panel that users can customise to suit their needs and to easily add database records. This is a highly desirable feature.

Prisma already provides the Prisma Studio application which while incredibly helpful and useful for browsing your data, it does not provide the customisability and utility that an admin panel can. It also cannot be easily exposed on your website which I would argue is one of the main benefits of an admin panel.

Suggested solution

While it would be better to provide a framework agnostic ASGI compatible app, this will result in increased implementation complexity and in my opinion it would be more beneficial to initially create an app that is only compatible with FastAPI.

We should provide a FastAPI app that can then be mounted into consumers own FastAPI application, e.g.

from fastapi import FastAPI
from prisma.ext.fastapi.admin import admin_app

app = FastAPI()
app.mount("/admin", admin_app)

There are a lot of implementation details left to bikeshed but it could follow a similar pattern to https://github.com/fastapi-admin/fastapi-admin and make use of https://github.com/tabler/tabler.

Goals:

To bikeshed:

Alternatives

We could try and integrate Prisma with the existing FastAPI admin panel but it was not designed to work with multiple ORMs and this would be a difficult task. It would be easier to implement this ourselves, this would also means that we'd be able to tailor it specifically for Prisma Python.

Additional Context

Laravel Nova looks like a good solution to build ideas from.

https://twitter.com/taylorotwell/status/1508870009703079942?t=Mt9R_x2Jh0k2mSs01_kZ5w&s=19

RobertCraigie commented 2 years ago

We can also support other frameworks. Any framework that supports "mounting" ASGI sub-applications would be supported out of the box and for other frameworks we would have to provide a wrapper, for example this is how it could be implemented in Sanic:

from sanic import Sanic, Request

app = Sanic('MyApp')

# catch all routes so we can pass them on to Prisma Admin to handle
@app.get('/<path:path>')
async def catch_all(request: Request, path: str):
    ...

There are some complexities with this as we would have to transform the Sanic request object into a valid ASGI scope, however this should be doable.

The downside to an approach like this is that we would then be introducing FastAPI as a dependency even for applications that don't directly use FastAPI however this is not a massive problem, just an area that should be improved in the future. The end goal with Prisma Admin would be a framework agnostic base that we would then build framework specific handlers on top of.

RobertCraigie commented 2 years ago

Eventual goals: