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.
# 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:
crates/features/src/lib.rs— addpub mod posts;.crates/features/src/posts/mod.rs(new) — the folder index:mod service; mod module; pub mod http;plus thepub uselines. A single crate needs none, becausemain.rsdeclares its modules.crates/features/src/posts/http/mod.rs(new) —mod controller; mod module;plus itspub uselines, and ahttp/module.rsholding#[module(imports = [PostsModule], providers = [PostsController])].crates/features/src/posts/module.rs— dropHttpModule::for_root(…)and the controller fromproviders. In a workspace the transport comes with the adapter’s controller, and the port module provides only the service:#[module(providers = [PostsService])].- Imports —
crate::service::PostsServicebecomescrate::posts::PostsService; the app’smodule.rsaddsfeatures::posts::PostsHttpModuleto 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:
Error: duplicate controller prefix "/": HelloController and PostsControllerboth mount there — a controller prefix is its exclusive namespace; give eachone a distinct pathGive 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.
Going further
Section titled “Going further”- CLI — the workspace
nestrs newwrites. - Generators — the adapters
nestrs gadds to the moved feature. - Modules — how a root module composes features.