The example usage of TypeORM with MySQL.
This repogitory contains:
- Node.js Backend API (Communicate MySQL with TypeORM) (accesible from host at PORT 13000)
- MySQL
The above are provided with Docker Container so you need to be able to run Docker Compose command.
- Database TimeZone is set to Japanese (Asia/Tokyo). If you want to change TimeZone, please edit
environment variables
written in docker-compose.yml.
Launch docker containers with Docker Compose command.
docker-compose up
Then, DB migration runs automatically and table will be created.
We assume the website where users post reviews for stores. The default tables are stores and reviews .
Field | Type | Null | Key |
---|---|---|---|
id | int(11) | NO | PRI |
score | float | NO | |
name | varchar(255) | NO | |
address | varchar(255) | NO | |
genre | varchar(255) | NO |
Field | Type | Null | Key |
---|---|---|---|
id | int(11) | NO | PRI |
storeId | int(11) | NO | |
score | float | NO | |
title | text | NO | |
content | text | NO |
You can log in MySQL in docker container by executing the following command.
docker exec -it typeorm-example-db mysql -u user -ppass example
You can dump MySQL by executing the following command.
docker exec typeorm-example-db mysqldump -u user -ppass example > dump.sql
# From Host Machine
docker exec -it typeorm-example-api yarn create:migration myFooBarTable
# Inside Container
$(npm bin)/ts-node -r tsconfig-paths/register $(npm bin)/typeorm migration:create -n myFooBarTable
Then edit api/src/migration/myFooBarTable/
docker exec -it typeorm-example-api yarn migrate
# The following command revert migration
# docker exec -it typeorm-example-api yarn migrate:revert
# Inside Container
yarn migrate
# Create a store
curl -X POST -H "Content-Type: application/json" -d '{"name":"my shop name", "genre":"Fast Food", "address":"Tokyo, Japan"}' http://localhost:13000/stores
# Updtate a store
curl -X PUT -H "Content-Type: application/json" -d '{"name":"my new shop name", "genre":" NEW Fast Food", "address":"Kyoto, Japan"}' http://localhost:13000/stores/1
# Delete a store
curl -X DELETE -H "Content-Type: application/json" http://localhost:13000/stores/2
# Get all store with child reviews
curl -X GET http://localhost:13000/stores
# Get a store
curl -X GET http://localhost:13000/stores/1
# Get a store with MySQL LIKE operator query
curl -X GET "http://localhost:13000/stores/find/likeName?name=new"
# Get a store with MySQL FullText Search (ngram)
curl "http://localhost:13000/stores/find/fullText?name=new&address=Kyoto&genre=Fast"
# Create one review and calculate and set store's avarage score
curl -X POST -H "Content-Type: application/json" -d '{"storeId":1, "score": 5, "title":"Good!", "content": "Yummy!"}' http://localhost:13000/reviews