Skip to content

Commit 652fa41

Browse files
committed
allow pasing function handle
1 parent 12acfec commit 652fa41

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/client/index.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type GenericDataModel,
77
type GenericMutationCtx,
88
type GenericQueryCtx,
9+
getFunctionAddress,
910
getFunctionName,
1011
internalMutationGeneric,
1112
makeFunctionReference,
@@ -27,6 +28,7 @@ export type { MigrationArgs, MigrationResult, MigrationStatus };
2728
import { ConvexError, type GenericId } from "convex/values";
2829
import type { ComponentApi } from "../component/_generated/component.js";
2930
import { logStatusAndInstructions } from "./log.js";
31+
import type { MigrationFunctionHandle } from "../component/lib.js";
3032

3133
// Note: this value is hard-coded in the docstring below. Please keep in sync.
3234
export const DEFAULT_BATCH_SIZE = 100;
@@ -637,8 +639,12 @@ export type MigrationFunctionReference = FunctionReference<
637639
export async function runToCompletion(
638640
ctx: ActionCtx,
639641
component: ComponentApi,
640-
fnRef: MigrationFunctionReference,
642+
fnRef: MigrationFunctionReference | MigrationFunctionHandle,
641643
opts?: {
644+
/**
645+
* The name of the migration function, generated with getFunctionName.
646+
*/
647+
name?: string;
642648
/**
643649
* The cursor to start from.
644650
* null: start from the beginning.
@@ -656,15 +662,23 @@ export async function runToCompletion(
656662
*/
657663
dryRun?: boolean;
658664
},
659-
) {
665+
): Promise<MigrationStatus> {
660666
let cursor = opts?.cursor;
667+
const {
668+
name = getFunctionName(fnRef),
669+
batchSize,
670+
dryRun = false,
671+
} = opts ?? {};
672+
const address = getFunctionAddress(fnRef);
673+
const fnHandle =
674+
address.functionHandle ?? (await createFunctionHandle(fnRef));
661675
while (true) {
662676
const status = await ctx.runMutation(component.lib.migrate, {
663-
name: getFunctionName(fnRef),
664-
fnHandle: await createFunctionHandle(fnRef),
677+
name,
678+
fnHandle,
665679
cursor,
666-
batchSize: opts?.batchSize,
667-
dryRun: opts?.dryRun ?? false,
680+
batchSize,
681+
dryRun,
668682
oneBatchOnly: true,
669683
});
670684
if (status.isDone) {

0 commit comments

Comments
 (0)