Skip to content

Commit a89e690

Browse files
committed
have internalMutation be a function on WorkflowManager
1 parent 654fa8a commit a89e690

File tree

5 files changed

+43
-48
lines changed

5 files changed

+43
-48
lines changed

example/convex/eventTimeout.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import { v } from "convex/values";
1010
import { components, internal } from "./_generated/api";
1111
import { internalMutation } from "./_generated/server";
1212

13-
const workflow = new WorkflowManager(components.workflow);
13+
const workflow = new WorkflowManager(components.workflow, {
14+
internalMutation,
15+
});
1416

1517
const approvalEvent = defineEvent({
1618
name: "approval",
@@ -31,7 +33,6 @@ const approvalEvent = defineEvent({
3133
export const eventTimeoutWorkflow = workflow.define({
3234
args: {},
3335
returns: v.string(),
34-
internalMutation,
3536
handler: async (step): Promise<string> => {
3637
// 1. Create the event so we have an ID to pass to the timeout function.
3738
const eventId = await step.runMutation(

example/convex/example.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import { components } from "./_generated/api.js";
66
import { vWorkflowId } from "@convex-dev/workflow";
77
import { vResultValidator } from "@convex-dev/workpool";
88

9-
export const workflow = new WorkflowManager(components.workflow);
9+
export const workflow = new WorkflowManager(components.workflow, {
10+
internalMutation,
11+
});
1012

1113
export const myWorkflow = workflow
1214
.define({
1315
args: {
1416
location: v.string(),
1517
},
16-
internalMutation,
1718
workpoolOptions: {
1819
retryActionsByDefault: true,
1920
},

src/client/index.ts

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,36 +77,16 @@ export type CallbackOptions = {
7777
export type WorkflowDefinition<
7878
ArgsValidator extends PropertyValidators,
7979
ReturnsValidator extends Validator<any, "required", any> | void = any,
80-
DataModel extends GenericDataModel = GenericDataModel,
8180
> = {
8281
args?: ArgsValidator;
8382
returns?: ReturnsValidator;
8483
workpoolOptions?: WorkpoolRetryOptions;
85-
/**
86-
* Provide your app's `internalMutation` (from `_generated/server`) to get
87-
* a fully typed `ctx` in `ctx.run()` handlers, with your data model's
88-
* tables available on `ctx.db`. This also lets any custom middleware
89-
* you've configured run around the workflow.
90-
*
91-
* ```ts
92-
* import { internalMutation } from "./_generated/server";
93-
* workflow.define({
94-
* internalMutation,
95-
* handler: async (ctx, args) => {
96-
* const user = await ctx.run(async (ctx) => {
97-
* return ctx.db.query("users").first(); // fully typed
98-
* });
99-
* },
100-
* });
101-
* ```
102-
*/
103-
internalMutation?: MutationBuilder<DataModel, "internal">;
10484
};
10585

10686
export type WorkflowHandler<
10787
ArgsValidator extends PropertyValidators,
10888
ReturnsValidator extends Validator<any, "required", any> | void,
109-
DataModel extends GenericDataModel,
89+
DataModel extends GenericDataModel = GenericDataModel,
11090
> = (
11191
step: WorkflowCtx<DataModel>,
11292
args: ObjectType<ArgsValidator>,
@@ -158,7 +138,9 @@ export function defineWorkflow<
158138
DM extends GenericDataModel = GenericDataModel,
159139
>(
160140
component: WorkflowComponent,
161-
config: WorkflowDefinition<AV, RV, DM>,
141+
config: WorkflowDefinition<AV, RV> & {
142+
internalMutation?: MutationBuilder<DM, "internal">;
143+
},
162144
): {
163145
/**
164146
* Define the workflow handler function.
@@ -538,11 +520,27 @@ export async function cleanup(
538520
});
539521
}
540522

541-
export class WorkflowManager {
523+
export class WorkflowManager<
524+
DataModel extends GenericDataModel = GenericDataModel,
525+
> {
542526
constructor(
543527
public component: WorkflowComponent,
544528
public options?: {
545-
workpoolOptions: WorkpoolOptions;
529+
workpoolOptions?: WorkpoolOptions;
530+
/**
531+
* Provide your app's `internalMutation` (from `_generated/server`) to get
532+
* a fully typed `ctx` in `step.run()` handlers, with your data model's
533+
* tables available on `ctx.db`. This also lets any custom middleware
534+
* you've configured run around the workflow.
535+
*
536+
* ```ts
537+
* import { internalMutation } from "./_generated/server";
538+
* const workflow = new WorkflowManager(components.workflow, {
539+
* internalMutation,
540+
* });
541+
* ```
542+
*/
543+
internalMutation?: MutationBuilder<DataModel, "internal">;
546544
},
547545
) {}
548546

@@ -555,9 +553,8 @@ export class WorkflowManager {
555553
define<
556554
ArgsValidator extends PropertyValidators,
557555
ReturnsValidator extends Validator<unknown, "required", string> | void,
558-
DataModel extends GenericDataModel = GenericDataModel,
559556
>(
560-
workflow: WorkflowDefinition<ArgsValidator, ReturnsValidator, DataModel> & {
557+
workflow: WorkflowDefinition<ArgsValidator, ReturnsValidator> & {
561558
handler: WorkflowHandler<ArgsValidator, ReturnsValidator, DataModel>;
562559
},
563560
): RegisteredMutation<
@@ -568,9 +565,8 @@ export class WorkflowManager {
568565
define<
569566
ArgsValidator extends PropertyValidators,
570567
ReturnsValidator extends Validator<unknown, "required", string> | void,
571-
DataModel extends GenericDataModel = GenericDataModel,
572568
>(
573-
workflow: WorkflowDefinition<ArgsValidator, ReturnsValidator, DataModel>,
569+
workflow: WorkflowDefinition<ArgsValidator, ReturnsValidator>,
574570
): {
575571
/**
576572
* Define the workflow handler function.
@@ -602,21 +598,21 @@ export class WorkflowManager {
602598
define<
603599
ArgsValidator extends PropertyValidators,
604600
ReturnsValidator extends Validator<unknown, "required", string> | void,
605-
DataModel extends GenericDataModel = GenericDataModel,
606601
>(
607-
workflow: WorkflowDefinition<ArgsValidator, ReturnsValidator, DataModel> & {
602+
workflow: WorkflowDefinition<ArgsValidator, ReturnsValidator> & {
608603
handler?: WorkflowHandler<ArgsValidator, ReturnsValidator, DataModel>;
609604
},
610605
): unknown {
606+
const withMutation = {
607+
...workflow,
608+
internalMutation: this.options?.internalMutation,
609+
};
611610
if (workflow.handler) {
612611
return workflowMutation(
613612
this.component,
614-
workflow as WorkflowDefinition<
615-
ArgsValidator,
616-
ReturnsValidator,
617-
DataModel
618-
> & {
613+
withMutation as WorkflowDefinition<ArgsValidator, ReturnsValidator> & {
619614
handler: WorkflowHandler<ArgsValidator, ReturnsValidator, DataModel>;
615+
internalMutation?: MutationBuilder<DataModel, "internal">;
620616
},
621617
this.options?.workpoolOptions,
622618
);
@@ -628,7 +624,7 @@ export class WorkflowManager {
628624
return defineWorkflow<ArgsValidator, ReturnsValidator, DataModel>(
629625
this.component,
630626
{
631-
...workflow,
627+
...withMutation,
632628
workpoolOptions: {
633629
...this.options?.workpoolOptions,
634630
...workflow.workpoolOptions,

src/client/workflowContext.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,12 @@ export type WorkflowCtx<DataModel extends GenericDataModel = GenericDataModel> =
116116
* value will not be replayed.
117117
*
118118
* To get a fully typed `ctx` with your data model, provide your app's
119-
* `internalMutation` in the workflow definition:
119+
* `internalMutation` when creating the `WorkflowManager`:
120120
*
121121
* ```ts
122122
* import { internalMutation } from "./_generated/server";
123-
* workflow.define({
123+
* const workflow = new WorkflowManager(components.workflow, {
124124
* internalMutation,
125-
* handler: async (ctx, args) => {
126-
* const user = await ctx.run(async (ctx) => {
127-
* return ctx.db.query("users").first(); // fully typed
128-
* });
129-
* },
130125
* });
131126
* ```
132127
*

src/client/workflowMutation.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
internalMutationGeneric,
77
makeFunctionReference,
88
type GenericDataModel,
9+
type MutationBuilder,
910
type RegisteredMutation,
1011
} from "convex/server";
1112
import {
@@ -54,8 +55,9 @@ export function workflowMutation<
5455
DataModel extends GenericDataModel = GenericDataModel,
5556
>(
5657
component: WorkflowComponent,
57-
registered: WorkflowDefinition<ArgsValidator, any, DataModel> & {
58+
registered: WorkflowDefinition<ArgsValidator> & {
5859
handler: WorkflowHandler<ArgsValidator, any, DataModel>;
60+
internalMutation?: MutationBuilder<DataModel, "internal">;
5961
},
6062
defaultWorkpoolOptions?: WorkpoolOptions,
6163
boundFn?: string,

0 commit comments

Comments
 (0)