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

PDE-5309 feat(core): add support for "buffered creates" #832

Merged
merged 24 commits into from
Sep 17, 2024
Merged

Conversation

eliangcs
Copy link
Member

@eliangcs eliangcs commented Aug 1, 2024

Introducing "Buffered Create Actions"...

A Buffered Create allows you to create objects in bulk with a single or fewer API request(s). This is useful when you want to reduce the number of requests made to your server. When enabled, Zapier holds the data until the buffer reaches a size limit or a certain time has passed, then sends the buffered data using the performBuffer function you define.

To implement a Buffered Create, you define a buffer configuration object and a performBuffer function in the operation object. In the buffer config object, you specify how you want to group the buffered data using the groupedBy setting and the maximum number of items to buffer using the limit setting.

Here's an example:

const performBuffer = async (z, bufferedBundle) => {
  // Send a request to create tasks in bulk
  const response = await z.request({
    method: 'POST',
    url: 'https://httpbin.zapier-tooling.com/post',
    body: {
      items: bufferedBundle.buffer,
    },
  });

  const items = response.data.json.items;
  const result = {};

  for (const item of items) {
    const idempotencyId = item.meta.id;
    const itemProps = item.inputData;
    result[idempotencyId] = {
      outputData: {
        id: Date.now(),
        ...itemProps,
      },
    };
  }

  return result;
};

module.exports = {
  key: 'task_buffered',
  noun: 'Task (Buffered)',

  display: {
    label: 'Create Task (Buffered)',
    description: 'Creates a task.',
  },

  operation: {
    performBuffer,
    buffer: {
      groupedBy: ['project_id'],
      limit: 100,
    },

    inputFields: [
      { key: 'project_id', required: true },
      { key: 'title', required: true },
      { key: 'content' },
    ],

    sample: {
      id: 1,
      name: 'Test',
    },

    outputFields: [],
  },
};

@eliangcs eliangcs changed the title (Don't merge) Bulk write PDE-5309 feat(core): buffered "create" Sep 10, 2024
},
});

// Create a matching result using the idempotency ID for each buffered invovation run.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invocation typo


BasicCreateActionOperationSchema.properties.buffer = {
description:
'Zapier uses this configuration for writing in bulk with `performBuffer`.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seldom use the word write/writing on the schema, I want to double check if this is the intended word to use

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! I've replaced "write" with "create" in several places I found.

},
limit: {
description:
"The maximum number of items to call `performBuffer` with. **Note** that it is capped by the platform to prevent exceeding the [AWS Lambda's request/response payload size quota of 6 MB](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html#function-configuration-deployment-and-execution). Also, the execution is time-bound; we recommend reducing it upon consistent timeout.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still relevant if we introduced this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was written before we added supported for large response payloads. I think for response payloads, it's no longer relevant. But for request payloads, it's still relevant, so I think we should keep it.

},
};

describe('bufferedWriteConstraints', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these usually include a happy path? 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I'm not sure what you mean. Can you clarify?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore me, I thought I only saw test cases for all the various error paths, but didn't see a happy success path, but I see it now.

@standielpls
Copy link
Contributor

I reviewed bc it was referenced in the backend MR, but noticed this was still in draft, so did a quick pass through, lmk when this is ready!

@eliangcs eliangcs changed the title PDE-5309 feat(core): buffered "create" PDE-5309 feat(core): add support for "buffered create" Sep 13, 2024
@eliangcs eliangcs changed the title PDE-5309 feat(core): add support for "buffered create" PDE-5309 feat(core): add support for "buffered creates" Sep 13, 2024
@eliangcs eliangcs marked this pull request as ready for review September 13, 2024 07:03
@eliangcs eliangcs requested a review from a team as a code owner September 13, 2024 07:03
standielpls
standielpls previously approved these changes Sep 13, 2024
Copy link
Contributor

@standielpls standielpls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look great, thank you.
I wasn't able to test this on an integration (found issues upstream, likely an environment thing) but it looks like you tested at length in the demo, so I'm happy with that.

@eliangcs
Copy link
Member Author

@standielpls thanks for the review! I added some text mentioning the feature is internal-only. Can you re-review?

standielpls
standielpls previously approved these changes Sep 16, 2024
Copy link
Contributor

@standielpls standielpls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll approve to unblock, just found a couple typos

@@ -1385,6 +1415,21 @@ export interface BasicCreateActionOperation {
* concurrency)?
*/
shouldLock?: boolean;

/**
* Currently an **internaly-only** feature. Zapier uses this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a typo here for internal


BasicCreateActionOperationSchema.properties.buffer = {
description:
'Currently an **internaly-only** feature. Zapier uses this configuration for creating objects in bulk with `performBuffer`.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

internal typo

@eliangcs eliangcs merged commit 8353f32 into main Sep 17, 2024
14 checks passed
@eliangcs eliangcs deleted the bulk-write branch September 17, 2024 03:55
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.

4 participants