evari-quotes-api
v0.1.0
Published
Evari Quotes Microservice API
Downloads
1
Readme
Evari Quotes Microservice API
This is the API component of the Evari quotes microservice. It is built using the typescript-rest package and is based off the Evari Template API. Tests and swagger documentation are included.
Initial setup (VS Code)
Install the tslint and tsfmt extensions.
Add Evari NPM token to local env
Add export EVARI_NPM_TOKEN=[Value from mission critical doc]
to bash profile.
Installing
Run npm install
to install dependencies
Getting Started
Setting up Local Postgres Instance
- Install the legacy version of postgres from postgresapp.com that is closest to the 9.6.8 version that is supported by aurora.
- Run Postgres.app
- Open the side menu and remove any existing servers by clicking the "-" symbol on the bottom left
- Create a 9.6 server from the side menu, name "EvariPlatform", everything else leave as default.
- Click Initialize on the postgres app
Connecting to local postgres
- Install Postico from the website.
- Open Postico and setup a new favourite with the nickname "EvariPlatform" and everything else default
- Click EvariPlatform at the top left in the breadcrumbs to show all the current databases
- Create a new database called "evariplatform"
- Hit the red button to disconnect from the database
- Edit the favourite to change the Database to "evariplatform"
- Click EvariPlatform at the top left in the breadcrumbs to show all the current databases
- Delete the database that had been created by default with the name of the user on your machine (do not delete the db called postgres, template1 or evariplatform)
Creating a Database Table and Schema
- Define entitiy in db/entity
- Create your schema defined in the @Entity decorator of your new entities by running
CREATE SCHEMA quote;
within postico - Run
npm run db:migration:apply
to run existing migrations - Edit the existing entities or create new ones in the db/entity folder
- If you made an entity change, run
npm run db:migration:generate -- -n descriptionOfFeature
to create the migration eg.npm run db:migration:generate -- -n createWidgetTables
ornpm run db:migration:generate -- -n addNewLastUpdatedColumnToWidget
npm run db:migration:apply
to create the table
Run npm run build
to generate type resources for request body validation.
Localstack
To run in test mode, all external dependencies will need to be hosted on your local machine via localstack
https://github.com/localstack/localstack
Download and install the JDK - https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
pip install localstack --user localstack
pip install amazon_kclpy
// you need to do this because of a bug in localstack - might be fixed by the time you read thislocalstack start
Create all the required AWS infrastructure in your localstack environment
aws --endpoint-url=http://localhost:4575 sns create-topic --name quotes-created
aws --endpoint-url=http://localhost:4575 sns create-topic --name quotes-updated
aws --endpoint-url=http://localhost:4576 sqs create-queue --queue-name evari-quotes-queue
aws --endpoint-url=http://localhost:4575 sns subscribe --topic-arn arn:aws:sns:ap-southeast-2:123456789012:quotes-created --protocol sqs --notification-endpoint arn:aws:sns:ap-southeast-2:123456789012:evari-quotes-queue
aws --endpoint-url=http://localhost:4575 sns subscribe --topic-arn arn:aws:sns:ap-southeast-2:123456789012:quotes-updated --protocol sqs --notification-endpoint arn:aws:sns:ap-southeast-2:123456789012:evari-quotes-queue
NOTE! ALL SNS/SQS RESOURCES ARE LOST WHEN YOU STOP LOCALSTACK - A SOLUTION FOR THIS WILL HAVE TO BE DEVELOPED
Running in different evironments
npm start
Will run the api with all possible external dependencies running locally via localstack. Particularly difficult to mock services will be created in the AWS Dev tennant
npm run start:stage
Will run the api with all external dependencies running in the AWS Stage tennant (not recommended to run here as you may "steal" SNS/SQS messages that other people are relying on)
npm run start:prod
Will run the api with all external dependencies running in the AWS Prod tennant (not recommended to run here as you may "steal" SNS/SQS messages that other people are relying on)
Running the tests
As part of the test script a prescript will run that will buld the project and run the linter. Failure on any of these and the tests will fail to run.
npm test
Deployment
TODO: configure circle ci
Versioning
Versioning will be controlled by the CI server. Any merges to master should be a patch version bump.
Database
npm scripts to manage migrations:
npm run db:migration:apply
- will run all migration fromsrc/db/migration
dir always apply before you generate the migration script in case of Type ORMnpm run db:migration:generate -- -n [name]
- - will generate a migration file named[timestamp]-[name].ts
insrc/db/migration
dirnpm run db:migration:revert
- will execute down in the latest executed migration. If you need to revert multiple migrations you must call this command multiple times.