Skip to content

Move an existing crate

Bring a crate you already have — a small service, a prototype — under a workspace's `crates/features/`, by hand, keeping the app boundary yours.

Bringing a crate you already have — a small service, a prototype — under crates/features/ is a handful of file relocations. There is no nestrs migrate for it: doing it by hand keeps you in control of the app boundary you are introducing.

The one thing to know before you start: nestrs new <workspace> already writes a hello feature and a hello app — the starter that proves the workspace runs. Move the incoming crate in under its own name. mv does not warn about overwriting, and nestrs new initialises no git repository, so a collision has nothing to recover from.

Terminal window
# 1. Scaffold the workspace beside the crate you are moving.
nestrs new acme && cd acme
# 2. Give the incoming crate its own feature folder, with the adapter
# sub-folder the workspace layout uses.
mkdir -p crates/features/src/posts/http
# 3. Move the port and the adapter into their conventional homes. `main.rs`
# stays behind: apps/hello/src/main.rs already exists and does the same job.
mv ../posts/src/service.rs crates/features/src/posts/
mv ../posts/src/module.rs crates/features/src/posts/
mv ../posts/src/controller.rs crates/features/src/posts/http/

Then the edits a move cannot do:

  1. crates/features/src/lib.rs — add pub mod posts;.
  2. crates/features/src/posts/mod.rs (new) — the folder index: mod service; mod module; pub mod http; plus the pub use lines. A single crate needs none, because main.rs declares its modules.
  3. crates/features/src/posts/http/mod.rs (new) — mod controller; mod module; plus its pub use lines, and a http/module.rs holding #[module(imports = [PostsModule], providers = [PostsController])].
  4. crates/features/src/posts/module.rs — drop HttpModule::for_root(…) and the controller from providers. In a workspace the transport comes with the adapter’s controller, and the port module provides only the service: #[module(providers = [PostsService])].
  5. Imports — crate::service::PostsService becomes crate::posts::PostsService; the app’s module.rs adds features::posts::PostsHttpModule to its imports.

Both halves of step 4 matter. Leaving HttpModule in the port would open an HTTP server in every binary reaching PostsService — a worker included — and a config pinned there contests the app’s own at boot. And a moved controller still declaring #[controller(path = "/")] collides with the starter one:

Terminal window
Error: duplicate controller prefix "/": HelloController and PostsController
both mount there — a controller prefix is its exclusive namespace; give each
one a distinct path

Give the moved controller its own prefix, or delete the starter hello feature and app once yours is serving.

crates/migrations/ and crates/seed/ come from nestrs new, so the database verbs and g migration work from the first nestrs run db up. From here every generator is available: nestrs g http <feature> and its siblings wire themselves into the app whose directory you are standing in, or the one --app names.

  • CLI — the workspace nestrs new writes.
  • Generators — the adapters nestrs g adds to the moved feature.
  • Modules — how a root module composes features.