Welcome to your Convex functions directory!
Write your Convex functions here.
See https://docs.convex.dev/functions for more.
A query function that takes two arguments looks like:
// convex/myFunctions.ts
import { query } from './_generated/server';
import { v } from 'convex/values';
export const myQueryFunction = query({
// Validators for arguments.
args: {
first: v.number(),
second: v.string()
},
// Function implementation.
handler: async (ctx, args) => {
// Read the database as many times as you need here.
// See https://docs.convex.dev/database/reading-data.
const documents = await ctx.db.query('tablename').collect();
// Arguments passed from the client are properties of the args object.
console.log(args.first, args.second);
// Write arbitrary JavaScript here: filter, aggregate, build derived data,
// remove non-public properties, or create new objects.
return documents;
}
});
Using this query function in a React component looks like:
const data = useQuery(api.myFunctions.myQueryFunction, {
first: 10,
second: 'hello'
});
A mutation function looks like:
// convex/myFunctions.ts
import { mutation } from './_generated/server';
import { v } from 'convex/values';
export const myMutationFunction = mutation({
// Validators for arguments.
args: {
first: v.string(),
second: v.string()
},
// Function implementation.
handler: async (ctx, args) => {
// Insert or modify documents in the database here.
// Mutations can also read from the database like queries.
// See https://docs.convex.dev/database/writing-data.
const message = { body: args.first, author: args.second };
const id = await ctx.db.insert('messages', message);
// Optionally, return a value from your mutation.
return await ctx.db.get('messages', id);
}
});
Using this mutation function in a React component looks like:
const mutation = useMutation(api.myFunctions.myMutationFunction);
function handleButtonPress() {
// fire and forget, the most common way to use mutations
mutation({ first: 'Hello!', second: 'me' });
// OR
// use the result once the mutation has completed
mutation({ first: 'Hello!', second: 'me' }).then((result) => console.log(result));
}
Use the Convex CLI to push your functions to a deployment. See everything
the Convex CLI can do by running npx convex -h in your project root
directory. To learn more, launch the docs with npx convex docs.
source
1# Welcome to your Convex functions directory!
2
3Write your Convex functions here.
4See https://docs.convex.dev/functions for more.
5
6A query function that takes two arguments looks like:
7
8```ts
9// convex/myFunctions.ts
10import { query } from './_generated/server';
11import { v } from 'convex/values';
12
13export const myQueryFunction = query({
14 // Validators for arguments.
15 args: {
16 first: v.number(),
17 second: v.string()
18 },
19
20 // Function implementation.
21 handler: async (ctx, args) => {
22 // Read the database as many times as you need here.
23 // See https://docs.convex.dev/database/reading-data.
24 const documents = await ctx.db.query('tablename').collect();
25
26 // Arguments passed from the client are properties of the args object.
27 console.log(args.first, args.second);
28
29 // Write arbitrary JavaScript here: filter, aggregate, build derived data,
30 // remove non-public properties, or create new objects.
31 return documents;
32 }
33});
34```
35
36Using this query function in a React component looks like:
37
38```ts
39const data = useQuery(api.myFunctions.myQueryFunction, {
40 first: 10,
41 second: 'hello'
42});
43```
44
45A mutation function looks like:
46
47```ts
48// convex/myFunctions.ts
49import { mutation } from './_generated/server';
50import { v } from 'convex/values';
51
52export const myMutationFunction = mutation({
53 // Validators for arguments.
54 args: {
55 first: v.string(),
56 second: v.string()
57 },
58
59 // Function implementation.
60 handler: async (ctx, args) => {
61 // Insert or modify documents in the database here.
62 // Mutations can also read from the database like queries.
63 // See https://docs.convex.dev/database/writing-data.
64 const message = { body: args.first, author: args.second };
65 const id = await ctx.db.insert('messages', message);
66
67 // Optionally, return a value from your mutation.
68 return await ctx.db.get('messages', id);
69 }
70});
71```
72
73Using this mutation function in a React component looks like:
74
75```ts
76const mutation = useMutation(api.myFunctions.myMutationFunction);
77function handleButtonPress() {
78 // fire and forget, the most common way to use mutations
79 mutation({ first: 'Hello!', second: 'me' });
80 // OR
81 // use the result once the mutation has completed
82 mutation({ first: 'Hello!', second: 'me' }).then((result) => console.log(result));
83}
84```
85
86Use the Convex CLI to push your functions to a deployment. See everything
87the Convex CLI can do by running `npx convex -h` in your project root
88directory. To learn more, launch the docs with `npx convex docs`.