About data management of anvil-app-server
Preface h2>
It's nice to be able to run the app on ʻanvil-app-server`, but let's run the `News Aggregater` app created on the [Anvil](https://anvil.works/) site. Even so, it doesn't seem to move smoothly. .. ..
I did some research.
Premise h4>
--You have already created the [News Aggregator](https://anvil.works/learn/tutorials/database-backed-apps) app in Anvil.
Environment h4>
--Ubuntu 20.04 LTS Japanese Remix
--Hyper-V Manager 10.0.18362.1
- anvil-app-server v1.1
Download and run the News Aggregator app h2>
Once you've created your app, you can download it from `View history` with the` git clone` command.
Click "View history"
![anvil_newsaggrigator_download_1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/667657/5609ecee-dab2-7876-bc56-69b93deb96a8.png)
Click "Clone with Git"
![anvil_newsaggrigator_download_2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/667657/49cd9a10-8602-8117-3892-7f6d2571985c.png)
Copy the line "git clone ssh: // ~" and execute it with the command.
![anvil_newsaggrigator_download_3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/667657/91f53d9f-74e9-23f4-6b87-1bc71e50000b.png)
git clone ssh://[email protected]:2222/324IEWAMVBYU2Y77.git News_Aggregater
XXXX
is the email address of the account registered on the Anvil site.
- If you have enabled two-factor authentication, you will have to use the SSH public key, so if you are using it, temporarily remove the two-step verification. I think it will be easier if you register a public key in Git.
Launch the application with ʻanvil-app-server`.
anvil-app-server --app News_Aggregater
Access http: // localhost: 3030
with a browser.
![anvil_app_server_url.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/667657/48bb2fdc-9270-159d-62bc-8bb7ac3a13df.png)
Suddenly an error.
![anvil_app_server_NewsAggrigator_error.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/667657/22dc1b5f-c26c-9e74-927c-802c3361b494.png)
Problems h2>
In the News Aggrigator app, the article `category` is registered in advance in the` categories` table and selected from there, but since the app does not contain table data, "empty data" Will result in an error.
Access PostgreSQL of anvil-app-server with pgadmin4 h2>
ʻAnvil-app-server` contains its own PostgreSQL server, so access it.
- Username : postgres
--Password: (directory where anvil-app-server was executed) /.anvi-data/postgres.password
--Port: (directory where anvil-app-server was executed) /.anvi-data/db/postmaster.opts
However, apparently, there is neither a ʻarticles table nor a
categories` table that the app should need.
![anvil_db_tables.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/667657/04e5cfb0-0a04-c6ae-5c86-22597adf8e84.png)
For the time being, if you look at the contents of the existing table, you will find the information of the table created by the application in the ʻapp_storage_tables` table.
![anvil_db_app_storage_tables.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/667657/f80f0436-9f55-6d23-db7f-49ecb8da4e0f.png)
Also, looking at creating data using the todo app that can be created with the create-anvil-app
command, it turns out that data has been added to the ʻapp_storage_data` table.
![anvil_db_app_storage_tables.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/667657/3b850d90-7258-a2cb-2e08-7fc9f16b9dc4.png)
What does that mean? H2>
From the above,
--Table information used by the app is stored in JSON format in the ʻapp_storage_tables table. --When accessing data from the app, access the JSON format data in the ʻapp_storage_data
table based on the information in the ʻapp_storage_tables` table.
From the point of view that the data layer and the logic layer can be matched no matter what app you make, it is not unthinkable to use a meta table, but it is a mechanism that frustrates the hearts of amateurs.
Others h2>
When I was able to see what kind of data management it was, I went back to Anvil's site and downloaded the data in order to import the data.
However, the downloaded data cannot be easily imported.
In the case of the `categories` table of News Aggrigator this time,
ID |
name |
[65114][57547127] |
entertainment |
[65114][57547131] |
business |
[65114][57547132] |
travel |
[65114][57547134] |
sport |
You have to write the JSON format data in a form that is consistent with the meta table of the local DB.
- The ID column is "probably" the information when the data is on the server hosted by Anvil, so it doesn't seem to make much sense in the local DB. Intuitively, I think it's about table ID and row ID.