city_retrofit/persistence
2022-11-15 20:48:42 -05:00
..
models Added Hub GitLab commit to manager versions of the same city 2022-11-11 16:16:31 -05:00
repositories Included tests, import and export for db operations 2022-11-15 20:48:42 -05:00
__init__.py Included tests, import and export for db operations 2022-11-15 20:48:42 -05:00
base.py Included tests, import and export for db operations 2022-11-15 20:48:42 -05:00
db_config.py Included tests, import and export for db operations 2022-11-15 20:48:42 -05:00
README.md Included tests, import and export for db operations 2022-11-15 20:48:42 -05:00

Database Persistence

The persistence package includes classes to store different class objects in a Postgres database.

models

This defines models for all class objects that we want to persist. It is used for Object Relation Mapping (ORM) of the class objects to database table columns

repositories

This defines repository classes that contain CRUD methods for database operations. The constructor of all repositories requires The database name to connect to and the application environment (PROD or TEST). Tests use a different database from the production environment, which is why this is necessary. An example is shown below

from persistence import CityRepo
# instantiate city repo for hub production database
city_repo = CityRepo(db_name='hub', app_env='PROD')

All database operations are conducted with the production database (PROD) named hub in the example above

config_db

This Python file is a configuration class that contains variables that map to configuration parameters in a .env file. It also contains a method def conn_string() which returns the connection string to a Postgres database.

Base

This class has a constructor that establishes a database connection and returns a reference for database-related CRUD operations.

Database Configuration Parameter

A .env file (or environment variables) with configuration parameters described below are needed to establish a database connection:

# production database credentials
PROD_DB_USER=postgres-database-user
PROD_DB_PASSWORD=postgres-database-password
PROD_DB_HOST=database-host
PROD_DB_PORT=database-port

# test database credentials
TEST_DB_USER=postgres-database-user
TEST_DB_PASSWORD=postgres-database-password
TEST_DB_HOST=database-host
TEST_DB_PORT=database-port