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
apex

SOQL Lib

Deploy to Scratch Org and run tests codecov

The SOQL Lib provides functional constructs for SOQL queries in Apex.

For more details, please refer to the documentation.

You may also find this blog post about SOQL Lib interesting.

Examples

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

Selector

Read how to build your selector class.

public inherited sharing class SOQL_Contact extends SOQL implements SOQL.Selector {
    public static final String MOCK_ID = 'SOQL_Contact';

    public static SOQL_Contact query() {
        return new SOQL_Contact();
    }

    private SOQL_Contact() {
        super(Contact.SObjectType);
        // default settings
        with(Contact.Id, Contact.Name, Contact.AccountId)
            .systemMode()
            .withoutSharing()
            .mockId(MOCK_ID);
    }

    public SOQL_Contact byAccountId(Id accountId) {
        whereAre(Filter.with(Contact.AccountId).equal(accountId));
        return this;
    }
}
public with sharing class ExampleController {
    @AuraEnabled
    public static List<Contact> getAccountContacts(Id accountId) {
        return SOQL_Contact.query()
            .byRecordType('Partner')
            .byAccountId(accountId)
            .with(Contact.Email)
            .toList();
    }
}

Deploy to Salesforce

<img alt="Deploy to Salesforce" src="https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/deploy.png">

Documentation

SOQL Lib documentation

Features

Read about the features in the documentation.

  1. Dynamic SOQL
  2. Automatic binding
  3. Control FLS
    • 3.1 User Mode
    • 3.2 System Mode
    • 3.3 stripInaccessible
  4. Control Sharings Mode
    • 4.1 with sharing
    • 4.2 without sharing
    • 4.3 inherited sharing
  5. Mocking
    • 5.1 Mock list of records
    • 5.2 Mock single record
    • 5.3 Mock with static resources
    • 5.4 Mock count result
  6. Avoid query duplicates
  7. The default configuration for all queries
  8. Dynamic conditions

"Buy Me A Coffee"


License notes