Reference
newt add event
Adds an event to a module, in its events folder.
newt add event adds a record of something that happened in a module, such as a task being completed. Other parts of the app can then react to it.
newt add event <context>/<module> <event> [options]Arguments
Section titled “Arguments”| Argument | Meaning |
|---|---|
context/module |
A module of the backend, as in tasks/task |
event |
What happened, as in completed or price-changed |
Options
Section titled “Options”| Option | Default | Meaning |
|---|---|---|
--backend <folder> |
the one you are in | The backend, in a monorepo with several |
It also takes the global flags.
Name rules
Section titled “Name rules”- Lowercase letters and digits, words joined by
-, starting with a letter. - It does not repeat the module: in
tasks/task,completed, nottask-completed. The file is namedtask-completed.tsanyway. - Not
module,repositoryordeps:Task_Module,Task_RepositoryandTask_Depsare the module’s own.
What it writes
Section titled “What it writes”newt add event tasks/task completed created src/contexts/tasks/modules/task/events/task-completed.ts
Next steps: emit it from a method of the entity: this.trackEvent(new Task_Completed({ id: this.id })) let other modules subscribe to it: add completed: Task_Completed to eventsOf in task.module.tsimport { defineEvent } from '@newt-js/core'// --------------------------------------------------------import { TaskSchema } from '../task.schema'
export const Task_Completed = defineEvent( 'task.completed', TaskSchema.pick({ id: true }),)If the module has no TaskSchema in <module>.schema.ts, the event’s schema is written inline instead.
Next steps
Section titled “Next steps”- Emit it from a method of the entity:
this.trackEvent(new Task_Completed({ id: this.id })). - To let other modules subscribe, add
completed: Task_CompletedtoeventsOfin<module>.module.ts.newt add subscriptiondoes this for you.
Errors
Section titled “Errors”- Missing arguments:
newt add event takes the module and the event, as in newt add event sales/order shipped. - A name that repeats the module:
'task-completed' repeats the module: name it completed, and it becomes Task_Completed in events/task-completed.ts. module,repositoryordeps: refused; name it by what happened.- A context or module that is not there: refused, naming the command that adds it.