Developing a nest-api
After generating
Section titled “After generating”Generation already copied .env.example to .env (and .env.test), ran
yarn install, formatted the code, generated the Prisma client and made the
first commit.
-
Finish
.env. SetSMTP_USERandSMTP_PASSWORD(see SMTP for a local mail catcher) and a realBETTER_AUTH_SECRET(openssl rand -base64 32). Or pull the whole file:yarn secrets. -
Start PostgreSQL and Redis:
Terminal window yarn dc:upyarn dc:wait # blocks until both accept connectionsWith
--with temporal, alsodocker compose up -d temporal(the Temporal UI is then at http://localhost:8233). -
Create the schema:
yarn db:migratecreates and applies the first migration (it asks for a name). -
Seed the superuser:
yarn db:seed. Run it once: a second run fails on the existing email. -
Run it:
yarn start(Nest in watch mode), then openhttp://localhost:5000/docsand sign in withBASIC_AUTH_USERandBASIC_AUTH_PASS.
Scripts
Section titled “Scripts”| Script | Does |
|---|---|
yarn start |
Nest in watch mode (--no-watch, --debug) |
yarn build, yarn start:prod |
Compile to dist/, run it |
yarn lint, yarn format |
ESLint (with fixes), Prettier |
yarn db:generate |
Generate the Prisma client |
yarn db:migrate, yarn db:migrate:create |
Create and apply migrations (:create writes without applying) |
yarn db:migrate:prod |
Apply migrations in production (prisma migrate deploy) |
yarn db:push |
Push the schema without a migration (prototyping) |
yarn db:seed, yarn db:reset, yarn db:studio |
Seed; reset the dev database; Prisma Studio on port 5025 |
yarn dc:up, dc:wait, dc:down, dc:recreate |
The development PostgreSQL and Redis |
yarn dc:reinstall |
Recreate, generate, migrate and seed in one go |
yarn email:dev |
Preview the email templates on port 5003 |
yarn secrets [-e env] |
Pull .env files from Infisical |
yarn test # unit testsyarn test:dc:reinstall && yarn test:e2e # e2e: a separate PostgreSQL and RedisThe e2e suite boots the real AppModule against the test stack (PostgreSQL
5499, Redis 6399, from compose.test.yml), with mail, media and feature flags
replaced by fakes. Specs run strictly one at a time: they share one database,
truncated and reseeded between specs. test/factory has the fixtures and a
signIn() helper that returns a session cookie.
Adding things
Section titled “Adding things”- A module:
yarn gen module <name>(Nest schematics), then register it inAppModule. Controllers opt into versioning withversion: '1'. - A route that’s public:
@AllowAnonymous(). Everything else requires a session. - Validation: pass a Zod schema,
@Body({ schema }), and declare the response with@Returns(schema). - A queue:
QueueModule.register('<name>')in the module that produces to it; the dashboard picks it up. - An email: a React Email template in
emails/, a job name insrc/common/types/email.ts, and a method onMailService. - A config variable: interface, schema and
getVariablesinconfig.service.ts, a line in.env.example, and a note in the docs.