Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

salesforce refactor query function #1030

Open
mtuchi opened this issue Mar 5, 2025 · 2 comments
Open

salesforce refactor query function #1030

mtuchi opened this issue Mar 5, 2025 · 2 comments
Assignees
Labels
Medium Complexity level P3 Priority Level

Comments

@mtuchi
Copy link
Collaborator

mtuchi commented Mar 5, 2025

Description
The query function auto fetching implementation will no longer be needed on jsforcev3, Once we upgrade the adaptor to use the latest version of jsforcev3, See #1028, We should update the query function to use conn.query See docs here also we should add support for queryOptions which will make it easy to auto fetch records when we hit the maximum fetch limit. See queryOptions here

@mtuchi
Copy link
Collaborator Author

mtuchi commented Mar 6, 2025

Notes

  • use conn.query()
  • Add important logs about queried records. totalFetched * totalInDb
  • Document all queryOptions and add links to jsfroce docs
  • Write unit test or integration test for each queryOption if possible

Start from the code below 👇🏽

export function query(queryString, options = {}) {
  return async state => {
    const { connection } = state;
    const [resolvedQuery, resolvedOptions] = expandReferences(
      state,
      queryString,
      options
    );
    console.log(`Executing query: ${resolvedQuery}`);
    // TODO: Add support for more query options
    // https://jsforce.github.io/jsforce/types/query.QueryOptions.html
    // {headers, responseTarget}
    const {
      autoFetch = true,
      scanAll = false,
      maxFetch = 10000,
    } = resolvedOptions;

    if (autoFetch) {
      console.log(
        `autoFetch is enabled: A maximum of ${maxFetch} records will be downloaded`
      );
    }

    let query = {};
    const records = [];
    await new Promise((resolve, reject) => {
      query = connection
        .query(resolvedQuery)
        .on('record', record => {
          records.push(record);
        })
        .on('end', () => {
          console.log('total in database : ' + query.totalSize);
          console.log('total fetched : ' + query.totalFetched);
          resolve();
        })
        .on('error', err => {
          const { message, errorCode } = err;
          console.error(`Error ${errorCode}: ${message}`);
          throwError('QUERY_ERROR', { message, errorCode });
          reject();
        })
        .run({ autoFetch, maxFetch, scanAll });
    });

    const result = {
      done: query.totalFetched === query.totalSize,
      records,
      totalSize: query.totalSize,
      totalFetched: query.totalFetched,
    };

    return composeNextState(state, result);
  };
}

@mtuchi mtuchi added Medium Complexity level P3 Priority Level labels Mar 6, 2025
@josephjclark
Copy link
Collaborator

Document all queryOptions and add links to jsfroce docs

Don't document jsforce options, just link to them :) Maybe if it's super commonly used then you can include it. Any options that WE provide that jsforce does NOT must be documented explicitly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Medium Complexity level P3 Priority Level
Projects
Status: New Issues
Development

When branches are created from issues, their pull requests are automatically linked.

3 participants