Looking into the Database

The API only shows game states and not usages or user stats. This is because we have no user story that needs to access those three resources. However, as developers, we might want to see into those tables. How can we do it? We might want to create separate routes that access all the tables. This may be useful at some point, but the purpose of the API is to serve the client. Until the client needs the information, it’s probably better to access it differently. Another option is to access the data using the database’s command-line interface (CLI). This requires learning how to use the sqlite3 and psql command-line interfaces. A third option, and the one we describe next, is to use the Flask Shell and push a request context.

Looking into the database using the Flask Shell

We’ll start the shell the same way we start the application, but with flask shell instead of flask app. After navigating to the directory that contains the langman directory, starting the shell looks like this:

PS C:> $env:FLASK_APP = 'server/app'
PS C:> $env:FLASK_ENV = 'dev_lite'
PS C:> pipenv run flask shell
Python 3.8.1 (tags/...) [MSC v.1916 64 bit (AMD64)] on win32
App: server.app [dev_lite]
Instance: C:\Users\socra\langman\instance
>>>

The following are commands for Linux:

$ export FLASK_APP=server/app
$ export FLASK_ENV=dev_lite
$ pipenv run flask shell

Get hands-on with 1200+ tech skills courses.