- Test db reconnecting somehow
- GET single user endpoint example
- PATCH endpoint example
- DELETE endpoint example
- add second entity and demonstrate JOIN behavior
Search for '#PROJECT_INIT' to update all template places to be a project specific
http://localhost:3000/api/v1/api-docs/
# required node >= 16
cp .env.sample .env # Make local .env file from sample
npm i # Install dependencies
npm run compose # Start server dependencies (database, .etc)
npm run migrate # Run database migrations
npm run dev # Start server
npm test
- Set
LOG_LEVEL=debug
for production to log all levels. For dev we only interested inwarn
anderror
levels
All files in camelCase. Exception - migrations(kebab-case)
Basic layered architecture of most endpoints is handled with following structure
# Required. Registers controller and middlewares
src/routes/<endpoints_folder>/index.ts
# Required. Controller
src/routes/<endpoints_folder>/controller.ts
# Required. Service
src/routes/<endpoints_folder>/service.ts
# Required. Schema
src/routes/<endpoints_folder>/schema.ts
# Required. Swagger (API doc)
src/routes/<endpoints_folder>/swagger.ts
# Optional. May be created to locate helpers that are needed
# only inside for these endpoint (to not overload src/utils folder)
src/routes/<endpoints_folder>/utils.ts
- Controller level:
- responsible for:
- parsing/mapping request params if needed
- calling service method (more than one if needed)
- handling errors from services to respond with proper http status
- cannot do db operations (cannot use ORM entities)
- responsible for:
- Service level:
- responsible for:
- business logic
- operations with DB (using ORM entities)
- throwing errors that will be catched by controller/middlewares to send http response
- services shouldn't use
req
,res
,next
params - this is responsibility of controller level to pass data from request object to service
- responsible for:
- Utils:
- contains any reusable utility code
- If there's clear domain of some utilities to combine - create new file in
src/utils
folder, otherwise - locate this code insrc/utils/index.ts
- In case there's service logic that need to be reused in other controllers/services it should be located in
src/services
folder
- If interface/type is needed only for a specific file and nowhere else:
- put it on the bottom of file
- don't export it to not overload autoimport's autocomplete
- If interface/type is reusable
- locate it in
src/interaces/index.ts
- Yes, it will be a mess of interfaces. If there's clear domain of some interfaces - create separate file for it in
src/interfaces
folder, otherwise don't create new file for a single interface - mess of interfaces insrc/interfaces/index.ts
is better than of million of small files insrc/interfaces
.
- locate it in