Skip to main content

Crate convex_typegen

Crate convex_typegen 

Source
Expand description

§convex-typegen

Rust types from your Convex schema.ts and function modules. Runs in build.rs so generated code stays in sync with your backend.

Docs: docs.rs/convex-typegen

§Setup

  1. Add the crate (library + build script):
cargo add convex-typegen
cargo add --build convex-typegen
  1. Add build.rs:
use convex_typegen::prelude::*;

fn main() {
    let config = Configuration::default();

    println!("cargo:rerun-if-changed={}", config.schema_path.display());
    println!("cargo:rerun-if-changed={}", config.convex_dir.display());
    for path in rcfp(&config).expect("resolve convex function sources") {
        println!("cargo:rerun-if-changed={}", path.display());
    }

    if let Err(e) = generate(config) {
        panic!("convex-typegen failed: {e}");
    }
}
  1. cargo build writes the generated file (see defaults below). Include it in your crate (mod convex_types; or whatever matches out_file) and pull in convex_typegen::prelude::* where you call Convex with the generated arg types.

A full minimal setup lives in examples/basic in this repo.

§Generated names

Function argument structs are named {Module}{Export}Args when the export does not already start with the module file name (without .ts). Examples:

Module fileExportGenerated struct
games.tsgetGameGamesGetGameArgs
mod_a.tslistModAListArgs
tasks.tstasksSearchTasksSearchArgs

FUNCTION_PATH strings are unchanged (e.g. "games:getGame").

§Defaults

FieldDefault
schema_pathconvex/schema.ts
out_filesrc/convex_types.rs
convex_dirconvex

Paths are relative to the package directory when Cargo runs the build script.

Function sources: every *.ts under convex_dir, except the schema file, _generated/, node_modules/, and *.d.ts. Set function_paths to a non-empty list to skip discovery and pass files explicitly.

§Features

FeatureDefaultDescription
clientonRe-exports ConvexClientExt, IntoConvexValue, ConvexValueExt (pulls in the convex crate).
verboseoffPrint Oxc diagnostics to stderr during parse failures.

Build-only usage (smaller dependency tree):

[build-dependencies]
convex-typegen = { version = "0.3", default-features = false }

§Serde

Generated code pulls serde and serde_json through convex-typegen, so a minimal app crate does not need those as direct dependencies unless you use them yourself.

§License

MIT — see LICENSE.

§Crate layout

  • config::Configuration — input paths and optional explicit function file list.
  • generate — end-to-end pipeline used from build.rs.
  • prelude — re-exports for build.rs and for crates that consume generated code.
  • fs::rcfp — resolves the same function source paths as generate (for cargo:rerun-if-changed).

Parsing and codegen live in the private convex module; the public API stays small on purpose.

§Serde

Generated files use #[serde(crate = "convex_typegen::serde")] and convex_typegen::serde_json::… so application crates do not need direct serde / serde_json dependencies unless they use those crates themselves.

Re-exports§

pub use serde;
pub use serde_json;

Modules§

config
Build-time inputs for the generator.
prelude
Convenient glob import for build.rs and for crates that call Convex with generated types.

Traits§

ConvexClientExtclient
Ergonomic helpers for the official Convex Rust client (see prelude). Blanket helpers on convex::ConvexClient for generated argument structs.

Functions§

generate
Runs the full generator: resolve TypeScript inputs, parse with Oxc to ESTree JSON, walk that JSON into an internal model, then emit Rust to Configuration::out_file.