|
| 1 | +# Welcome to your Convex functions directory! |
| 2 | + |
| 3 | +Write your Convex functions here. See |
| 4 | +https://docs.convex.dev/using/writing-convex-functions for more. |
| 5 | + |
| 6 | +A query function that takes two arguments looks like: |
| 7 | + |
| 8 | +```javascript |
| 9 | +// myQueryFunction.js |
| 10 | +import { query } from "./_generated/server"; |
| 11 | + |
| 12 | +export default query(async ({ db }, { first, second }) => { |
| 13 | + // Validate arguments here. |
| 14 | + if (typeof first !== "number" || first < 0) { |
| 15 | + throw new Error("First argument is not a non-negative number."); |
| 16 | + } |
| 17 | + if (typeof second !== "string" || second.length > 1000) { |
| 18 | + throw new Error("Second argument is not a string of length 1000 or less."); |
| 19 | + } |
| 20 | + |
| 21 | + // Query the database as many times as you need here. |
| 22 | + // See https://docs.convex.dev/using/database-queries to learn how to write queries. |
| 23 | + const documents = await db.query("tablename").collect(); |
| 24 | + |
| 25 | + // Write arbitrary JavaScript here: filter, aggregate, build derived data, |
| 26 | + // remove non-public properties, or create new objects. |
| 27 | + return documents; |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | +Using this query function in a React component looks like: |
| 32 | + |
| 33 | +```javascript |
| 34 | +const data = useQuery("myQueryFunction", { first: 10, second: "hello" }); |
| 35 | +``` |
| 36 | + |
| 37 | +A mutation function looks like: |
| 38 | + |
| 39 | +```javascript |
| 40 | +// myMutationFunction.js |
| 41 | +import { mutation } from "./_generated/server"; |
| 42 | + |
| 43 | +export default mutation(async ({ db }, { first, second }) => { |
| 44 | + // Validate arguments here. |
| 45 | + if (typeof first !== "string" || typeof second !== "string") { |
| 46 | + throw new Error("Both arguments must be strings"); |
| 47 | + } |
| 48 | + |
| 49 | + // Insert or modify documents in the database here. |
| 50 | + // Mutations can also read from the database like queries. |
| 51 | + const message = { body: first, author: second }; |
| 52 | + const id = await db.insert("messages", message); |
| 53 | + |
| 54 | + // Optionally, return a value from your mutation. |
| 55 | + return await db.get(id); |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +Using this mutation function in a React component looks like: |
| 60 | + |
| 61 | +```javascript |
| 62 | +const mutation = useMutation("myMutationFunction"); |
| 63 | +function handleButtonPress() { |
| 64 | + // fire and forget, the most common way to use mutations |
| 65 | + mutation({ first: "Hello!", second: "me" }); |
| 66 | + // OR |
| 67 | + // use the result once the mutation has completed |
| 68 | + mutation({ first: "Hello!", second: "me" }).then((result) => |
| 69 | + console.log(result) |
| 70 | + ); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +The Convex CLI is your friend. See everything it can do by running |
| 75 | +`npx convex -h` in your project root directory. To learn more, launch the docs |
| 76 | +with `npx convex docs`. |
0 commit comments