convex 1.42.0 → 1.42.2-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/browser.bundle.js +24 -6
- package/dist/browser.bundle.js.map +2 -2
- package/dist/cjs/browser/simple_client.js +2 -2
- package/dist/cjs/browser/simple_client.js.map +2 -2
- package/dist/cjs/browser/sync/authentication_manager.js +21 -4
- package/dist/cjs/browser/sync/authentication_manager.js.map +2 -2
- package/dist/cjs/browser/sync/client.js +2 -1
- package/dist/cjs/browser/sync/client.js.map +2 -2
- package/dist/cjs/cli/dev.js +1 -0
- package/dist/cjs/cli/dev.js.map +2 -2
- package/dist/cjs/cli/generatedApi.js.map +1 -1
- package/dist/cjs/cli/lib/api.js.map +2 -2
- package/dist/cjs/cli/lib/typecheck.js +28 -10
- package/dist/cjs/cli/lib/typecheck.js.map +2 -2
- package/dist/cjs/cli/lib/utils/utils.js +4 -12
- package/dist/cjs/cli/lib/utils/utils.js.map +2 -2
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/react/client.js +1 -1
- package/dist/cjs/react/client.js.map +2 -2
- package/dist/cjs/react-clerk/ConvexProviderWithClerk.js +2 -1
- package/dist/cjs/react-clerk/ConvexProviderWithClerk.js.map +2 -2
- package/dist/cjs/server/database.js.map +1 -1
- package/dist/cjs/server/impl/meta_impl.js +2 -2
- package/dist/cjs/server/impl/meta_impl.js.map +2 -2
- package/dist/cjs/server/impl/query_impl.js.map +2 -2
- package/dist/cjs/server/impl/registration_impl.js +0 -2
- package/dist/cjs/server/impl/registration_impl.js.map +2 -2
- package/dist/cjs/server/meta.js.map +1 -1
- package/dist/cjs/server/query.js.map +1 -1
- package/dist/cjs/server/registration.js.map +1 -1
- package/dist/cjs/server/schema.js.map +1 -1
- package/dist/cjs-types/browser/simple_client.d.ts +1 -2
- package/dist/cjs-types/browser/simple_client.d.ts.map +1 -1
- package/dist/cjs-types/browser/sync/authentication_manager.d.ts +2 -0
- package/dist/cjs-types/browser/sync/authentication_manager.d.ts.map +1 -1
- package/dist/cjs-types/browser/sync/client.d.ts +16 -0
- package/dist/cjs-types/browser/sync/client.d.ts.map +1 -1
- package/dist/cjs-types/cli/dev.d.ts.map +1 -1
- package/dist/cjs-types/cli/dev.test.d.ts +2 -0
- package/dist/cjs-types/cli/dev.test.d.ts.map +1 -0
- package/dist/cjs-types/cli/generatedApi.d.ts +0 -1
- package/dist/cjs-types/cli/generatedApi.d.ts.map +1 -1
- package/dist/cjs-types/cli/lib/api.d.ts +0 -1
- package/dist/cjs-types/cli/lib/api.d.ts.map +1 -1
- package/dist/cjs-types/cli/lib/utils/utils.d.ts.map +1 -1
- package/dist/cjs-types/index.d.ts +1 -1
- package/dist/cjs-types/index.d.ts.map +1 -1
- package/dist/cjs-types/react/client.d.ts.map +1 -1
- package/dist/cjs-types/react/client_provided_base_client.test.d.ts +2 -0
- package/dist/cjs-types/react/client_provided_base_client.test.d.ts.map +1 -0
- package/dist/cjs-types/react-clerk/ConvexProviderWithClerk.d.ts +1 -0
- package/dist/cjs-types/react-clerk/ConvexProviderWithClerk.d.ts.map +1 -1
- package/dist/cjs-types/server/database.d.ts.map +1 -1
- package/dist/cjs-types/server/impl/query_impl.d.ts.map +1 -1
- package/dist/cjs-types/server/impl/registration_impl.d.ts.map +1 -1
- package/dist/cjs-types/server/meta.d.ts.map +1 -1
- package/dist/cjs-types/server/query.d.ts.map +1 -1
- package/dist/cjs-types/server/registration.d.ts.map +1 -1
- package/dist/cjs-types/server/schema.d.ts.map +1 -1
- package/dist/cli.bundle.cjs +58 -28
- package/dist/cli.bundle.cjs.map +3 -3
- package/dist/esm/browser/simple_client.js +5 -3
- package/dist/esm/browser/simple_client.js.map +2 -2
- package/dist/esm/browser/sync/authentication_manager.js +21 -4
- package/dist/esm/browser/sync/authentication_manager.js.map +2 -2
- package/dist/esm/browser/sync/client.js +2 -1
- package/dist/esm/browser/sync/client.js.map +2 -2
- package/dist/esm/cli/dev.js +3 -1
- package/dist/esm/cli/dev.js.map +2 -2
- package/dist/esm/cli/lib/api.js.map +2 -2
- package/dist/esm/cli/lib/typecheck.js +28 -10
- package/dist/esm/cli/lib/typecheck.js.map +3 -3
- package/dist/esm/cli/lib/utils/utils.js +4 -12
- package/dist/esm/cli/lib/utils/utils.js.map +2 -2
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react/client.js +1 -1
- package/dist/esm/react/client.js.map +2 -2
- package/dist/esm/react-clerk/ConvexProviderWithClerk.js +2 -1
- package/dist/esm/react-clerk/ConvexProviderWithClerk.js.map +2 -2
- package/dist/esm/server/impl/meta_impl.js +2 -2
- package/dist/esm/server/impl/meta_impl.js.map +2 -2
- package/dist/esm/server/impl/query_impl.js.map +2 -2
- package/dist/esm/server/impl/registration_impl.js +0 -2
- package/dist/esm/server/impl/registration_impl.js.map +2 -2
- package/dist/esm/server/schema.js.map +1 -1
- package/dist/esm-types/browser/simple_client.d.ts +1 -2
- package/dist/esm-types/browser/simple_client.d.ts.map +1 -1
- package/dist/esm-types/browser/sync/authentication_manager.d.ts +2 -0
- package/dist/esm-types/browser/sync/authentication_manager.d.ts.map +1 -1
- package/dist/esm-types/browser/sync/client.d.ts +16 -0
- package/dist/esm-types/browser/sync/client.d.ts.map +1 -1
- package/dist/esm-types/cli/dev.d.ts.map +1 -1
- package/dist/esm-types/cli/dev.test.d.ts +2 -0
- package/dist/esm-types/cli/dev.test.d.ts.map +1 -0
- package/dist/esm-types/cli/generatedApi.d.ts +0 -1
- package/dist/esm-types/cli/generatedApi.d.ts.map +1 -1
- package/dist/esm-types/cli/lib/api.d.ts +0 -1
- package/dist/esm-types/cli/lib/api.d.ts.map +1 -1
- package/dist/esm-types/cli/lib/utils/utils.d.ts.map +1 -1
- package/dist/esm-types/index.d.ts +1 -1
- package/dist/esm-types/index.d.ts.map +1 -1
- package/dist/esm-types/react/client.d.ts.map +1 -1
- package/dist/esm-types/react/client_provided_base_client.test.d.ts +2 -0
- package/dist/esm-types/react/client_provided_base_client.test.d.ts.map +1 -0
- package/dist/esm-types/react-clerk/ConvexProviderWithClerk.d.ts +1 -0
- package/dist/esm-types/react-clerk/ConvexProviderWithClerk.d.ts.map +1 -1
- package/dist/esm-types/server/database.d.ts.map +1 -1
- package/dist/esm-types/server/impl/query_impl.d.ts.map +1 -1
- package/dist/esm-types/server/impl/registration_impl.d.ts.map +1 -1
- package/dist/esm-types/server/meta.d.ts.map +1 -1
- package/dist/esm-types/server/query.d.ts.map +1 -1
- package/dist/esm-types/server/registration.d.ts.map +1 -1
- package/dist/esm-types/server/schema.d.ts.map +1 -1
- package/dist/react.bundle.js +25 -7
- package/dist/react.bundle.js.map +2 -2
- package/package.json +2 -2
- package/src/browser/simple_client.ts +12 -10
- package/src/browser/sync/authentication_manager.ts +42 -14
- package/src/browser/sync/client.ts +17 -0
- package/src/cli/dev.test.ts +157 -0
- package/src/cli/dev.ts +7 -0
- package/src/cli/generatedApi.ts +0 -1
- package/src/cli/lib/api.ts +0 -1
- package/src/cli/lib/config.test.ts +1 -4
- package/src/cli/lib/typecheck.ts +26 -10
- package/src/cli/lib/utils/utils.ts +4 -12
- package/src/index.ts +1 -1
- package/src/react/ConvexAuthState.test.tsx +145 -0
- package/src/react/client.ts +23 -6
- package/src/react/client_provided_base_client.test.tsx +56 -0
- package/src/react-clerk/ConvexProviderWithClerk.tsx +3 -1
- package/src/server/database.ts +6 -4
- package/src/server/impl/meta_impl.ts +2 -2
- package/src/server/impl/query_impl.ts +1 -3
- package/src/server/impl/registration_impl.ts +0 -2
- package/src/server/meta.ts +10 -0
- package/src/server/query.ts +9 -6
- package/src/server/registration.ts +10 -10
- package/src/server/schema.ts +3 -2
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/react/client.ts"],
|
|
4
|
-
"sourcesContent": ["import { BaseConvexClient } from \"../browser/index.js\";\nimport type {\n OptimisticUpdate,\n PaginatedQueryToken,\n QueryToken,\n PaginationStatus,\n} from \"../browser/index.js\";\nimport React, { useCallback, useContext, useMemo } from \"react\";\nimport { convexToJson, Value } from \"../values/index.js\";\nimport { QueryJournal } from \"../browser/sync/protocol.js\";\nimport {\n AuthTokenFetcher,\n BaseConvexClientOptions,\n ConnectionState,\n} from \"../browser/sync/client.js\";\nimport type { UserIdentityAttributes } from \"../browser/sync/protocol.js\";\nimport { RequestForQueries, useQueries } from \"./use_queries.js\";\nimport { useSubscription } from \"./use_subscription.js\";\nimport { parseArgs } from \"../common/index.js\";\nimport {\n ArgsAndOptions,\n FunctionArgs,\n FunctionReference,\n FunctionReturnType,\n OptionalRestArgs,\n getFunctionName,\n makeFunctionReference,\n} from \"../server/api.js\";\nimport { EmptyObject } from \"../server/registration.js\";\nimport {\n instantiateDefaultLogger,\n instantiateNoopLogger,\n Logger,\n} from \"../browser/logging.js\";\nimport type { QueryOptions } from \"../browser/query_options.js\";\nimport { LoadMoreOfPaginatedQuery } from \"../browser/sync/pagination.js\";\nimport {\n PaginatedQueryClient,\n ExtendedTransition,\n} from \"../browser/sync/paginated_query_client.js\";\n\n// When no arguments are passed, extend subscriptions (for APIs that do this by default)\n// for this amount after the subscription would otherwise be dropped.\nconst DEFAULT_EXTEND_SUBSCRIPTION_FOR = 5_000;\n\nif (typeof React === \"undefined\") {\n throw new Error(\"Required dependency 'react' not found\");\n}\n\n// TODO Typedoc doesn't generate documentation for the comment below perhaps\n// because it's a callable interface.\n/**\n * An interface to execute a Convex mutation function on the server.\n *\n * @public\n */\nexport interface ReactMutation<Mutation extends FunctionReference<\"mutation\">> {\n /**\n * Execute the mutation on the server, returning a `Promise` of its return value.\n *\n * @param args - Arguments for the mutation to pass up to the server.\n * @returns The return value of the server-side function call.\n */\n (...args: OptionalRestArgs<Mutation>): Promise<FunctionReturnType<Mutation>>;\n\n /**\n * Define an optimistic update to apply as part of this mutation.\n *\n * This is a temporary update to the local query results to facilitate a\n * fast, interactive UI. It enables query results to update before a mutation\n * executed on the server.\n *\n * When the mutation is invoked, the optimistic update will be applied.\n *\n * Optimistic updates can also be used to temporarily remove queries from the\n * client and create loading experiences until a mutation completes and the\n * new query results are synced.\n *\n * The update will be automatically rolled back when the mutation is fully\n * completed and queries have been updated.\n *\n * @param optimisticUpdate - The optimistic update to apply.\n * @returns A new `ReactMutation` with the update configured.\n *\n * @public\n */\n withOptimisticUpdate<T extends OptimisticUpdate<FunctionArgs<Mutation>>>(\n optimisticUpdate: T &\n (ReturnType<T> extends Promise<any>\n ? \"Optimistic update handlers must be synchronous\"\n : {}),\n ): ReactMutation<Mutation>;\n}\n\n// Exported only for testing.\nexport function createMutation(\n mutationReference: FunctionReference<\"mutation\">,\n client: ConvexReactClient,\n update?: OptimisticUpdate<any>,\n): ReactMutation<any> {\n function mutation(args?: Record<string, Value>): Promise<unknown> {\n assertNotAccidentalArgument(args);\n\n return client.mutation(mutationReference, args, {\n optimisticUpdate: update,\n });\n }\n mutation.withOptimisticUpdate = function withOptimisticUpdate(\n optimisticUpdate: OptimisticUpdate<any>,\n ): ReactMutation<any> {\n if (update !== undefined) {\n throw new Error(\n `Already specified optimistic update for mutation ${getFunctionName(\n mutationReference,\n )}`,\n );\n }\n return createMutation(mutationReference, client, optimisticUpdate);\n };\n return mutation as ReactMutation<any>;\n}\n\n/**\n * An interface to execute a Convex action on the server.\n *\n * @public\n */\nexport interface ReactAction<Action extends FunctionReference<\"action\">> {\n /**\n * Execute the function on the server, returning a `Promise` of its return value.\n *\n * @param args - Arguments for the function to pass up to the server.\n * @returns The return value of the server-side function call.\n * @public\n */\n (...args: OptionalRestArgs<Action>): Promise<FunctionReturnType<Action>>;\n}\n\nfunction createAction(\n actionReference: FunctionReference<\"action\">,\n client: ConvexReactClient,\n): ReactAction<any> {\n return function (args?: Record<string, Value>): Promise<unknown> {\n return client.action(actionReference, args);\n } as ReactAction<any>;\n}\n\n// Watches should be stateless: in QueriesObserver we create a watch just to get\n// the current value.\n/**\n * A watch on the output of a Convex query function.\n *\n * @public\n */\nexport interface Watch<T> {\n /**\n * Initiate a watch on the output of a query.\n *\n * This will subscribe to this query and call\n * the callback whenever the query result changes.\n *\n * **Important: If the client is already subscribed to this query with the\n * same arguments this callback will not be invoked until the query result is\n * updated.** To get the current, local result call\n * {@link react.Watch.localQueryResult}.\n *\n * @param callback - Function that is called whenever the query result changes.\n * @returns - A function that disposes of the subscription.\n */\n onUpdate(callback: () => void): () => void;\n\n /**\n * Get the current result of a query.\n *\n * This will only return a result if we're already subscribed to the query\n * and have received a result from the server or the query value has been set\n * optimistically.\n *\n * @returns The result of the query or `undefined` if it isn't known.\n * @throws An error if the query encountered an error on the server.\n */\n localQueryResult(): T | undefined;\n\n /**\n * @internal\n */\n localQueryLogs(): string[] | undefined;\n\n /**\n * Get the current {@link browser.QueryJournal} for this query.\n *\n * If we have not yet received a result for this query, this will be `undefined`.\n */\n journal(): QueryJournal | undefined;\n}\n\n/**\n * A watch on the output of a paginated Convex query function.\n *\n * @public\n */\nexport interface PaginatedWatch<T> {\n /**\n * Initiate a watch on the output of a paginated query.\n *\n * This will subscribe to this query and call\n * the callback whenever the query result changes.\n *\n * @param callback - Function that is called whenever the query result changes.\n * @returns - A function that disposes of the subscription.\n */\n onUpdate(callback: () => void): () => void;\n\n /**\n * Get the current result of a paginated query.\n *\n * @returns The current results, status, and loadMore function, or `undefined` if not loaded.\n */\n localQueryResult():\n | {\n results: T[];\n status: PaginationStatus;\n loadMore: LoadMoreOfPaginatedQuery;\n }\n | undefined;\n}\n\n/**\n * Options for {@link ConvexReactClient.watchQuery}.\n *\n * @public\n */\nexport interface WatchQueryOptions {\n /**\n * An (optional) journal produced from a previous execution of this query\n * function.\n *\n * If there is an existing subscription to a query function with the same\n * name and arguments, this journal will have no effect.\n */\n journal?: QueryJournal;\n\n /**\n * @internal\n */\n componentPath?: string;\n}\n\n/**\n * Options for {@link ConvexReactClient.watchPaginatedQuery}.\n *\n * @internal\n */\nexport interface WatchPaginatedQueryOptions {\n /**\n * The initial number of items to load.\n */\n initialNumItems: number;\n\n // We may be able to remove this in the future, but to preserve the existing behavior of\n // usePaginatedQuery() it's still here.\n id: number;\n\n /**\n * @internal\n */\n componentPath?: string;\n}\n\n/**\n * Options for {@link ConvexReactClient.mutation}.\n *\n * @public\n */\nexport interface MutationOptions<Args extends Record<string, Value>> {\n /**\n * An optimistic update to apply along with this mutation.\n *\n * An optimistic update locally updates queries while a mutation is pending.\n * Once the mutation completes, the update will be rolled back.\n */\n optimisticUpdate?: OptimisticUpdate<Args> | undefined;\n}\n\n/**\n * Options for {@link ConvexReactClient}.\n *\n * @public\n */\nexport interface ConvexReactClientOptions extends BaseConvexClientOptions {}\n\n/**\n * A Convex client for use within React.\n *\n * This loads reactive queries and executes mutations over a WebSocket.\n *\n * @public\n */\nexport class ConvexReactClient {\n private address: string;\n private cachedSync?: BaseConvexClient | undefined;\n private cachedPaginatedQueryClient?: PaginatedQueryClient | undefined;\n private listeners: Map<QueryToken | PaginatedQueryToken, Set<() => void>>;\n private options: ConvexReactClientOptions;\n // \"closed\" means this client is done, not just that the underlying WS connection is closed.\n private closed = false;\n private _logger: Logger;\n\n private adminAuth?: string;\n private fakeUserIdentity?: UserIdentityAttributes | undefined;\n\n /**\n * @param address - The url of your Convex deployment, often provided\n * by an environment variable. E.g. `https://small-mouse-123.convex.cloud`.\n * @param options - See {@link ConvexReactClientOptions} for a full description.\n */\n constructor(address: string, options?: ConvexReactClientOptions) {\n // Validate address immediately since validation by the lazily-instantiated\n // internal client does not occur synchronously.\n if (address === undefined) {\n throw new Error(\n \"No address provided to ConvexReactClient.\\n\" +\n \"If trying to deploy to production, make sure to follow all the instructions found at https://docs.convex.dev/production/hosting/\\n\" +\n \"If running locally, make sure to run `convex dev` and ensure the .env.local file is populated.\",\n );\n }\n if (typeof address !== \"string\") {\n throw new Error(\n `ConvexReactClient requires a URL like 'https://happy-otter-123.convex.cloud', received something of type ${typeof address} instead.`,\n );\n }\n if (!address.includes(\"://\")) {\n throw new Error(\"Provided address was not an absolute URL.\");\n }\n this.address = address;\n this.listeners = new Map();\n this._logger =\n options?.logger === false\n ? instantiateNoopLogger({ verbose: options?.verbose ?? false })\n : options?.logger !== true && options?.logger\n ? options.logger\n : instantiateDefaultLogger({ verbose: options?.verbose ?? false });\n this.options = { ...options, logger: this._logger };\n }\n\n /**\n * Return the address for this client, useful for creating a new client.\n *\n * Not guaranteed to match the address with which this client was constructed:\n * it may be canonicalized.\n */\n get url() {\n return this.address;\n }\n\n /**\n * Lazily instantiate the `BaseConvexClient` so we don't create the WebSocket\n * when server-side rendering.\n *\n * @internal\n */\n get sync() {\n if (this.closed) {\n throw new Error(\"ConvexReactClient has already been closed.\");\n }\n if (this.cachedSync) {\n return this.cachedSync;\n }\n // BaseConvexClient and paginated query client are always created together.\n this.cachedSync = new BaseConvexClient(\n this.address,\n () => {}, // Use the PaginatedQueryClient's transition instead.\n this.options,\n );\n if (this.adminAuth) {\n this.cachedSync.setAdminAuth(this.adminAuth, this.fakeUserIdentity);\n }\n this.cachedPaginatedQueryClient = new PaginatedQueryClient(\n this.cachedSync,\n (transition) => this.handleTransition(transition),\n );\n return this.cachedSync;\n }\n\n /**\n * Lazily instantiate the `PaginatedQueryClient` so we don't create it\n * when server-side rendering.\n *\n * @internal\n */\n get paginatedQueryClient() {\n // access sync to instantiate the clients\n this.sync;\n if (this.cachedPaginatedQueryClient) {\n return this.cachedPaginatedQueryClient;\n }\n throw new Error(\"Should already be instantiated\");\n }\n\n /**\n * Set the authentication token to be used for subsequent queries and mutations.\n * `fetchToken` will be called automatically again if a token expires.\n * `fetchToken` should return `null` if the token cannot be retrieved, for example\n * when the user's rights were permanently revoked.\n * @param fetchToken - an async function returning the JWT-encoded OpenID Connect Identity Token\n * @param onChange - a callback that will be called when the authentication status changes\n * @param onRefreshChange - a callback called with `true` when the socket is paused to fetch a replacement token after a server rejection, and `false` when refresh completes\n */\n setAuth(\n fetchToken: AuthTokenFetcher,\n onChange?: (isAuthenticated: boolean) => void,\n onRefreshChange?: (isRefreshing: boolean) => void,\n ) {\n if (typeof fetchToken === \"string\") {\n throw new Error(\n \"Passing a string to ConvexReactClient.setAuth is no longer supported, \" +\n \"please upgrade to passing in an async function to handle reauthentication.\",\n );\n }\n this.sync.setAuth(\n fetchToken,\n onChange ??\n (() => {\n // Do nothing\n }),\n onRefreshChange,\n );\n }\n\n /**\n * Clear the current authentication token if set.\n */\n clearAuth() {\n this.sync.clearAuth();\n }\n\n /**\n * @internal\n */\n setAdminAuth(token: string, identity?: UserIdentityAttributes) {\n this.adminAuth = token;\n this.fakeUserIdentity = identity;\n if (this.closed) {\n throw new Error(\"ConvexReactClient has already been closed.\");\n }\n if (this.cachedSync) {\n this.sync.setAdminAuth(token, identity);\n }\n }\n\n /**\n * Construct a new {@link Watch} on a Convex query function.\n *\n * **Most application code should not call this method directly. Instead use\n * the {@link useQuery} hook.**\n *\n * The act of creating a watch does nothing, a Watch is stateless.\n *\n * @param query - A {@link server.FunctionReference} for the public query to run.\n * @param args - An arguments object for the query. If this is omitted,\n * the arguments will be `{}`.\n * @param options - A {@link WatchQueryOptions} options object for this query.\n *\n * @returns The {@link Watch} object.\n */\n watchQuery<Query extends FunctionReference<\"query\">>(\n query: Query,\n ...argsAndOptions: ArgsAndOptions<Query, WatchQueryOptions>\n ): Watch<FunctionReturnType<Query>> {\n const [args, options] = argsAndOptions;\n const name = getFunctionName(query);\n\n return {\n onUpdate: (callback) => {\n const { queryToken, unsubscribe } = this.sync.subscribe(\n name as string,\n args,\n options,\n );\n\n const currentListeners = this.listeners.get(queryToken);\n if (currentListeners !== undefined) {\n currentListeners.add(callback);\n } else {\n this.listeners.set(queryToken, new Set([callback]));\n }\n\n return () => {\n if (this.closed) {\n return;\n }\n\n const currentListeners = this.listeners.get(queryToken)!;\n currentListeners.delete(callback);\n if (currentListeners.size === 0) {\n this.listeners.delete(queryToken);\n }\n unsubscribe();\n };\n },\n\n localQueryResult: () => {\n // Use the cached client because we can't have a query result if we don't\n // even have a client yet!\n if (this.cachedSync) {\n return this.cachedSync.localQueryResult(name, args);\n }\n return undefined;\n },\n\n localQueryLogs: () => {\n if (this.cachedSync) {\n return this.cachedSync.localQueryLogs(name, args);\n }\n return undefined;\n },\n\n journal: () => {\n if (this.cachedSync) {\n return this.cachedSync.queryJournal(name, args);\n }\n return undefined;\n },\n };\n }\n\n // Let's try out a queryOptions-style API.\n // This method is similar to the React Query API `queryClient.prefetchQuery()`.\n // In the future an ensureQueryData(): Promise<Data> method could exist.\n /**\n * Indicates likely future interest in a query subscription.\n *\n * The implementation currently immediately subscribes to a query. In the future this method\n * may prioritize some queries over others, fetch the query result without subscribing, or\n * do nothing in slow network connections or high load scenarios.\n *\n * To use this in a React component, call useQuery() and ignore the return value.\n *\n * @param queryOptions - A query (function reference from an api object) and its args, plus\n * an optional extendSubscriptionFor for how long to subscribe to the query.\n */\n prewarmQuery<Query extends FunctionReference<\"query\">>(\n queryOptions: QueryOptions<Query> & { extendSubscriptionFor?: number },\n ) {\n const extendSubscriptionFor =\n queryOptions.extendSubscriptionFor ?? DEFAULT_EXTEND_SUBSCRIPTION_FOR;\n const watch = this.watchQuery(queryOptions.query, queryOptions.args || {});\n const unsubscribe = watch.onUpdate(() => {});\n setTimeout(unsubscribe, extendSubscriptionFor);\n }\n\n /**\n * Construct a new {@link PaginatedWatch} on a Convex paginated query function.\n *\n * **Most application code should not call this method directly. Instead use\n * the {@link usePaginatedQuery} hook.**\n *\n * The act of creating a watch does nothing, a Watch is stateless.\n *\n * @param query - A {@link server.FunctionReference} for the public query to run.\n * @param args - An arguments object for the query. If this is omitted,\n * the arguments will be `{}`.\n * @param options - A {@link WatchPaginatedQueryOptions} options object for this query.\n *\n * @returns The {@link PaginatedWatch} object.\n *\n * @internal\n */\n watchPaginatedQuery<Query extends FunctionReference<\"query\">>(\n query: Query,\n args: Query[\"_args\"],\n options: WatchPaginatedQueryOptions,\n ): PaginatedWatch<FunctionReturnType<Query>> {\n const name = getFunctionName(query);\n\n return {\n onUpdate: (callback) => {\n const { paginatedQueryToken, unsubscribe } =\n this.paginatedQueryClient.subscribe(name, args || {}, options);\n\n const currentListeners = this.listeners.get(paginatedQueryToken);\n if (currentListeners !== undefined) {\n currentListeners.add(callback);\n } else {\n this.listeners.set(paginatedQueryToken, new Set([callback]));\n }\n\n return () => {\n if (this.closed) {\n return;\n }\n\n const currentListeners = this.listeners.get(paginatedQueryToken)!;\n currentListeners.delete(callback);\n if (currentListeners.size === 0) {\n this.listeners.delete(paginatedQueryToken);\n }\n unsubscribe();\n };\n },\n\n localQueryResult: () => {\n // Use our new paginated query client\n return this.paginatedQueryClient.localQueryResult(name, args, options);\n },\n };\n }\n\n /**\n * Execute a mutation function.\n *\n * @param mutation - A {@link server.FunctionReference} for the public mutation\n * to run.\n * @param args - An arguments object for the mutation. If this is omitted,\n * the arguments will be `{}`.\n * @param options - A {@link MutationOptions} options object for the mutation.\n * @returns A promise of the mutation's result.\n */\n mutation<Mutation extends FunctionReference<\"mutation\">>(\n mutation: Mutation,\n ...argsAndOptions: ArgsAndOptions<\n Mutation,\n MutationOptions<FunctionArgs<Mutation>>\n >\n ): Promise<FunctionReturnType<Mutation>> {\n const [args, options] = argsAndOptions;\n const name = getFunctionName(mutation);\n return this.sync.mutation(name, args, options);\n }\n\n /**\n * Execute an action function.\n *\n * @param action - A {@link server.FunctionReference} for the public action\n * to run.\n * @param args - An arguments object for the action. If this is omitted,\n * the arguments will be `{}`.\n * @returns A promise of the action's result.\n */\n action<Action extends FunctionReference<\"action\">>(\n action: Action,\n ...args: OptionalRestArgs<Action>\n ): Promise<FunctionReturnType<Action>> {\n const name = getFunctionName(action);\n return this.sync.action(name, ...args);\n }\n\n /**\n * Fetch a query result once.\n *\n * **Most application code should subscribe to queries instead, using\n * the {@link useQuery} hook.**\n *\n * @param query - A {@link server.FunctionReference} for the public query\n * to run.\n * @param args - An arguments object for the query. If this is omitted,\n * the arguments will be `{}`.\n * @returns A promise of the query's result.\n */\n query<Query extends FunctionReference<\"query\">>(\n query: Query,\n ...args: OptionalRestArgs<Query>\n ): Promise<FunctionReturnType<Query>> {\n const watch = this.watchQuery(query, ...args);\n const existingResult = watch.localQueryResult();\n if (existingResult !== undefined) {\n return Promise.resolve(existingResult);\n }\n return new Promise((resolve, reject) => {\n const unsubscribe = watch.onUpdate(() => {\n unsubscribe();\n try {\n resolve(watch.localQueryResult());\n } catch (e) {\n reject(e);\n }\n });\n });\n }\n\n /**\n * Get the current {@link ConnectionState} between the client and the Convex\n * backend.\n *\n * @returns The {@link ConnectionState} with the Convex backend.\n */\n connectionState(): ConnectionState {\n return this.sync.connectionState();\n }\n\n /**\n * Subscribe to the {@link ConnectionState} between the client and the Convex\n * backend, calling a callback each time it changes.\n *\n * Subscribed callbacks will be called when any part of ConnectionState changes.\n * ConnectionState may grow in future versions (e.g. to provide a array of\n * inflight requests) in which case callbacks would be called more frequently.\n * ConnectionState may also *lose* properties in future versions as we figure\n * out what information is most useful. As such this API is considered unstable.\n *\n * @returns An unsubscribe function to stop listening.\n */\n subscribeToConnectionState(\n cb: (connectionState: ConnectionState) => void,\n ): () => void {\n return this.sync.subscribeToConnectionState(cb);\n }\n\n /**\n * Get the logger for this client.\n *\n * @returns The {@link Logger} for this client.\n */\n get logger(): Logger {\n return this._logger;\n }\n\n /**\n * Close any network handles associated with this client and stop all subscriptions.\n *\n * Call this method when you're done with a {@link ConvexReactClient} to\n * dispose of its sockets and resources.\n *\n * @returns A `Promise` fulfilled when the connection has been completely closed.\n */\n async close(): Promise<void> {\n this.closed = true;\n // Prevent outstanding React batched updates from invoking listeners.\n this.listeners = new Map();\n if (this.cachedPaginatedQueryClient) {\n this.cachedPaginatedQueryClient = undefined;\n }\n if (this.cachedSync) {\n const sync = this.cachedSync;\n this.cachedSync = undefined;\n await sync.close();\n }\n }\n\n /**\n * Handle transitions from both base client and paginated client.\n * This ensures all transitions are processed synchronously and in order.\n */\n private handleTransition(transition: ExtendedTransition) {\n const simple = transition.queries.map((q) => q.token);\n const paginated = transition.paginatedQueries.map((q) => q.token);\n this.transition([...simple, ...paginated]);\n }\n\n private transition(updatedQueries: (QueryToken | PaginatedQueryToken)[]) {\n for (const queryToken of updatedQueries) {\n const callbacks = this.listeners.get(queryToken);\n if (callbacks) {\n for (const callback of callbacks) {\n callback();\n }\n }\n }\n }\n}\n\nconst ConvexContext = React.createContext<ConvexReactClient>(\n undefined as unknown as ConvexReactClient, // in the future this will be a mocked client for testing\n);\n\n/**\n * Get the {@link ConvexReactClient} within a React component.\n *\n * This relies on the {@link ConvexProvider} being above in the React component tree.\n *\n * @returns The active {@link ConvexReactClient} object, or `undefined`.\n *\n * @public\n */\nexport function useConvex(): ConvexReactClient {\n return useContext(ConvexContext);\n}\n\n/**\n * Provides an active Convex {@link ConvexReactClient} to descendants of this component.\n *\n * Wrap your app in this component to use Convex hooks `useQuery`,\n * `useMutation`, and `useConvex`.\n *\n * @param props - an object with a `client` property that refers to a {@link ConvexReactClient}.\n *\n * @public\n */\nexport const ConvexProvider: React.FC<{\n client: ConvexReactClient;\n children?: React.ReactNode;\n}> = ({ client, children }) => {\n return React.createElement(\n ConvexContext.Provider,\n { value: client },\n children,\n );\n};\n\nexport type OptionalRestArgsOrSkip<FuncRef extends FunctionReference<any>> =\n FuncRef[\"_args\"] extends EmptyObject\n ? [args?: EmptyObject | \"skip\"]\n : [args: FuncRef[\"_args\"] | \"skip\"];\n\n/**\n * Result returned by object-form {@link useQuery_experimental}.\n *\n * @public\n */\nexport type UseQueryResult<QueryResult, ThrowOnError extends boolean = false> =\n | { status: \"pending\" }\n | { status: \"success\"; data: QueryResult }\n | (ThrowOnError extends true ? never : { status: \"error\"; error: Error });\n\ntype UseQueryOptions<\n Query extends FunctionReference<\"query\">,\n ThrowOnError extends boolean,\n> = {\n query: Query;\n args: FunctionArgs<Query> | \"skip\";\n throwOnError?: ThrowOnError;\n};\n\n/**\n * Load a reactive query within a React component.\n *\n * This React hook subscribes to a Convex query and causes a rerender whenever\n * the query result changes. The subscription is managed automatically --\n * it starts when the component mounts and stops when it unmounts.\n *\n * Throws an error if not used under {@link ConvexProvider}.\n *\n * @example\n * ```tsx\n * import { useQuery } from \"convex/react\";\n * import { api } from \"../convex/_generated/api\";\n *\n * function TaskList() {\n * // Reactively loads tasks, re-renders when data changes:\n * const tasks = useQuery(api.tasks.list, { completed: false });\n *\n * // Returns `undefined` while loading:\n * if (tasks === undefined) return <div>Loading...</div>;\n *\n * return tasks.map((task) => <div key={task._id}>{task.text}</div>);\n * }\n *\n * // Pass \"skip\" to conditionally disable the query:\n * function MaybeProfile({ userId }: { userId?: Id<\"users\"> }) {\n * const profile = useQuery(\n * api.users.get,\n * userId ? { userId } : \"skip\",\n * );\n * // ...\n * }\n * ```\n *\n * @param query - a {@link server.FunctionReference} for the public query to run\n * like `api.dir1.dir2.filename.func`.\n * @param args - The arguments to the query function or the string `\"skip\"` if the\n * query should not be loaded.\n * @returns the result of the query. Returns `undefined` while loading.\n *\n * @see https://docs.convex.dev/client/react#fetching-data\n * @public\n */\nexport function useQuery<Query extends FunctionReference<\"query\">>(\n query: Query,\n ...args: OptionalRestArgsOrSkip<Query>\n): Query[\"_returnType\"] | undefined {\n const skip = args[0] === \"skip\";\n const argsObject = args[0] === \"skip\" ? {} : parseArgs(args[0]);\n const queryReference =\n typeof query === \"string\"\n ? makeFunctionReference<\"query\", any, any>(query)\n : query;\n\n const queryName = getFunctionName(queryReference);\n\n const queries = useMemo(\n () =>\n skip\n ? ({} as RequestForQueries)\n : { query: { query: queryReference, args: argsObject } },\n // Stringify args so args that are semantically the same don't trigger a\n // rerender. Saves developers from adding `useMemo` on every args usage.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [JSON.stringify(convexToJson(argsObject)), queryName, skip],\n );\n\n const results = useQueries(queries);\n const result = results[\"query\"];\n\n if (result instanceof Error) {\n throw result;\n }\n return result;\n}\n\n/**\n * Load a reactive query within a React component using an options object.\n *\n * This is an experimental form of {@link useQuery} that accepts a single\n * {@link UseQueryOptions} object instead of positional arguments.\n *\n * Consumers are expected to check the returned object `status` field to\n * make proper use of the result. If an error occurs, it will be present\n * in the result object unless `throwOnError` is `true`, in which case\n * the error will be thrown instead.\n *\n * @example\n * ```tsx\n * import { useQuery_experimental as useQuery } from \"convex/react\";\n * import { api } from \"../convex/_generated/api\";\n *\n * function TaskList() {\n * const state = useQuery({ query: api.tasks.list, args: { completed: false } });\n *\n * if (state.status === \"pending\") return <div>Loading...</div>;\n * if (state.status === \"error\") return <div>Error: {state.error.message}</div>;\n * return state.data.map((task) => <div key={task._id}>{task.text}</div>);\n * }\n * ```\n *\n * @param options - Query options. Pass `args: \"skip\"` to disable the query.\n * @returns the current query state as a {@link UseQueryResult} object.\n *\n * @see https://docs.convex.dev/client/react#fetching-data\n * @public\n */\nexport function useQuery_experimental<\n Query extends FunctionReference<\"query\">,\n ThrowOnError extends boolean = false,\n>(\n options: UseQueryOptions<Query, ThrowOnError>,\n): UseQueryResult<Query[\"_returnType\"], ThrowOnError>;\n\nexport function useQuery_experimental<\n Query extends FunctionReference<\"query\">,\n ThrowOnError extends boolean = false,\n>(\n options: UseQueryOptions<Query, ThrowOnError>,\n): UseQueryResult<Query[\"_returnType\"], false> {\n const throwOnError = options.throwOnError ?? false;\n const queryReference =\n typeof options.query === \"string\"\n ? (makeFunctionReference<\"query\", any, any>(options.query) as Query)\n : options.query;\n const skip = options.args === \"skip\";\n const argsObject = !skip\n ? parseArgs(options.args as Record<string, Value>)\n : {};\n\n const queryName = getFunctionName(queryReference);\n const queries = useMemo(\n () =>\n skip\n ? ({} as RequestForQueries)\n : { query: { query: queryReference, args: argsObject } },\n // Stringify args so args that are semantically the same don't trigger a\n // rerender. Saves developers from adding `useMemo` on every args usage.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [JSON.stringify(convexToJson(argsObject)), queryName, skip],\n );\n\n const results = useQueries(queries);\n const result = results[\"query\"];\n\n if (result instanceof Error) {\n if (throwOnError) {\n throw result;\n }\n return {\n error: result,\n status: \"error\",\n };\n }\n\n if (result === undefined) {\n return {\n status: \"pending\",\n };\n }\n\n return {\n data: result,\n status: \"success\",\n };\n}\n\n/**\n * Construct a new {@link ReactMutation}.\n *\n * Returns a function that you can call to execute a Convex mutation. The\n * returned function is stable across renders (same reference identity), so\n * it can be safely used in dependency arrays and memoization.\n *\n * Mutations can optionally be configured with\n * [optimistic updates](https://docs.convex.dev/client/react/optimistic-updates)\n * for instant UI feedback.\n *\n * Throws an error if not used under {@link ConvexProvider}.\n *\n * @example\n * ```tsx\n * import { useMutation } from \"convex/react\";\n * import { api } from \"../convex/_generated/api\";\n *\n * function CreateTask() {\n * const createTask = useMutation(api.tasks.create);\n *\n * const handleClick = async () => {\n * await createTask({ text: \"New task\" });\n * };\n *\n * return <button onClick={handleClick}>Add Task</button>;\n * }\n * ```\n *\n * @param mutation - A {@link server.FunctionReference} for the public mutation\n * to run like `api.dir1.dir2.filename.func`.\n * @returns The {@link ReactMutation} object with that name.\n *\n * @see https://docs.convex.dev/client/react#editing-data\n * @public\n */\nexport function useMutation<Mutation extends FunctionReference<\"mutation\">>(\n mutation: Mutation,\n): ReactMutation<Mutation> {\n const mutationReference =\n typeof mutation === \"string\"\n ? makeFunctionReference<\"mutation\", any, any>(mutation)\n : mutation;\n\n const convex = useContext(ConvexContext);\n if (convex === undefined) {\n throw new Error(\n \"Could not find Convex client! `useMutation` must be used in the React component \" +\n \"tree under `ConvexProvider`. Did you forget it? \" +\n \"See https://docs.convex.dev/quick-start#set-up-convex-in-your-react-app\",\n );\n }\n return useMemo(\n () => createMutation(mutationReference, convex),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [convex, getFunctionName(mutationReference)],\n );\n}\n\n/**\n * Construct a new {@link ReactAction}.\n *\n * Returns a function that you can call to execute a Convex action. Actions\n * can call third-party APIs and perform side effects. The returned function\n * is stable across renders (same reference identity).\n *\n * **Error handling:** Actions can fail (e.g., if an external API is down).\n * Always wrap action calls in try/catch or handle the rejected promise.\n *\n * **Note:** In most cases, calling an action directly from a client is an\n * anti-pattern. Prefer having the client call a mutation that captures the\n * user's intent (by writing to the database) and then schedules the action\n * via `ctx.scheduler.runAfter`. This ensures the intent is durably recorded\n * even if the client disconnects.\n *\n * Throws an error if not used under {@link ConvexProvider}.\n *\n * @example\n * ```tsx\n * import { useAction } from \"convex/react\";\n * import { api } from \"../convex/_generated/api\";\n *\n * function GenerateSummary() {\n * const generate = useAction(api.ai.generateSummary);\n *\n * const handleClick = async () => {\n * try {\n * const summary = await generate({ text: \"Some long text...\" });\n * console.log(summary);\n * } catch (error) {\n * console.error(\"Action failed:\", error);\n * }\n * };\n *\n * return <button onClick={handleClick}>Generate</button>;\n * }\n * ```\n *\n * @param action - A {@link server.FunctionReference} for the public action\n * to run like `api.dir1.dir2.filename.func`.\n * @returns The {@link ReactAction} object with that name.\n *\n * @see https://docs.convex.dev/functions/actions#calling-actions-from-clients\n * @public\n */\nexport function useAction<Action extends FunctionReference<\"action\">>(\n action: Action,\n): ReactAction<Action> {\n const convex = useContext(ConvexContext);\n const actionReference =\n typeof action === \"string\"\n ? makeFunctionReference<\"action\", any, any>(action)\n : action;\n\n if (convex === undefined) {\n throw new Error(\n \"Could not find Convex client! `useAction` must be used in the React component \" +\n \"tree under `ConvexProvider`. Did you forget it? \" +\n \"See https://docs.convex.dev/quick-start#set-up-convex-in-your-react-app\",\n );\n }\n return useMemo(\n () => createAction(actionReference, convex),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [convex, getFunctionName(actionReference)],\n );\n}\n\n/**\n * React hook to get the current {@link ConnectionState} and subscribe to changes.\n *\n * This hook returns the current connection state and automatically rerenders\n * when any part of the connection state changes (e.g., when going online/offline,\n * when requests start/complete, etc.).\n *\n * The shape of ConnectionState may change in the future which may cause this\n * hook to rerender more frequently.\n *\n * Throws an error if not used under {@link ConvexProvider}.\n *\n * @returns The current {@link ConnectionState} with the Convex backend.\n *\n * @public\n */\nexport function useConvexConnectionState(): ConnectionState {\n const convex = useContext(ConvexContext);\n if (convex === undefined) {\n throw new Error(\n \"Could not find Convex client! `useConvexConnectionState` must be used in the React component \" +\n \"tree under `ConvexProvider`. Did you forget it? \" +\n \"See https://docs.convex.dev/quick-start#set-up-convex-in-your-react-app\",\n );\n }\n\n const getCurrentValue = useCallback(() => {\n return convex.connectionState();\n }, [convex]);\n\n const subscribe = useCallback(\n (callback: () => void) => {\n return convex.subscribeToConnectionState(() => {\n callback();\n });\n },\n [convex],\n );\n\n return useSubscription({ getCurrentValue, subscribe });\n}\n\n// When a function is called with a single argument that looks like a\n// React SyntheticEvent it was likely called as an event handler.\nfunction assertNotAccidentalArgument(value: any) {\n // these are properties of a React.SyntheticEvent\n // https://reactjs.org/docs/events.html\n if (\n typeof value === \"object\" &&\n value !== null &&\n \"bubbles\" in value &&\n \"persist\" in value &&\n \"isDefaultPrevented\" in value\n ) {\n throw new Error(\n `Convex function called with SyntheticEvent object. Did you use a Convex function as an event handler directly? Event handlers like onClick receive an event object as their first argument. These SyntheticEvent objects are not valid Convex values. Try wrapping the function like \\`const handler = () => myMutation();\\` and using \\`handler\\` in the event handler.`,\n );\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAAiC;AAOjC,mBAAwD;AACxD,oBAAoC;AAQpC,yBAA8C;AAC9C,8BAAgC;AAChC,oBAA0B;AAC1B,iBAQO;AAEP,qBAIO;AAGP,oCAGO;AAIP,MAAM,kCAAkC;AAExC,IAAI,OAAO,aAAAA,YAAU,aAAa;AAChC,QAAM,IAAI,MAAM,uCAAuC;AACzD;AAgDO,SAAS,eACd,mBACA,QACA,QACoB;AACpB,WAAS,SAAS,MAAgD;AAChE,gCAA4B,IAAI;AAEhC,WAAO,OAAO,SAAS,mBAAmB,MAAM;AAAA,MAC9C,kBAAkB;AAAA,IACpB,CAAC;AAAA,EACH;AACA,WAAS,uBAAuB,SAAS,qBACvC,kBACoB;AACpB,QAAI,WAAW,QAAW;AACxB,YAAM,IAAI;AAAA,QACR,wDAAoD;AAAA,UAClD;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AACA,WAAO,eAAe,mBAAmB,QAAQ,gBAAgB;AAAA,EACnE;AACA,SAAO;AACT;AAkBA,SAAS,aACP,iBACA,QACkB;AAClB,SAAO,SAAU,MAAgD;AAC/D,WAAO,OAAO,OAAO,iBAAiB,IAAI;AAAA,EAC5C;AACF;
|
|
4
|
+
"sourcesContent": ["import { BaseConvexClient } from \"../browser/index.js\";\nimport type {\n OptimisticUpdate,\n PaginatedQueryToken,\n QueryToken,\n PaginationStatus,\n} from \"../browser/index.js\";\nimport React, { useCallback, useContext, useMemo } from \"react\";\nimport { convexToJson, Value } from \"../values/index.js\";\nimport { QueryJournal } from \"../browser/sync/protocol.js\";\nimport {\n AuthTokenFetcher,\n BaseConvexClientOptions,\n ConnectionState,\n} from \"../browser/sync/client.js\";\nimport type { UserIdentityAttributes } from \"../browser/sync/protocol.js\";\nimport { RequestForQueries, useQueries } from \"./use_queries.js\";\nimport { useSubscription } from \"./use_subscription.js\";\nimport { parseArgs } from \"../common/index.js\";\nimport {\n ArgsAndOptions,\n FunctionArgs,\n FunctionReference,\n FunctionReturnType,\n OptionalRestArgs,\n getFunctionName,\n makeFunctionReference,\n} from \"../server/api.js\";\nimport { EmptyObject } from \"../server/registration.js\";\nimport {\n instantiateDefaultLogger,\n instantiateNoopLogger,\n Logger,\n} from \"../browser/logging.js\";\nimport type { QueryOptions } from \"../browser/query_options.js\";\nimport { LoadMoreOfPaginatedQuery } from \"../browser/sync/pagination.js\";\nimport {\n PaginatedQueryClient,\n ExtendedTransition,\n} from \"../browser/sync/paginated_query_client.js\";\n\n// When no arguments are passed, extend subscriptions (for APIs that do this by default)\n// for this amount after the subscription would otherwise be dropped.\nconst DEFAULT_EXTEND_SUBSCRIPTION_FOR = 5_000;\n\nif (typeof React === \"undefined\") {\n throw new Error(\"Required dependency 'react' not found\");\n}\n\n// TODO Typedoc doesn't generate documentation for the comment below perhaps\n// because it's a callable interface.\n/**\n * An interface to execute a Convex mutation function on the server.\n *\n * @public\n */\nexport interface ReactMutation<Mutation extends FunctionReference<\"mutation\">> {\n /**\n * Execute the mutation on the server, returning a `Promise` of its return value.\n *\n * @param args - Arguments for the mutation to pass up to the server.\n * @returns The return value of the server-side function call.\n */\n (...args: OptionalRestArgs<Mutation>): Promise<FunctionReturnType<Mutation>>;\n\n /**\n * Define an optimistic update to apply as part of this mutation.\n *\n * This is a temporary update to the local query results to facilitate a\n * fast, interactive UI. It enables query results to update before a mutation\n * executed on the server.\n *\n * When the mutation is invoked, the optimistic update will be applied.\n *\n * Optimistic updates can also be used to temporarily remove queries from the\n * client and create loading experiences until a mutation completes and the\n * new query results are synced.\n *\n * The update will be automatically rolled back when the mutation is fully\n * completed and queries have been updated.\n *\n * @param optimisticUpdate - The optimistic update to apply.\n * @returns A new `ReactMutation` with the update configured.\n *\n * @public\n */\n withOptimisticUpdate<T extends OptimisticUpdate<FunctionArgs<Mutation>>>(\n optimisticUpdate: T &\n (ReturnType<T> extends Promise<any>\n ? \"Optimistic update handlers must be synchronous\"\n : {}),\n ): ReactMutation<Mutation>;\n}\n\n// Exported only for testing.\nexport function createMutation(\n mutationReference: FunctionReference<\"mutation\">,\n client: ConvexReactClient,\n update?: OptimisticUpdate<any>,\n): ReactMutation<any> {\n function mutation(args?: Record<string, Value>): Promise<unknown> {\n assertNotAccidentalArgument(args);\n\n return client.mutation(mutationReference, args, {\n optimisticUpdate: update,\n });\n }\n mutation.withOptimisticUpdate = function withOptimisticUpdate(\n optimisticUpdate: OptimisticUpdate<any>,\n ): ReactMutation<any> {\n if (update !== undefined) {\n throw new Error(\n `Already specified optimistic update for mutation ${getFunctionName(\n mutationReference,\n )}`,\n );\n }\n return createMutation(mutationReference, client, optimisticUpdate);\n };\n return mutation as ReactMutation<any>;\n}\n\n/**\n * An interface to execute a Convex action on the server.\n *\n * @public\n */\nexport interface ReactAction<Action extends FunctionReference<\"action\">> {\n /**\n * Execute the function on the server, returning a `Promise` of its return value.\n *\n * @param args - Arguments for the function to pass up to the server.\n * @returns The return value of the server-side function call.\n * @public\n */\n (...args: OptionalRestArgs<Action>): Promise<FunctionReturnType<Action>>;\n}\n\nfunction createAction(\n actionReference: FunctionReference<\"action\">,\n client: ConvexReactClient,\n): ReactAction<any> {\n return function (args?: Record<string, Value>): Promise<unknown> {\n return client.action(actionReference, args);\n } as ReactAction<any>;\n}\n\n// Watches should be stateless: in QueriesObserver we create a watch just to get\n// the current value.\n/**\n * A watch on the output of a Convex query function.\n *\n * @public\n */\nexport interface Watch<T> {\n /**\n * Initiate a watch on the output of a query.\n *\n * This will subscribe to this query and call\n * the callback whenever the query result changes.\n *\n * **Important: If the client is already subscribed to this query with the\n * same arguments this callback will not be invoked until the query result is\n * updated.** To get the current, local result call\n * {@link react.Watch.localQueryResult}.\n *\n * @param callback - Function that is called whenever the query result changes.\n * @returns - A function that disposes of the subscription.\n */\n onUpdate(callback: () => void): () => void;\n\n /**\n * Get the current result of a query.\n *\n * This will only return a result if we're already subscribed to the query\n * and have received a result from the server or the query value has been set\n * optimistically.\n *\n * @returns The result of the query or `undefined` if it isn't known.\n * @throws An error if the query encountered an error on the server.\n */\n localQueryResult(): T | undefined;\n\n /**\n * @internal\n */\n localQueryLogs(): string[] | undefined;\n\n /**\n * Get the current {@link browser.QueryJournal} for this query.\n *\n * If we have not yet received a result for this query, this will be `undefined`.\n */\n journal(): QueryJournal | undefined;\n}\n\n/**\n * A watch on the output of a paginated Convex query function.\n *\n * @public\n */\nexport interface PaginatedWatch<T> {\n /**\n * Initiate a watch on the output of a paginated query.\n *\n * This will subscribe to this query and call\n * the callback whenever the query result changes.\n *\n * @param callback - Function that is called whenever the query result changes.\n * @returns - A function that disposes of the subscription.\n */\n onUpdate(callback: () => void): () => void;\n\n /**\n * Get the current result of a paginated query.\n *\n * @returns The current results, status, and loadMore function, or `undefined` if not loaded.\n */\n localQueryResult():\n | {\n results: T[];\n status: PaginationStatus;\n loadMore: LoadMoreOfPaginatedQuery;\n }\n | undefined;\n}\n\n/**\n * Options for {@link ConvexReactClient.watchQuery}.\n *\n * @public\n */\nexport interface WatchQueryOptions {\n /**\n * An (optional) journal produced from a previous execution of this query\n * function.\n *\n * If there is an existing subscription to a query function with the same\n * name and arguments, this journal will have no effect.\n */\n journal?: QueryJournal;\n\n /**\n * @internal\n */\n componentPath?: string;\n}\n\n/**\n * Options for {@link ConvexReactClient.watchPaginatedQuery}.\n *\n * @internal\n */\nexport interface WatchPaginatedQueryOptions {\n /**\n * The initial number of items to load.\n */\n initialNumItems: number;\n\n // We may be able to remove this in the future, but to preserve the existing behavior of\n // usePaginatedQuery() it's still here.\n id: number;\n\n /**\n * @internal\n */\n componentPath?: string;\n}\n\n/**\n * Options for {@link ConvexReactClient.mutation}.\n *\n * @public\n */\nexport interface MutationOptions<Args extends Record<string, Value>> {\n /**\n * An optimistic update to apply along with this mutation.\n *\n * An optimistic update locally updates queries while a mutation is pending.\n * Once the mutation completes, the update will be rolled back.\n */\n optimisticUpdate?: OptimisticUpdate<Args> | undefined;\n}\n\n/**\n * Options for {@link ConvexReactClient}.\n *\n * @public\n */\nexport interface ConvexReactClientOptions extends BaseConvexClientOptions {\n /**\n * An already-constructed {@link BaseConvexClient} to use instead of\n * constructing one internally. When provided, this client (and its\n * derived {@link PaginatedQueryClient}) is used for all operations.\n *\n * @internal\n */\n baseClient?: BaseConvexClient;\n\n // FIXME: Ideally we define `ConvexReactClientOptions` as\n // `type ConvexReactClientOptions = BaseConvexClientOptions | { baseClient: BaseConvexClient }`\n // so that callers won\u2019t try to set `BaseConvexClientOptions` attributes that are ineffective.\n // But since `baseClient` is internal at the moment, I don\u2019t think we can do that.\n // We can fix this if we ever make `baseClient` public.\n}\n\n/**\n * A Convex client for use within React.\n *\n * This loads reactive queries and executes mutations over a WebSocket.\n *\n * @public\n */\nexport class ConvexReactClient {\n private address: string;\n private cachedSync?: BaseConvexClient | undefined;\n private cachedPaginatedQueryClient?: PaginatedQueryClient | undefined;\n private listeners: Map<QueryToken | PaginatedQueryToken, Set<() => void>>;\n private options: ConvexReactClientOptions;\n // \"closed\" means this client is done, not just that the underlying WS connection is closed.\n private closed = false;\n private _logger: Logger;\n\n private adminAuth?: string;\n private fakeUserIdentity?: UserIdentityAttributes | undefined;\n\n /**\n * @param address - The url of your Convex deployment, often provided\n * by an environment variable. E.g. `https://small-mouse-123.convex.cloud`.\n * @param options - See {@link ConvexReactClientOptions} for a full description.\n */\n constructor(address: string, options?: ConvexReactClientOptions) {\n // Validate address immediately since validation by the lazily-instantiated\n // internal client does not occur synchronously.\n if (address === undefined) {\n throw new Error(\n \"No address provided to ConvexReactClient.\\n\" +\n \"If trying to deploy to production, make sure to follow all the instructions found at https://docs.convex.dev/production/hosting/\\n\" +\n \"If running locally, make sure to run `convex dev` and ensure the .env.local file is populated.\",\n );\n }\n if (typeof address !== \"string\") {\n throw new Error(\n `ConvexReactClient requires a URL like 'https://happy-otter-123.convex.cloud', received something of type ${typeof address} instead.`,\n );\n }\n if (!address.includes(\"://\")) {\n throw new Error(\"Provided address was not an absolute URL.\");\n }\n this.address = address;\n this.listeners = new Map();\n this._logger =\n options?.logger === false\n ? instantiateNoopLogger({ verbose: options?.verbose ?? false })\n : options?.logger !== true && options?.logger\n ? options.logger\n : instantiateDefaultLogger({ verbose: options?.verbose ?? false });\n this.options = { ...options, logger: this._logger };\n }\n\n /**\n * Return the address for this client, useful for creating a new client.\n *\n * Not guaranteed to match the address with which this client was constructed:\n * it may be canonicalized.\n */\n get url() {\n return this.address;\n }\n\n /**\n * Lazily instantiate the `BaseConvexClient` so we don't create the WebSocket\n * when server-side rendering.\n *\n * @internal\n */\n get sync() {\n if (this.closed) {\n throw new Error(\"ConvexReactClient has already been closed.\");\n }\n if (this.cachedSync) {\n return this.cachedSync;\n }\n // BaseConvexClient and paginated query client are always created together.\n this.cachedSync =\n this.options.baseClient ??\n new BaseConvexClient(\n this.address,\n () => {}, // Use the PaginatedQueryClient's transition instead.\n this.options,\n );\n if (this.adminAuth) {\n this.cachedSync.setAdminAuth(this.adminAuth, this.fakeUserIdentity);\n }\n this.cachedPaginatedQueryClient = new PaginatedQueryClient(\n this.cachedSync,\n (transition) => this.handleTransition(transition),\n );\n return this.cachedSync;\n }\n\n /**\n * Lazily instantiate the `PaginatedQueryClient` so we don't create it\n * when server-side rendering.\n *\n * @internal\n */\n get paginatedQueryClient() {\n // access sync to instantiate the clients\n this.sync;\n if (this.cachedPaginatedQueryClient) {\n return this.cachedPaginatedQueryClient;\n }\n throw new Error(\"Should already be instantiated\");\n }\n\n /**\n * Set the authentication token to be used for subsequent queries and mutations.\n * `fetchToken` will be called automatically again if a token expires.\n * `fetchToken` should return `null` if the token cannot be retrieved, for example\n * when the user's rights were permanently revoked.\n * @param fetchToken - an async function returning the JWT-encoded OpenID Connect Identity Token\n * @param onChange - a callback that will be called when the authentication status changes\n * @param onRefreshChange - a callback called with `true` when the socket is paused to fetch a replacement token after a server rejection, and `false` when refresh completes\n */\n setAuth(\n fetchToken: AuthTokenFetcher,\n onChange?: (isAuthenticated: boolean) => void,\n onRefreshChange?: (isRefreshing: boolean) => void,\n ) {\n if (typeof fetchToken === \"string\") {\n throw new Error(\n \"Passing a string to ConvexReactClient.setAuth is no longer supported, \" +\n \"please upgrade to passing in an async function to handle reauthentication.\",\n );\n }\n this.sync.setAuth(\n fetchToken,\n onChange ??\n (() => {\n // Do nothing\n }),\n onRefreshChange,\n );\n }\n\n /**\n * Clear the current authentication token if set.\n */\n clearAuth() {\n this.sync.clearAuth();\n }\n\n /**\n * @internal\n */\n setAdminAuth(token: string, identity?: UserIdentityAttributes) {\n this.adminAuth = token;\n this.fakeUserIdentity = identity;\n if (this.closed) {\n throw new Error(\"ConvexReactClient has already been closed.\");\n }\n if (this.cachedSync) {\n this.sync.setAdminAuth(token, identity);\n }\n }\n\n /**\n * Construct a new {@link Watch} on a Convex query function.\n *\n * **Most application code should not call this method directly. Instead use\n * the {@link useQuery} hook.**\n *\n * The act of creating a watch does nothing, a Watch is stateless.\n *\n * @param query - A {@link server.FunctionReference} for the public query to run.\n * @param args - An arguments object for the query. If this is omitted,\n * the arguments will be `{}`.\n * @param options - A {@link WatchQueryOptions} options object for this query.\n *\n * @returns The {@link Watch} object.\n */\n watchQuery<Query extends FunctionReference<\"query\">>(\n query: Query,\n ...argsAndOptions: ArgsAndOptions<Query, WatchQueryOptions>\n ): Watch<FunctionReturnType<Query>> {\n const [args, options] = argsAndOptions;\n const name = getFunctionName(query);\n\n return {\n onUpdate: (callback) => {\n const { queryToken, unsubscribe } = this.sync.subscribe(\n name as string,\n args,\n options,\n );\n\n const currentListeners = this.listeners.get(queryToken);\n if (currentListeners !== undefined) {\n currentListeners.add(callback);\n } else {\n this.listeners.set(queryToken, new Set([callback]));\n }\n\n return () => {\n if (this.closed) {\n return;\n }\n\n const currentListeners = this.listeners.get(queryToken)!;\n currentListeners.delete(callback);\n if (currentListeners.size === 0) {\n this.listeners.delete(queryToken);\n }\n unsubscribe();\n };\n },\n\n localQueryResult: () => {\n // Use the cached client because we can't have a query result if we don't\n // even have a client yet!\n if (this.cachedSync) {\n return this.cachedSync.localQueryResult(name, args);\n }\n return undefined;\n },\n\n localQueryLogs: () => {\n if (this.cachedSync) {\n return this.cachedSync.localQueryLogs(name, args);\n }\n return undefined;\n },\n\n journal: () => {\n if (this.cachedSync) {\n return this.cachedSync.queryJournal(name, args);\n }\n return undefined;\n },\n };\n }\n\n // Let's try out a queryOptions-style API.\n // This method is similar to the React Query API `queryClient.prefetchQuery()`.\n // In the future an ensureQueryData(): Promise<Data> method could exist.\n /**\n * Indicates likely future interest in a query subscription.\n *\n * The implementation currently immediately subscribes to a query. In the future this method\n * may prioritize some queries over others, fetch the query result without subscribing, or\n * do nothing in slow network connections or high load scenarios.\n *\n * To use this in a React component, call useQuery() and ignore the return value.\n *\n * @param queryOptions - A query (function reference from an api object) and its args, plus\n * an optional extendSubscriptionFor for how long to subscribe to the query.\n */\n prewarmQuery<Query extends FunctionReference<\"query\">>(\n queryOptions: QueryOptions<Query> & { extendSubscriptionFor?: number },\n ) {\n const extendSubscriptionFor =\n queryOptions.extendSubscriptionFor ?? DEFAULT_EXTEND_SUBSCRIPTION_FOR;\n const watch = this.watchQuery(queryOptions.query, queryOptions.args || {});\n const unsubscribe = watch.onUpdate(() => {});\n setTimeout(unsubscribe, extendSubscriptionFor);\n }\n\n /**\n * Construct a new {@link PaginatedWatch} on a Convex paginated query function.\n *\n * **Most application code should not call this method directly. Instead use\n * the {@link usePaginatedQuery} hook.**\n *\n * The act of creating a watch does nothing, a Watch is stateless.\n *\n * @param query - A {@link server.FunctionReference} for the public query to run.\n * @param args - An arguments object for the query. If this is omitted,\n * the arguments will be `{}`.\n * @param options - A {@link WatchPaginatedQueryOptions} options object for this query.\n *\n * @returns The {@link PaginatedWatch} object.\n *\n * @internal\n */\n watchPaginatedQuery<Query extends FunctionReference<\"query\">>(\n query: Query,\n args: Query[\"_args\"],\n options: WatchPaginatedQueryOptions,\n ): PaginatedWatch<FunctionReturnType<Query>> {\n const name = getFunctionName(query);\n\n return {\n onUpdate: (callback) => {\n const { paginatedQueryToken, unsubscribe } =\n this.paginatedQueryClient.subscribe(name, args || {}, options);\n\n const currentListeners = this.listeners.get(paginatedQueryToken);\n if (currentListeners !== undefined) {\n currentListeners.add(callback);\n } else {\n this.listeners.set(paginatedQueryToken, new Set([callback]));\n }\n\n return () => {\n if (this.closed) {\n return;\n }\n\n const currentListeners = this.listeners.get(paginatedQueryToken)!;\n currentListeners.delete(callback);\n if (currentListeners.size === 0) {\n this.listeners.delete(paginatedQueryToken);\n }\n unsubscribe();\n };\n },\n\n localQueryResult: () => {\n // Use our new paginated query client\n return this.paginatedQueryClient.localQueryResult(name, args, options);\n },\n };\n }\n\n /**\n * Execute a mutation function.\n *\n * @param mutation - A {@link server.FunctionReference} for the public mutation\n * to run.\n * @param args - An arguments object for the mutation. If this is omitted,\n * the arguments will be `{}`.\n * @param options - A {@link MutationOptions} options object for the mutation.\n * @returns A promise of the mutation's result.\n */\n mutation<Mutation extends FunctionReference<\"mutation\">>(\n mutation: Mutation,\n ...argsAndOptions: ArgsAndOptions<\n Mutation,\n MutationOptions<FunctionArgs<Mutation>>\n >\n ): Promise<FunctionReturnType<Mutation>> {\n const [args, options] = argsAndOptions;\n const name = getFunctionName(mutation);\n return this.sync.mutation(name, args, options);\n }\n\n /**\n * Execute an action function.\n *\n * @param action - A {@link server.FunctionReference} for the public action\n * to run.\n * @param args - An arguments object for the action. If this is omitted,\n * the arguments will be `{}`.\n * @returns A promise of the action's result.\n */\n action<Action extends FunctionReference<\"action\">>(\n action: Action,\n ...args: OptionalRestArgs<Action>\n ): Promise<FunctionReturnType<Action>> {\n const name = getFunctionName(action);\n return this.sync.action(name, ...args);\n }\n\n /**\n * Fetch a query result once.\n *\n * **Most application code should subscribe to queries instead, using\n * the {@link useQuery} hook.**\n *\n * @param query - A {@link server.FunctionReference} for the public query\n * to run.\n * @param args - An arguments object for the query. If this is omitted,\n * the arguments will be `{}`.\n * @returns A promise of the query's result.\n */\n query<Query extends FunctionReference<\"query\">>(\n query: Query,\n ...args: OptionalRestArgs<Query>\n ): Promise<FunctionReturnType<Query>> {\n const watch = this.watchQuery(query, ...args);\n const existingResult = watch.localQueryResult();\n if (existingResult !== undefined) {\n return Promise.resolve(existingResult);\n }\n return new Promise((resolve, reject) => {\n const unsubscribe = watch.onUpdate(() => {\n unsubscribe();\n try {\n resolve(watch.localQueryResult());\n } catch (e) {\n reject(e);\n }\n });\n });\n }\n\n /**\n * Get the current {@link ConnectionState} between the client and the Convex\n * backend.\n *\n * @returns The {@link ConnectionState} with the Convex backend.\n */\n connectionState(): ConnectionState {\n return this.sync.connectionState();\n }\n\n /**\n * Subscribe to the {@link ConnectionState} between the client and the Convex\n * backend, calling a callback each time it changes.\n *\n * Subscribed callbacks will be called when any part of ConnectionState changes.\n * ConnectionState may grow in future versions (e.g. to provide a array of\n * inflight requests) in which case callbacks would be called more frequently.\n * ConnectionState may also *lose* properties in future versions as we figure\n * out what information is most useful. As such this API is considered unstable.\n *\n * @returns An unsubscribe function to stop listening.\n */\n subscribeToConnectionState(\n cb: (connectionState: ConnectionState) => void,\n ): () => void {\n return this.sync.subscribeToConnectionState(cb);\n }\n\n /**\n * Get the logger for this client.\n *\n * @returns The {@link Logger} for this client.\n */\n get logger(): Logger {\n return this._logger;\n }\n\n /**\n * Close any network handles associated with this client and stop all subscriptions.\n *\n * Call this method when you're done with a {@link ConvexReactClient} to\n * dispose of its sockets and resources.\n *\n * @returns A `Promise` fulfilled when the connection has been completely closed.\n */\n async close(): Promise<void> {\n this.closed = true;\n // Prevent outstanding React batched updates from invoking listeners.\n this.listeners = new Map();\n if (this.cachedPaginatedQueryClient) {\n this.cachedPaginatedQueryClient = undefined;\n }\n if (this.cachedSync) {\n const sync = this.cachedSync;\n this.cachedSync = undefined;\n await sync.close();\n }\n }\n\n /**\n * Handle transitions from both base client and paginated client.\n * This ensures all transitions are processed synchronously and in order.\n */\n private handleTransition(transition: ExtendedTransition) {\n const simple = transition.queries.map((q) => q.token);\n const paginated = transition.paginatedQueries.map((q) => q.token);\n this.transition([...simple, ...paginated]);\n }\n\n private transition(updatedQueries: (QueryToken | PaginatedQueryToken)[]) {\n for (const queryToken of updatedQueries) {\n const callbacks = this.listeners.get(queryToken);\n if (callbacks) {\n for (const callback of callbacks) {\n callback();\n }\n }\n }\n }\n}\n\nconst ConvexContext = React.createContext<ConvexReactClient>(\n undefined as unknown as ConvexReactClient, // in the future this will be a mocked client for testing\n);\n\n/**\n * Get the {@link ConvexReactClient} within a React component.\n *\n * This relies on the {@link ConvexProvider} being above in the React component tree.\n *\n * @returns The active {@link ConvexReactClient} object, or `undefined`.\n *\n * @public\n */\nexport function useConvex(): ConvexReactClient {\n return useContext(ConvexContext);\n}\n\n/**\n * Provides an active Convex {@link ConvexReactClient} to descendants of this component.\n *\n * Wrap your app in this component to use Convex hooks `useQuery`,\n * `useMutation`, and `useConvex`.\n *\n * @param props - an object with a `client` property that refers to a {@link ConvexReactClient}.\n *\n * @public\n */\nexport const ConvexProvider: React.FC<{\n client: ConvexReactClient;\n children?: React.ReactNode;\n}> = ({ client, children }) => {\n return React.createElement(\n ConvexContext.Provider,\n { value: client },\n children,\n );\n};\n\nexport type OptionalRestArgsOrSkip<FuncRef extends FunctionReference<any>> =\n FuncRef[\"_args\"] extends EmptyObject\n ? [args?: EmptyObject | \"skip\"]\n : [args: FuncRef[\"_args\"] | \"skip\"];\n\n/**\n * Result returned by object-form {@link useQuery_experimental}.\n *\n * @public\n */\nexport type UseQueryResult<QueryResult, ThrowOnError extends boolean = false> =\n | { status: \"pending\" }\n | { status: \"success\"; data: QueryResult }\n | (ThrowOnError extends true ? never : { status: \"error\"; error: Error });\n\ntype UseQueryOptions<\n Query extends FunctionReference<\"query\">,\n ThrowOnError extends boolean,\n> = {\n query: Query;\n args: FunctionArgs<Query> | \"skip\";\n throwOnError?: ThrowOnError;\n};\n\n/**\n * Load a reactive query within a React component.\n *\n * This React hook subscribes to a Convex query and causes a rerender whenever\n * the query result changes. The subscription is managed automatically --\n * it starts when the component mounts and stops when it unmounts.\n *\n * Throws an error if not used under {@link ConvexProvider}.\n *\n * @example\n * ```tsx\n * import { useQuery } from \"convex/react\";\n * import { api } from \"../convex/_generated/api\";\n *\n * function TaskList() {\n * // Reactively loads tasks, re-renders when data changes:\n * const tasks = useQuery(api.tasks.list, { completed: false });\n *\n * // Returns `undefined` while loading:\n * if (tasks === undefined) return <div>Loading...</div>;\n *\n * return tasks.map((task) => <div key={task._id}>{task.text}</div>);\n * }\n *\n * // Pass \"skip\" to conditionally disable the query:\n * function MaybeProfile({ userId }: { userId?: Id<\"users\"> }) {\n * const profile = useQuery(\n * api.users.get,\n * userId ? { userId } : \"skip\",\n * );\n * // ...\n * }\n * ```\n *\n * @param query - a {@link server.FunctionReference} for the public query to run\n * like `api.dir1.dir2.filename.func`.\n * @param args - The arguments to the query function or the string `\"skip\"` if the\n * query should not be loaded.\n * @returns the result of the query. Returns `undefined` while loading.\n *\n * @see https://docs.convex.dev/client/react#fetching-data\n * @public\n */\nexport function useQuery<Query extends FunctionReference<\"query\">>(\n query: Query,\n ...args: OptionalRestArgsOrSkip<Query>\n): Query[\"_returnType\"] | undefined {\n const skip = args[0] === \"skip\";\n const argsObject = args[0] === \"skip\" ? {} : parseArgs(args[0]);\n const queryReference =\n typeof query === \"string\"\n ? makeFunctionReference<\"query\", any, any>(query)\n : query;\n\n const queryName = getFunctionName(queryReference);\n\n const queries = useMemo(\n () =>\n skip\n ? ({} as RequestForQueries)\n : { query: { query: queryReference, args: argsObject } },\n // Stringify args so args that are semantically the same don't trigger a\n // rerender. Saves developers from adding `useMemo` on every args usage.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [JSON.stringify(convexToJson(argsObject)), queryName, skip],\n );\n\n const results = useQueries(queries);\n const result = results[\"query\"];\n\n if (result instanceof Error) {\n throw result;\n }\n return result;\n}\n\n/**\n * Load a reactive query within a React component using an options object.\n *\n * This is an experimental form of {@link useQuery} that accepts a single\n * {@link UseQueryOptions} object instead of positional arguments.\n *\n * Consumers are expected to check the returned object `status` field to\n * make proper use of the result. If an error occurs, it will be present\n * in the result object unless `throwOnError` is `true`, in which case\n * the error will be thrown instead.\n *\n * @example\n * ```tsx\n * import { useQuery_experimental as useQuery } from \"convex/react\";\n * import { api } from \"../convex/_generated/api\";\n *\n * function TaskList() {\n * const state = useQuery({ query: api.tasks.list, args: { completed: false } });\n *\n * if (state.status === \"pending\") return <div>Loading...</div>;\n * if (state.status === \"error\") return <div>Error: {state.error.message}</div>;\n * return state.data.map((task) => <div key={task._id}>{task.text}</div>);\n * }\n * ```\n *\n * @param options - Query options. Pass `args: \"skip\"` to disable the query.\n * @returns the current query state as a {@link UseQueryResult} object.\n *\n * @see https://docs.convex.dev/client/react#fetching-data\n * @public\n */\nexport function useQuery_experimental<\n Query extends FunctionReference<\"query\">,\n ThrowOnError extends boolean = false,\n>(\n options: UseQueryOptions<Query, ThrowOnError>,\n): UseQueryResult<Query[\"_returnType\"], ThrowOnError>;\n\nexport function useQuery_experimental<\n Query extends FunctionReference<\"query\">,\n ThrowOnError extends boolean = false,\n>(\n options: UseQueryOptions<Query, ThrowOnError>,\n): UseQueryResult<Query[\"_returnType\"], false> {\n const throwOnError = options.throwOnError ?? false;\n const queryReference =\n typeof options.query === \"string\"\n ? (makeFunctionReference<\"query\", any, any>(options.query) as Query)\n : options.query;\n const skip = options.args === \"skip\";\n const argsObject = !skip\n ? parseArgs(options.args as Record<string, Value>)\n : {};\n\n const queryName = getFunctionName(queryReference);\n const queries = useMemo(\n () =>\n skip\n ? ({} as RequestForQueries)\n : { query: { query: queryReference, args: argsObject } },\n // Stringify args so args that are semantically the same don't trigger a\n // rerender. Saves developers from adding `useMemo` on every args usage.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [JSON.stringify(convexToJson(argsObject)), queryName, skip],\n );\n\n const results = useQueries(queries);\n const result = results[\"query\"];\n\n if (result instanceof Error) {\n if (throwOnError) {\n throw result;\n }\n return {\n error: result,\n status: \"error\",\n };\n }\n\n if (result === undefined) {\n return {\n status: \"pending\",\n };\n }\n\n return {\n data: result,\n status: \"success\",\n };\n}\n\n/**\n * Construct a new {@link ReactMutation}.\n *\n * Returns a function that you can call to execute a Convex mutation. The\n * returned function is stable across renders (same reference identity), so\n * it can be safely used in dependency arrays and memoization.\n *\n * Mutations can optionally be configured with\n * [optimistic updates](https://docs.convex.dev/client/react/optimistic-updates)\n * for instant UI feedback.\n *\n * Throws an error if not used under {@link ConvexProvider}.\n *\n * @example\n * ```tsx\n * import { useMutation } from \"convex/react\";\n * import { api } from \"../convex/_generated/api\";\n *\n * function CreateTask() {\n * const createTask = useMutation(api.tasks.create);\n *\n * const handleClick = async () => {\n * await createTask({ text: \"New task\" });\n * };\n *\n * return <button onClick={handleClick}>Add Task</button>;\n * }\n * ```\n *\n * @param mutation - A {@link server.FunctionReference} for the public mutation\n * to run like `api.dir1.dir2.filename.func`.\n * @returns The {@link ReactMutation} object with that name.\n *\n * @see https://docs.convex.dev/client/react#editing-data\n * @public\n */\nexport function useMutation<Mutation extends FunctionReference<\"mutation\">>(\n mutation: Mutation,\n): ReactMutation<Mutation> {\n const mutationReference =\n typeof mutation === \"string\"\n ? makeFunctionReference<\"mutation\", any, any>(mutation)\n : mutation;\n\n const convex = useContext(ConvexContext);\n if (convex === undefined) {\n throw new Error(\n \"Could not find Convex client! `useMutation` must be used in the React component \" +\n \"tree under `ConvexProvider`. Did you forget it? \" +\n \"See https://docs.convex.dev/quick-start#set-up-convex-in-your-react-app\",\n );\n }\n return useMemo(\n () => createMutation(mutationReference, convex),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [convex, getFunctionName(mutationReference)],\n );\n}\n\n/**\n * Construct a new {@link ReactAction}.\n *\n * Returns a function that you can call to execute a Convex action. Actions\n * can call third-party APIs and perform side effects. The returned function\n * is stable across renders (same reference identity).\n *\n * **Error handling:** Actions can fail (e.g., if an external API is down).\n * Always wrap action calls in try/catch or handle the rejected promise.\n *\n * **Note:** In most cases, calling an action directly from a client is an\n * anti-pattern. Prefer having the client call a mutation that captures the\n * user's intent (by writing to the database) and then schedules the action\n * via `ctx.scheduler.runAfter`. This ensures the intent is durably recorded\n * even if the client disconnects.\n *\n * Throws an error if not used under {@link ConvexProvider}.\n *\n * @example\n * ```tsx\n * import { useAction } from \"convex/react\";\n * import { api } from \"../convex/_generated/api\";\n *\n * function GenerateSummary() {\n * const generate = useAction(api.ai.generateSummary);\n *\n * const handleClick = async () => {\n * try {\n * const summary = await generate({ text: \"Some long text...\" });\n * console.log(summary);\n * } catch (error) {\n * console.error(\"Action failed:\", error);\n * }\n * };\n *\n * return <button onClick={handleClick}>Generate</button>;\n * }\n * ```\n *\n * @param action - A {@link server.FunctionReference} for the public action\n * to run like `api.dir1.dir2.filename.func`.\n * @returns The {@link ReactAction} object with that name.\n *\n * @see https://docs.convex.dev/functions/actions#calling-actions-from-clients\n * @public\n */\nexport function useAction<Action extends FunctionReference<\"action\">>(\n action: Action,\n): ReactAction<Action> {\n const convex = useContext(ConvexContext);\n const actionReference =\n typeof action === \"string\"\n ? makeFunctionReference<\"action\", any, any>(action)\n : action;\n\n if (convex === undefined) {\n throw new Error(\n \"Could not find Convex client! `useAction` must be used in the React component \" +\n \"tree under `ConvexProvider`. Did you forget it? \" +\n \"See https://docs.convex.dev/quick-start#set-up-convex-in-your-react-app\",\n );\n }\n return useMemo(\n () => createAction(actionReference, convex),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [convex, getFunctionName(actionReference)],\n );\n}\n\n/**\n * React hook to get the current {@link ConnectionState} and subscribe to changes.\n *\n * This hook returns the current connection state and automatically rerenders\n * when any part of the connection state changes (e.g., when going online/offline,\n * when requests start/complete, etc.).\n *\n * The shape of ConnectionState may change in the future which may cause this\n * hook to rerender more frequently.\n *\n * Throws an error if not used under {@link ConvexProvider}.\n *\n * @returns The current {@link ConnectionState} with the Convex backend.\n *\n * @public\n */\nexport function useConvexConnectionState(): ConnectionState {\n const convex = useContext(ConvexContext);\n if (convex === undefined) {\n throw new Error(\n \"Could not find Convex client! `useConvexConnectionState` must be used in the React component \" +\n \"tree under `ConvexProvider`. Did you forget it? \" +\n \"See https://docs.convex.dev/quick-start#set-up-convex-in-your-react-app\",\n );\n }\n\n const getCurrentValue = useCallback(() => {\n return convex.connectionState();\n }, [convex]);\n\n const subscribe = useCallback(\n (callback: () => void) => {\n return convex.subscribeToConnectionState(() => {\n callback();\n });\n },\n [convex],\n );\n\n return useSubscription({ getCurrentValue, subscribe });\n}\n\n// When a function is called with a single argument that looks like a\n// React SyntheticEvent it was likely called as an event handler.\nfunction assertNotAccidentalArgument(value: any) {\n // these are properties of a React.SyntheticEvent\n // https://reactjs.org/docs/events.html\n if (\n typeof value === \"object\" &&\n value !== null &&\n \"bubbles\" in value &&\n \"persist\" in value &&\n \"isDefaultPrevented\" in value\n ) {\n throw new Error(\n `Convex function called with SyntheticEvent object. Did you use a Convex function as an event handler directly? Event handlers like onClick receive an event object as their first argument. These SyntheticEvent objects are not valid Convex values. Try wrapping the function like \\`const handler = () => myMutation();\\` and using \\`handler\\` in the event handler.`,\n );\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAAiC;AAOjC,mBAAwD;AACxD,oBAAoC;AAQpC,yBAA8C;AAC9C,8BAAgC;AAChC,oBAA0B;AAC1B,iBAQO;AAEP,qBAIO;AAGP,oCAGO;AAIP,MAAM,kCAAkC;AAExC,IAAI,OAAO,aAAAA,YAAU,aAAa;AAChC,QAAM,IAAI,MAAM,uCAAuC;AACzD;AAgDO,SAAS,eACd,mBACA,QACA,QACoB;AACpB,WAAS,SAAS,MAAgD;AAChE,gCAA4B,IAAI;AAEhC,WAAO,OAAO,SAAS,mBAAmB,MAAM;AAAA,MAC9C,kBAAkB;AAAA,IACpB,CAAC;AAAA,EACH;AACA,WAAS,uBAAuB,SAAS,qBACvC,kBACoB;AACpB,QAAI,WAAW,QAAW;AACxB,YAAM,IAAI;AAAA,QACR,wDAAoD;AAAA,UAClD;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AACA,WAAO,eAAe,mBAAmB,QAAQ,gBAAgB;AAAA,EACnE;AACA,SAAO;AACT;AAkBA,SAAS,aACP,iBACA,QACkB;AAClB,SAAO,SAAU,MAAgD;AAC/D,WAAO,OAAO,OAAO,iBAAiB,IAAI;AAAA,EAC5C;AACF;AAwKO,MAAM,kBAAkB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkB7B,YAAY,SAAiB,SAAoC;AAjBjE,wBAAQ;AACR,wBAAQ;AACR,wBAAQ;AACR,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AW,aAAa;AACvC,MAAI,WAAW,QAAW;AACxB,UAAM,IAAI;AAAA,MACR;AAAA,IAGF;AAAA,EACF;AAEA,QAAM,sBAAkB,0BAAY,MAAM;AACxC,WAAO,OAAO,gBAAgB;AAAA,EAChC,GAAG,CAAC,MAAM,CAAC;AAEX,QAAM,gBAAY;AAAA,IAChB,CAAC,aAAyB;AACxB,aAAO,OAAO,2BAA2B,MAAM;AAC7C,iBAAS;AAAA,MACX,CAAC;AAAA,IACH;AAAA,IACA,CAAC,MAAM;AAAA,EACT;AAEA,aAAO,yCAAgB,EAAE,iBAAiB,UAAU,CAAC;AACvD;AAIA,SAAS,4BAA4B,OAAY;AAG/C,MACE,OAAO,UAAU,YACjB,UAAU,QACV,aAAa,SACb,aAAa,SACb,wBAAwB,OACxB;AACA,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
6
|
"names": ["React", "currentListeners"]
|
|
7
7
|
}
|
|
@@ -51,6 +51,7 @@ function useUseAuthFromClerk(useAuth) {
|
|
|
51
51
|
getToken,
|
|
52
52
|
orgId,
|
|
53
53
|
orgRole,
|
|
54
|
+
sessionId,
|
|
54
55
|
sessionClaims
|
|
55
56
|
} = useAuth();
|
|
56
57
|
const fetchAccessToken = (0, import_react2.useCallback)(
|
|
@@ -74,7 +75,7 @@ function useUseAuthFromClerk(useAuth) {
|
|
|
74
75
|
// Anything else from the JWT Clerk wants to be reactive goes here too.
|
|
75
76
|
// Clerk's Expo useAuth hook is not memoized so we don't include getToken.
|
|
76
77
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
77
|
-
[orgId, orgRole]
|
|
78
|
+
[orgId, orgRole, sessionId]
|
|
78
79
|
);
|
|
79
80
|
return (0, import_react2.useMemo)(
|
|
80
81
|
() => ({
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/react-clerk/ConvexProviderWithClerk.tsx"],
|
|
4
|
-
"sourcesContent": ["import React from \"react\";\n\nimport { ReactNode, useCallback, useMemo } from \"react\";\nimport { AuthTokenFetcher } from \"../browser/sync/client.js\";\nimport { ConvexProviderWithAuth } from \"../react/ConvexAuthState.js\";\n\n// Until we can import from our own entry points (requires TypeScript 4.7),\n// just describe the interface enough to help users pass the right type.\ntype IConvexReactClient = {\n setAuth(fetchToken: AuthTokenFetcher): void;\n clearAuth(): void;\n};\n\n// https://clerk.com/docs/reference/clerk-react/useauth\ntype UseAuth = () => {\n isLoaded: boolean;\n isSignedIn: boolean | undefined;\n getToken: (options: {\n template?: \"convex\";\n skipCache?: boolean;\n }) => Promise<string | null>;\n // We don't use these properties but they should trigger a new token fetch.\n orgId: string | undefined | null;\n orgRole: string | undefined | null;\n sessionClaims: Record<string, unknown> | undefined | null;\n};\n\n/**\n * A wrapper React component which provides a {@link react.ConvexReactClient}\n * authenticated with Clerk.\n *\n * It must be wrapped by a configured `ClerkProvider`, from\n * `@clerk/react`, `@clerk/clerk-expo`, `@clerk/nextjs` or\n * another React-based Clerk client library and have the corresponding\n * `useAuth` hook passed in.\n *\n * See [Convex Clerk](https://docs.convex.dev/auth/clerk) on how to set up\n * Convex with Clerk.\n *\n * @public\n */\nexport function ConvexProviderWithClerk({\n children,\n client,\n useAuth,\n}: {\n children: ReactNode;\n client: IConvexReactClient;\n useAuth: UseAuth; // useAuth from Clerk\n}) {\n const useAuthFromClerk = useUseAuthFromClerk(useAuth);\n return (\n <ConvexProviderWithAuth client={client} useAuth={useAuthFromClerk}>\n {children}\n </ConvexProviderWithAuth>\n );\n}\n\nfunction useUseAuthFromClerk(useAuth: UseAuth) {\n return useMemo(\n () =>\n function useAuthFromClerk() {\n const {\n isLoaded,\n isSignedIn,\n getToken,\n orgId,\n orgRole,\n sessionClaims,\n } = useAuth();\n const fetchAccessToken = useCallback(\n async ({ forceRefreshToken }: { forceRefreshToken: boolean }) => {\n try {\n if (sessionClaims?.aud === \"convex\") {\n // Using the Convex integration\n return await getToken({\n skipCache: forceRefreshToken,\n });\n } else {\n // Using the JWT token template\n return await getToken({\n template: \"convex\",\n skipCache: forceRefreshToken,\n });\n }\n } catch {\n return null;\n }\n },\n // Build a new fetchAccessToken to trigger setAuth() whenever these change.\n // Anything else from the JWT Clerk wants to be reactive goes here too.\n // Clerk's Expo useAuth hook is not memoized so we don't include getToken.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [orgId, orgRole],\n );\n return useMemo(\n () => ({\n isLoading: !isLoaded,\n isAuthenticated: isSignedIn ?? false,\n fetchAccessToken,\n }),\n [isLoaded, isSignedIn, fetchAccessToken],\n );\n },\n [useAuth],\n );\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AAElB,IAAAA,gBAAgD;AAEhD,6BAAuC;
|
|
4
|
+
"sourcesContent": ["import React from \"react\";\n\nimport { ReactNode, useCallback, useMemo } from \"react\";\nimport { AuthTokenFetcher } from \"../browser/sync/client.js\";\nimport { ConvexProviderWithAuth } from \"../react/ConvexAuthState.js\";\n\n// Until we can import from our own entry points (requires TypeScript 4.7),\n// just describe the interface enough to help users pass the right type.\ntype IConvexReactClient = {\n setAuth(fetchToken: AuthTokenFetcher): void;\n clearAuth(): void;\n};\n\n// https://clerk.com/docs/reference/clerk-react/useauth\ntype UseAuth = () => {\n isLoaded: boolean;\n isSignedIn: boolean | undefined;\n getToken: (options: {\n template?: \"convex\";\n skipCache?: boolean;\n }) => Promise<string | null>;\n // We don't use these properties but they should trigger a new token fetch.\n orgId: string | undefined | null;\n orgRole: string | undefined | null;\n sessionId: string | undefined | null;\n sessionClaims: Record<string, unknown> | undefined | null;\n};\n\n/**\n * A wrapper React component which provides a {@link react.ConvexReactClient}\n * authenticated with Clerk.\n *\n * It must be wrapped by a configured `ClerkProvider`, from\n * `@clerk/react`, `@clerk/clerk-expo`, `@clerk/nextjs` or\n * another React-based Clerk client library and have the corresponding\n * `useAuth` hook passed in.\n *\n * See [Convex Clerk](https://docs.convex.dev/auth/clerk) on how to set up\n * Convex with Clerk.\n *\n * @public\n */\nexport function ConvexProviderWithClerk({\n children,\n client,\n useAuth,\n}: {\n children: ReactNode;\n client: IConvexReactClient;\n useAuth: UseAuth; // useAuth from Clerk\n}) {\n const useAuthFromClerk = useUseAuthFromClerk(useAuth);\n return (\n <ConvexProviderWithAuth client={client} useAuth={useAuthFromClerk}>\n {children}\n </ConvexProviderWithAuth>\n );\n}\n\nfunction useUseAuthFromClerk(useAuth: UseAuth) {\n return useMemo(\n () =>\n function useAuthFromClerk() {\n const {\n isLoaded,\n isSignedIn,\n getToken,\n orgId,\n orgRole,\n sessionId,\n sessionClaims,\n } = useAuth();\n const fetchAccessToken = useCallback(\n async ({ forceRefreshToken }: { forceRefreshToken: boolean }) => {\n try {\n if (sessionClaims?.aud === \"convex\") {\n // Using the Convex integration\n return await getToken({\n skipCache: forceRefreshToken,\n });\n } else {\n // Using the JWT token template\n return await getToken({\n template: \"convex\",\n skipCache: forceRefreshToken,\n });\n }\n } catch {\n return null;\n }\n },\n // Build a new fetchAccessToken to trigger setAuth() whenever these change.\n // Anything else from the JWT Clerk wants to be reactive goes here too.\n // Clerk's Expo useAuth hook is not memoized so we don't include getToken.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [orgId, orgRole, sessionId],\n );\n return useMemo(\n () => ({\n isLoading: !isLoaded,\n isAuthenticated: isSignedIn ?? false,\n fetchAccessToken,\n }),\n [isLoaded, isSignedIn, fetchAccessToken],\n );\n },\n [useAuth],\n );\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AAElB,IAAAA,gBAAgD;AAEhD,6BAAuC;AAsChC,SAAS,wBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AACF,GAIG;AACD,QAAM,mBAAmB,oBAAoB,OAAO;AACpD,SACE,6BAAAC,QAAA,cAAC,iDAAuB,QAAgB,SAAS,oBAC9C,QACH;AAEJ;AAEA,SAAS,oBAAoB,SAAkB;AAC7C,aAAO;AAAA,IACL,MACE,SAAS,mBAAmB;AAC1B,YAAM;AAAA,QACJ;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF,IAAI,QAAQ;AACZ,YAAM,uBAAmB;AAAA,QACvB,OAAO,EAAE,kBAAkB,MAAsC;AAC/D,cAAI;AACF,gBAAI,eAAe,QAAQ,UAAU;AAEnC,qBAAO,MAAM,SAAS;AAAA,gBACpB,WAAW;AAAA,cACb,CAAC;AAAA,YACH,OAAO;AAEL,qBAAO,MAAM,SAAS;AAAA,gBACpB,UAAU;AAAA,gBACV,WAAW;AAAA,cACb,CAAC;AAAA,YACH;AAAA,UACF,QAAQ;AACN,mBAAO;AAAA,UACT;AAAA,QACF;AAAA;AAAA;AAAA;AAAA;AAAA,QAKA,CAAC,OAAO,SAAS,SAAS;AAAA,MAC5B;AACA,iBAAO;AAAA,QACL,OAAO;AAAA,UACL,WAAW,CAAC;AAAA,UACZ,iBAAiB,cAAc;AAAA,UAC/B;AAAA,QACF;AAAA,QACA,CAAC,UAAU,YAAY,gBAAgB;AAAA,MACzC;AAAA,IACF;AAAA,IACF,CAAC,OAAO;AAAA,EACV;AACF;",
|
|
6
6
|
"names": ["import_react", "React"]
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/server/database.ts"],
|
|
4
|
-
"sourcesContent": ["import { GenericId } from \"../values/index.js\";\nimport {\n DocumentByName,\n GenericDataModel,\n NamedTableInfo,\n TableNamesInDataModel,\n} from \"./data_model.js\";\nimport { QueryInitializer } from \"./query.js\";\nimport { SystemDataModel } from \"./schema.js\";\nimport {\n WithOptionalSystemFields,\n WithoutSystemFields,\n} from \"./system_fields.js\";\n\ninterface BaseDatabaseReader<DataModel extends GenericDataModel> {\n /**\n * Fetch a single document from the database by table name and\n * {@link values.GenericId}.\n *\n * @example\n * ```typescript\n * const user = await ctx.db.get(\"users\", userId);\n * ```\n *\n * @param table - The name of the table to fetch the document from.\n * @param id - The {@link values.GenericId} of the document to fetch from the database.\n * @returns - The {@link GenericDocument} of the document at the given {@link values.GenericId}, or `null` if it no longer exists.\n */\n get<TableName extends TableNamesInDataModel<DataModel>>(\n table: TableName,\n id: GenericId<NonUnion<TableName>>,\n ): Promise<DocumentByName<DataModel, TableName> | null>;\n\n /**\n * Fetch a single document from the database by its {@link values.GenericId}.\n *\n * Supported for backwards compatibility. Prefer `db.get(tableName, id)` in\n * new code, or `db.system.get(tableName, id)` for system tables.\n *\n * @param id - The {@link values.GenericId} of the document to fetch from the database.\n * @returns - The {@link GenericDocument} of the document at the given {@link values.GenericId}, or `null` if it no longer exists.\n */\n get<TableName extends TableNamesInDataModel<DataModel>>(\n id: GenericId<TableName>,\n ): Promise<DocumentByName<DataModel, TableName> | null>;\n\n /**\n * Begin a query for the given table name.\n *\n * Queries don't execute immediately, so calling this method and extending its\n * query are free until the results are actually used.\n *\n * @param tableName - The name of the table to query.\n * @returns - A {@link QueryInitializer} object to start building a query.\n */\n query<TableName extends TableNamesInDataModel<DataModel>>(\n tableName: TableName,\n ): QueryInitializer<NamedTableInfo<DataModel, TableName>>;\n\n /**\n * Returns the string ID format for the ID in a given table, or null if the ID\n * is from a different table or is not a valid ID.\n *\n * This accepts the string ID format as well as the `.toString()` representation\n * of the legacy class-based ID format.\n *\n * This does not guarantee that the ID exists (i.e. `db.get(tableName, id)` may return `null`).\n *\n * @param tableName - The name of the table.\n * @param id - The ID string.\n */\n normalizeId<TableName extends TableNamesInDataModel<DataModel>>(\n tableName: TableName,\n id: string,\n ): GenericId<TableName> | null;\n}\n\ninterface BaseDatabaseReaderWithTable<DataModel extends GenericDataModel> {\n /**\n * Scope the database to a specific table.\n */\n table<TableName extends TableNamesInDataModel<DataModel>>(\n tableName: TableName,\n ): BaseTableReader<DataModel, TableName>;\n}\n\nexport interface BaseTableReader<\n DataModel extends GenericDataModel,\n TableName extends TableNamesInDataModel<DataModel>,\n> {\n /**\n * Fetch a single document from the table by its {@link values.GenericId}.\n *\n * @param id - The {@link values.GenericId} of the document to fetch from the database.\n * @returns - The {@link GenericDocument} of the document at the given {@link values.GenericId}, or `null` if it no longer exists.\n */\n get(\n id: GenericId<TableName>,\n ): Promise<DocumentByName<DataModel, TableName> | null>;\n\n /**\n * Begin a query for the table.\n *\n * Queries don't execute immediately, so calling this method and extending its\n * query are free until the results are actually used.\n *\n * @returns - A {@link QueryInitializer} object to start building a query.\n */\n query(): QueryInitializer<NamedTableInfo<DataModel, TableName>>;\n}\n\n/**\n * An interface to read from the database within Convex query functions.\n *\n * Available as `ctx.db` in queries (read-only) and mutations (read-write).\n * You should generally use the `DatabaseReader` type from\n * `\"./_generated/server\"`.\n *\n * The two entry points are:\n * - {@link GenericDatabaseReader.get}, which fetches a single document\n * by table name and {@link values.GenericId}.\n * - {@link GenericDatabaseReader.query}, which starts building a query.\n *\n * @example\n * ```typescript\n * // Fetch a single document by ID:\n * const user = await ctx.db.get(\"users\", userId);\n *\n * // Query documents with an index:\n * const messages = await ctx.db\n * .query(\"messages\")\n * .withIndex(\"by_channel\", (q) => q.eq(\"channelId\", channelId))\n * .order(\"desc\")\n * .take(50);\n * ```\n *\n * **Best practice:** Use `.withIndex()` instead of `.filter()` for efficient\n * queries. Define indexes in your schema for fields you query frequently.\n *\n * @see https://docs.convex.dev/database/reading-data\n * @public\n */\nexport interface GenericDatabaseReader<DataModel extends GenericDataModel>\n extends BaseDatabaseReader<DataModel> {\n /**\n * An interface to read from the system tables within Convex query functions.\n *\n * System tables include `_storage` (file metadata) and\n * `_scheduled_functions` (scheduled function state). Use `ctx.db.system.get()`\n * and `ctx.db.system.query()` just like regular tables.\n *\n * @example\n * ```typescript\n * // Get file metadata from the _storage system table:\n * const metadata = await ctx.db.system.get(\"_storage\", storageId);\n * // metadata has: _id, _creationTime, contentType, sha256, size\n * ```\n *\n * @public\n */\n system: BaseDatabaseReader<SystemDataModel>;\n}\n\nexport interface GenericDatabaseReaderWithTable<\n DataModel extends GenericDataModel,\n> extends BaseDatabaseReaderWithTable<DataModel> {\n /**\n * An interface to read from the system tables within Convex query functions\n *\n * The two entry points are:\n * - {@link GenericDatabaseReader.get}, which fetches a single document\n * by its {@link values.GenericId}.\n * - {@link GenericDatabaseReader.query}, which starts building a query.\n *\n * @public\n */\n system: BaseDatabaseReaderWithTable<SystemDataModel>;\n}\n\n/**\n * An interface to read from and write to the database within Convex mutation\n * functions.\n *\n * Available as `ctx.db` in mutations. You should generally use the\n * `DatabaseWriter` type from `\"./_generated/server\"`.\n *\n * Extends {@link GenericDatabaseReader}\n * with write operations. All reads and writes within a single mutation are\n * executed **atomically**, you never have to worry about partial writes\n * leaving your data in an inconsistent state.\n *\n * @example\n * ```typescript\n * // Insert a new document:\n * const userId = await ctx.db.insert(\"users\", { name: \"Alice\", email: \"alice@example.com\" });\n *\n * // Update specific fields (shallow merge):\n * await ctx.db.patch(\"users\", userId, { name: \"Alice Smith\" });\n *\n * // Replace entire document (all non-system fields):\n * await ctx.db.replace(\"users\", userId, { name: \"Bob\", email: \"bob@example.com\" });\n *\n * // Delete a document:\n * await ctx.db.delete(\"users\", userId);\n *\n * // Delete multiple documents (collect first, then delete each):\n * const oldTasks = await ctx.db\n * .query(\"tasks\")\n * .withIndex(\"by_completed\", (q) => q.eq(\"completed\", true))\n * .collect();\n * for (const task of oldTasks) {\n * await ctx.db.delete(\"tasks\", task._id);\n * }\n * ```\n *\n * @see https://docs.convex.dev/database/writing-data\n * @public\n */\nexport interface GenericDatabaseWriter<DataModel extends GenericDataModel>\n extends GenericDatabaseReader<DataModel> {\n /**\n * Insert a new document into a table.\n *\n * @example\n * ```typescript\n * const taskId = await ctx.db.insert(\"tasks\", {\n * text: \"Buy groceries\",\n * completed: false,\n * });\n * ```\n *\n * @param table - The name of the table to insert a new document into.\n * @param value - The document to insert. System fields (`_id`, `_creationTime`)\n * are added automatically and should not be included.\n * @returns The {@link values.GenericId} of the new document.\n */\n insert<TableName extends TableNamesInDataModel<DataModel>>(\n table: TableName,\n value: WithoutSystemFields<DocumentByName<DataModel, TableName>>,\n ): Promise<GenericId<TableName>>;\n\n /**\n * Patch an existing document, shallow merging it with the given partial\n * document.\n *\n * New fields are added. Existing fields are overwritten. Fields set to\n * `undefined` are removed. Fields not specified in the patch are left\n * unchanged.\n *\n * This method will throw if the document does not exist.\n *\n * @example\n * ```typescript\n * // Update only the \"completed\" field, leaving other fields unchanged:\n * await ctx.db.patch(\"tasks\", taskId, { completed: true });\n *\n * // Remove an optional field by setting it to undefined:\n * await ctx.db.patch(\"tasks\", taskId, { assignee: undefined });\n * ```\n *\n * **Tip:** Use `patch` for partial updates. Use `replace` when you want to\n * overwrite the entire document.\n *\n * @param table - The name of the table the document is in.\n * @param id - The {@link values.GenericId} of the document to patch.\n * @param value - The partial document to merge into the existing document.\n */\n patch<TableName extends TableNamesInDataModel<DataModel>>(\n table: TableName,\n id: GenericId<NonUnion<TableName>>,\n value: PatchValue<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Patch an existing document, shallow merging it with the given partial\n * document.\n *\n * New fields are added. Existing fields are overwritten. Fields set to\n * `undefined` are removed. Fields not specified in the patch are left\n * unchanged.\n *\n * This method will throw if the document does not exist.\n *\n * Supported for backwards compatibility. Prefer `db.patch(tableName, id, value)`\n * in new code.\n *\n * @param id - The {@link values.GenericId} of the document to patch.\n * @param value - The partial document to merge into the existing document.\n */\n patch<TableName extends TableNamesInDataModel<DataModel>>(\n id: GenericId<TableName>,\n value: PatchValue<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Replace the value of an existing document, overwriting its old value\n * completely.\n *\n * Unlike `patch`, which does a shallow merge, `replace` overwrites the\n * entire document. Any fields not included in the new value will be removed\n * (except system fields `_id` and `_creationTime`).\n *\n * This method will throw if the document does not exist.\n *\n * @example\n * ```typescript\n * // Replace the entire document:\n * await ctx.db.replace(\"users\", userId, {\n * name: \"New Name\",\n * email: \"new@example.com\",\n * });\n * ```\n *\n * @param table - The name of the table the document is in.\n * @param id - The {@link values.GenericId} of the document to replace.\n * @param value - The new document. System fields can be omitted.\n */\n replace<TableName extends TableNamesInDataModel<DataModel>>(\n table: TableName,\n id: GenericId<NonUnion<TableName>>,\n value: WithOptionalSystemFields<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Replace the value of an existing document, overwriting its old value\n * completely.\n *\n * Unlike `patch`, which does a shallow merge, `replace` overwrites the\n * entire document.\n *\n * Supported for backwards compatibility. Prefer `db.replace(tableName, id, value)`\n * in new code.\n *\n * @param id - The {@link values.GenericId} of the document to replace.\n * @param value - The new document. System fields can be omitted.\n */\n replace<TableName extends TableNamesInDataModel<DataModel>>(\n id: GenericId<TableName>,\n value: WithOptionalSystemFields<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Delete an existing document.\n *\n * @example\n * ```typescript\n * await ctx.db.delete(\"tasks\", taskId);\n * ```\n *\n * @param table - The name of the table the document is in.\n * @param id - The {@link values.GenericId} of the document to remove.\n */\n delete<TableName extends TableNamesInDataModel<DataModel>>(\n table: TableName,\n id: GenericId<NonUnion<TableName>>,\n ): Promise<void>;\n\n /**\n * Delete an existing document.\n *\n * Supported for backwards compatibility. Prefer `db.delete(tableName, id)` in\n * new code.\n *\n * **Note:** Convex queries do not support `.delete()` directly on query\n * results. To delete multiple documents, `.collect()` them first, then\n * delete each one individually.\n *\n * @param id - The {@link values.GenericId} of the document to remove.\n */\n delete(id: GenericId<TableNamesInDataModel<DataModel>>): Promise<void>;\n}\n\n/**\n * An interface to read from and write to the database within Convex mutation\n * functions.\n *\n * You should generally use the `DatabaseWriter` type from\n * `\"./_generated/server\"`.\n *\n * Convex guarantees that all writes within a single mutation are\n * executed atomically, so you never have to worry about partial writes leaving\n * your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)\n * for the guarantees Convex provides your functions.\n *\n * @public\n */\nexport interface GenericDatabaseWriterWithTable<\n DataModel extends GenericDataModel,\n> extends GenericDatabaseReaderWithTable<DataModel> {\n /**\n * Scope the database to a specific table.\n */\n table<TableName extends TableNamesInDataModel<DataModel>>(\n tableName: TableName,\n ): BaseTableWriter<DataModel, TableName>;\n}\n\nexport interface BaseTableWriter<\n DataModel extends GenericDataModel,\n TableName extends TableNamesInDataModel<DataModel>,\n> extends BaseTableReader<DataModel, TableName> {\n /**\n * Insert a new document into the table.\n *\n * @param value - The {@link values.Value} to insert into the given table.\n * @returns - {@link values.GenericId} of the new document.\n */\n insert(\n value: WithoutSystemFields<DocumentByName<DataModel, TableName>>,\n ): Promise<GenericId<TableName>>;\n\n /**\n * Patch an existing document, shallow merging it with the given partial\n * document.\n *\n * New fields are added. Existing fields are overwritten. Fields set to\n * `undefined` are removed.\n *\n * @param id - The {@link values.GenericId} of the document to patch.\n * @param value - The partial {@link GenericDocument} to merge into the specified document. If this new value\n * specifies system fields like `_id`, they must match the document's existing field values.\n */\n patch(\n id: GenericId<TableName>,\n value: PatchValue<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Replace the value of an existing document, overwriting its old value.\n *\n * @param id - The {@link values.GenericId} of the document to replace.\n * @param value - The new {@link GenericDocument} for the document. This value can omit the system fields,\n * and the database will fill them in.\n */\n replace(\n id: GenericId<TableName>,\n value: WithOptionalSystemFields<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Delete an existing document.\n *\n * @param id - The {@link values.GenericId} of the document to remove.\n */\n delete(id: GenericId<TableName>): Promise<void>;\n}\n\n/**\n * This prevents TypeScript from inferring that the generic `TableName` type is\n * a union type when `table` and `id` disagree.\n */\ntype NonUnion<T> = T extends never // `never` is the bottom type for TypeScript unions\n ? never\n : T;\n\n/**\n * This is like Partial, but it also allows undefined to be passed to optional\n * fields when `exactOptionalPropertyTypes` is enabled in the tsconfig.\n */\ntype PatchValue<T> = {\n [P in keyof T]?: undefined extends T[P] ? T[P] | undefined : T[P];\n};\n"],
|
|
4
|
+
"sourcesContent": ["import { GenericId } from \"../values/index.js\";\nimport {\n DocumentByName,\n GenericDataModel,\n NamedTableInfo,\n TableNamesInDataModel,\n} from \"./data_model.js\";\nimport { QueryInitializer } from \"./query.js\";\nimport { SystemDataModel } from \"./schema.js\";\nimport {\n WithOptionalSystemFields,\n WithoutSystemFields,\n} from \"./system_fields.js\";\n\ninterface BaseDatabaseReader<DataModel extends GenericDataModel> {\n /**\n * Fetch a single document from the database by table name and\n * {@link values.GenericId}.\n *\n * @example\n * ```typescript\n * const user = await ctx.db.get(\"users\", userId);\n * ```\n *\n * @param table - The name of the table to fetch the document from.\n * @param id - The {@link values.GenericId} of the document to fetch from the database.\n * @returns - The {@link GenericDocument} of the document at the given {@link values.GenericId}, or `null` if it no longer exists.\n */\n get<TableName extends TableNamesInDataModel<DataModel>>(\n table: TableName,\n id: GenericId<NonUnion<TableName>>,\n ): Promise<DocumentByName<DataModel, TableName> | null>;\n\n /**\n * Fetch a single document from the database by its {@link values.GenericId}.\n *\n * Supported for backwards compatibility. Prefer `db.get(tableName, id)` in\n * new code, or `db.system.get(tableName, id)` for system tables.\n *\n * @param id - The {@link values.GenericId} of the document to fetch from the database.\n * @returns - The {@link GenericDocument} of the document at the given {@link values.GenericId}, or `null` if it no longer exists.\n */\n get<TableName extends TableNamesInDataModel<DataModel>>(\n id: GenericId<TableName>,\n ): Promise<DocumentByName<DataModel, TableName> | null>;\n\n /**\n * Begin a query for the given table name.\n *\n * Queries don't execute immediately, so calling this method and extending its\n * query are free until the results are actually used.\n *\n * @param tableName - The name of the table to query.\n * @returns - A {@link QueryInitializer} object to start building a query.\n */\n query<TableName extends TableNamesInDataModel<DataModel>>(\n tableName: TableName,\n ): QueryInitializer<NamedTableInfo<DataModel, TableName>>;\n\n /**\n * Returns the string ID format for the ID in a given table, or null if the ID\n * is from a different table or is not a valid ID.\n *\n * This accepts the string ID format as well as the `.toString()` representation\n * of the legacy class-based ID format.\n *\n * This does not guarantee that the ID exists (i.e. `db.get(tableName, id)` may return `null`).\n *\n * @param tableName - The name of the table.\n * @param id - The ID string.\n */\n normalizeId<TableName extends TableNamesInDataModel<DataModel>>(\n tableName: TableName,\n id: string,\n ): GenericId<TableName> | null;\n}\n\ninterface BaseDatabaseReaderWithTable<DataModel extends GenericDataModel> {\n /**\n * Scope the database to a specific table.\n */\n table<TableName extends TableNamesInDataModel<DataModel>>(\n tableName: TableName,\n ): BaseTableReader<DataModel, TableName>;\n}\n\nexport interface BaseTableReader<\n DataModel extends GenericDataModel,\n TableName extends TableNamesInDataModel<DataModel>,\n> {\n /**\n * Fetch a single document from the table by its {@link values.GenericId}.\n *\n * @param id - The {@link values.GenericId} of the document to fetch from the database.\n * @returns - The {@link GenericDocument} of the document at the given {@link values.GenericId}, or `null` if it no longer exists.\n */\n get(\n id: GenericId<TableName>,\n ): Promise<DocumentByName<DataModel, TableName> | null>;\n\n /**\n * Begin a query for the table.\n *\n * Queries don't execute immediately, so calling this method and extending its\n * query are free until the results are actually used.\n *\n * @returns - A {@link QueryInitializer} object to start building a query.\n */\n query(): QueryInitializer<NamedTableInfo<DataModel, TableName>>;\n}\n\n/**\n * An interface to read from the database within Convex query functions.\n *\n * Available as `ctx.db` in queries (read-only) and mutations (read-write).\n * You should generally use the `DatabaseReader` type from\n * `\"./_generated/server\"`.\n *\n * The two entry points are:\n * - {@link GenericDatabaseReader.get}, which fetches a single document\n * by table name and {@link values.GenericId}.\n * - {@link GenericDatabaseReader.query}, which starts building a query.\n *\n * @example\n * ```typescript\n * // Fetch a single document by ID:\n * const user = await ctx.db.get(\"users\", userId);\n *\n * // Query documents with an index:\n * const messages = await ctx.db\n * .query(\"messages\")\n * .withIndex(\"by_channel\", (q) => q.eq(\"channelId\", channelId))\n * .order(\"desc\")\n * .take(50);\n * ```\n *\n * **Best practice:** Use `.withIndex()` instead of `.filter()` for efficient\n * queries. Define indexes in your schema for fields you query frequently.\n *\n * @see https://docs.convex.dev/database/reading-data\n * @public\n */\nexport interface GenericDatabaseReader<\n DataModel extends GenericDataModel,\n> extends BaseDatabaseReader<DataModel> {\n /**\n * An interface to read from the system tables within Convex query functions.\n *\n * System tables include `_storage` (file metadata) and\n * `_scheduled_functions` (scheduled function state). Use `ctx.db.system.get()`\n * and `ctx.db.system.query()` just like regular tables.\n *\n * @example\n * ```typescript\n * // Get file metadata from the _storage system table:\n * const metadata = await ctx.db.system.get(\"_storage\", storageId);\n * // metadata has: _id, _creationTime, contentType, sha256, size\n * ```\n *\n * @public\n */\n system: BaseDatabaseReader<SystemDataModel>;\n}\n\nexport interface GenericDatabaseReaderWithTable<\n DataModel extends GenericDataModel,\n> extends BaseDatabaseReaderWithTable<DataModel> {\n /**\n * An interface to read from the system tables within Convex query functions\n *\n * The two entry points are:\n * - {@link GenericDatabaseReader.get}, which fetches a single document\n * by its {@link values.GenericId}.\n * - {@link GenericDatabaseReader.query}, which starts building a query.\n *\n * @public\n */\n system: BaseDatabaseReaderWithTable<SystemDataModel>;\n}\n\n/**\n * An interface to read from and write to the database within Convex mutation\n * functions.\n *\n * Available as `ctx.db` in mutations. You should generally use the\n * `DatabaseWriter` type from `\"./_generated/server\"`.\n *\n * Extends {@link GenericDatabaseReader}\n * with write operations. All reads and writes within a single mutation are\n * executed **atomically**, you never have to worry about partial writes\n * leaving your data in an inconsistent state.\n *\n * @example\n * ```typescript\n * // Insert a new document:\n * const userId = await ctx.db.insert(\"users\", { name: \"Alice\", email: \"alice@example.com\" });\n *\n * // Update specific fields (shallow merge):\n * await ctx.db.patch(\"users\", userId, { name: \"Alice Smith\" });\n *\n * // Replace entire document (all non-system fields):\n * await ctx.db.replace(\"users\", userId, { name: \"Bob\", email: \"bob@example.com\" });\n *\n * // Delete a document:\n * await ctx.db.delete(\"users\", userId);\n *\n * // Delete multiple documents (collect first, then delete each):\n * const oldTasks = await ctx.db\n * .query(\"tasks\")\n * .withIndex(\"by_completed\", (q) => q.eq(\"completed\", true))\n * .collect();\n * for (const task of oldTasks) {\n * await ctx.db.delete(\"tasks\", task._id);\n * }\n * ```\n *\n * @see https://docs.convex.dev/database/writing-data\n * @public\n */\nexport interface GenericDatabaseWriter<\n DataModel extends GenericDataModel,\n> extends GenericDatabaseReader<DataModel> {\n /**\n * Insert a new document into a table.\n *\n * @example\n * ```typescript\n * const taskId = await ctx.db.insert(\"tasks\", {\n * text: \"Buy groceries\",\n * completed: false,\n * });\n * ```\n *\n * @param table - The name of the table to insert a new document into.\n * @param value - The document to insert. System fields (`_id`, `_creationTime`)\n * are added automatically and should not be included.\n * @returns The {@link values.GenericId} of the new document.\n */\n insert<TableName extends TableNamesInDataModel<DataModel>>(\n table: TableName,\n value: WithoutSystemFields<DocumentByName<DataModel, TableName>>,\n ): Promise<GenericId<TableName>>;\n\n /**\n * Patch an existing document, shallow merging it with the given partial\n * document.\n *\n * New fields are added. Existing fields are overwritten. Fields set to\n * `undefined` are removed. Fields not specified in the patch are left\n * unchanged.\n *\n * This method will throw if the document does not exist.\n *\n * @example\n * ```typescript\n * // Update only the \"completed\" field, leaving other fields unchanged:\n * await ctx.db.patch(\"tasks\", taskId, { completed: true });\n *\n * // Remove an optional field by setting it to undefined:\n * await ctx.db.patch(\"tasks\", taskId, { assignee: undefined });\n * ```\n *\n * **Tip:** Use `patch` for partial updates. Use `replace` when you want to\n * overwrite the entire document.\n *\n * @param table - The name of the table the document is in.\n * @param id - The {@link values.GenericId} of the document to patch.\n * @param value - The partial document to merge into the existing document.\n */\n patch<TableName extends TableNamesInDataModel<DataModel>>(\n table: TableName,\n id: GenericId<NonUnion<TableName>>,\n value: PatchValue<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Patch an existing document, shallow merging it with the given partial\n * document.\n *\n * New fields are added. Existing fields are overwritten. Fields set to\n * `undefined` are removed. Fields not specified in the patch are left\n * unchanged.\n *\n * This method will throw if the document does not exist.\n *\n * Supported for backwards compatibility. Prefer `db.patch(tableName, id, value)`\n * in new code.\n *\n * @param id - The {@link values.GenericId} of the document to patch.\n * @param value - The partial document to merge into the existing document.\n */\n patch<TableName extends TableNamesInDataModel<DataModel>>(\n id: GenericId<TableName>,\n value: PatchValue<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Replace the value of an existing document, overwriting its old value\n * completely.\n *\n * Unlike `patch`, which does a shallow merge, `replace` overwrites the\n * entire document. Any fields not included in the new value will be removed\n * (except system fields `_id` and `_creationTime`).\n *\n * This method will throw if the document does not exist.\n *\n * @example\n * ```typescript\n * // Replace the entire document:\n * await ctx.db.replace(\"users\", userId, {\n * name: \"New Name\",\n * email: \"new@example.com\",\n * });\n * ```\n *\n * @param table - The name of the table the document is in.\n * @param id - The {@link values.GenericId} of the document to replace.\n * @param value - The new document. System fields can be omitted.\n */\n replace<TableName extends TableNamesInDataModel<DataModel>>(\n table: TableName,\n id: GenericId<NonUnion<TableName>>,\n value: WithOptionalSystemFields<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Replace the value of an existing document, overwriting its old value\n * completely.\n *\n * Unlike `patch`, which does a shallow merge, `replace` overwrites the\n * entire document.\n *\n * Supported for backwards compatibility. Prefer `db.replace(tableName, id, value)`\n * in new code.\n *\n * @param id - The {@link values.GenericId} of the document to replace.\n * @param value - The new document. System fields can be omitted.\n */\n replace<TableName extends TableNamesInDataModel<DataModel>>(\n id: GenericId<TableName>,\n value: WithOptionalSystemFields<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Delete an existing document.\n *\n * @example\n * ```typescript\n * await ctx.db.delete(\"tasks\", taskId);\n * ```\n *\n * @param table - The name of the table the document is in.\n * @param id - The {@link values.GenericId} of the document to remove.\n */\n delete<TableName extends TableNamesInDataModel<DataModel>>(\n table: TableName,\n id: GenericId<NonUnion<TableName>>,\n ): Promise<void>;\n\n /**\n * Delete an existing document.\n *\n * Supported for backwards compatibility. Prefer `db.delete(tableName, id)` in\n * new code.\n *\n * **Note:** Convex queries do not support `.delete()` directly on query\n * results. To delete multiple documents, `.collect()` them first, then\n * delete each one individually.\n *\n * @param id - The {@link values.GenericId} of the document to remove.\n */\n delete(id: GenericId<TableNamesInDataModel<DataModel>>): Promise<void>;\n}\n\n/**\n * An interface to read from and write to the database within Convex mutation\n * functions.\n *\n * You should generally use the `DatabaseWriter` type from\n * `\"./_generated/server\"`.\n *\n * Convex guarantees that all writes within a single mutation are\n * executed atomically, so you never have to worry about partial writes leaving\n * your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)\n * for the guarantees Convex provides your functions.\n *\n * @public\n */\nexport interface GenericDatabaseWriterWithTable<\n DataModel extends GenericDataModel,\n> extends GenericDatabaseReaderWithTable<DataModel> {\n /**\n * Scope the database to a specific table.\n */\n table<TableName extends TableNamesInDataModel<DataModel>>(\n tableName: TableName,\n ): BaseTableWriter<DataModel, TableName>;\n}\n\nexport interface BaseTableWriter<\n DataModel extends GenericDataModel,\n TableName extends TableNamesInDataModel<DataModel>,\n> extends BaseTableReader<DataModel, TableName> {\n /**\n * Insert a new document into the table.\n *\n * @param value - The {@link values.Value} to insert into the given table.\n * @returns - {@link values.GenericId} of the new document.\n */\n insert(\n value: WithoutSystemFields<DocumentByName<DataModel, TableName>>,\n ): Promise<GenericId<TableName>>;\n\n /**\n * Patch an existing document, shallow merging it with the given partial\n * document.\n *\n * New fields are added. Existing fields are overwritten. Fields set to\n * `undefined` are removed.\n *\n * @param id - The {@link values.GenericId} of the document to patch.\n * @param value - The partial {@link GenericDocument} to merge into the specified document. If this new value\n * specifies system fields like `_id`, they must match the document's existing field values.\n */\n patch(\n id: GenericId<TableName>,\n value: PatchValue<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Replace the value of an existing document, overwriting its old value.\n *\n * @param id - The {@link values.GenericId} of the document to replace.\n * @param value - The new {@link GenericDocument} for the document. This value can omit the system fields,\n * and the database will fill them in.\n */\n replace(\n id: GenericId<TableName>,\n value: WithOptionalSystemFields<DocumentByName<DataModel, TableName>>,\n ): Promise<void>;\n\n /**\n * Delete an existing document.\n *\n * @param id - The {@link values.GenericId} of the document to remove.\n */\n delete(id: GenericId<TableName>): Promise<void>;\n}\n\n/**\n * This prevents TypeScript from inferring that the generic `TableName` type is\n * a union type when `table` and `id` disagree.\n */\ntype NonUnion<T> = T extends never // `never` is the bottom type for TypeScript unions\n ? never\n : T;\n\n/**\n * This is like Partial, but it also allows undefined to be passed to optional\n * fields when `exactOptionalPropertyTypes` is enabled in the tsconfig.\n */\ntype PatchValue<T> = {\n [P in keyof T]?: undefined extends T[P] ? T[P] | undefined : T[P];\n};\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -62,8 +62,8 @@ async function getDeploymentMetadata() {
|
|
|
62
62
|
};
|
|
63
63
|
}
|
|
64
64
|
async function getRequestMetadata() {
|
|
65
|
-
const { ip, userAgent, requestId, scheduledFunctionId } = await (0, import_syscall.performAsyncSyscall)("1.0/getRequestMetadata", {});
|
|
66
|
-
return { ip, userAgent, requestId, scheduledFunctionId };
|
|
65
|
+
const { ip, userAgent, requestId, scheduledFunctionId, authToken } = await (0, import_syscall.performAsyncSyscall)("1.0/getRequestMetadata", {});
|
|
66
|
+
return { ip, userAgent, requestId, scheduledFunctionId, authToken };
|
|
67
67
|
}
|
|
68
68
|
function setupQueryMeta(visibility) {
|
|
69
69
|
return {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/server/impl/meta_impl.ts"],
|
|
4
|
-
"sourcesContent": ["import { jsonToConvex } from \"../../values/index.js\";\nimport {\n ActionMeta,\n MutationMeta,\n QueryMeta,\n RequestMetadata,\n FunctionMetadata,\n TransactionMetrics,\n DeploymentMetadata,\n} from \"../meta.js\";\nimport { performAsyncSyscall } from \"./syscall.js\";\n\nasync function getTransactionMetrics(): Promise<TransactionMetrics> {\n let syscallJSON;\n try {\n syscallJSON = await performAsyncSyscall(\"1.0/getTransactionMetrics\", {});\n } catch (e: any) {\n if (e.message?.includes(\"Unknown async operation\")) {\n throw new Error(\n \"getTransactionMetrics() can only be called from a query or mutation. \" +\n \"It is not available in actions or outside of a Convex function.\",\n );\n }\n throw e;\n }\n return jsonToConvex(syscallJSON) as any;\n}\n\nasync function getFunctionMetadata(): Promise<{\n name: string;\n componentPath: string;\n}> {\n const { name, componentPath } = await performAsyncSyscall(\n \"1.0/getFunctionMetadata\",\n {},\n );\n return {\n name,\n componentPath,\n };\n}\n\nasync function getDeploymentMetadata(): Promise<DeploymentMetadata> {\n const syscallJSON = await performAsyncSyscall(\n \"1.0/getDeploymentMetadata\",\n {},\n );\n const result = jsonToConvex(syscallJSON) as any;\n return {\n name: result.name,\n region: result.region ?? null,\n class: result.class,\n };\n}\n\nasync function getRequestMetadata(): Promise<RequestMetadata> {\n const { ip, userAgent, requestId, scheduledFunctionId } =\n await performAsyncSyscall(\"1.0/getRequestMetadata\", {});\n return { ip, userAgent, requestId, scheduledFunctionId };\n}\n\nexport function setupQueryMeta(\n visibility: FunctionMetadata[\"visibility\"],\n): QueryMeta {\n return {\n getFunctionMetadata: async () => ({\n ...(await getFunctionMetadata()),\n type: \"query\",\n visibility,\n }),\n getTransactionMetrics,\n getDeploymentMetadata,\n };\n}\n\nexport function setupMutationMeta(\n visibility: FunctionMetadata[\"visibility\"],\n): MutationMeta {\n return {\n getFunctionMetadata: async () => ({\n ...(await getFunctionMetadata()),\n type: \"mutation\",\n visibility,\n }),\n getTransactionMetrics,\n getDeploymentMetadata,\n getRequestMetadata,\n };\n}\n\nexport function setupActionMeta(\n visibility: FunctionMetadata[\"visibility\"],\n): ActionMeta {\n return {\n getFunctionMetadata: async () => ({\n ...(await getFunctionMetadata()),\n type: \"action\",\n visibility,\n }),\n getDeploymentMetadata,\n getRequestMetadata,\n };\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA6B;AAU7B,qBAAoC;AAEpC,eAAe,wBAAqD;AAClE,MAAI;AACJ,MAAI;AACF,kBAAc,UAAM,oCAAoB,6BAA6B,CAAC,CAAC;AAAA,EACzE,SAAS,GAAQ;AACf,QAAI,EAAE,SAAS,SAAS,yBAAyB,GAAG;AAClD,YAAM,IAAI;AAAA,QACR;AAAA,MAEF;AAAA,IACF;AACA,UAAM;AAAA,EACR;AACA,aAAO,4BAAa,WAAW;AACjC;AAEA,eAAe,sBAGZ;AACD,QAAM,EAAE,MAAM,cAAc,IAAI,UAAM;AAAA,IACpC;AAAA,IACA,CAAC;AAAA,EACH;AACA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;AAEA,eAAe,wBAAqD;AAClE,QAAM,cAAc,UAAM;AAAA,IACxB;AAAA,IACA,CAAC;AAAA,EACH;AACA,QAAM,aAAS,4BAAa,WAAW;AACvC,SAAO;AAAA,IACL,MAAM,OAAO;AAAA,IACb,QAAQ,OAAO,UAAU;AAAA,IACzB,OAAO,OAAO;AAAA,EAChB;AACF;AAEA,eAAe,qBAA+C;AAC5D,QAAM,EAAE,IAAI,WAAW,WAAW,
|
|
4
|
+
"sourcesContent": ["import { jsonToConvex } from \"../../values/index.js\";\nimport {\n ActionMeta,\n MutationMeta,\n QueryMeta,\n RequestMetadata,\n FunctionMetadata,\n TransactionMetrics,\n DeploymentMetadata,\n} from \"../meta.js\";\nimport { performAsyncSyscall } from \"./syscall.js\";\n\nasync function getTransactionMetrics(): Promise<TransactionMetrics> {\n let syscallJSON;\n try {\n syscallJSON = await performAsyncSyscall(\"1.0/getTransactionMetrics\", {});\n } catch (e: any) {\n if (e.message?.includes(\"Unknown async operation\")) {\n throw new Error(\n \"getTransactionMetrics() can only be called from a query or mutation. \" +\n \"It is not available in actions or outside of a Convex function.\",\n );\n }\n throw e;\n }\n return jsonToConvex(syscallJSON) as any;\n}\n\nasync function getFunctionMetadata(): Promise<{\n name: string;\n componentPath: string;\n}> {\n const { name, componentPath } = await performAsyncSyscall(\n \"1.0/getFunctionMetadata\",\n {},\n );\n return {\n name,\n componentPath,\n };\n}\n\nasync function getDeploymentMetadata(): Promise<DeploymentMetadata> {\n const syscallJSON = await performAsyncSyscall(\n \"1.0/getDeploymentMetadata\",\n {},\n );\n const result = jsonToConvex(syscallJSON) as any;\n return {\n name: result.name,\n region: result.region ?? null,\n class: result.class,\n };\n}\n\nasync function getRequestMetadata(): Promise<RequestMetadata> {\n const { ip, userAgent, requestId, scheduledFunctionId, authToken } =\n await performAsyncSyscall(\"1.0/getRequestMetadata\", {});\n return { ip, userAgent, requestId, scheduledFunctionId, authToken };\n}\n\nexport function setupQueryMeta(\n visibility: FunctionMetadata[\"visibility\"],\n): QueryMeta {\n return {\n getFunctionMetadata: async () => ({\n ...(await getFunctionMetadata()),\n type: \"query\",\n visibility,\n }),\n getTransactionMetrics,\n getDeploymentMetadata,\n };\n}\n\nexport function setupMutationMeta(\n visibility: FunctionMetadata[\"visibility\"],\n): MutationMeta {\n return {\n getFunctionMetadata: async () => ({\n ...(await getFunctionMetadata()),\n type: \"mutation\",\n visibility,\n }),\n getTransactionMetrics,\n getDeploymentMetadata,\n getRequestMetadata,\n };\n}\n\nexport function setupActionMeta(\n visibility: FunctionMetadata[\"visibility\"],\n): ActionMeta {\n return {\n getFunctionMetadata: async () => ({\n ...(await getFunctionMetadata()),\n type: \"action\",\n visibility,\n }),\n getDeploymentMetadata,\n getRequestMetadata,\n };\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA6B;AAU7B,qBAAoC;AAEpC,eAAe,wBAAqD;AA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9B,MAAM;AAAA,MACN;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/server/impl/query_impl.ts"],
|
|
4
|
-
"sourcesContent": ["import { Value, JSONValue, jsonToConvex } from \"../../values/index.js\";\nimport { PaginationResult, PaginationOptions } from \"../pagination.js\";\nimport { performAsyncSyscall, performSyscall } from \"./syscall.js\";\nimport {\n filterBuilderImpl,\n serializeExpression,\n} from \"./filter_builder_impl.js\";\nimport { Query, QueryInitializer } from \"../query.js\";\nimport { ExpressionOrValue, FilterBuilder } from \"../filter_builder.js\";\nimport { GenericTableInfo } from \"../data_model.js\";\nimport {\n IndexRangeBuilderImpl,\n SerializedRangeExpression,\n} from \"./index_range_builder_impl.js\";\nimport {\n SearchFilterBuilderImpl,\n SerializedSearchFilter,\n} from \"./search_filter_builder_impl.js\";\nimport { validateArg, validateArgIsNonNegativeInteger } from \"./validate.js\";\nimport { version } from \"../../index.js\";\n\nconst MAX_QUERY_OPERATORS = 256;\n\ntype QueryOperator = { filter: JSONValue } | { limit: number };\ntype Source =\n | { type: \"FullTableScan\"; tableName: string; order: \"asc\" | \"desc\" | null }\n | {\n type: \"IndexRange\";\n indexName: string;\n range: ReadonlyArray<SerializedRangeExpression>;\n order: \"asc\" | \"desc\" | null;\n }\n | {\n type: \"Search\";\n indexName: string;\n filters: ReadonlyArray<SerializedSearchFilter>;\n };\n\ntype SerializedQuery = {\n source: Source;\n operators: Array<QueryOperator>;\n};\n\nexport class QueryInitializerImpl\n implements QueryInitializer<GenericTableInfo>\n{\n private tableName: string;\n\n constructor(tableName: string) {\n this.tableName = tableName;\n }\n\n withIndex(\n indexName: string,\n indexRange?: (q: IndexRangeBuilderImpl) => IndexRangeBuilderImpl,\n ): QueryImpl {\n validateArg(indexName, 1, \"withIndex\", \"indexName\");\n let rangeBuilder = IndexRangeBuilderImpl.new();\n if (indexRange !== undefined) {\n rangeBuilder = indexRange(rangeBuilder);\n }\n return new QueryImpl({\n source: {\n type: \"IndexRange\",\n indexName: this.tableName + \".\" + indexName,\n range: rangeBuilder.export(),\n order: null,\n },\n operators: [],\n });\n }\n\n withSearchIndex(\n indexName: string,\n searchFilter: (q: SearchFilterBuilderImpl) => SearchFilterBuilderImpl,\n ): QueryImpl {\n validateArg(indexName, 1, \"withSearchIndex\", \"indexName\");\n validateArg(searchFilter, 2, \"withSearchIndex\", \"searchFilter\");\n const searchFilterBuilder = SearchFilterBuilderImpl.new();\n return new QueryImpl({\n source: {\n type: \"Search\",\n indexName: this.tableName + \".\" + indexName,\n filters: searchFilter(searchFilterBuilder).export(),\n },\n operators: [],\n });\n }\n\n fullTableScan(): QueryImpl {\n return new QueryImpl({\n source: {\n type: \"FullTableScan\",\n tableName: this.tableName,\n order: null,\n },\n operators: [],\n });\n }\n\n order(order: \"asc\" | \"desc\"): QueryImpl {\n return this.fullTableScan().order(order);\n }\n\n // This is internal API and should not be exposed to developers yet.\n async count(): Promise<number> {\n const syscallJSON = await performAsyncSyscall(\"1.0/count\", {\n table: this.tableName,\n });\n const syscallResult = jsonToConvex(syscallJSON) as number;\n return syscallResult;\n }\n\n filter(\n predicate: (\n q: FilterBuilder<GenericTableInfo>,\n ) => ExpressionOrValue<boolean>,\n ) {\n return this.fullTableScan().filter(predicate);\n }\n\n limit(n: number) {\n return this.fullTableScan().limit(n);\n }\n\n collect(): Promise<any[]> {\n return this.fullTableScan().collect();\n }\n\n take(n: number): Promise<Array<any>> {\n return this.fullTableScan().take(n);\n }\n\n paginate(paginationOpts: PaginationOptions): Promise<PaginationResult<any>> {\n return this.fullTableScan().paginate(paginationOpts);\n }\n\n first(): Promise<any> {\n return this.fullTableScan().first();\n }\n\n unique(): Promise<any> {\n return this.fullTableScan().unique();\n }\n\n [Symbol.asyncIterator](): AsyncIterableIterator<any> {\n return this.fullTableScan()[Symbol.asyncIterator]();\n }\n}\n\n/**\n * @param type Whether the query was consumed or closed.\n * @throws An error indicating the query has been closed.\n */\nfunction throwClosedError(type: \"closed\" | \"consumed\"): never {\n throw new Error(\n type === \"consumed\"\n ? \"This query is closed and can't emit any more values.\"\n : \"This query has been chained with another operator and can't be reused.\",\n );\n}\n\nexport class QueryImpl implements Query<GenericTableInfo> {\n private state:\n | { type: \"preparing\"; query: SerializedQuery }\n | { type: \"executing\"; queryId: number }\n | { type: \"closed\" }\n | { type: \"consumed\" };\n private tableNameForErrorMessages: string;\n\n constructor(query: SerializedQuery) {\n this.state = { type: \"preparing\", query };\n if (query.source.type === \"FullTableScan\") {\n this.tableNameForErrorMessages = query.source.tableName;\n } else {\n this.tableNameForErrorMessages = query.source.indexName.split(\".\")[0];\n }\n }\n\n private takeQuery(): SerializedQuery {\n if (this.state.type !== \"preparing\") {\n throw new Error(\n \"A query can only be chained once and can't be chained after iteration begins.\",\n );\n }\n const query = this.state.query;\n this.state = { type: \"closed\" };\n return query;\n }\n\n private startQuery(): number {\n if (this.state.type === \"executing\") {\n throw new Error(\"Iteration can only begin on a query once.\");\n }\n if (this.state.type === \"closed\" || this.state.type === \"consumed\") {\n throwClosedError(this.state.type);\n }\n const query = this.state.query;\n const { queryId } = performSyscall(\"1.0/queryStream\", { query, version });\n this.state = { type: \"executing\", queryId };\n return queryId;\n }\n\n private closeQuery() {\n if (this.state.type === \"executing\") {\n const queryId = this.state.queryId;\n performSyscall(\"1.0/queryCleanup\", { queryId });\n }\n this.state = { type: \"consumed\" };\n }\n\n order(order: \"asc\" | \"desc\"): QueryImpl {\n validateArg(order, 1, \"order\", \"order\");\n const query = this.takeQuery();\n if (query.source.type === \"Search\") {\n throw new Error(\n \"Search queries must always be in relevance order. Can not set order manually.\",\n );\n }\n if (query.source.order !== null) {\n throw new Error(\"Queries may only specify order at most once\");\n }\n query.source.order = order;\n return new QueryImpl(query);\n }\n\n filter(\n predicate: (\n q: FilterBuilder<GenericTableInfo>,\n ) => ExpressionOrValue<boolean>,\n ): any {\n validateArg(predicate, 1, \"filter\", \"predicate\");\n const query = this.takeQuery();\n if (query.operators.length >= MAX_QUERY_OPERATORS) {\n throw new Error(\n `Can't construct query with more than ${MAX_QUERY_OPERATORS} operators`,\n );\n }\n query.operators.push({\n filter: serializeExpression(predicate(filterBuilderImpl)),\n });\n return new QueryImpl(query);\n }\n\n limit(n: number): any {\n validateArg(n, 1, \"limit\", \"n\");\n const query = this.takeQuery();\n query.operators.push({ limit: n });\n return new QueryImpl(query);\n }\n\n [Symbol.asyncIterator](): AsyncIterableIterator<any> {\n this.startQuery();\n return this;\n }\n\n async next(): Promise<IteratorResult<any>> {\n if (this.state.type === \"closed\" || this.state.type === \"consumed\") {\n throwClosedError(this.state.type);\n }\n // Allow calling `.next()` when the query is in \"preparing\" state to implicitly start the\n // query. This allows the developer to call `.next()` on the query without having to use\n // a `for await` statement.\n const queryId =\n this.state.type === \"preparing\" ? this.startQuery() : this.state.queryId;\n const { value, done } = await performAsyncSyscall(\"1.0/queryStreamNext\", {\n queryId,\n });\n if (done) {\n this.closeQuery();\n }\n const convexValue = jsonToConvex(value);\n return { value: convexValue, done };\n }\n\n return() {\n this.closeQuery();\n return Promise.resolve({ done: true, value: undefined });\n }\n\n async paginate(\n paginationOpts: PaginationOptions,\n ): Promise<PaginationResult<any>> {\n validateArg(paginationOpts, 1, \"paginate\", \"options\");\n if (\n typeof paginationOpts?.numItems !== \"number\" ||\n paginationOpts.numItems < 0\n ) {\n throw new Error(\n `\\`options.numItems\\` must be a positive number. Received \\`${paginationOpts?.numItems}\\`.`,\n );\n }\n const query = this.takeQuery();\n const pageSize = paginationOpts.numItems;\n const cursor = paginationOpts.cursor;\n const endCursor = paginationOpts?.endCursor ?? null;\n const maximumRowsRead = paginationOpts.maximumRowsRead ?? null;\n const { page, isDone, continueCursor, splitCursor, pageStatus } =\n await performAsyncSyscall(\"1.0/queryPage\", {\n query,\n cursor,\n endCursor,\n pageSize,\n maximumRowsRead,\n maximumBytesRead: paginationOpts.maximumBytesRead,\n version,\n });\n return {\n page: page.map((json: string) => jsonToConvex(json)),\n isDone,\n continueCursor,\n splitCursor,\n pageStatus,\n };\n }\n\n async collect(): Promise<Array<any>> {\n const out: Value[] = [];\n for await (const item of this) {\n out.push(item);\n }\n return out;\n }\n\n async take(n: number): Promise<Array<any>> {\n validateArg(n, 1, \"take\", \"n\");\n validateArgIsNonNegativeInteger(n, 1, \"take\", \"n\");\n return this.limit(n).collect();\n }\n\n async first(): Promise<any | null> {\n const first_array = await this.take(1);\n return first_array.length === 0 ? null : first_array[0];\n }\n\n async unique(): Promise<any | null> {\n const first_two_array = await this.take(2);\n if (first_two_array.length === 0) {\n return null;\n }\n if (first_two_array.length === 2) {\n throw new Error(`unique() query returned more than one result from table ${this.tableNameForErrorMessages}:\n [${first_two_array[0]._id}, ${first_two_array[1]._id}, ...]`);\n }\n return first_two_array[0];\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAE/C,qBAAoD;AACpD,iCAGO;AAIP,sCAGO;AACP,wCAGO;AACP,sBAA6D;AAC7D,eAAwB;AAExB,MAAM,sBAAsB;AAsBrB,MAAM,
|
|
4
|
+
"sourcesContent": ["import { Value, JSONValue, jsonToConvex } from \"../../values/index.js\";\nimport { PaginationResult, PaginationOptions } from \"../pagination.js\";\nimport { performAsyncSyscall, performSyscall } from \"./syscall.js\";\nimport {\n filterBuilderImpl,\n serializeExpression,\n} from \"./filter_builder_impl.js\";\nimport { Query, QueryInitializer } from \"../query.js\";\nimport { ExpressionOrValue, FilterBuilder } from \"../filter_builder.js\";\nimport { GenericTableInfo } from \"../data_model.js\";\nimport {\n IndexRangeBuilderImpl,\n SerializedRangeExpression,\n} from \"./index_range_builder_impl.js\";\nimport {\n SearchFilterBuilderImpl,\n SerializedSearchFilter,\n} from \"./search_filter_builder_impl.js\";\nimport { validateArg, validateArgIsNonNegativeInteger } from \"./validate.js\";\nimport { version } from \"../../index.js\";\n\nconst MAX_QUERY_OPERATORS = 256;\n\ntype QueryOperator = { filter: JSONValue } | { limit: number };\ntype Source =\n | { type: \"FullTableScan\"; tableName: string; order: \"asc\" | \"desc\" | null }\n | {\n type: \"IndexRange\";\n indexName: string;\n range: ReadonlyArray<SerializedRangeExpression>;\n order: \"asc\" | \"desc\" | null;\n }\n | {\n type: \"Search\";\n indexName: string;\n filters: ReadonlyArray<SerializedSearchFilter>;\n };\n\ntype SerializedQuery = {\n source: Source;\n operators: Array<QueryOperator>;\n};\n\nexport class QueryInitializerImpl implements QueryInitializer<GenericTableInfo> {\n private tableName: string;\n\n constructor(tableName: string) {\n this.tableName = tableName;\n }\n\n withIndex(\n indexName: string,\n indexRange?: (q: IndexRangeBuilderImpl) => IndexRangeBuilderImpl,\n ): QueryImpl {\n validateArg(indexName, 1, \"withIndex\", \"indexName\");\n let rangeBuilder = IndexRangeBuilderImpl.new();\n if (indexRange !== undefined) {\n rangeBuilder = indexRange(rangeBuilder);\n }\n return new QueryImpl({\n source: {\n type: \"IndexRange\",\n indexName: this.tableName + \".\" + indexName,\n range: rangeBuilder.export(),\n order: null,\n },\n operators: [],\n });\n }\n\n withSearchIndex(\n indexName: string,\n searchFilter: (q: SearchFilterBuilderImpl) => SearchFilterBuilderImpl,\n ): QueryImpl {\n validateArg(indexName, 1, \"withSearchIndex\", \"indexName\");\n validateArg(searchFilter, 2, \"withSearchIndex\", \"searchFilter\");\n const searchFilterBuilder = SearchFilterBuilderImpl.new();\n return new QueryImpl({\n source: {\n type: \"Search\",\n indexName: this.tableName + \".\" + indexName,\n filters: searchFilter(searchFilterBuilder).export(),\n },\n operators: [],\n });\n }\n\n fullTableScan(): QueryImpl {\n return new QueryImpl({\n source: {\n type: \"FullTableScan\",\n tableName: this.tableName,\n order: null,\n },\n operators: [],\n });\n }\n\n order(order: \"asc\" | \"desc\"): QueryImpl {\n return this.fullTableScan().order(order);\n }\n\n // This is internal API and should not be exposed to developers yet.\n async count(): Promise<number> {\n const syscallJSON = await performAsyncSyscall(\"1.0/count\", {\n table: this.tableName,\n });\n const syscallResult = jsonToConvex(syscallJSON) as number;\n return syscallResult;\n }\n\n filter(\n predicate: (\n q: FilterBuilder<GenericTableInfo>,\n ) => ExpressionOrValue<boolean>,\n ) {\n return this.fullTableScan().filter(predicate);\n }\n\n limit(n: number) {\n return this.fullTableScan().limit(n);\n }\n\n collect(): Promise<any[]> {\n return this.fullTableScan().collect();\n }\n\n take(n: number): Promise<Array<any>> {\n return this.fullTableScan().take(n);\n }\n\n paginate(paginationOpts: PaginationOptions): Promise<PaginationResult<any>> {\n return this.fullTableScan().paginate(paginationOpts);\n }\n\n first(): Promise<any> {\n return this.fullTableScan().first();\n }\n\n unique(): Promise<any> {\n return this.fullTableScan().unique();\n }\n\n [Symbol.asyncIterator](): AsyncIterableIterator<any> {\n return this.fullTableScan()[Symbol.asyncIterator]();\n }\n}\n\n/**\n * @param type Whether the query was consumed or closed.\n * @throws An error indicating the query has been closed.\n */\nfunction throwClosedError(type: \"closed\" | \"consumed\"): never {\n throw new Error(\n type === \"consumed\"\n ? \"This query is closed and can't emit any more values.\"\n : \"This query has been chained with another operator and can't be reused.\",\n );\n}\n\nexport class QueryImpl implements Query<GenericTableInfo> {\n private state:\n | { type: \"preparing\"; query: SerializedQuery }\n | { type: \"executing\"; queryId: number }\n | { type: \"closed\" }\n | { type: \"consumed\" };\n private tableNameForErrorMessages: string;\n\n constructor(query: SerializedQuery) {\n this.state = { type: \"preparing\", query };\n if (query.source.type === \"FullTableScan\") {\n this.tableNameForErrorMessages = query.source.tableName;\n } else {\n this.tableNameForErrorMessages = query.source.indexName.split(\".\")[0];\n }\n }\n\n private takeQuery(): SerializedQuery {\n if (this.state.type !== \"preparing\") {\n throw new Error(\n \"A query can only be chained once and can't be chained after iteration begins.\",\n );\n }\n const query = this.state.query;\n this.state = { type: \"closed\" };\n return query;\n }\n\n private startQuery(): number {\n if (this.state.type === \"executing\") {\n throw new Error(\"Iteration can only begin on a query once.\");\n }\n if (this.state.type === \"closed\" || this.state.type === \"consumed\") {\n throwClosedError(this.state.type);\n }\n const query = this.state.query;\n const { queryId } = performSyscall(\"1.0/queryStream\", { query, version });\n this.state = { type: \"executing\", queryId };\n return queryId;\n }\n\n private closeQuery() {\n if (this.state.type === \"executing\") {\n const queryId = this.state.queryId;\n performSyscall(\"1.0/queryCleanup\", { queryId });\n }\n this.state = { type: \"consumed\" };\n }\n\n order(order: \"asc\" | \"desc\"): QueryImpl {\n validateArg(order, 1, \"order\", \"order\");\n const query = this.takeQuery();\n if (query.source.type === \"Search\") {\n throw new Error(\n \"Search queries must always be in relevance order. Can not set order manually.\",\n );\n }\n if (query.source.order !== null) {\n throw new Error(\"Queries may only specify order at most once\");\n }\n query.source.order = order;\n return new QueryImpl(query);\n }\n\n filter(\n predicate: (\n q: FilterBuilder<GenericTableInfo>,\n ) => ExpressionOrValue<boolean>,\n ): any {\n validateArg(predicate, 1, \"filter\", \"predicate\");\n const query = this.takeQuery();\n if (query.operators.length >= MAX_QUERY_OPERATORS) {\n throw new Error(\n `Can't construct query with more than ${MAX_QUERY_OPERATORS} operators`,\n );\n }\n query.operators.push({\n filter: serializeExpression(predicate(filterBuilderImpl)),\n });\n return new QueryImpl(query);\n }\n\n limit(n: number): any {\n validateArg(n, 1, \"limit\", \"n\");\n const query = this.takeQuery();\n query.operators.push({ limit: n });\n return new QueryImpl(query);\n }\n\n [Symbol.asyncIterator](): AsyncIterableIterator<any> {\n this.startQuery();\n return this;\n }\n\n async next(): Promise<IteratorResult<any>> {\n if (this.state.type === \"closed\" || this.state.type === \"consumed\") {\n throwClosedError(this.state.type);\n }\n // Allow calling `.next()` when the query is in \"preparing\" state to implicitly start the\n // query. This allows the developer to call `.next()` on the query without having to use\n // a `for await` statement.\n const queryId =\n this.state.type === \"preparing\" ? this.startQuery() : this.state.queryId;\n const { value, done } = await performAsyncSyscall(\"1.0/queryStreamNext\", {\n queryId,\n });\n if (done) {\n this.closeQuery();\n }\n const convexValue = jsonToConvex(value);\n return { value: convexValue, done };\n }\n\n return() {\n this.closeQuery();\n return Promise.resolve({ done: true, value: undefined });\n }\n\n async paginate(\n paginationOpts: PaginationOptions,\n ): Promise<PaginationResult<any>> {\n validateArg(paginationOpts, 1, \"paginate\", \"options\");\n if (\n typeof paginationOpts?.numItems !== \"number\" ||\n paginationOpts.numItems < 0\n ) {\n throw new Error(\n `\\`options.numItems\\` must be a positive number. Received \\`${paginationOpts?.numItems}\\`.`,\n );\n }\n const query = this.takeQuery();\n const pageSize = paginationOpts.numItems;\n const cursor = paginationOpts.cursor;\n const endCursor = paginationOpts?.endCursor ?? null;\n const maximumRowsRead = paginationOpts.maximumRowsRead ?? null;\n const { page, isDone, continueCursor, splitCursor, pageStatus } =\n await performAsyncSyscall(\"1.0/queryPage\", {\n query,\n cursor,\n endCursor,\n pageSize,\n maximumRowsRead,\n maximumBytesRead: paginationOpts.maximumBytesRead,\n version,\n });\n return {\n page: page.map((json: string) => jsonToConvex(json)),\n isDone,\n continueCursor,\n splitCursor,\n pageStatus,\n };\n }\n\n async collect(): Promise<Array<any>> {\n const out: Value[] = [];\n for await (const item of this) {\n out.push(item);\n }\n return out;\n }\n\n async take(n: number): Promise<Array<any>> {\n validateArg(n, 1, \"take\", \"n\");\n validateArgIsNonNegativeInteger(n, 1, \"take\", \"n\");\n return this.limit(n).collect();\n }\n\n async first(): Promise<any | null> {\n const first_array = await this.take(1);\n return first_array.length === 0 ? null : first_array[0];\n }\n\n async unique(): Promise<any | null> {\n const first_two_array = await this.take(2);\n if (first_two_array.length === 0) {\n return null;\n }\n if (first_two_array.length === 2) {\n throw new Error(`unique() query returned more than one result from table ${this.tableNameForErrorMessages}:\n [${first_two_array[0]._id}, ${first_two_array[1]._id}, ...]`);\n }\n return first_two_array[0];\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;A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|
|
206
206
|
async function invokeAction(func, requestId, argsStr, visibility) {
|
|
207
|
-
globalThis.Convex?.setupPerformance?.();
|
|
208
207
|
const args = (0, import_values.jsonToConvex)(JSON.parse(argsStr));
|
|
209
208
|
const calls = (0, import_actions_impl.setupActionCalls)(requestId);
|
|
210
209
|
const ctx = {
|
|
@@ -243,7 +242,6 @@ const internalActionGeneric = ((functionDefinition) => {
|
|
|
243
242
|
return func;
|
|
244
243
|
});
|
|
245
244
|
async function invokeHttpAction(func, request) {
|
|
246
|
-
globalThis.Convex?.setupPerformance?.();
|
|
247
245
|
const requestId = "";
|
|
248
246
|
const calls = (0, import_actions_impl.setupActionCalls)(requestId);
|
|
249
247
|
const ctx = {
|