This example repository shows how you can use tsoa with koa to generate swagger and routes for an api that would write to a SQL database via TypeORM
Note: This has a lot of advanced cases that you may not need:
- It builds a different swagger.yaml file based on the environment it's running in. See tsoaCustom.ts to learn how that works
- This has dockerfiles and CodeFresh pipeline files so it can be easily run in CICD
- It enforces that certain environment variables are present at compile time and at runtime. See src\config\configInitiator.ts for more information
- It has a docker-compose.yml file that will create a local database for you. If you want to try running without that, you can set process.env.DEMO_MODE to true
- Ctrl+shift+f for REPLACE_ME and insert your own values
- VSCode Setup
- Postgres
- TypeORM guide - Includes information about writing migrations
- API - Swagger Editor - Import current config by using Import -> Url
http://localhost:5555/swagger.json
instructions here - Postgres Tools - Adminr
- Postgres Tools - Pg Admin
- Node 10.15.3
- npm
- yarn
- Docker Desktop
- Install Dependencies:
yarn
- Initialize the database:
yarn db:init
- run
yarn db:where-is-dev-db-running
to get the ip address you will use for PGAdmin setup
- run
- Run migrations so that your schemas are current
yarn db:migrate:run
- To run the development server:
yarn start:dev
. If you would prefer to debug inside your editor, you can look at the editor config doc for help. - To open up PgAdmim - (use values from file docker-compose.yml)
- get the pgadmin.ports value WITHOUT the colon and part to the right as pgAdminValue (for example: localhost:9000)
- PgAdmin will run in a browser at localhost:{pgAdminValue}
- use the PGADMIN_DEFAULT_EMAIL and PGADMIN_DEFAULT_PASSWORD to login
- create a new Server and connection.
- the server name is the value of POSTGRES_DB
- the connection Host name/Address is where the dev db is running (see step 2)
- the UserName is the value of POSTGRES_USER
- the Passowrd is the value of POSTGRES_PASSWORD
- Post a new orator using the swagger page and expect a 200 response to validate your dev setup is complete.
To build for production: yarn build
. Which can then be started with: $ yarn start
start:dev
- this is what you'll want to run if you're running locallytest
- run unit teststest:cov
- run tests with code coverage. Note this will mangle stack traces on failure reports, so it is generally recommended to use the baretest
command for development an only run this when a coverage report is desired. HTML coverage report will be written to thecoverage
directory.build
- build for release. writes output tobuild
directorystart
- run application from built source- Dev Server
dev-server
- run development server from the command linedev-server:debug
- run development server with additional logging
lint
- run typescript linteravailable-routes
- see which routes koa is currently making available- Docker
docker:build
docker:run
- Database Interaction
db:migrate:generate nameOfTheNewMigration
- this should be run any time there is a schema changedb:migrate:run
- run all migrations (utilizes a tablemigrations
)db:migrate:revert
- undo most recent migration
docker build -t msvc-example-service .
- build the image RUN FROM CMD WINDOW FAILS _ RUN FROM TERMINAL IN VSCODE FAILSdocker run -d -p 5555:5555 msvc-example-service
- run docker container- on the host machine open a browser and test that it is running here: http://localhost:5555/api/health