Skip to content

Commit 3eafb89

Browse files
ldanilekConvex, Inc.
authored andcommitted
[components] npx convex data (#29645)
GitOrigin-RevId: 4946d2eb61bf66545c98b8b2a1efb92cd8278c62
1 parent 9e372b1 commit 3eafb89

File tree

10 files changed

+75
-32
lines changed

10 files changed

+75
-32
lines changed

crates/isolate/src/environment/udf/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ impl<RT: Runtime> IsolateEnvironment<RT> for DatabaseUdfEnvironment<RT> {
237237
}
238238

239239
fn get_table_mapping_without_system_tables(&mut self) -> anyhow::Result<TableMappingValue> {
240-
Ok(self.phase.tx()?.table_mapping().clone().into())
240+
let namespace = self.phase.component()?.into();
241+
Ok(self.phase.tx()?.table_mapping().namespace(namespace).into())
241242
}
242243

243244
fn get_all_table_mappings(&mut self) -> anyhow::Result<NamespacedTableMapping> {

crates/isolate/src/isolate2/runner.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,10 @@ impl<RT: Runtime> UdfShared<RT> {
781781

782782
fn get_table_mapping_without_system_tables(&self) -> TableMappingValue {
783783
let inner = self.inner.lock();
784-
inner.table_mapping.clone().into()
784+
inner
785+
.table_mapping
786+
.namespace(TableNamespace::by_component_TODO())
787+
.into()
785788
}
786789
}
787790

crates/value/src/table_mapping.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,13 +439,14 @@ fn table_does_not_exist(table: &TableName) -> ErrorMetadata {
439439
#[derive(Serialize)]
440440
pub struct TableMappingValue(BTreeMap<TableNumber, TableName>);
441441

442-
impl From<TableMapping> for TableMappingValue {
443-
fn from(table_mapping: TableMapping) -> Self {
442+
impl From<NamespacedTableMapping> for TableMappingValue {
443+
fn from(table_mapping: NamespacedTableMapping) -> Self {
444444
TableMappingValue(
445445
table_mapping
446446
.iter()
447-
.filter(|(_, _, _, name)| !name.is_system())
448-
.map(|(_, _, number, name)| (number, name.clone()))
447+
.filter(|(_, _, name)| !name.is_system())
448+
.filter(|(tablet_id, ..)| table_mapping.is_active(*tablet_id))
449+
.map(|(_, number, name)| (number, name.clone()))
449450
.collect(),
450451
)
451452
}

npm-packages/convex/src/cli/data.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export const data = new Command("data")
4343
.choices(["asc", "desc"])
4444
.default("desc"),
4545
)
46+
.addOption(
47+
new Option(
48+
"--component-path <path>",
49+
"Path to the component in the component tree defined in convex.config.ts.\n" +
50+
" By default, inspects data in the root component",
51+
// TODO(ENG-6967): Remove hideHelp before launching components
52+
).hideHelp(),
53+
)
4654
.addDeploymentSelectionOptions(actionDescription("Inspect the database in"))
4755
.showHelpAfterError()
4856
.action(async (tableName, options) => {
@@ -59,9 +67,16 @@ export const data = new Command("data")
5967
await listDocuments(ctx, deploymentUrl, adminKey, tableName, {
6068
...options,
6169
order: options.order as "asc" | "desc",
70+
componentPath: options.componentPath ?? "",
6271
});
6372
} else {
64-
await listTables(ctx, deploymentUrl, adminKey, deploymentName);
73+
await listTables(
74+
ctx,
75+
deploymentUrl,
76+
adminKey,
77+
deploymentName,
78+
options.componentPath ?? "",
79+
);
6580
}
6681
});
6782

@@ -70,12 +85,14 @@ async function listTables(
7085
deploymentUrl: string,
7186
adminKey: string,
7287
deploymentName: string | undefined,
88+
componentPath: string,
7389
) {
7490
const tables = (await runPaginatedQuery(
7591
ctx,
7692
deploymentUrl,
7793
adminKey,
7894
"_system/cli/tables",
95+
componentPath,
7996
{},
8097
)) as { name: string }[];
8198
if (tables.length === 0) {
@@ -100,13 +117,15 @@ async function listDocuments(
100117
options: {
101118
limit: number;
102119
order: "asc" | "desc";
120+
componentPath: string;
103121
},
104122
) {
105123
const data = (await runPaginatedQuery(
106124
ctx,
107125
deploymentUrl,
108126
adminKey,
109127
"_system/cli/tableData",
128+
options.componentPath,
110129
{
111130
table: tableName,
112131
order: options.order ?? "desc",

npm-packages/convex/src/cli/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const envGet = new Command("get")
9393
url,
9494
adminKey,
9595
"_system/cli/queryEnvironmentVariables:get",
96+
undefined,
9697
{ name: envVarName },
9798
)) as EnvVar | null;
9899
if (envVar === null) {
@@ -145,6 +146,7 @@ const envList = new Command("list")
145146
url,
146147
adminKey,
147148
"_system/cli/queryEnvironmentVariables",
149+
undefined,
148150
{},
149151
)) as EnvVar[];
150152
if (envs.length === 0) {

npm-packages/convex/src/cli/functionSpec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const functionSpec = new Command("function-spec")
3434
deploymentUrl,
3535
adminKey,
3636
"_system/cli/modules:apiSpec",
37+
undefined,
3738
{},
3839
)) as any[];
3940

npm-packages/convex/src/cli/lib/localDeployment/upgrade.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ async function handleUpgrade(
146146
deploymentUrl,
147147
args.adminKey,
148148
"_system/cli/queryEnvironmentVariables",
149+
undefined,
149150
{},
150151
)) as Array<{
151152
name: string;

npm-packages/convex/src/cli/lib/run.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export async function runPaginatedQuery(
5555
deploymentUrl: string,
5656
adminKey: string,
5757
functionName: string,
58+
componentPath: string | undefined,
5859
args: Record<string, Value>,
5960
limit?: number,
6061
) {
@@ -67,6 +68,7 @@ export async function runPaginatedQuery(
6768
deploymentUrl,
6869
adminKey,
6970
functionName,
71+
componentPath,
7072
{
7173
...args,
7274
// The pagination is limited on the backend, so the 10000
@@ -89,23 +91,30 @@ export async function runQuery(
8991
deploymentUrl: string,
9092
adminKey: string,
9193
functionName: string,
94+
componentPath: string | undefined,
9295
args: Record<string, Value>,
9396
): Promise<Value> {
94-
const client = new ConvexHttpClient(deploymentUrl);
95-
client.setAdminAuth(adminKey);
96-
97-
try {
98-
return await client.query(
99-
makeFunctionReference<"query">(functionName),
100-
args,
101-
);
102-
} catch (err) {
103-
return await ctx.crash({
104-
exitCode: 1,
105-
errorType: "invalid filesystem or env vars",
106-
printedMessage: `Failed to run query "${functionName}":\n${chalk.red((err as Error).toString().trim())}`,
107-
});
108-
}
97+
let onResult: (result: Value) => void;
98+
const resultPromise = new Promise<Value>((resolve) => {
99+
onResult = resolve;
100+
});
101+
const [donePromise, onDone] = waitUntilCalled();
102+
await subscribe(
103+
ctx,
104+
deploymentUrl,
105+
adminKey,
106+
functionName,
107+
args,
108+
componentPath,
109+
donePromise,
110+
{
111+
onChange: (result) => {
112+
onDone();
113+
onResult(result);
114+
},
115+
},
116+
);
117+
return resultPromise;
109118
}
110119

111120
export function formatValue(value: Value) {
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { paginationOptsValidator } from "convex/server";
22
import { queryPrivateSystem } from "../secretSystemTables";
3-
import { maximumBytesRead, maximumRowsRead } from "../paginationLimits";
3+
import { performOp } from "udf-syscall-ffi";
44

55
export default queryPrivateSystem({
66
args: {
77
paginationOpts: paginationOptsValidator,
88
},
9-
handler: async (ctx, { paginationOpts }) => {
10-
const results = await ctx.db
11-
.query("_tables")
12-
.filter((q) => q.eq(q.field("state"), "active"))
13-
.paginate({ ...paginationOpts, maximumBytesRead, maximumRowsRead });
9+
handler: async () => {
10+
const tables: Record<number, string> = performOp(
11+
"getTableMappingWithoutSystemTables",
12+
);
13+
// We don't need to paginate but keep the PaginationResult return type for backwards
14+
// compatibility.
1415
return {
15-
...results,
16-
page: results.page
17-
.filter((table) => !table.name.startsWith("_"))
18-
.map((table) => ({ name: table.name })),
16+
page: Object.values(tables).map((name) => ({ name })),
17+
isDone: true,
18+
continueCursor: "end",
1919
};
2020
},
2121
});

npm-packages/system-udfs/convex/schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ export default defineSchema({
242242
v.literal("deleting"),
243243
v.literal("hidden"),
244244
),
245+
namespace: v.optional(
246+
v.object({
247+
kind: v.literal("byComponent"),
248+
id: v.string(),
249+
}),
250+
),
245251
}),
246252
_components: defineTable({
247253
definitionId: v.id("_component_definitions"),

0 commit comments

Comments
 (0)