In this session. we will implement a feature to log fire detection data into the database.
Since this functionality involves handling the database.
we will add it to the `crud` file within the `predict_fire` domain that we previously created.
The feature itself is simple—process the detected data and store it in the database.
The official documentation covers how to use SQLAlchemy.
But since the content is extensive and not always intuitive.
it is convenient to ask an LLM first when needed.
Here. `add()`. `commit()`. and `refresh()` serve the following functions.
`add()` adds an object to the session. The changes are only applied after calling `commit()`.
`commit()` saves the changes to the database.
`refresh()` fetches the latest data from the database and updates the object.
To modify the database structure. we will delete the existing database file and recreate it.
We considered adding a login feature.
But including it in this lecture would unnecessarily increase the complexity.
so we will not add it this time.
Therefore. we will remove the relationship between the user table and the detection log table.
We will change the result file key used for logging to the UUID-generated filename we created earlier.
By storing this key. we can later access the image files saved in the log folder.
We will call the `create_detection_log` function implemented in the `crud` file. To log data whenever a fire is detected.
We will update the file path.
And use FastAPI's `Depends` to inject a new DB session for each request.
Once the request is completed. the session will be automatically cleaned up.
We will run the server and check the database file using the SQLite extension.
Using Postman. we will send a request to the `predict_fire` endpoint
We can verify that the data has been successfully logged.