Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

async support #339

Closed
prryplatypus opened this issue Jan 27, 2021 · 5 comments
Closed

async support #339

prryplatypus opened this issue Jan 27, 2021 · 5 comments

Comments

@prryplatypus
Copy link

Hi there! 馃憢

Just thought I'd ask if adding async support is planned for this project anytime in the future. There seems to be a wide range of ORM packages for sync projects, but those which aren't sync are usually left to interact with the "raw" DB directly by using libraries like asyncpg, aiopg or aiomysql.

It would be great to have an Eloquent-like ORM which supported asynchronous code, so that projects following the asynchronous model (or libraries like Sanic or discord.py) could also make use of a neat abstraction layer that packages like this one provide.

Thank you very much in advance!

@josephmancuso
Copy link
Member

josephmancuso commented Jan 27, 2021

@prryplatypus there is no plan for it. Mainly for 2 reasons:

  1. I'm not familiar with how async ORM's would handle queries
  2. Because of 1, I'm not sure how much effort it would be to maintain both.

If you would like to explore what the effort would be to add async support to this project I would be more than willing to listen, even help you during that discovery phase.

It could be as easy as adding a new model or query builder. Or maybe as easy as inheriting from ModelAsync instead of Model. If its just adding some flags to the query builder maybe we can just set flags on the configuration settings async: True which will either call the sync or async query builder.

In other words, I'm not sure what the effort would be to do something like this. Also I'm not sure what the effort would be to maintain something like this. Maybe its better off inside this package or maybe its better off as an adapter package.

Either way I'm personally unfamiliar with how async ORM's are working and really have a lot more questions than answers about this issue lol.

If you are very familiar with how it works I'd love to discuss more with you but this is not something I want to explore on my own and in my own time right now

@prryplatypus
Copy link
Author

Understandable! I'm not very familiar with all of the intrinsics of asynchronous code, but it is something I do use pretty much every day. In general most of the SQL wrappers out there are pretty much a drop-in replacement of the non-async versions, except for having to prepend the async keyword in function definitions which have asynchronous operations in them and then prepending keywords like await to their calls. For example:

async def execute(self, query, *args, **kwargs):
  async with self.pool.acquire() as conn:
    async with conn.cursor() as cur:
      await cur.execute(query, args)
    await conn.commit()

That being said, if/when I have some free time (don't even know what free time is at this point) I will try messing around with this project and see if I manage to do anything with it!

Many thanks for your reply :)

@josephmancuso
Copy link
Member

josephmancuso commented Jan 27, 2021

Sounds great. Anybody reading this in the future, I am open to the idea but this idea needs to be explored more by someone who is more familiar with async ORM's than I am, or someone who can answer some open questions I have.

This is not a simple feature request so I will need to write a feature product specification document for this feature which takes a lot of time to discover and create.

@girardinsamuel before we close this issue for good do you have any thoughts?

@girardinsamuel
Copy link
Contributor

girardinsamuel commented Jan 27, 2021

Hello there !
No I am not an async expert either and I agree with what has already been said 馃憤

@dinkopehar
Copy link

I was just curious does Masonite support Async and found this thread.

There is a package from the creators of Django Rest Framework called databases that gives asyncio support for a range of databases.
It can be used with raw SQL or SQLAlchemy Core (which generates query from model).
I found to_sql from Masonite has ability to generate yet to be executed SQL, but it states it's vulnerable to SQL Injection. If there is some safe way to generate raw SQL query that is safe from Masonite, I think it can be used with databases package:

import asyncio 

from databases import Database
from masoniteorm.query import QueryBuilder

from config.database import DB

database = Database('sqlite+aiosqlite:///masonite.db')

async def main():
    await database.connect()

    # Create a table.
    # NOTE: Can be generated using Masonite Model & Migrations.
    query = """CREATE TABLE IF NOT EXISTS highscores (id INTEGER PRIMARY KEY, name VARCHAR(100), score INTEGER)"""
    await database.execute(query=query)

    # Run a database query.
    
    query = QueryBuilder().table("highscores").to_sql()
    print(query)
    rows = await database.fetch_all(query=query)
    print('High Scores:', rows)


asyncio.run(main())

Not sure about down sides...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants