Skip to content

Add convenience .one() to do single search match#103

Merged
piercefreeman merged 1 commit intomainfrom
feature/typehint-single-access
Apr 6, 2026
Merged

Add convenience .one() to do single search match#103
piercefreeman merged 1 commit intomainfrom
feature/typehint-single-access

Conversation

@piercefreeman
Copy link
Copy Markdown
Owner

@piercefreeman piercefreeman commented Apr 6, 2026

Queries of primary keys are implemented as single select() statements in iceaxe, since this matches what we actually send to the query engine. It's often somewhat of a special case, however, because you usually want to do an implicit limit 1 and you want to typehint the returning object as only a single value.

This PR adds support for a .one() modifier to queries that will:

  • Verify that you are querying for a whole serialized model
  • Inject a limit 1
  • Assert that one value is actually returned at runtime
  • Typehint the output as a single object instead of a list, which is normally used by exec

This allows clients to replace:

results = await db_connection.exec(select(MyModel).where(MyModel.id == my_id).limit(1))
if not results:
  raise ValueError()
result = results[0]

with:

result = await db_connection.exec(select(MyModel).where(MyModel.id == my_id).one())

@piercefreeman piercefreeman merged commit 38ec313 into main Apr 6, 2026
23 checks passed
@piercefreeman piercefreeman deleted the feature/typehint-single-access branch April 6, 2026 01:09
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

Successfully merging this pull request may close these issues.

1 participant