beyond-the-cloud-dev / soql-lib

The SOQL Lib provides functional constructs for SOQL queries in Apex.
https://soql.beyondthecloud.dev/
MIT License
63 stars 10 forks source link

`.with(field1 - field5)` #31

Closed pgajek2 closed 1 year ago

pgajek2 commented 1 year ago

The SOQL library has two methods to specify a field:

.with(SObjectField field) .with(List<SObjectField> fields)

It requires a List<SObjectField> to add more than one field.

List<Account> accounts = SOQL.of(Account.SObjectType)
   .with(new List<SObjectField>{
      Account.Id, Account.Name, Account.Industry
   }).toList();

Of course, you can chain methods:

List<Account> accounts = SOQL.of(Account.SObjectType)
   .with(Account.Id)
   .with(Account.Name)
   .with(Account.Industry)
   .toList();

However, it is still not very clean.

Enhancement

Most of the queries require up to five fields.

Add methods like:

.with(SObjectField field1, SObjectField field2) .with(SObjectField field1, SObjectField field2, SObjectField field3) .with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4) .with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5)

So, the usage looks more like native SOQL:

List<Account> accounts = SOQL.of(Account.SObjectType)
   .with(Account.Id, Account.Name, Account.Industry)
   .toList();

BEFORE:

SOQL.of(Account.SObjectType)
    .with(new List<SObjectField>{ Account.Id, Account.Name })
    .with(
        SOQL.SubQuery.of('Contacts')
            .with(new List<SObjectField>{ Contact.Id, Contact.Name })
            .orderBy(Contact.Name).sortDesc().nullsLast()
    )
    .toList();

AFTER:

SOQL.of(Account.SObjectType)
    .with(Account.Id, Account.Name)
    .with(
        SOQL.SubQuery.of('Contacts')
            .with(Contact.Id, Contact.Name)
            .orderBy(Contact.Name).sortDesc().nullsLast()
    )
    .toList();
janeksledziewski commented 1 year ago

I see two problems with these solutions: — Unnecessary complexity — Additional code, which is essentially duplicated

Following your examples: This:

.with(field1, field2, field3, field4, field5)

Is not so much different than this:

.with(new List<SObjectField>{
      field1, field2, field3, field4, field5
})

But first solution requires much more code from the library, and we want to keep it light. If user wants to add less fields and just write less than full list declaration, then there is possibility of chaining .with. Same goes for usability, instead of simple completion that shows .with(field), .with(list), IDE would show list with multiple input fields totally messing simplicity of current solution.