Skip to content

Commit

Permalink
[ENG-7490] server-side system UDF for listing modules in all componen…
Browse files Browse the repository at this point in the history
…ts at once (#30296)

server side of #30286

GitOrigin-RevId: 08684ede970ddd8e891eaeafd676fdbe5f7b6f1c
  • Loading branch information
ldanilek authored and Convex, Inc. committed Oct 3, 2024
1 parent 93c11bb commit e779f2c
Showing 1 changed file with 60 additions and 26 deletions.
86 changes: 60 additions & 26 deletions npm-packages/system-udfs/convex/_system/frontend/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ import {
import { queryPrivateSystem } from "../secretSystemTables";
import { v } from "convex/values";
import { DEFAULT_ARGS_VALIDATOR } from "../cli/modules";
import { currentSystemUdfInComponent } from "convex/server";
import { DatabaseReader } from "../../_generated/server";

export const listForAllComponents = queryPrivateSystem({
args: {},
handler: async (ctx): Promise<[string | null, [string, Module][]][]> => {
// NOTE this UDF calls itself recursively in each component with
// `currentSystemUdfInComponent` below.
const modulesInCurrentComponent = await listHandler(ctx.db);
const result: [string | null, [string, Module][]][] = [
[null, modulesInCurrentComponent],
];
// When this UDF is running in a non-root component, the _components table
// is empty, so that's the base case of the recursion.
const componentDocs = await ctx.db.query("_components").collect();
for (const doc of componentDocs) {
if (!doc.parent) {
// Root component, which is the current component.
continue;
}
const ref = currentSystemUdfInComponent(doc._id);
for (const [_, modulesInChildComponent] of await ctx.runQuery(
ref as any,
)) {
result.push([doc._id, modulesInChildComponent]);
}
}
return result;
},
});

/**
* Return all user defined modules + their functions.
Expand All @@ -20,35 +50,39 @@ export const list = queryPrivateSystem({
componentId: v.optional(v.union(v.string(), v.null())),
},
handler: async ({ db }): Promise<[string, Module][]> => {
const result: [string, Module][] = [];
for await (const module of db.query("_modules")) {
const analyzeResult = module.analyzeResult;
if (!analyzeResult) {
// `Skipping ${module.path}`
continue;
}

const functions =
analyzeResult.sourceMapped?.functions.map(processFunction) ?? [];
// Stuff HTTP routes into the functions (the format the dashboard expects).
for (const route of analyzeResult.httpRoutes || []) {
functions.push(processHttpRoute(route));
}
return await listHandler(db);
},
});

const cronSpecs = processCronSpecs(analyzeResult.cronSpecs);
async function listHandler(db: DatabaseReader): Promise<[string, Module][]> {
const result: [string, Module][] = [];
for await (const module of db.query("_modules")) {
const analyzeResult = module.analyzeResult;
if (!analyzeResult) {
// `Skipping ${module.path}`
continue;
}

result.push([
module.path,
{
functions,
sourcePackageId: module.sourcePackageId,
...(cronSpecs !== null ? { cronSpecs } : {}),
},
]);
const functions =
analyzeResult.sourceMapped?.functions.map(processFunction) ?? [];
// Stuff HTTP routes into the functions (the format the dashboard expects).
for (const route of analyzeResult.httpRoutes || []) {
functions.push(processHttpRoute(route));
}
return result;
},
});

const cronSpecs = processCronSpecs(analyzeResult.cronSpecs);

result.push([
module.path,
{
functions,
sourcePackageId: module.sourcePackageId,
...(cronSpecs !== null ? { cronSpecs } : {}),
},
]);
}
return result;
}

function processCronSpecs(
cronSpecs: null | undefined | Array<{ identifier: string; spec: CronSpec }>,
Expand Down

0 comments on commit e779f2c

Please sign in to comment.