ZeroIntensity / view.py

The Batteries-Detachable Web Framework
https://view.zintensity.dev
MIT License
206 stars 15 forks source link

Database ORM #10

Open ZeroIntensity opened 10 months ago

ZeroIntensity commented 10 months ago

Waiting on #9, probably. Should probably support SQLite first.

Hypothetical API:

@model()
class User:
    name: str
    password: Hashed[str]
    id: Id[int] = auto_increment()

@post("/signup")
@body("data", User)
async def signup(data: User):
    await data.save()
    return "Created account"
Gupta-Aryaman commented 9 months ago

Hey! I would like to work on this. Even though i have worked with sqlite and orms in flask and django, I don't know how they are working/connected. Could you please elaborate on what exactly do you want?

ZeroIntensity commented 9 months ago

I was thinking it could be a system that wraps several different databases into one API. For building this, you would need to design a driver for each database.

Ideally, it should look like this:

@model()
class User:
    name: str

async for user in User.many():  # fetch all users
    ...

zero = await User.fetch(name="ZeroIntensity")

await User(name="test").save()

The above code would work with any database that the user selects. I was planning on supporting the following:

I think support for Redis caching could also be done, but that's for a future issue.

On the backend, driving each database should look something like this:

class _MongoDriver(_Driver):
    ...  # code specific to mongodb

class _PostgresDriver(_Driver):
    ...  # code specific to postgres

class _Connection:
    def __init__(self, driver: _Driver):  # this can take in any driver
        ...

Then, models can use a global _Connection instance to access the db.

Note that implementing each database protocol is probably unnecessary, and instead we should just use libraries that already exist to drive each database (i.e. PyMongo for MongoDB).

ZeroIntensity commented 9 months ago

Also, #9 needs to be finished before this can really get going. It's almost done (see this branch), but it's untested and probably a bit buggy so far.

Gupta-Aryaman commented 9 months ago

Ok, I will see what can I do till you get #9 merged

Gupta-Aryaman commented 9 months ago

Just to inform, I am working on this issue...this stuff is a bit new so I am researching and hence may take some time. But I am working on it

Gupta-Aryaman commented 9 months ago

I have created a new folder named database and creating database related files in it. But how do I use it like i use views? It is showing no module named database even after I build it (pip install .) image

ZeroIntensity commented 9 months ago

Python code should go in the view folder, not src. I would actually rather you not use a folder, and instead just put it in databases.py. I think codebase sorts of questions should go into the discord from now on, GitHub isn't really for debugging.

ZeroIntensity commented 7 months ago

Enough of this has been done to mark it as "not hot"

ZeroIntensity commented 6 months ago

For batteries detachable purposes, the Django ORM, PeeWee, and probably SQLAlchemy should all be supported natively by view.py

ZeroIntensity commented 5 months ago

SQLModel looks like a good choice to support as well.