view.py icon indicating copy to clipboard operation
view.py copied to clipboard

Database ORM

Open ZeroIntensity opened this issue 2 years ago • 10 comments

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"

ZeroIntensity avatar Aug 17 '23 14:08 ZeroIntensity

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?

Gupta-Aryaman avatar Sep 19 '23 14:09 Gupta-Aryaman

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:

  • MySQL
  • PostgreSQL
  • MongoDB
  • SQLite
  • CockroachDB
  • DGraph

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 avatar Sep 19 '23 18:09 ZeroIntensity

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.

ZeroIntensity avatar Sep 19 '23 18:09 ZeroIntensity

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

Gupta-Aryaman avatar Sep 20 '23 03:09 Gupta-Aryaman

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 avatar Sep 21 '23 17:09 Gupta-Aryaman

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

Gupta-Aryaman avatar Sep 22 '23 17:09 Gupta-Aryaman

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 avatar Sep 22 '23 17:09 ZeroIntensity

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

ZeroIntensity avatar Dec 05 '23 18:12 ZeroIntensity

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

ZeroIntensity avatar Dec 30 '23 20:12 ZeroIntensity

SQLModel looks like a good choice to support as well.

ZeroIntensity avatar Jan 29 '24 15:01 ZeroIntensity