@@ -7,66 +7,66 @@ A query function that takes two arguments looks like:
77
88``` ts
99// functions.js
10- import { query } from " ./_generated/server" ;
11- import { v } from " convex/values" ;
10+ import { query } from ' ./_generated/server' ;
11+ import { v } from ' convex/values' ;
1212
1313export 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- },
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+ }
3333});
3434```
3535
3636Using this query function in a React component looks like:
3737
3838``` ts
3939const data = useQuery (api .functions .myQueryFunction , {
40- first: 10 ,
41- second: " hello" ,
40+ first: 10 ,
41+ second: ' hello'
4242});
4343```
4444
4545A mutation function looks like:
4646
4747``` ts
4848// functions.js
49- import { mutation } from " ./_generated/server" ;
50- import { v } from " convex/values" ;
49+ import { mutation } from ' ./_generated/server' ;
50+ import { v } from ' convex/values' ;
5151
5252export 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 (id );
69- },
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 (id );
69+ }
7070});
7171```
7272
@@ -75,13 +75,11 @@ Using this mutation function in a React component looks like:
7575``` ts
7676const mutation = useMutation (api .functions .myMutationFunction );
7777function 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 ) =>
83- console .log (result )
84- );
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 ));
8583}
8684```
8785
0 commit comments