Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store data in database (TypeORM) #12

Merged
merged 23 commits into from Jun 20, 2020
Merged

Store data in database (TypeORM) #12

merged 23 commits into from Jun 20, 2020

Conversation

hesyifei
Copy link
Member

Previously all the data are stored as JSON.stringify string in Async Storage. This PR uses TypeORM with Expo SQLite to store pings data and answers data in the database. Each study will have its own SQLite file, and in the file, there are two tables: ping and answer. Refer to PingEntity and AnswerEntity for details.

The types Answers are replaced by the classes AnswerEntitys. The type PingInfo is replaced by PingEntity.

Async Storage no longer stores PingInfos, but is still used to store SurveyScreenStates. It means that AnswersList exists both in state (and hence the Async Storage that stores all SurveyScreenStates) and the database. This decision was based on the following reasons:

  • Many of the existing functions and components relies on synchronous loading of previous answers, whereas loading from database has to be asynchronous.
  • It makes reloading back to the current state after the user exits the app easier.
  • It serves as a backup to the database.

It means that we get all ping info from the database, get all answers info from the state during the survey, and get all answers info from the database during data upload. Notice that we .save() an entry in database first, then .reload() and save it to the state. This is so that our data in state can match data in database, hence making the code less prone to bugs.

For data upload, currently we upload add PingEntity[] and AnswerEntity[] as JSON (Notice this means that the data structure has changed - previous each answer is nested inside a ping, but now all answers are flattened to the same layer and identified by the composite key pingId and questionId). In the future, we might consider directly uploading SQL to the server.

This provide basic support for #10.

Note: at the time of merging, there is typeorm/typeorm#6142. It should fix with the next version of TypeORM.

@hesyifei hesyifei added the enhancement New feature or request label Jun 20, 2020
@hesyifei hesyifei self-assigned this Jun 20, 2020
@hesyifei hesyifei merged commit f372150 into master Jun 20, 2020
@hesyifei hesyifei deleted the database-with-state branch June 20, 2020 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant