FieldResolver
The FieldResolver
class allows you to generate field API names via the properties & relationships on the objects.
select
select
Accepts one or more non-relationship properties key or FunctionField
const f = new FieldResolver(Account);
f.select('name'); //-> Name
f.select('name', 'myCustomField'); //-> ['Name', 'My_Custom_Field__c']
f.select({fn: 'COUNT', field: 'id', alias: 'c'}); //-> "COUNT(Id) c"
parent()
parent()
Allows you to traverse to a parent relationship. Returns a new FieldResolver
for the parent SObject type. Keeps track of "where it's been" so resulting fields are fully qualified. Can go multiple levels deep, but SOQL only supports a depth of 5.
let f = new FieldResolver(Contact);
f.parent('account').select('name'); //-> Account.Name
f.parent('account').parent('owner').select('email'); //-> Account.Owner.Email
subQuery()
subQuery()
Allows you to generate a "sub query" on a child relationship, to be used inside a SELECT statement of the parent object. The first parameter is a key of a child relationship. The second parameter is a function, which accepts a FieldResolver
for the child SObject type and return the subquery to generate.
const contactQuery = (cF: FieldResolver<Contact>): SOQLQueryParams => ({
select: [
...cF.select('name', 'birthdate'),
cF.parent('owner').select('name')
],
where: [
{ field: cf.select('email'), op: '!=', val: null }
]
});
buildQuery(Account, (aF) => ({
select: [ aF.subQuery('contacts', contactQuery) ],
}));
/* ->
SELECT (
SELECT Name, Birthday, Owner.Name FROM Contacts WHERE Email != null
) FROM Account
*/
Advanced Usage
To see the real power of the FieldResolver
checkout the sections on SELECT Models and WHERE Filters.
Last updated
Was this helpful?