Reference
newt add module
Adds a module to a context, with its entity, use cases, routes, table and test.
newt add module adds one kind of thing to a context, such as task in tasks. It writes a working module: an entity, its schema, a repository, use cases to create, get and list, routes, a table and a test.
newt add module <context>/<module> [options]Arguments
Section titled “Arguments”| Argument | Meaning |
|---|---|
context |
A context of the backend, as in tasks |
module |
The module’s name, singular, as in task or price-change |
Options
Section titled “Options”| Option | Default | Meaning |
|---|---|---|
--plural <name> |
English rules | Its plural, when the rules get it wrong: --plural people |
--backend <folder> |
the one you are in | The backend, in a monorepo with several |
It also takes the global flags.
- The plural names the routes (
/tasks) and the module’s variable (tasks). - The rules add
s,esafters x z ch sh,iesfor a consonant plusy, andesfor-siswords.child,person,man,woman,mouse,goose,foot,tooth,quiz,news,seriesandspeciesare known.
Name rules
Section titled “Name rules”- Lowercase letters and digits, words joined by
-, starting with a letter:task,price-change. The plural follows the same form. - Not a word JavaScript keeps (
class,delete,new…). - Not a name newt’s generated code uses for something else (
module,base,router,events,id,name,entity,page…). - Its plural cannot be
files:/<context>/filesserves the context’s files. - Not starting with
newt-: tables starting withnewt_are newt’s own. - The table name (
price_change) fits in 63 characters.
From price-change, newt makes PriceChange, priceChange, the table price_change, and the plural priceChanges / price-changes.
What it writes
Section titled “What it writes”newt add module tasks/task created src/contexts/tasks/modules/task/value-objects/task-id.ts created src/contexts/tasks/modules/task/task.schema.ts created src/contexts/tasks/modules/task/events/task-created.ts created src/contexts/tasks/modules/task/errors/task-not-found.ts created src/contexts/tasks/modules/task/task.ts created src/contexts/tasks/modules/task/task.repository.ts created src/contexts/tasks/modules/task/task.module.ts created src/contexts/tasks/modules/task/task.routes.ts created src/contexts/tasks/modules/task/task.module.test.ts created src/contexts/tasks/migrations/00001_task_up.sql created src/contexts/tasks/migrations/00001_task_down.sql updated src/contexts/tasks/tasks-modules.ts
Next steps: bun test src/contexts/tasks/modules/task its routes ask for a session: write authenticate in src/auth.ts to call them from outside the tests- The migrations get the next number in the context’s
migrations/folder, and are written in the backend’s SQL (SQLite, Postgres or MySQL). <context>-modules.tsgets the line that starts the module.- The routes are
POST /<plural>,GET /<plural>andGET /<plural>/:id, under/<context>/api.
Next steps
Section titled “Next steps”bun test src/contexts/<context>/modules/<module>- Write
authenticateinsrc/auth.tsto call the routes from outside the tests.
Errors
Section titled “Errors”- Not
<context>/<module>:newt add module takes the context and the module, as in newt add module sales/order. - A context the backend does not have: refused; add it with
newt add context. - A name that breaks the rules:
'<name>' cannot name a module: …, saying which rule. - A bad
--plural:'<plural>' cannot be the plural of <module>. src/stores.tsopens no database newt knows: refused, since newt cannot tell which SQL to write.
See Contexts and modules and Entities.