|
| 1 | +import { v } from 'convex/values' |
| 2 | +import { action, mutation, query } from './_generated/server' |
| 3 | +import { api } from './_generated/api' |
| 4 | +import { authComponent } from './auth' |
| 5 | + |
| 6 | +// Write your Convex functions in any file inside this directory (`convex`). |
| 7 | +// See https://docs.convex.dev/functions for more. |
| 8 | + |
| 9 | +// You can read data from the database via a query: |
| 10 | +export const listNumbers = query({ |
| 11 | + // Validators for arguments. |
| 12 | + args: { |
| 13 | + count: v.number(), |
| 14 | + }, |
| 15 | + |
| 16 | + // Query implementation. |
| 17 | + handler: async (ctx, args) => { |
| 18 | + // Get the current authenticated user |
| 19 | + const authUser = await authComponent.getAuthUser(ctx) |
| 20 | + if (!authUser._id) { |
| 21 | + throw new Error('User must be authenticated to list random numbers') |
| 22 | + } |
| 23 | + |
| 24 | + // Read the database as many times as you need here. |
| 25 | + // See https://docs.convex.dev/database/reading-data. |
| 26 | + const numbers = await ctx.db |
| 27 | + .query('numbers') |
| 28 | + .withIndex('userId', (q) => q.eq('userId', authUser._id)) |
| 29 | + // Ordered by _creationTime, return most recent |
| 30 | + .order('desc') |
| 31 | + .take(args.count) |
| 32 | + return { |
| 33 | + viewer: (await ctx.auth.getUserIdentity())?.name ?? null, |
| 34 | + numbers: numbers.reverse().map((number) => number.value), |
| 35 | + } |
| 36 | + }, |
| 37 | +}) |
| 38 | + |
| 39 | +// You can write data to the database via a mutation: |
| 40 | +export const addNumber = mutation({ |
| 41 | + // Validators for arguments. |
| 42 | + args: { |
| 43 | + value: v.number(), |
| 44 | + }, |
| 45 | + |
| 46 | + // Mutation implementation. |
| 47 | + handler: async (ctx, args) => { |
| 48 | + // Get the current authenticated user |
| 49 | + const authUser = await authComponent.getAuthUser(ctx) |
| 50 | + if (!authUser._id) { |
| 51 | + throw new Error('User must be authenticated to create a random number') |
| 52 | + } |
| 53 | + |
| 54 | + // Insert or modify documents in the database here. |
| 55 | + // Mutations can also read from the database like queries. |
| 56 | + // See https://docs.convex.dev/database/writing-data. |
| 57 | + |
| 58 | + const id = await ctx.db.insert('numbers', { |
| 59 | + value: args.value, |
| 60 | + userId: authUser._id, |
| 61 | + }) |
| 62 | + |
| 63 | + console.log('Added new document with id:', id) |
| 64 | + // Optionally, return a value from your mutation. |
| 65 | + // return id; |
| 66 | + }, |
| 67 | +}) |
| 68 | + |
| 69 | +// You can fetch data from and send data to third-party APIs via an action: |
| 70 | +export const myAction = action({ |
| 71 | + // Validators for arguments. |
| 72 | + args: { |
| 73 | + first: v.number(), |
| 74 | + }, |
| 75 | + |
| 76 | + // Action implementation. |
| 77 | + handler: async (ctx, args) => { |
| 78 | + // // Use the browser-like `fetch` API to send HTTP requests. |
| 79 | + // // See https://docs.convex.dev/functions/actions#calling-third-party-apis-and-using-npm-packages. |
| 80 | + // const response = await ctx.fetch("https://api.thirdpartyservice.com"); |
| 81 | + // const data = await response.json(); |
| 82 | + |
| 83 | + // // Query data by running Convex queries. |
| 84 | + const data = await ctx.runQuery(api.myFunctions.listNumbers, { |
| 85 | + count: 10, |
| 86 | + }) |
| 87 | + console.log(data) |
| 88 | + |
| 89 | + // // Write data by running Convex mutations. |
| 90 | + await ctx.runMutation(api.myFunctions.addNumber, { |
| 91 | + value: args.first, |
| 92 | + }) |
| 93 | + }, |
| 94 | +}) |
0 commit comments