Skip to content

Commit c6df63b

Browse files
thomasballingerConvex, Inc.
authored andcommitted
Example of using tsconfig.json option exact-optional-property-types: true (#40969)
GitOrigin-RevId: 3006df5d2b5b742cf7b43978af8daac8d2f3d67f
1 parent 4413b53 commit c6df63b

File tree

19 files changed

+675
-0
lines changed

19 files changed

+675
-0
lines changed

npm-packages/common/config/rush/pnpm-lock.yaml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# To access values in this file from the frontend with Vite, use
2+
# import.meta.env.VARIABLE_NAME_HERE
3+
# this line and below will be removed when publishing demos
4+
VITE_CONVEX_URL="http://127.0.0.1:8000"
5+
# this line and above will be removed when publishing demos
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.DS_Store
3+
dist
4+
.env.local
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# TypeScript
2+
3+
This is a recent TypeScript version with the tsconfig.json option
4+
`exactOptionalPropertyTypes: true` set.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Welcome to your Convex functions directory!
2+
3+
Write your Convex functions here.
4+
See https://docs.convex.dev/functions for more.
5+
6+
A query function that takes two arguments looks like:
7+
8+
```ts
9+
// convex/myFunctions.ts
10+
import { query } from "./_generated/server";
11+
import { v } from "convex/values";
12+
13+
export 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+
36+
Using this query function in a React component looks like:
37+
38+
```ts
39+
const data = useQuery(api.myFunctions.myQueryFunction, {
40+
first: 10,
41+
second: "hello",
42+
});
43+
```
44+
45+
A mutation function looks like:
46+
47+
```ts
48+
// convex/myFunctions.ts
49+
import { mutation } from "./_generated/server";
50+
import { v } from "convex/values";
51+
52+
export 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+
},
70+
});
71+
```
72+
73+
Using this mutation function in a React component looks like:
74+
75+
```ts
76+
const mutation = useMutation(api.myFunctions.myMutationFunction);
77+
function 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+
);
85+
}
86+
```
87+
88+
Use the Convex CLI to push your functions to a deployment. See everything
89+
the Convex CLI can do by running `npx convex -h` in your project root
90+
directory. To learn more, launch the docs with `npx convex docs`.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-disable */
2+
/**
3+
* Generated `api` utility.
4+
*
5+
* THIS CODE IS AUTOMATICALLY GENERATED.
6+
*
7+
* To regenerate, run `npx convex dev`.
8+
* @module
9+
*/
10+
11+
import type {
12+
ApiFromModules,
13+
FilterApi,
14+
FunctionReference,
15+
} from "convex/server";
16+
import type * as messages from "../messages.js";
17+
18+
/**
19+
* A utility for referencing Convex functions in your app's API.
20+
*
21+
* Usage:
22+
* ```js
23+
* const myFunctionReference = api.myModule.myFunction;
24+
* ```
25+
*/
26+
declare const fullApi: ApiFromModules<{
27+
messages: typeof messages;
28+
}>;
29+
export declare const api: FilterApi<
30+
typeof fullApi,
31+
FunctionReference<any, "public">
32+
>;
33+
export declare const internal: FilterApi<
34+
typeof fullApi,
35+
FunctionReference<any, "internal">
36+
>;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-disable */
2+
/**
3+
* Generated `api` utility.
4+
*
5+
* THIS CODE IS AUTOMATICALLY GENERATED.
6+
*
7+
* To regenerate, run `npx convex dev`.
8+
* @module
9+
*/
10+
11+
import { anyApi } from "convex/server";
12+
13+
/**
14+
* A utility for referencing Convex functions in your app's API.
15+
*
16+
* Usage:
17+
* ```js
18+
* const myFunctionReference = api.myModule.myFunction;
19+
* ```
20+
*/
21+
export const api = anyApi;
22+
export const internal = anyApi;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* eslint-disable */
2+
/**
3+
* Generated data model types.
4+
*
5+
* THIS CODE IS AUTOMATICALLY GENERATED.
6+
*
7+
* To regenerate, run `npx convex dev`.
8+
* @module
9+
*/
10+
11+
import type {
12+
DataModelFromSchemaDefinition,
13+
DocumentByName,
14+
TableNamesInDataModel,
15+
SystemTableNames,
16+
} from "convex/server";
17+
import type { GenericId } from "convex/values";
18+
import schema from "../schema.js";
19+
20+
/**
21+
* The names of all of your Convex tables.
22+
*/
23+
export type TableNames = TableNamesInDataModel<DataModel>;
24+
25+
/**
26+
* The type of a document stored in Convex.
27+
*
28+
* @typeParam TableName - A string literal type of the table name (like "users").
29+
*/
30+
export type Doc<TableName extends TableNames> = DocumentByName<
31+
DataModel,
32+
TableName
33+
>;
34+
35+
/**
36+
* An identifier for a document in Convex.
37+
*
38+
* Convex documents are uniquely identified by their `Id`, which is accessible
39+
* on the `_id` field. To learn more, see [Document IDs](https://docs.convex.dev/using/document-ids).
40+
*
41+
* Documents can be loaded using `db.get(id)` in query and mutation functions.
42+
*
43+
* IDs are just strings at runtime, but this type can be used to distinguish them from other
44+
* strings when type checking.
45+
*
46+
* @typeParam TableName - A string literal type of the table name (like "users").
47+
*/
48+
export type Id<TableName extends TableNames | SystemTableNames> =
49+
GenericId<TableName>;
50+
51+
/**
52+
* A type describing your Convex data model.
53+
*
54+
* This type includes information about what tables you have, the type of
55+
* documents stored in those tables, and the indexes defined on them.
56+
*
57+
* This type is used to parameterize methods like `queryGeneric` and
58+
* `mutationGeneric` to make them type-safe.
59+
*/
60+
export type DataModel = DataModelFromSchemaDefinition<typeof schema>;

0 commit comments

Comments
 (0)