Skip to content

Commit 68f7a74

Browse files
atrakhConvex, Inc.
authored and
Convex, Inc.
committed
sizeOfAllTables system udf (#24059)
GitOrigin-RevId: 8c1c08529b42ecc131758e99cd863670e1df2c94
1 parent d6c8625 commit 68f7a74

File tree

1 file changed

+25
-0
lines changed
  • npm-packages/system-udfs/convex/_system/frontend

1 file changed

+25
-0
lines changed

npm-packages/system-udfs/convex/_system/frontend/tableSize.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import sum from "lodash/sum";
12
import { queryGeneric } from "../secretSystemTables";
23
import { v } from "convex/values";
4+
import { DatabaseReader } from "../../_generated/server";
35

46
export default queryGeneric({
57
args: { tableName: v.string() },
@@ -11,3 +13,26 @@ export default queryGeneric({
1113
return await db.query(tableName).count();
1214
},
1315
});
16+
17+
export const sizeOfAllTables = queryGeneric({
18+
args: {},
19+
handler: async function allTableSizes({ db }): Promise<number> {
20+
// Getting private system table here is OK because there are no args to this
21+
// system UDF.
22+
const tables = await ((db as any).privateSystem as DatabaseReader)
23+
.query("_tables")
24+
.filter((q) => q.eq(q.field("state"), "active"))
25+
.collect();
26+
const tablesWithoutSystemTables = tables
27+
.map((table) => table.name)
28+
.filter((tableName) => !tableName.startsWith("_"));
29+
30+
const tableCounts = Promise.all(
31+
tablesWithoutSystemTables.map(async (tableName) => {
32+
return await db.query(tableName as any).count();
33+
}),
34+
);
35+
36+
return sum(await tableCounts);
37+
},
38+
});

0 commit comments

Comments
 (0)