Skip to content

Commit

Permalink
feat: add AioModelRaw (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalombos authored May 24, 2024
1 parent 42bcf1e commit 89506c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,11 @@ async def fetch_results(self, cursor):
return await self._database.last_insert_id_async(cursor)


class AioModelRaw(peewee.ModelRaw, AioQueryMixin):
async def fetch_results(self, cursor):
return await self.make_async_query_wrapper(cursor)


class AioModelSelect(peewee.ModelSelect, AioQueryMixin):

async def fetch_results(self, cursor):
Expand Down Expand Up @@ -764,6 +769,10 @@ def insert_from(cls, query, fields):
else field for field in fields]
return AioModelInsert(cls, insert=query, columns=columns)

@classmethod
def raw(cls, sql, *params):
return AioModelRaw(cls, sql, params)

@classmethod
def delete(cls):
return AioModelDelete(cls)
Expand Down
14 changes: 14 additions & 0 deletions tests/aio_model/test_selecting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import peewee

from peewee_async import AioModelRaw
from tests.conftest import manager_for_all_dbs, dbs_all
from tests.models import TestModel, TestModelAlpha, TestModelBeta

Expand All @@ -17,6 +19,18 @@ async def test_select_w_join(db):
assert result.joined_alpha.id == alpha.id


@dbs_all
async def test_raw_select(db):
obj1 = await TestModel.aio_create(text="Test 1")
obj2 = await TestModel.aio_create(text="Test 2")
query = TestModel.raw(
'SELECT id, text, data FROM testmodel m ORDER BY m.text'
)
assert isinstance(query, AioModelRaw)
result = await query.aio_execute()
assert list(result) == [obj1, obj2]


@manager_for_all_dbs
async def test_select_compound(manager):
obj1 = await manager.create(TestModel, text="Test 1")
Expand Down

0 comments on commit 89506c8

Please sign in to comment.