Skip to content

Expose .iter(), .opt(), etc. methods directly on the params struct. #290

@LouisGariepy

Description

@LouisGariepy

Migrated from halcyonnouveau/clorinde#85, originally reported by @jvdwrf on 2025-04-06.

Currently, I use the following pattern to execute clorinde methods:

queries::folder::my_query()
    .params(
        client,
        &queries::scoring::MyQueryParams {
            field_a: "value_a",
            field_b: "value_b",
        },
    )
    .iter()

I feel like my code could be cleaned up a lot if the following were instead possible:

queries::folder::MyQueryParams {
    field_a: "value_a",
    field_b: "value_b",
}
.iter(client)

// or even:
client.iter(queries::folder::MyQueryParams {
    field_a: "value_a",
    field_b: "value_b",
})

This removes a lot of code-duplication, and improves readability a lot. I don't like using the method directly without params struct, since it becomes very easy to accidentally map params to the wrong field.

I also think it would be nice if, instead of them being regular methods, it would instead be traits that are implemented. This would allow me to much more easily build abstractions over the various database methods, by implementing them for all param structs at once. (e.g. automatically logging the error in a specific way, using a default impl. super trait)


Original discussion

@beanpuppy (2025-04-06):

client.iter(queries::folder::MyQueryParams {
    field_a: "value_a",
    field_b: "value_b",
})

This isn't possible since client is whatever postgres client you're using, and not something we control.

queries::folder::MyQueryParams {
    field_a: "value_a",
    field_b: "value_b",
}
.iter(client)

But never thought of doing something like this. It seems like a nice idea especially as a trait, and I could see myself using it a lot as well. I'll look into it, thanks for the suggestion.

@jvdwrf (2025-04-07):

For the first version, it would be possible to create an extension trait that would implement these methods for all clients; given that the params structs would implement traits rather than methods. (Though I could also write this myself, it doesnt have to be included in clorinde itself)

(Also, maybe params structs could be generated for single param methods as well? :))

Metadata

Metadata

Assignees

No one assigned

    Labels

    codegenAn issue regarding code generation, or the generated code itself.featureNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions