Reference
newt add subscription
Runs a use case of a module each time one of its events happens.
newt add subscription makes a use case run after an event, such as notifying the assignee when a task is completed. The use case runs later as a job: a pending piece of work kept in a queue in your database (the outbox).
newt add subscription <context>/<module> <use-case> --on <event> [options]Arguments
Section titled “Arguments”| Argument | Meaning |
|---|---|
context/module |
A module of the backend, as in tasks/task |
use-case |
A use case of that module, as in notify-assignee |
Options
Section titled “Options”| Option | Default | Meaning |
|---|---|---|
--on <event> |
required | The event of the module it runs on, as in completed |
--backend <folder> |
the one you are in | The backend, in a monorepo with several |
It also takes the global flags.
What it writes
Section titled “What it writes”newt add subscription tasks/task notify-assignee --on completed created src/contexts/tasks/modules/task/task.workers.ts updated src/contexts/tasks/modules/task/task.module.ts
Warnings: newt cannot tell how to make completed happen, so it wrote no test of notifyAssignee on it: write one in task.module.test.ts
Next steps: bun test src/contexts/tasks/modules/taskimport type { Task_Module } from './task.module'
export function registerTaskWorkers(tasks: Task_Module): void { tasks.on(tasks.events.completed, 'notifyAssignee', completed => [ { id: completed.id }, ])}<module>.workers.tsis created if missing, and gets the line that subscribes the use case.<module>.module.tsgets the event ineventsOf, and a call toregister<Module>Workers(module).- When the use case takes an input, it receives
{ id }from the event, or the id itself when its input is the entity’s id (TaskId). When it takes none, it is called with nothing. - On the
createdevent, newt also writes a test that creates an entity and checks the job ran. For other events it warns instead.
Next steps
Section titled “Next steps”bun test src/contexts/<context>/modules/<module>
Errors
Section titled “Errors”- Missing arguments or
--on:newt add subscription takes the module, its use case and --on the event. - A use case the module does not have: refused; add it with
newt add use-case. - An event the module does not have: refused; it listens to the module’s own events only. Add it with
newt add event.
See Events and subscriptions and Jobs and workers.