### Getting Started with npm Source: https://github.com/get-convex/convex-auth-example/blob/main/README.md Install project dependencies and start the development server using npm commands. ```sh npm install npm run dev ``` -------------------------------- ### Using a Convex Query in React (TypeScript) Source: https://github.com/get-convex/convex-auth-example/blob/main/convex/README.md Demonstrates how to call a defined Convex query function from a React component using the 'useQuery' hook provided by Convex, passing the required arguments. ```TypeScript const data = useQuery(api.functions.myQueryFunction, { first: 10, second: "hello", }); ``` -------------------------------- ### Defining a Convex Query Function (TypeScript) Source: https://github.com/get-convex/convex-auth-example/blob/main/convex/README.md Defines a Convex query function that reads data from the database. It includes argument validation using 'convex/values' and demonstrates reading documents using 'ctx.db.query'. ```TypeScript // functions.js 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; }, }); ``` -------------------------------- ### Defining a Convex Mutation Function (TypeScript) Source: https://github.com/get-convex/convex-auth-example/blob/main/convex/README.md Defines a Convex mutation function that writes data to the database. It includes argument validation and demonstrates inserting and retrieving documents using 'ctx.db.insert' and 'ctx.db.get'. ```TypeScript // functions.js 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(id); }, }); ``` -------------------------------- ### Using a Convex Mutation in React (TypeScript) Source: https://github.com/get-convex/convex-auth-example/blob/main/convex/README.md Shows how to call a defined Convex mutation function from a React component using the 'useMutation' hook. It illustrates both the fire-and-forget pattern and handling the promise result. ```TypeScript const mutation = useMutation(api.functions.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) ); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.