Skip to content

Commit 865f17f

Browse files
ianmacartneyConvex, Inc.
authored andcommitted
ian/reference component api (#42813)
- show off & exercise using ComponentApi type from all codegen paths GitOrigin-RevId: 1e6881f347c214e5b903087b1278d001a43e5992
1 parent eb9d226 commit 865f17f

File tree

4 files changed

+56
-96
lines changed

4 files changed

+56
-96
lines changed

npm-packages/private-demos/components-api-import-ts/convex/messages.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { query } from "./_generated/server";
33
import { components } from "./_generated/api";
44
import { Doc } from "./_generated/dataModel";
55
import { v } from "convex/values";
6+
import type { ComponentApi } from "../examples/waitlist@name-with-dashes/_generated/component.js";
7+
8+
const waitlist = components.waitlist satisfies ComponentApi;
69

710
export const list = query(async (ctx): Promise<Doc<"messages">[]> => {
8-
const result = await ctx.runQuery(
9-
components.waitlist.index.sayGoodbyeFromQuery,
10-
{},
11-
);
11+
const result = await ctx.runQuery(waitlist.index.sayGoodbyeFromQuery, {});
1212
console.log(result);
1313
return await ctx.db.query("messages").collect();
1414
});
@@ -24,50 +24,40 @@ export const save = action({
2424
args: { message: v.string() },
2525
returns: v.string(),
2626
handler: async (ctx, { message }) => {
27-
return ctx.runAction(components.waitlist.index.storeInFile, { message });
27+
return ctx.runAction(waitlist.index.storeInFile, { message });
2828
},
2929
});
3030

3131
export const componentTest = action(async (ctx) => {
3232
console.log("calling into component...");
33-
const response = await ctx.runAction(
34-
components.waitlist.index.repeatMessage,
35-
{
36-
message: "hello",
37-
n: 3,
38-
},
39-
);
33+
const response = await ctx.runAction(waitlist.index.repeatMessage, {
34+
message: "hello",
35+
n: 3,
36+
});
4037
console.log("received response from component:", response);
4138
return response;
4239
});
4340

4441
export const scheduleSendWaitlistMessage = mutation(async (ctx) => {
4542
console.log("scheduling message");
46-
await ctx.scheduler.runAfter(
47-
30 * 1000,
48-
components.waitlist.index.scheduleMessage,
49-
{},
50-
);
43+
await ctx.scheduler.runAfter(30 * 1000, waitlist.index.scheduleMessage, {});
5144
console.log(await ctx.db.system.query("_scheduled_functions").collect());
5245
return "scheduled";
5346
});
5447

5548
export const testPartialRollback = mutation(async (ctx) => {
56-
const initialResult = await ctx.runQuery(
57-
components.waitlist.index.latestWrite,
58-
{},
59-
);
49+
const initialResult = await ctx.runQuery(waitlist.index.latestWrite, {});
6050
console.log(initialResult);
61-
await ctx.runMutation(components.waitlist.index.writeSuccessfully, {
51+
await ctx.runMutation(waitlist.index.writeSuccessfully, {
6252
text: "hello",
6353
});
6454
try {
65-
await ctx.runMutation(components.waitlist.index.writeThenFail, {
55+
await ctx.runMutation(waitlist.index.writeThenFail, {
6656
text: "world",
6757
});
6858
} catch (e) {
6959
console.log("caught error", e);
7060
}
71-
const result = await ctx.runQuery(components.waitlist.index.latestWrite, {});
61+
const result = await ctx.runQuery(waitlist.index.latestWrite, {});
7262
console.log(result);
7363
});

npm-packages/private-demos/components-api-import/convex/messages.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { query } from "./_generated/server";
33
import { components } from "./_generated/api";
44
import { Doc } from "./_generated/dataModel";
55
import { v } from "convex/values";
6+
import type { ComponentApi } from "../examples/waitlist@name-with-dashes/_generated/component.js";
7+
8+
const waitlist = components.waitlist satisfies ComponentApi;
69

710
export const list = query(async (ctx): Promise<Doc<"messages">[]> => {
8-
const result = await ctx.runQuery(
9-
components.waitlist.index.sayGoodbyeFromQuery,
10-
{},
11-
);
11+
const result = await ctx.runQuery(waitlist.index.sayGoodbyeFromQuery, {});
1212
console.log(result);
1313
return await ctx.db.query("messages").collect();
1414
});
@@ -24,50 +24,40 @@ export const save = action({
2424
args: { message: v.string() },
2525
returns: v.string(),
2626
handler: async (ctx, { message }) => {
27-
return ctx.runAction(components.waitlist.index.storeInFile, { message });
27+
return ctx.runAction(waitlist.index.storeInFile, { message });
2828
},
2929
});
3030

3131
export const componentTest = action(async (ctx) => {
3232
console.log("calling into component...");
33-
const response = await ctx.runAction(
34-
components.waitlist.index.repeatMessage,
35-
{
36-
message: "hello",
37-
n: 3,
38-
},
39-
);
33+
const response = await ctx.runAction(waitlist.index.repeatMessage, {
34+
message: "hello",
35+
n: 3,
36+
});
4037
console.log("received response from component:", response);
4138
return response;
4239
});
4340

4441
export const scheduleSendWaitlistMessage = mutation(async (ctx) => {
4542
console.log("scheduling message");
46-
await ctx.scheduler.runAfter(
47-
30 * 1000,
48-
components.waitlist.index.scheduleMessage,
49-
{},
50-
);
43+
await ctx.scheduler.runAfter(30 * 1000, waitlist.index.scheduleMessage, {});
5144
console.log(await ctx.db.system.query("_scheduled_functions").collect());
5245
return "scheduled";
5346
});
5447

5548
export const testPartialRollback = mutation(async (ctx) => {
56-
const initialResult = await ctx.runQuery(
57-
components.waitlist.index.latestWrite,
58-
{},
59-
);
49+
const initialResult = await ctx.runQuery(waitlist.index.latestWrite, {});
6050
console.log(initialResult);
61-
await ctx.runMutation(components.waitlist.index.writeSuccessfully, {
51+
await ctx.runMutation(waitlist.index.writeSuccessfully, {
6252
text: "hello",
6353
});
6454
try {
65-
await ctx.runMutation(components.waitlist.index.writeThenFail, {
55+
await ctx.runMutation(waitlist.index.writeThenFail, {
6656
text: "world",
6757
});
6858
} catch (e) {
6959
console.log("caught error", e);
7060
}
71-
const result = await ctx.runQuery(components.waitlist.index.latestWrite, {});
61+
const result = await ctx.runQuery(waitlist.index.latestWrite, {});
7262
console.log(result);
7363
});

npm-packages/private-demos/components-legacy-ts/convex/messages.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { query } from "./_generated/server";
33
import { components } from "./_generated/api";
44
import { Doc } from "./_generated/dataModel";
55
import { v } from "convex/values";
6+
import type { ComponentApi } from "../examples/waitlist@name-with-dashes/_generated/component.js";
7+
8+
const waitlist = components.waitlist satisfies ComponentApi;
69

710
export const list = query(async (ctx): Promise<Doc<"messages">[]> => {
8-
const result = await ctx.runQuery(
9-
components.waitlist.index.sayGoodbyeFromQuery,
10-
{},
11-
);
11+
const result = await ctx.runQuery(waitlist.index.sayGoodbyeFromQuery, {});
1212
console.log(result);
1313
return await ctx.db.query("messages").collect();
1414
});
@@ -24,50 +24,40 @@ export const save = action({
2424
args: { message: v.string() },
2525
returns: v.string(),
2626
handler: async (ctx, { message }) => {
27-
return ctx.runAction(components.waitlist.index.storeInFile, { message });
27+
return ctx.runAction(waitlist.index.storeInFile, { message });
2828
},
2929
});
3030

3131
export const componentTest = action(async (ctx) => {
3232
console.log("calling into component...");
33-
const response = await ctx.runAction(
34-
components.waitlist.index.repeatMessage,
35-
{
36-
message: "hello",
37-
n: 3,
38-
},
39-
);
33+
const response = await ctx.runAction(waitlist.index.repeatMessage, {
34+
message: "hello",
35+
n: 3,
36+
});
4037
console.log("received response from component:", response);
4138
return response;
4239
});
4340

4441
export const scheduleSendWaitlistMessage = mutation(async (ctx) => {
4542
console.log("scheduling message");
46-
await ctx.scheduler.runAfter(
47-
30 * 1000,
48-
components.waitlist.index.scheduleMessage,
49-
{},
50-
);
43+
await ctx.scheduler.runAfter(30 * 1000, waitlist.index.scheduleMessage, {});
5144
console.log(await ctx.db.system.query("_scheduled_functions").collect());
5245
return "scheduled";
5346
});
5447

5548
export const testPartialRollback = mutation(async (ctx) => {
56-
const initialResult = await ctx.runQuery(
57-
components.waitlist.index.latestWrite,
58-
{},
59-
);
49+
const initialResult = await ctx.runQuery(waitlist.index.latestWrite, {});
6050
console.log(initialResult);
61-
await ctx.runMutation(components.waitlist.index.writeSuccessfully, {
51+
await ctx.runMutation(waitlist.index.writeSuccessfully, {
6252
text: "hello",
6353
});
6454
try {
65-
await ctx.runMutation(components.waitlist.index.writeThenFail, {
55+
await ctx.runMutation(waitlist.index.writeThenFail, {
6656
text: "world",
6757
});
6858
} catch (e) {
6959
console.log("caught error", e);
7060
}
71-
const result = await ctx.runQuery(components.waitlist.index.latestWrite, {});
61+
const result = await ctx.runQuery(waitlist.index.latestWrite, {});
7262
console.log(result);
7363
});

npm-packages/private-demos/components-legacy/convex/messages.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { query } from "./_generated/server";
33
import { components } from "./_generated/api";
44
import { Doc } from "./_generated/dataModel";
55
import { v } from "convex/values";
6+
import type { ComponentApi } from "../examples/waitlist@name-with-dashes/_generated/component.js";
7+
8+
const waitlist = components.waitlist satisfies ComponentApi;
69

710
export const list = query(async (ctx): Promise<Doc<"messages">[]> => {
8-
const result = await ctx.runQuery(
9-
components.waitlist.index.sayGoodbyeFromQuery,
10-
{},
11-
);
11+
const result = await ctx.runQuery(waitlist.index.sayGoodbyeFromQuery, {});
1212
console.log(result);
1313
return await ctx.db.query("messages").collect();
1414
});
@@ -24,50 +24,40 @@ export const save = action({
2424
args: { message: v.string() },
2525
returns: v.string(),
2626
handler: async (ctx, { message }) => {
27-
return ctx.runAction(components.waitlist.index.storeInFile, { message });
27+
return ctx.runAction(waitlist.index.storeInFile, { message });
2828
},
2929
});
3030

3131
export const componentTest = action(async (ctx) => {
3232
console.log("calling into component...");
33-
const response = await ctx.runAction(
34-
components.waitlist.index.repeatMessage,
35-
{
36-
message: "hello",
37-
n: 3,
38-
},
39-
);
33+
const response = await ctx.runAction(waitlist.index.repeatMessage, {
34+
message: "hello",
35+
n: 3,
36+
});
4037
console.log("received response from component:", response);
4138
return response;
4239
});
4340

4441
export const scheduleSendWaitlistMessage = mutation(async (ctx) => {
4542
console.log("scheduling message");
46-
await ctx.scheduler.runAfter(
47-
30 * 1000,
48-
components.waitlist.index.scheduleMessage,
49-
{},
50-
);
43+
await ctx.scheduler.runAfter(30 * 1000, waitlist.index.scheduleMessage, {});
5144
console.log(await ctx.db.system.query("_scheduled_functions").collect());
5245
return "scheduled";
5346
});
5447

5548
export const testPartialRollback = mutation(async (ctx) => {
56-
const initialResult = await ctx.runQuery(
57-
components.waitlist.index.latestWrite,
58-
{},
59-
);
49+
const initialResult = await ctx.runQuery(waitlist.index.latestWrite, {});
6050
console.log(initialResult);
61-
await ctx.runMutation(components.waitlist.index.writeSuccessfully, {
51+
await ctx.runMutation(waitlist.index.writeSuccessfully, {
6252
text: "hello",
6353
});
6454
try {
65-
await ctx.runMutation(components.waitlist.index.writeThenFail, {
55+
await ctx.runMutation(waitlist.index.writeThenFail, {
6656
text: "world",
6757
});
6858
} catch (e) {
6959
console.log("caught error", e);
7060
}
71-
const result = await ctx.runQuery(components.waitlist.index.latestWrite, {});
61+
const result = await ctx.runQuery(waitlist.index.latestWrite, {});
7262
console.log(result);
7363
});

0 commit comments

Comments
 (0)