Skip to content

Commit

Permalink
sizeOfAllTables system udf (#24059)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 8c1c08529b42ecc131758e99cd863670e1df2c94
  • Loading branch information
atrakh authored and Convex, Inc. committed Mar 27, 2024
1 parent d6c8625 commit 68f7a74
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions npm-packages/system-udfs/convex/_system/frontend/tableSize.ts
Original file line number Diff line number Diff line change
@@ -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() },
Expand All @@ -11,3 +13,26 @@ export default queryGeneric({
return await db.query(tableName).count();
},
});

export const sizeOfAllTables = queryGeneric({
args: {},
handler: async function allTableSizes({ db }): Promise<number> {
// 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);
},
});

0 comments on commit 68f7a74

Please sign in to comment.