0.1.0 • Published 3 years ago

evari-quotes-api v0.1.0

Weekly downloads
2
License
UNLICENSED
Repository
github
Last release
3 years ago

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

  1. Install the legacy version of postgres from postgresapp.com that is closest to the 9.6.8 version that is supported by aurora.
  2. Run Postgres.app
  3. Open the side menu and remove any existing servers by clicking the "-" symbol on the bottom left
  4. Create a 9.6 server from the side menu, name "EvariPlatform", everything else leave as default.
  5. Click Initialize on the postgres app

Connecting to local postgres

  1. Install Postico from the website.
  2. Open Postico and setup a new favourite with the nickname "EvariPlatform" and everything else default
  3. Click EvariPlatform at the top left in the breadcrumbs to show all the current databases
  4. Create a new database called "evariplatform"
  5. Hit the red button to disconnect from the database
  6. Edit the favourite to change the Database to "evariplatform"
  7. Click EvariPlatform at the top left in the breadcrumbs to show all the current databases
  8. 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

  1. Define entitiy in db/entity
  2. Create your schema defined in the @Entity decorator of your new entities by running CREATE SCHEMA quote; within postico
  3. Run npm run db:migration:apply to run existing migrations
  4. Edit the existing entities or create new ones in the db/entity folder
  5. 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 or npm run db:migration:generate -- -n addNewLastUpdatedColumnToWidget
  6. 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

  1. Download and install the JDK - https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
  2. pip install localstack --user localstack
  3. pip install amazon_kclpy // you need to do this because of a bug in localstack - might be fixed by the time you read this
  4. localstack start
  5. 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 from src/db/migration dir always apply before you generate the migration script in case of Type ORM
  • npm run db:migration:generate -- -n [name] - - will generate a migration file named [timestamp]-[name].ts in src/db/migration dir
  • npm 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.