Skip to content

Convex.Functions reference

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.

export class Notify extends Convex.Action<Notify>()("messages:notify", {
args: Schema.Struct({ id: Convex.Id("messages") }),
internal: true,
}) {}

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.

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));

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.

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,
}) {}
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.

export class ListMessages extends Convex.Query<ListMessages>()("messages:list", {
args: Schema.Struct({ channel: Schema.String }),
returns: Schema.Array(Messages.Document),
}) {}
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;
}),
);

The path is the file in convex/ and the export name.

export const list = Convex.serve(ListMessages, ListMessagesLive);

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.

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));

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.

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));

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.

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));

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.

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.

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.

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);
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 });