This python module handles any type of databases connection(s) using a yaml configuration file.
It is a fork coming from fastapi-framework-mvc and flask-framework-mvc.
Created for better package management in between both projects.
Can be loaded standalone outside its original projects.
Note
If using side by side with either fastapi-framework-mvc or flask-framework-mvc, it will load Environment from one of this framework accordingly to the one you installed int your python env or python root libs.
...
DATABASES:
default: mysql
mysql:
driver: mysql+pymysql
user: "replace this with your database user"
password: "replace this with your database user's password"
database: "replace this with your database name"
address: "replace this with your hostname"
models: "mysql (python module that require to be put under models.persistent module, non blocking if module doesn't exist)"
readonly: false
......
DATABASES:
informix:
driver: informix
user: "replace this with your database user"
password: "replace this with your database user's password"
database: "replace this with your database name"
address: "replace this with your hostname"
models: "informix (python module that require to be put under models.persistent module, non blocking if module doesn't exist)"
params:
SERVER: "replace with your server name"
CLIENT_LOCALE: "replace with your client locale"
DB_LOCALE: "replace with your server locale"
dialects:
informix:
module: IfxAlchemy.IfxPy
class: IfxDialect_IfxPy
informix.IfxPy:
module: IfxAlchemy.IfxPy
class: IfxDialect_IfxPy
informix.pyodbc:
module: IfxAlchemy.pyodbc
class: IfxDialect_pyodbc
readonly: false
...or:
...
DATABASES:
bigquery:
driver: bigquery
user: "replace this with your database user"
password: "replace this with your database user's password"
database: "replace this with your database name"
address: "replace this with your hostname"
models: "bigquery (python module that require to be put under models.persistent module)"
readonly: false
engine:
location: US
...dialects, params and engine are non mandatory within the yaml config file.
dialects are for registering database dialects base what the python database lib provides.
params are for putting additional params within the database url link used when sqlalchemy create_engine function is called
engine is all other arguments that can be given to the sqlalchemy create_engine function
In order to load it you need to create a yaml configuration file with entries as described on sections bellow. This yaml configuration files can have environment variable within encapsuled in ${ENVIRONMENT_NAME} syntax.
import os
from database_connector_kit.config import Environment
Environment.load('config.yml')After loading the configuration file, you can create the database's connection(s) by:
from database_connector_kit import Driver
Driver.register_engines()
Driver.init()There is a safe decorator that enable some sqlalchemy errors to be dealt with and retry reprocessing the function that handles database operations. In order to use it, use de following example:
from database_connector_kit import safe
@safe
def func():
"""
Function that update, select, insert ot even delete contents from the database
"""
pass