Skip to content

Commit e5cc432

Browse files
committed
feat: add createBatch API for Workflows binding
We want to allow users to batch create instances in the same request - this implies that the RPC limit is shared for the batch.
1 parent 96568b0 commit e5cc432

File tree

25 files changed

+245
-2
lines changed

25 files changed

+245
-2
lines changed

src/cloudflare/internal/test/workflows/workflows-api-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,21 @@ export const tests = {
2020
const instance = await env.workflow.get('bar');
2121
assert.deepStrictEqual(instance.id, 'bar');
2222
}
23+
24+
{
25+
// Test createBatch
26+
const instances = await env.workflow.createBatch([
27+
{
28+
id: 'foo',
29+
payload: { bar: 'baz' },
30+
},
31+
{
32+
id: 'bar',
33+
payload: { bar: 'baz' },
34+
},
35+
]);
36+
assert.deepStrictEqual(instances[0].id, 'foo');
37+
assert.deepStrictEqual(instances[1].id, 'bar');
38+
}
2339
},
2440
};

src/cloudflare/internal/test/workflows/workflows-mock.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
export default {
66
async fetch(request, env, ctx) {
77
const data = await request.json();
8+
const reqUrl = new URL(request.url);
89

9-
if (request.url.includes('/get') && request.method === 'POST') {
10+
if (reqUrl.pathname === '/get' && request.method === 'POST') {
1011
return Response.json(
1112
{
1213
result: {
@@ -22,7 +23,7 @@ export default {
2223
);
2324
}
2425

25-
if (request.url.includes('/create') && request.method === 'POST') {
26+
if (reqUrl.pathname === '/create' && request.method === 'POST') {
2627
return Response.json(
2728
{
2829
result: {
@@ -37,5 +38,19 @@ export default {
3738
}
3839
);
3940
}
41+
42+
if (reqUrl.pathname === '/createBatch' && request.method === 'POST') {
43+
return Response.json(
44+
{
45+
result: data.map((val) => ({ id: val.id })),
46+
},
47+
{
48+
status: 201,
49+
headers: {
50+
'content-type': 'application/json',
51+
},
52+
}
53+
);
54+
}
4055
},
4156
};

src/cloudflare/internal/workflows-api.ts

+12
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ class WorkflowImpl {
103103

104104
return new InstanceImpl(result.id, this.fetcher);
105105
}
106+
107+
public async createBatch(
108+
options: WorkflowInstanceCreateOptions[]
109+
): Promise<WorkflowInstance[]> {
110+
const results = await callFetcher<
111+
{
112+
id: string;
113+
}[]
114+
>(this.fetcher, '/createBatch', options);
115+
116+
return results.map((result) => new InstanceImpl(result.id, this.fetcher));
117+
}
106118
}
107119

108120
export function makeBinding(env: { fetcher: Fetcher }): Workflow {

src/cloudflare/internal/workflows.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ declare abstract class Workflow<PARAMS = unknown> {
3838
public create(
3939
options?: WorkflowInstanceCreateOptions<PARAMS>
4040
): Promise<WorkflowInstance>;
41+
42+
/**
43+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
44+
* `createBatch` is limited at 100 instances at a time or when the RPC limit (1MiB) is reached.
45+
* @param batch List of Options when creating an instance including name and params
46+
* @returns A promise that resolves with a list of handles for the created instances.
47+
*/
48+
public createBatch(
49+
batch: WorkflowInstanceCreateOptions<PARAMS>[]
50+
): Promise<WorkflowInstance[]>;
4151
}
4252

4353
interface WorkflowInstanceCreateOptions<PARAMS = unknown> {

types/defines/workflows.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ declare abstract class Workflow<PARAMS = unknown> {
2424
public create(
2525
options?: WorkflowInstanceCreateOptions<PARAMS>
2626
): Promise<WorkflowInstance>;
27+
28+
/**
29+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
30+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
31+
* @param batch List of Options when creating an instance including name and params
32+
* @returns A promise that resolves with a list of handles for the created instances.
33+
*/
34+
public createBatch(
35+
batch: WorkflowInstanceCreateOptions<PARAMS>[]
36+
): Promise<WorkflowInstance[]>;
2737
}
2838

2939
interface WorkflowInstanceCreateOptions<PARAMS = unknown> {

types/generated-snapshot/2021-11-03/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6480,6 +6480,15 @@ declare abstract class Workflow<PARAMS = unknown> {
64806480
public create(
64816481
options?: WorkflowInstanceCreateOptions<PARAMS>,
64826482
): Promise<WorkflowInstance>;
6483+
/**
6484+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6485+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6486+
* @param batch List of Options when creating an instance including name and params
6487+
* @returns A promise that resolves with a list of handles for the created instances.
6488+
*/
6489+
public createBatch(
6490+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6491+
): Promise<WorkflowInstance[]>;
64836492
}
64846493
interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
64856494
/**

types/generated-snapshot/2021-11-03/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6381,6 +6381,15 @@ export declare abstract class Workflow<PARAMS = unknown> {
63816381
public create(
63826382
options?: WorkflowInstanceCreateOptions<PARAMS>,
63836383
): Promise<WorkflowInstance>;
6384+
/**
6385+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6386+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6387+
* @param batch List of Options when creating an instance including name and params
6388+
* @returns A promise that resolves with a list of handles for the created instances.
6389+
*/
6390+
public createBatch(
6391+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6392+
): Promise<WorkflowInstance[]>;
63846393
}
63856394
export interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
63866395
/**

types/generated-snapshot/2022-01-31/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6506,6 +6506,15 @@ declare abstract class Workflow<PARAMS = unknown> {
65066506
public create(
65076507
options?: WorkflowInstanceCreateOptions<PARAMS>,
65086508
): Promise<WorkflowInstance>;
6509+
/**
6510+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6511+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6512+
* @param batch List of Options when creating an instance including name and params
6513+
* @returns A promise that resolves with a list of handles for the created instances.
6514+
*/
6515+
public createBatch(
6516+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6517+
): Promise<WorkflowInstance[]>;
65096518
}
65106519
interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
65116520
/**

types/generated-snapshot/2022-01-31/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6407,6 +6407,15 @@ export declare abstract class Workflow<PARAMS = unknown> {
64076407
public create(
64086408
options?: WorkflowInstanceCreateOptions<PARAMS>,
64096409
): Promise<WorkflowInstance>;
6410+
/**
6411+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6412+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6413+
* @param batch List of Options when creating an instance including name and params
6414+
* @returns A promise that resolves with a list of handles for the created instances.
6415+
*/
6416+
public createBatch(
6417+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6418+
): Promise<WorkflowInstance[]>;
64106419
}
64116420
export interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
64126421
/**

types/generated-snapshot/2022-03-21/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6531,6 +6531,15 @@ declare abstract class Workflow<PARAMS = unknown> {
65316531
public create(
65326532
options?: WorkflowInstanceCreateOptions<PARAMS>,
65336533
): Promise<WorkflowInstance>;
6534+
/**
6535+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6536+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6537+
* @param batch List of Options when creating an instance including name and params
6538+
* @returns A promise that resolves with a list of handles for the created instances.
6539+
*/
6540+
public createBatch(
6541+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6542+
): Promise<WorkflowInstance[]>;
65346543
}
65356544
interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
65366545
/**

types/generated-snapshot/2022-03-21/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6432,6 +6432,15 @@ export declare abstract class Workflow<PARAMS = unknown> {
64326432
public create(
64336433
options?: WorkflowInstanceCreateOptions<PARAMS>,
64346434
): Promise<WorkflowInstance>;
6435+
/**
6436+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6437+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6438+
* @param batch List of Options when creating an instance including name and params
6439+
* @returns A promise that resolves with a list of handles for the created instances.
6440+
*/
6441+
public createBatch(
6442+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6443+
): Promise<WorkflowInstance[]>;
64356444
}
64366445
export interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
64376446
/**

types/generated-snapshot/2022-08-04/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6532,6 +6532,15 @@ declare abstract class Workflow<PARAMS = unknown> {
65326532
public create(
65336533
options?: WorkflowInstanceCreateOptions<PARAMS>,
65346534
): Promise<WorkflowInstance>;
6535+
/**
6536+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6537+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6538+
* @param batch List of Options when creating an instance including name and params
6539+
* @returns A promise that resolves with a list of handles for the created instances.
6540+
*/
6541+
public createBatch(
6542+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6543+
): Promise<WorkflowInstance[]>;
65356544
}
65366545
interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
65376546
/**

types/generated-snapshot/2022-08-04/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6433,6 +6433,15 @@ export declare abstract class Workflow<PARAMS = unknown> {
64336433
public create(
64346434
options?: WorkflowInstanceCreateOptions<PARAMS>,
64356435
): Promise<WorkflowInstance>;
6436+
/**
6437+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6438+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6439+
* @param batch List of Options when creating an instance including name and params
6440+
* @returns A promise that resolves with a list of handles for the created instances.
6441+
*/
6442+
public createBatch(
6443+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6444+
): Promise<WorkflowInstance[]>;
64366445
}
64376446
export interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
64386447
/**

types/generated-snapshot/2022-10-31/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6536,6 +6536,15 @@ declare abstract class Workflow<PARAMS = unknown> {
65366536
public create(
65376537
options?: WorkflowInstanceCreateOptions<PARAMS>,
65386538
): Promise<WorkflowInstance>;
6539+
/**
6540+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6541+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6542+
* @param batch List of Options when creating an instance including name and params
6543+
* @returns A promise that resolves with a list of handles for the created instances.
6544+
*/
6545+
public createBatch(
6546+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6547+
): Promise<WorkflowInstance[]>;
65396548
}
65406549
interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
65416550
/**

types/generated-snapshot/2022-10-31/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6437,6 +6437,15 @@ export declare abstract class Workflow<PARAMS = unknown> {
64376437
public create(
64386438
options?: WorkflowInstanceCreateOptions<PARAMS>,
64396439
): Promise<WorkflowInstance>;
6440+
/**
6441+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6442+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6443+
* @param batch List of Options when creating an instance including name and params
6444+
* @returns A promise that resolves with a list of handles for the created instances.
6445+
*/
6446+
public createBatch(
6447+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6448+
): Promise<WorkflowInstance[]>;
64406449
}
64416450
export interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
64426451
/**

types/generated-snapshot/2022-11-30/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6541,6 +6541,15 @@ declare abstract class Workflow<PARAMS = unknown> {
65416541
public create(
65426542
options?: WorkflowInstanceCreateOptions<PARAMS>,
65436543
): Promise<WorkflowInstance>;
6544+
/**
6545+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6546+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6547+
* @param batch List of Options when creating an instance including name and params
6548+
* @returns A promise that resolves with a list of handles for the created instances.
6549+
*/
6550+
public createBatch(
6551+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6552+
): Promise<WorkflowInstance[]>;
65446553
}
65456554
interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
65466555
/**

types/generated-snapshot/2022-11-30/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6442,6 +6442,15 @@ export declare abstract class Workflow<PARAMS = unknown> {
64426442
public create(
64436443
options?: WorkflowInstanceCreateOptions<PARAMS>,
64446444
): Promise<WorkflowInstance>;
6445+
/**
6446+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6447+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6448+
* @param batch List of Options when creating an instance including name and params
6449+
* @returns A promise that resolves with a list of handles for the created instances.
6450+
*/
6451+
public createBatch(
6452+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6453+
): Promise<WorkflowInstance[]>;
64456454
}
64466455
export interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
64476456
/**

types/generated-snapshot/2023-03-01/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6543,6 +6543,15 @@ declare abstract class Workflow<PARAMS = unknown> {
65436543
public create(
65446544
options?: WorkflowInstanceCreateOptions<PARAMS>,
65456545
): Promise<WorkflowInstance>;
6546+
/**
6547+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6548+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6549+
* @param batch List of Options when creating an instance including name and params
6550+
* @returns A promise that resolves with a list of handles for the created instances.
6551+
*/
6552+
public createBatch(
6553+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6554+
): Promise<WorkflowInstance[]>;
65466555
}
65476556
interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
65486557
/**

types/generated-snapshot/2023-03-01/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6444,6 +6444,15 @@ export declare abstract class Workflow<PARAMS = unknown> {
64446444
public create(
64456445
options?: WorkflowInstanceCreateOptions<PARAMS>,
64466446
): Promise<WorkflowInstance>;
6447+
/**
6448+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6449+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6450+
* @param batch List of Options when creating an instance including name and params
6451+
* @returns A promise that resolves with a list of handles for the created instances.
6452+
*/
6453+
public createBatch(
6454+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6455+
): Promise<WorkflowInstance[]>;
64476456
}
64486457
export interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
64496458
/**

types/generated-snapshot/2023-07-01/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6543,6 +6543,15 @@ declare abstract class Workflow<PARAMS = unknown> {
65436543
public create(
65446544
options?: WorkflowInstanceCreateOptions<PARAMS>,
65456545
): Promise<WorkflowInstance>;
6546+
/**
6547+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6548+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6549+
* @param batch List of Options when creating an instance including name and params
6550+
* @returns A promise that resolves with a list of handles for the created instances.
6551+
*/
6552+
public createBatch(
6553+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6554+
): Promise<WorkflowInstance[]>;
65466555
}
65476556
interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
65486557
/**

types/generated-snapshot/2023-07-01/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6444,6 +6444,15 @@ export declare abstract class Workflow<PARAMS = unknown> {
64446444
public create(
64456445
options?: WorkflowInstanceCreateOptions<PARAMS>,
64466446
): Promise<WorkflowInstance>;
6447+
/**
6448+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6449+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6450+
* @param batch List of Options when creating an instance including name and params
6451+
* @returns A promise that resolves with a list of handles for the created instances.
6452+
*/
6453+
public createBatch(
6454+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6455+
): Promise<WorkflowInstance[]>;
64476456
}
64486457
export interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
64496458
/**

types/generated-snapshot/experimental/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6624,6 +6624,15 @@ declare abstract class Workflow<PARAMS = unknown> {
66246624
public create(
66256625
options?: WorkflowInstanceCreateOptions<PARAMS>,
66266626
): Promise<WorkflowInstance>;
6627+
/**
6628+
* Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown.
6629+
* `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached.
6630+
* @param batch List of Options when creating an instance including name and params
6631+
* @returns A promise that resolves with a list of handles for the created instances.
6632+
*/
6633+
public createBatch(
6634+
batch: WorkflowInstanceCreateOptions<PARAMS>[],
6635+
): Promise<WorkflowInstance[]>;
66276636
}
66286637
interface WorkflowInstanceCreateOptions<PARAMS = unknown> {
66296638
/**

0 commit comments

Comments
 (0)