The backend (API and Database) for the revised version of the Small Bodies Image Browser (SBIB) at SBN PSI. This application is built in Typescript, an object-oriented riff on Javascript, using Node and Express to power the API, and TypeORM as an ORM to interface with the MySQL database.
The application is dockerized, so unless you wish to run the application locally, the only dependency is Docker. Otherwise, you'll need to install Node and MySQL separately. Every other dependency is managed by npm.
First, you will need to configure a .env file that configures all the related services as part of this application stack. Copy the .env.example file, and see below for explanation:
- MYSQL_ROOT_PASSWORD: A randomized password to set for the MySQL Root user (should be very strong, and will only be used to create a non-root user)
- MYSQL_DATABASE: Your preferred name of the database to load data into (should match TYPEORM_DATABASE)
- MYSQL_USER: Your preferred username for the application's database user
- MYSQL_PASSWORD: Your preferred username for the admin user
- TYPEORM_CONNECTION: Keep this as
mysql; Used to configure TypeORM - TYPEORM_HOST: Keep on
dbwhen using docker-compose. For local deployments/databases, change to localhost - TYPEORM_PORT: Keep this as
3306. Should match the port specified for the MySQL database indocker-compose.yml - TYPEORM_USERNAME: Should match the username specified in
MYSQL_USERabove - TYPEORM_PASSWORD: Should match the username specified in
MYSQL_PASSWORDabove - TYPEORM_DATABASE: Should match the username specified in
MYSQL_DATABASEabove - TYPEORM_SYNCHRONIZE: Whether or not to allow TypeORM to automatically perform changes to the database schema when model code is changed. Keep this as
falseunless you're actively developing. - TYPEORM_MIGRATIONS_RUN: Whether or not to allow TypeORM to automatically perform migrations when being deployed.
- TYPEORM_LOGGING: Whether or not to allow TypeORM to log any queries performed
- TYPEORM_ENTITIES: The location of the entity model classes used by TypeORM
- TYPEORM_MIGRATIONS: The location of the migrations used by TypeORM. These are generated by TypeORM CLI
This application is hosted as a docker container, and should be instantiated along with the database by using docker-compose:
$ docker-compose build
$ docker-compose up -dIf you have the correct .env file created, this will build and run your server locally at http://localhost:9494/.
It may be useful to run this application outside of docker-compose for a faster build-run cycle. To do so, you will need to stand up each service independently. First, you will need to set up a mysql database running on your system. You can achieve this by starting up the container that is created by docker-compose independently, or pulling a new mysql docker image and configuring it similarly, or by pointing to any other MySQL databse you have access to. Update the values for MYSQL_HOST and MYSQL_PORT in .env to match the database.
Then run:
$ npm install
$ npm run devAny time that you are ready to deploy changes that include any updates to the model/database, you will need to create a database migration file using the TypeORM CLI. Once you're ready to do this, install the CLI globally with npm:
npm i -g typeormAnd then run:
$ typeorm migration:create -d src/migrations -n [Your name for the migration]This should create a new file with the name specified, as well as some semirandom characters to keep migrations in the right order. Ensure that this file ends up in src/migrations.