sleepless
sleepless3w ago

hello, I'm working on a generic util

hello, I'm working on a generic util that takes a query and deletes all documents this is the general idea
export const deleteAllInQuery = async <T extends GenericTableInfo>(ctx: MutationCtx, q: OrderedQuery<T>, cursor: string | null) => {
do {
const result = await q.paginate({
cursor,
numItems: 100,
});
await Promise.all(
result.page.map(async (item) => {
await ctx.db.delete(item._id as Id<TableNamesInDataModel<DataModel>>);
}),
);
cursor = result.continueCursor;
} while (cursor);
};
export const deleteAllInQuery = async <T extends GenericTableInfo>(ctx: MutationCtx, q: OrderedQuery<T>, cursor: string | null) => {
do {
const result = await q.paginate({
cursor,
numItems: 100,
});
await Promise.all(
result.page.map(async (item) => {
await ctx.db.delete(item._id as Id<TableNamesInDataModel<DataModel>>);
}),
);
cursor = result.continueCursor;
} while (cursor);
};
From what I understand, to avoid potentially running into limits, each batch should be scheduled separately, but how can I pass a query to an interalMutation?
1 Reply
sleepless
sleeplessOP3w ago
I don't think this would work, do args have to be serializable?
export const deleteNextPage = internalMutation({
args: {
query: v.any(),
cursor: v.union(v.string(), v.null()),
},
handler: async (ctx, args) => {
const q = args.query as OrderedQuery<GenericTableInfo>;
const cursor = args.cursor;
const result = await q.paginate({
cursor,
numItems: 100,
});
await Promise.all(
result.page.map(async (item) => {
await ctx.db.delete(item._id as Id<TableNamesInDataModel<DataModel>>);
}),
);

return { cursor: result.continueCursor, isDone: result.isDone };
},
});
export const deleteNextPage = internalMutation({
args: {
query: v.any(),
cursor: v.union(v.string(), v.null()),
},
handler: async (ctx, args) => {
const q = args.query as OrderedQuery<GenericTableInfo>;
const cursor = args.cursor;
const result = await q.paginate({
cursor,
numItems: 100,
});
await Promise.all(
result.page.map(async (item) => {
await ctx.db.delete(item._id as Id<TableNamesInDataModel<DataModel>>);
}),
);

return { cursor: result.continueCursor, isDone: result.isDone };
},
});
oh also ctx.scheduler.runAt returns Promise<Id<"_scheduled_functions">> how to get the returned value of the scheduled mutation?

Did you find this page helpful?