Skip to content

Commit

Permalink
PDE-4989 feat(schema): Add support for bulk writes
Browse files Browse the repository at this point in the history
  • Loading branch information
kola-er committed May 8, 2024
1 parent 0ba70f9 commit b2ce016
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/schema/lib/functional-constraints/bulkWriteConstraints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const _ = require('lodash');
const jsonschema = require('jsonschema');

const bulkWriteConstraints = (definition) => {
const errors = [];
const actionType = 'creates';

if (definition[actionType]) {
_.each(definition[actionType], (actionDef) => {
if (actionDef.operation && actionDef.operation.bulk) {
if (!actionDef.operation.performBulk) {
errors.push(
new jsonschema.ValidationError(
'must contain performBulk because bulk is configured.',
actionDef.operation,
'/BasicCreateActionOperationSchema',
`instance.${actionType}.${actionDef.key}.performBulk`,
'invalid',
'performBulk'
)
);
}

if (actionDef.operation.perform) {
errors.push(
new jsonschema.ValidationError(
'must not contain perform because it is mutually exclusive with bulk.',
actionDef.operation,
'/BasicCreateActionOperationSchema',
`instance.${actionType}.${actionDef.key}.perform`,
'invalid',
'perform'
)
);
}
}
});
}

return errors;
};

module.exports = bulkWriteConstraints;
1 change: 1 addition & 0 deletions packages/schema/lib/functional-constraints/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const checks = [
require('./matchingKeys'),
require('./labelWhenVisible'),
require('./uniqueInputFieldKeys'),
require('./bulkWriteConstraints'),
];

const runFunctionalConstraints = (definition, mainSchema) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const makeSchema = require('../utils/makeSchema');

const BasicActionOperationSchema = require('./BasicActionOperationSchema');
const BulkObjectSchema = require('./BulkObjectSchema');
const FunctionSchema = require('./FunctionSchema');

// TODO: would be nice to deep merge these instead
// or maybe use allOf which is built into json-schema
Expand All @@ -20,6 +22,13 @@ BasicCreateActionOperationSchema.properties.shouldLock = {
type: 'boolean',
};

BasicCreateActionOperationSchema.properties.bulk = BulkObjectSchema;
BasicCreateActionOperationSchema.properties.performBulk = {
description:
'A function to write in bulk with.',
$ref: FunctionSchema.id,
};

module.exports = makeSchema(
BasicCreateActionOperationSchema,
BasicActionOperationSchema.dependencies
Expand Down
48 changes: 48 additions & 0 deletions packages/schema/lib/schemas/BulkObjectSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const makeSchema = require('../utils/makeSchema');

module.exports = makeSchema({
id: '/LockObjectSchema',
description:
'**INTERNAL USE ONLY**. Zapier uses this configuration for internal operation locking.',
type: 'object',
required: ['groupedBy', 'limit'],
properties: {
groupedBy: {
description:
'The list of keys of input fields to group bulk-write with. The actual user data provided for the fields will be used during execution. Note that a required input field should be referenced to get user data always.'
type: 'array',
minLength: 1,
},
limit: {
description:
'The maximum number of items to call performBulk with.',
type: 'integer',
},
},
examples: [
{
groupedBy: ['workspace', 'sheet'],
limit: 100,
},
],
antiExamples: [
{
example: {
groupedBy: [],
limit: 100,
},
reason: 'Empty groupedBy list provided: `[]`.',
},
{
example: {groupedBy: ['workspace']},
reason: 'Missing required key: `limit`.',
},
{
example: {limit: 1},
reason: 'Missing required key: `groupedBy`.',
},
],
additionalProperties: false,
});

0 comments on commit b2ce016

Please sign in to comment.