diff --git a/npm-packages/system-udfs/convex/_system/frontend/tableSize.ts b/npm-packages/system-udfs/convex/_system/frontend/tableSize.ts index 140b0ef5..b8421b46 100644 --- a/npm-packages/system-udfs/convex/_system/frontend/tableSize.ts +++ b/npm-packages/system-udfs/convex/_system/frontend/tableSize.ts @@ -1,5 +1,7 @@ +import sum from "lodash/sum"; import { queryGeneric } from "../secretSystemTables"; import { v } from "convex/values"; +import { DatabaseReader } from "../../_generated/server"; export default queryGeneric({ args: { tableName: v.string() }, @@ -11,3 +13,26 @@ export default queryGeneric({ return await db.query(tableName).count(); }, }); + +export const sizeOfAllTables = queryGeneric({ + args: {}, + handler: async function allTableSizes({ db }): Promise { + // Getting private system table here is OK because there are no args to this + // system UDF. + const tables = await ((db as any).privateSystem as DatabaseReader) + .query("_tables") + .filter((q) => q.eq(q.field("state"), "active")) + .collect(); + const tablesWithoutSystemTables = tables + .map((table) => table.name) + .filter((tableName) => !tableName.startsWith("_")); + + const tableCounts = Promise.all( + tablesWithoutSystemTables.map(async (tableName) => { + return await db.query(tableName as any).count(); + }), + ); + + return sum(await tableCounts); + }, +});