WHERE Filters

There are often times where you need to run multiple queries with the same WHERE conditions. In those cases, you can create a "filter" function which accepts a FieldResolver<T> and returns a ConditionalClause

import { buildQuery, ConditionalClause, FieldResolver } from 'ts-force';

const accountFilter = (f: FieldResolver<Account>): ConditionalClause => {
  return [
    { field: f.select('active'), val: true },
    { field: f.select('accountSource'), val: 'email' },
    { field: f.select('annualRevenue'), op: '>', val: 100 },
  ];
};


let accountQuery = buildQuery(Account, f => (
    {
      select: ['Id'],
      where: accountFilter(f),
      limit: 1
    }
));

let contactQuery = buildQuery(Contact, f => (
    {
      select: ['Id'],
      where: accountFilter(f.parent('account')),
      limit: 1
    }
));

let contactQueryNested = buildQuery(Contact, f => (
    {
      select: ['Id'],
      where: [
        {field: f.select('leadSource'), val: 'web'},
        accountFilter(f.parent('account'))
      ],
      limit: 1
    }
));

Last updated

Was this helpful?