Skip to content

Commit fdfe529

Browse files
emmaling27Convex, Inc.
authored andcommitted
Fix system UDF for indexes to not use componentId arg (#39152)
When `componentId` is an argument, we automatically execute the query in the component's namespace. We shouldn't do that for this system UDF because the `_index` and `_index_backfills` tables are not per-component. GitOrigin-RevId: 2fc6bec624302b34d6e55986eb37f2464b3ee490
1 parent cc4e8c0 commit fdfe529

File tree

1 file changed

+12
-7
lines changed
  • npm-packages/system-udfs/convex/_system/frontend

1 file changed

+12
-7
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { GenericDatabaseReader } from "convex/server";
77
async function getTableId(
88
db: GenericDatabaseReader<DataModel>,
99
tableName: string,
10-
componentId: string | null,
10+
tableNamespace: string | null,
1111
): Promise<string> {
1212
// Get the table id for the tablename
1313
const tablesWithName = await db
@@ -16,7 +16,7 @@ async function getTableId(
1616
.filter((q) => q.eq(q.field("state"), "active"))
1717
.collect();
1818
let tableId;
19-
if (componentId === null) {
19+
if (tableNamespace === null) {
2020
const tables = tablesWithName.filter(
2121
(table) => table.namespace === undefined,
2222
);
@@ -28,14 +28,14 @@ async function getTableId(
2828
tableId = tables[0]._id;
2929
} else {
3030
const tables = tablesWithName.filter(
31-
(table) => table.namespace && table.namespace.id === componentId,
31+
(table) => table.namespace && table.namespace.id === tableNamespace,
3232
);
3333
if (tables.length !== 1) {
3434
throw new Error(
3535
"Table not found for tableName" +
3636
tableName +
3737
" in the componentId " +
38-
componentId,
38+
tableNamespace,
3939
);
4040
}
4141
tableId = tables[0]._id;
@@ -50,13 +50,18 @@ async function getTableId(
5050
export default queryPrivateSystem({
5151
args: {
5252
tableName: v.optional(v.union(v.string(), v.null())),
53-
componentId: v.union(v.string(), v.null()),
53+
// Pass the `componentId` for this arg.
54+
// Note that this arg is named `tableNamespace` not `componentId` because if it is `componentId`,
55+
// the queries will be executed within the component's table namespace,
56+
// which doesn't have the `_index` or `_index_backfills` tables
57+
// We only need this argument to get the correct tableId.
58+
tableNamespace: v.union(v.string(), v.null()),
5459
},
55-
handler: async ({ db }, { tableName, componentId }) => {
60+
handler: async ({ db }, { tableName, tableNamespace }) => {
5661
if (!tableName) {
5762
return undefined;
5863
}
59-
const tableId = await getTableId(db, tableName, componentId);
64+
const tableId = await getTableId(db, tableName, tableNamespace);
6065
const indexes = await db
6166
.query("_index")
6267
.withIndex("by_id", (q) => q)

0 commit comments

Comments
 (0)