From acba80511e2115ed3b3e441e4357d75695255ce8 Mon Sep 17 00:00:00 2001 From: Tom Ballinger Date: Mon, 3 Oct 2022 16:22:16 -0700 Subject: [PATCH] Update Convex demo for v0.2.0 --- .../{dataModel.ts => dataModel.d.ts} | 4 +- .../convex/convex/_generated/dataModel.js | 26 +++++++++ .../_generated/{react.ts => react.d.ts} | 37 ++++++------ examples/convex/convex/_generated/react.js | 57 +++++++++++++++++++ .../_generated/{server.ts => server.d.ts} | 12 ++-- examples/convex/convex/_generated/server.js | 32 +++++++++++ examples/convex/convex/getCounter.ts | 2 +- examples/convex/convex/incrementCounter.ts | 2 +- 8 files changed, 144 insertions(+), 28 deletions(-) rename examples/convex/convex/_generated/{dataModel.ts => dataModel.d.ts} (95%) create mode 100644 examples/convex/convex/_generated/dataModel.js rename examples/convex/convex/_generated/{react.ts => react.d.ts} (73%) create mode 100644 examples/convex/convex/_generated/react.js rename examples/convex/convex/_generated/{server.ts => server.d.ts} (89%) create mode 100644 examples/convex/convex/_generated/server.js diff --git a/examples/convex/convex/_generated/dataModel.ts b/examples/convex/convex/_generated/dataModel.d.ts similarity index 95% rename from examples/convex/convex/_generated/dataModel.ts rename to examples/convex/convex/_generated/dataModel.d.ts index 80646144ba8a4..9991156417571 100644 --- a/examples/convex/convex/_generated/dataModel.ts +++ b/examples/convex/convex/_generated/dataModel.d.ts @@ -4,7 +4,7 @@ * * THIS CODE IS AUTOMATICALLY GENERATED. * - * Generated by convex@0.1.9. + * Generated by convex@0.2.0. * To regenerate, run `npx convex codegen`. * @module */ @@ -46,7 +46,7 @@ export type Document = any; * to the same document. */ export type Id = GenericId; -export const Id = GenericId; +export declare const Id: typeof GenericId; /** * A type describing your Convex data model. diff --git a/examples/convex/convex/_generated/dataModel.js b/examples/convex/convex/_generated/dataModel.js new file mode 100644 index 0000000000000..54938fefac120 --- /dev/null +++ b/examples/convex/convex/_generated/dataModel.js @@ -0,0 +1,26 @@ +/* eslint-disable */ +/** + * Generated data model types. + * + * THIS CODE IS AUTOMATICALLY GENERATED. + * + * Generated by convex@0.2.0. + * To regenerate, run `npx convex codegen`. + * @module + */ + +import { GenericId } from "convex/values"; + +/** + * An identifier for a document in Convex. + * + * Convex documents are uniquely identified by their `Id`, which is accessible + * on the `_id` field. To learn more, see [Data Modeling](https://docs.convex.dev/using/data-modeling). + * + * Documents can be loaded using `db.get(id)` in query and mutation functions. + * + * **Important**: Use `myId.equals(otherId)` to check for equality. + * Using `===` will not work because two different instances of `Id` can refer + * to the same document. + */ +export const Id = GenericId; diff --git a/examples/convex/convex/_generated/react.ts b/examples/convex/convex/_generated/react.d.ts similarity index 73% rename from examples/convex/convex/_generated/react.ts rename to examples/convex/convex/_generated/react.d.ts index 66c8d8ca03647..53b69d9b1246a 100644 --- a/examples/convex/convex/_generated/react.ts +++ b/examples/convex/convex/_generated/react.d.ts @@ -4,15 +4,22 @@ * * THIS CODE IS AUTOMATICALLY GENERATED. * - * Generated by convex@0.1.9. + * Generated by convex@0.2.0. * To regenerate, run `npx convex codegen`. * @module */ -import type getCounter from "../getCounter"; -import type incrementCounter from "../incrementCounter"; -import type { OptimisticLocalStore as GenericOptimisticLocalStore } from "convex/browser"; -import type { ClientMutation, ClientQuery } from "convex/server"; +import type { + ApiFromModules, + OptimisticLocalStore as GenericOptimisticLocalStore, +} from "convex/browser"; +import type { + UseQueryForAPI, + UseMutationForAPI, + UseConvexForAPI, +} from "convex/react"; +import type * as getCounter from "../getCounter"; +import type * as incrementCounter from "../incrementCounter"; /** * A type describing your app's public Convex API. @@ -23,16 +30,10 @@ import type { ClientMutation, ClientQuery } from "convex/server"; * This type should be used with type-parameterized classes like * `ConvexReactClient` to create app-specific types. */ -export type ConvexAPI = { - queries: { - getCounter: ClientQuery; - }; - mutations: { - incrementCounter: ClientMutation; - }; -}; - -import { makeUseQuery, makeUseMutation, makeUseConvex } from "convex/react"; +export type ConvexAPI = ApiFromModules<{ + getCounter: typeof getCounter; + incrementCounter: typeof incrementCounter; +}>; /** * Load a reactive query within a React component. @@ -46,7 +47,7 @@ import { makeUseQuery, makeUseMutation, makeUseConvex } from "convex/react"; * @param args - The arguments to the query function. * @returns `undefined` if loading and the query's return value otherwise. */ -export const useQuery = makeUseQuery(); +export declare const useQuery: UseQueryForAPI; /** * Construct a new {@link ReactMutation}. @@ -64,7 +65,7 @@ export const useQuery = makeUseQuery(); * @param name - The name of the mutation. * @returns The {@link ReactMutation} object with that name. */ -export const useMutation = makeUseMutation(); +export declare const useMutation: UseMutationForAPI; /** * Get the {@link ConvexReactClient} within a React component. @@ -73,7 +74,7 @@ export const useMutation = makeUseMutation(); * * @returns The active {@link ConvexReactClient} object, or `undefined`. */ -export const useConvex = makeUseConvex(); +export declare const useConvex: UseConvexForAPI; /** * A view of the query results currently in the Convex client for use within diff --git a/examples/convex/convex/_generated/react.js b/examples/convex/convex/_generated/react.js new file mode 100644 index 0000000000000..ae2e759bce8d7 --- /dev/null +++ b/examples/convex/convex/_generated/react.js @@ -0,0 +1,57 @@ +/* eslint-disable */ +/** + * Generated React hooks. + * + * THIS CODE IS AUTOMATICALLY GENERATED. + * + * Generated by convex@0.2.0. + * To regenerate, run `npx convex codegen`. + * @module + */ + +import { + useQueryGeneric, + useMutationGeneric, + useConvexGeneric, +} from "convex/react"; + +/** + * Load a reactive query within a React component. + * + * This React hook contains internal state that will cause a rerender whenever + * the query result changes. + * + * This relies on the {@link ConvexProvider} being above in the React component tree. + * + * @param name - The name of the query function. + * @param args - The arguments to the query function. + * @returns `undefined` if loading and the query's return value otherwise. + */ +export const useQuery = useQueryGeneric; + +/** + * Construct a new {@link ReactMutation}. + * + * Mutation objects can be called like functions to request execution of the + * corresponding Convex function, or further configured with + * [optimistic updates](https://docs.convex.dev/using/optimistic-updates). + * + * The value returned by this hook is stable across renders, so it can be used + * by React dependency arrays and memoization logic relying on object identity + * without causing rerenders. + * + * This relies on the {@link ConvexProvider} being above in the React component tree. + * + * @param name - The name of the mutation. + * @returns The {@link ReactMutation} object with that name. + */ +export const useMutation = useMutationGeneric; + +/** + * Get the {@link ConvexReactClient} within a React component. + * + * This relies on the {@link ConvexProvider} being above in the React component tree. + * + * @returns The active {@link ConvexReactClient} object, or `undefined`. + */ +export const useConvex = useConvexGeneric; diff --git a/examples/convex/convex/_generated/server.ts b/examples/convex/convex/_generated/server.d.ts similarity index 89% rename from examples/convex/convex/_generated/server.ts rename to examples/convex/convex/_generated/server.d.ts index c752845659e6c..69df5120c264a 100644 --- a/examples/convex/convex/_generated/server.ts +++ b/examples/convex/convex/_generated/server.d.ts @@ -4,14 +4,14 @@ * * THIS CODE IS AUTOMATICALLY GENERATED. * - * Generated by convex@0.1.9. + * Generated by convex@0.2.0. * To regenerate, run `npx convex codegen`. * @module */ import { - makeQuery, - makeMutation, + MutationBuilderForDataModel, + QueryBuilderForDataModel, QueryCtx as GenericQueryCtx, MutationCtx as GenericMutationCtx, DatabaseReader as GenericDatabaseReader, @@ -27,7 +27,7 @@ import { DataModel } from "./dataModel.js"; * @param func - The query function. It receives a {@link QueryCtx} as its first argument. * @returns The wrapped query. Include this as an `export` to name it and make it accessible. */ -export const query = makeQuery(); +export declare const query: QueryBuilderForDataModel; /** * Define a mutation in this Convex app's public API. @@ -37,7 +37,7 @@ export const query = makeQuery(); * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument. * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible. */ -export const mutation = makeMutation(); +export declare const mutation: MutationBuilderForDataModel; /** * A set of services for use within Convex query functions. @@ -62,7 +62,7 @@ export type MutationCtx = GenericMutationCtx; * An interface to read from the database within Convex query functions. * * The two entry points are {@link DatabaseReader.get}, which fetches a single - * document by its {@link Id}, or {@link DatabaseReader.table}, which starts + * document by its {@link Id}, or {@link DatabaseReader.query}, which starts * building a query. */ export type DatabaseReader = GenericDatabaseReader; diff --git a/examples/convex/convex/_generated/server.js b/examples/convex/convex/_generated/server.js new file mode 100644 index 0000000000000..14579409231f9 --- /dev/null +++ b/examples/convex/convex/_generated/server.js @@ -0,0 +1,32 @@ +/* eslint-disable */ +/** + * Generated utilities for implementing server-side Convex query and mutation functions. + * + * THIS CODE IS AUTOMATICALLY GENERATED. + * + * Generated by convex@0.2.0. + * To regenerate, run `npx convex codegen`. + * @module + */ + +import { queryGeneric, mutationGeneric } from "convex/server"; + +/** + * Define a query in this Convex app's public API. + * + * This function will be allowed to read your Convex database and will be accessible from the client. + * + * @param func - The query function. It receives a {@link QueryCtx} as its first argument. + * @returns The wrapped query. Include this as an `export` to name it and make it accessible. + */ +export const query = queryGeneric; + +/** + * Define a mutation in this Convex app's public API. + * + * This function will be allowed to modify your Convex database and will be accessible from the client. + * + * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument. + * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible. + */ +export const mutation = mutationGeneric; diff --git a/examples/convex/convex/getCounter.ts b/examples/convex/convex/getCounter.ts index 3184094d43fdb..0314dfeaf6293 100644 --- a/examples/convex/convex/getCounter.ts +++ b/examples/convex/convex/getCounter.ts @@ -2,7 +2,7 @@ import { query } from './_generated/server' export default query(async ({ db }, counterName: string): Promise => { const counterDoc = await db - .table('counter_table') + .query('counter_table') .filter((q) => q.eq(q.field('name'), counterName)) .first() console.log('Got stuff') diff --git a/examples/convex/convex/incrementCounter.ts b/examples/convex/convex/incrementCounter.ts index b7f6d035883de..2482ffe0d825e 100644 --- a/examples/convex/convex/incrementCounter.ts +++ b/examples/convex/convex/incrementCounter.ts @@ -3,7 +3,7 @@ import { mutation } from './_generated/server' export default mutation( async ({ db }, counterName: string, increment: number) => { const counterDoc = await db - .table('counter_table') + .query('counter_table') .filter((q) => q.eq(q.field('name'), counterName)) .first() if (counterDoc === null) {