Convex.Functions reference
Action
Section titled “Action”Source:
packages/convex/src/Action.ts
Declare a Convex action. An action may call the outside world, and calls
queries and mutations with run. It is not a transaction: each
run commits on its own.
Action: Declaring a Function
Section titled “Action: Declaring a Function”export class Notify extends Convex.Action<Notify>()("messages:notify", { args: Schema.Struct({ id: Convex.Id("messages") }), internal: true,}) {}Connect
Section titled “Connect”Source:
packages/convex/src/Connect.ts
Give a Worker, Lambda or other host a deployment’s URLs, for the convex
client. No credentials are involved: public functions need none.
Connect: Connecting
Section titled “Connect: Connecting”Effect.gen(function* () { const convex = yield* Convex.Connect(Backend); return { fetch: Effect.gen(function* () { const client = new ConvexHttpClient(yield* convex.url); const rows = yield* Effect.tryPromise(() => client.query(api.messages.list, {})); return yield* HttpServerResponse.json(rows); }), };}).pipe(Effect.provide(Convex.ConnectHttp));Mutation
Section titled “Mutation”Source:
packages/convex/src/Mutation.ts
Declare a Convex mutation. A mutation reads and writes the database in one
transaction: a failure rolls every write back, and so does a conflict,
after which Convex runs it again. Side effects go through schedule,
which commits with the writes.
Mutation: Declaring a Function
Section titled “Mutation: Declaring a Function”export class EmptyBody extends Schema.TaggedError<EmptyBody>()("EmptyBody", {}) {}
export class AddMessage extends Convex.Mutation<AddMessage>()("messages:add", { args: Schema.Struct({ channel: Schema.String, body: Schema.String }), returns: Convex.Id("messages"), error: EmptyBody,}) {}Mutation: Implementing It
Section titled “Mutation: Implementing It”export const AddMessageLive = Layer.succeed(AddMessage, Effect.fn(function* ({ channel, body }) { if (body === "") return yield* new EmptyBody(); const id = yield* Messages.insert({ channel, body }); yield* Convex.schedule(Notify, { id }); return id; }),);Source:
packages/convex/src/Query.ts
Declare a Convex query: its path, arguments, result and failure. A query only reads, and Convex reruns it for subscribers when what it read changes.
The declaration holds no code, so a browser, Worker or Lambda can import
it to call the function. Implement it with a Layer, and serve it from a
file in convex/ with serve.
Query: Declaring a Function
Section titled “Query: Declaring a Function”export class ListMessages extends Convex.Query<ListMessages>()("messages:list", { args: Schema.Struct({ channel: Schema.String }), returns: Schema.Array(Messages.Document),}) {}Query: Implementing It
Section titled “Query: Implementing It”export const ListMessagesLive = Layer.succeed(ListMessages, Effect.fn(function* ({ channel }) { return yield* Messages.query((q) => q.withIndex("by_channel", (r) => r.eq("channel", channel)), ).collect; }),);Query: Serving It
Section titled “Query: Serving It”The path is the file in convex/ and the export name.
export const list = Convex.serve(ListMessages, ListMessagesLive);RunAction
Section titled “RunAction”Source:
packages/convex/src/RunAction.ts
Run a public Convex action from a Worker, Lambda or other host.
It needs no credentials and grants nothing: the host gets the deployment’s
URL. A function that throws fails with ConvexFunctionError. Values use
Convex’s JSON format, so Int64 and Bytes arrive as tagged objects.
RunAction: Calling a Function
Section titled “RunAction: Calling a Function”Effect.gen(function* () { const run = yield* Convex.RunAction(Backend); return { fetch: Effect.gen(function* () { const result = yield* run("messages:ping", { n: 41 }); return yield* HttpServerResponse.json(result); }), };}).pipe(Effect.provide(Convex.RunActionHttp));RunMutation
Section titled “RunMutation”Source:
packages/convex/src/RunMutation.ts
Run a public Convex mutation from a Worker, Lambda or other host.
It needs no credentials and grants nothing: the host gets the deployment’s
URL. A function that throws fails with ConvexFunctionError. Values use
Convex’s JSON format, so Int64 and Bytes arrive as tagged objects.
RunMutation: Calling a Function
Section titled “RunMutation: Calling a Function”Effect.gen(function* () { const run = yield* Convex.RunMutation(Backend); return { fetch: Effect.gen(function* () { const result = yield* run("messages:add", { body: "hello" }); return yield* HttpServerResponse.json(result); }), };}).pipe(Effect.provide(Convex.RunMutationHttp));RunQuery
Section titled “RunQuery”Source:
packages/convex/src/RunQuery.ts
Run a public Convex query from a Worker, Lambda or other host.
It needs no credentials and grants nothing: the host gets the deployment’s
URL. A function that throws fails with ConvexFunctionError. Values use
Convex’s JSON format, so Int64 and Bytes arrive as tagged objects.
RunQuery: Calling a Function
Section titled “RunQuery: Calling a Function”Effect.gen(function* () { const run = yield* Convex.RunQuery(Backend); return { fetch: Effect.gen(function* () { const result = yield* run("messages:list"); return yield* HttpServerResponse.json(result); }), };}).pipe(Effect.provide(Convex.RunQueryHttp));RunQueryBatch
Section titled “RunQueryBatch”Source:
packages/convex/src/RunQueryBatch.ts
Run several public Convex queries in one request, from a Worker, Lambda or other host. Every query sees the same snapshot of the database.
It needs no credentials and grants nothing. If any query throws, the batch
fails with ConvexFunctionError.
RunQueryBatch: Calling Functions
Section titled “RunQueryBatch: Calling Functions”Effect.gen(function* () { const batch = yield* Convex.RunQueryBatch(Backend); return { fetch: Effect.gen(function* () { const results = yield* batch([{ path: "messages:list" }, { path: "users:me" }]); return yield* HttpServerResponse.json(results); }), };}).pipe(Effect.provide(Convex.RunQueryBatchHttp));Source:
packages/convex/src/Serve.ts
Register a declared function with Convex, implemented by layer. Export
it from a file in convex/ under the declaration’s path: messages:list
is the list export of convex/messages.ts.
Convex checks the arguments’ encoded shape before the handler runs, and
the handler decodes them with the declaration’s schema. A typed failure
reaches the caller as a ConvexError holding the encoded value, and rolls
a mutation back.
The Layer is built once per Convex isolate, the first time any function that uses it runs, and shared by every function it provides.
Serve: Serving Functions
Section titled “Serve: Serving Functions”import { MessagesLive } from "../src/messages.live.ts";import { AddMessage, ListMessages } from "../src/messages.ts";
export const list = Convex.serve(ListMessages, MessagesLive);export const add = Convex.serve(AddMessage, MessagesLive);Source:
packages/convex/src/Table.ts
A Convex table whose documents are described by an Effect Schema. Indexes use Convex’s own builder, so every index type Convex has works here.
Its methods run on the database of the function they’re called from: a query may only read, and writing from one doesn’t type-check.
Table: Defining a Table
Section titled “Table: Defining a Table”A table with an index
export const Messages = Convex.Table( "messages", Schema.Struct({ channel: Schema.String, body: Schema.String }), (table) => table.index("by_channel", ["channel"]),);convex/schema.ts
export default Convex.schema(Messages);Table: Reading and Writing
Section titled “Table: Reading and Writing”const recent = yield* Messages.query((q) => q.withIndex("by_channel", (r) => r.eq("channel", channel)).order("desc"),).take(10);const id = yield* Messages.insert({ channel, body });