Skip to content

Commit

Permalink
feat: add aio_exists
Browse files Browse the repository at this point in the history
  • Loading branch information
kalombos committed Jun 22, 2024
1 parent daad213 commit 9b8a59b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,13 @@ async def aio_count(self, database, clear_limit=False):
pass
return await AioSelect([clone], [peewee.fn.COUNT(peewee.SQL('1'))]).aio_scalar(database)

@peewee.database_required
async def aio_exists(self, database):
clone = self.columns(peewee.SQL('1'))
clone._limit = 1
clone._offset = None
return bool(await clone.aio_scalar())


class AioSelect(peewee.Select, AioSelectMixin):
pass
Expand Down
9 changes: 9 additions & 0 deletions tests/aio_model/test_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,12 @@ async def test_aio_get_or_create__created(db):
t2, created = await TestModel.aio_get_or_create(text="text")
assert t2.text == "text"
assert created is True


@dbs_all
async def test_aio_exists(db):
await TestModel.aio_create(text="text1", data="data")
await TestModel.aio_create(text="text2", data="data")

assert await TestModel.select().where(TestModel.data=="data").aio_exists() is True
assert await TestModel.select().where(TestModel.data == "not_existed").aio_exists() is False

0 comments on commit 9b8a59b

Please sign in to comment.