databases icon indicating copy to clipboard operation
databases copied to clipboard

Awkward/Brittle syntax for using create_function in sqlite backend

Open epoz opened this issue 2 years ago • 0 comments

To use custom functions in the SQLite backend, in a FASTAPI project, I am doing:

database = databases.Database(DATABASE_URL)

@app.on_event("startup")
async def startup():
    await database.connect()

# and then inside a different function being called via a web route

async with database.connection() as connection:
        await connection._connection.raw_connection.create_function(...
        res = await connection.fetch_all(s)

where s contains some statements using the defined function. This is needed to make sure the defined function is in the same connection used for the query

The above syntax calling the raw connection chain like that feels "wrong" to me, it seems brittle.

Is there a better, more canonical way of doing the above?


(Possibly related to #163, the above code will also allow you to load extensions in the same query)

epoz avatar Apr 21 '22 11:04 epoz