Skip to main content
Orionjs apps can call Rust in the same process. Each folder in ./rust is a small library; each one becomes a service you inject like any other. Every exported function returns a Promise and runs on a separate thread pool, so it never blocks the event loop. You need a Rust toolchain (rustup) on the machines that build the app, and @orion-js/native in your dependencies.

Create a library

This creates:
Commit both rust/ and app/rust/. rust/target is added to .gitignore.

Write Rust

  • #[model] structs become TypeScript interfaces. Field names are camelCased (created_at → createdAt), _id is kept and Option<T> fields are optional.
  • #[export] functions become async functions on the library’s service.
  • Supported types: String, bool, f64, f32, 32-bit and smaller integers, Option, Vec, HashMap<String, T>, DateTime (a JS Date), Bytes (a Buffer), and other models. 64-bit integers don’t fit in a JS number and are a compile error.

Use it from TypeScript

Errors

Large data

Top-level Vec<T> params and returns are sent in small frames, so 100k documents don’t pause the event loop. To read many Mongo documents, skip JS decoding entirely and pass raw BSON:

Libraries using each other

A library can depend on another one in its Cargo.toml (orders = { path = "../orders" }) and call it directly in Rust. Models are shared too: the generated TypeScript imports them from the other library’s file. Model names must be unique across libraries.

Commands

  • orion dev compiles ./rust before starting and rebuilds on changes. A rebuild restarts the app.
  • orion build compiles in release mode, fails if app/rust is out of date, and writes native.node next to the bundle. Deploy them together.
  • orion rust build [--release] [--check] compiles and updates app/rust. Use --check in CI.
  • orion rust new <name> creates a library.
All libraries are linked into one addon and share one thread pool (one thread fewer than the number of cores, set with ORION_NATIVE_THREADS).