tiny-pilot / tinypilot

Use your Raspberry Pi as a browser-based KVM.
https://tinypilotkvm.com
MIT License
2.95k stars 245 forks source link

Find way for accessing SQlite DB programmatically #1690

Open jotaen4tinypilot opened 7 months ago

jotaen4tinypilot commented 7 months ago

In https://github.com/tiny-pilot/tinypilot/issues/1524, we discussed that we should make it easier to use the SQlite / tinypilot.db file in programmatic contexts, e.g. in bash scripts.

Currently, you have to add a CLI endpoint to the Flask app (see, this example, where we read the streaming mode). This has some downsides:

Note that we can’t “naively” read from the tinypilot.db file as is, since the DB file might not be in the latest migration state. We probably don’t want to port our migration logic to a different language.

So we should explore options to access the tinypilot.db file in programmatic contexts. Requirements for the solution:

Potential solutions / approaches:

(Non-exhaustive, just as starting points)

jotaen4tinypilot commented 7 months ago

Re the idea of isolating the app.db module and providing our own, Flask-less CLI entry point:

Out of curiosity, I ran a quick-and-dirty POC on this branch. With this, you could do the following on the command line:

$ ./cli settings streaming-mode
MJPEG
$ ./cli settings requires-https
True
$ ./cli users names
['John Doe']

For that, we’d have to refactor the db_connection handling, and effectively make the app.db module to have no upwards dependencies, otherwise we’d unintendedly loop in Flask.

Execution time on device (including activating venv) is ~120ms, which seems reasonable (well, at least more reasonable than 1500ms).