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

Binary / base64 field automatic encoding and eager loading #400

Merged
merged 26 commits into from
Sep 28, 2022

Conversation

joshwlewis
Copy link
Contributor

This PR adds automatic support for base64 / binary content, and is supported by the changes to type signatures in forcedotcom/sf-fx-sdk-nodejs#141.

The additional functionality has two parts:

  • Population of the binaryFields property by eager-loading binary content during queries.
  • Automatic base64 encoding of the values in binaryFields when performing upstream record modification.

An example function leveraging this new functionality might look like this:

import { InvocationEvent, Context, Logger, Record, RecordModificationResult } from "sf-fx-sdk-nodejs";
import sharp from 'sharp';

/**
 * thumber queries an org for images matching a title, then creates a 50px png
 * thumbnail of each image, and uploads each thumbnail as a new image.
 */

export default async function thumber(event: InvocationEvent<any>, context: Context, logger: Logger):
    Promise<PromiseSettledResult<RecordModificationResult>[]> {

    const queryResult = await context.org.dataApi
        .query(`SELECT Id, Title, VersionData FROM ContentVersion WHERE Title = '${event.data.title}' AND FileExtension IN ('svg', 'png', 'jpg')`);

    return await Promise.allSettled(queryResult.records.map((record) => createThumb(context, record)));
}

async function createThumb(context: Context, record: Record): Promise<RecordModificationResult> {
  const resizedData = await sharp(record.binaryFields.versiondata)
    .resize(50)
    .png()
    .toBuffer();
  return await context.org.dataApi.create({
    type: "ContentVersion",
    binaryFields: {
      VersionData: resizedData,
    },
    fields: {
      Title: `${record.fields.title}-thumbnail`,
      PathOnClient: `${record.fields.title}-thumnail.png`,
      Description: `Thumbnail generated from file ${record.fields.id}`,
    }
  });
}

GUS-W-11543492

@joshwlewis joshwlewis requested a review from a team as a code owner September 8, 2022 16:02
@joshwlewis joshwlewis changed the title Binary fields Binary / base64 field automatic encoding and eager loading Sep 8, 2022
@joshwlewis joshwlewis marked this pull request as draft September 8, 2022 16:26
src/sdk/data-api.ts Outdated Show resolved Hide resolved
src/sdk/data-api.ts Outdated Show resolved Hide resolved
src/sdk/data-api.ts Show resolved Hide resolved
src/sdk/data-api.ts Show resolved Hide resolved
src/sdk/data-api.ts Outdated Show resolved Hide resolved
src/utils/maps.ts Show resolved Hide resolved
test/sdk/data-api.ts Show resolved Hide resolved
@joshwlewis joshwlewis marked this pull request as ready for review September 27, 2022 20:55
@joshwlewis joshwlewis enabled auto-merge (squash) September 27, 2022 21:26
@joshwlewis joshwlewis merged commit 14eea6e into main Sep 28, 2022
@joshwlewis joshwlewis deleted the binary-fields branch September 28, 2022 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants