engine_creator

Safe database credential loader.

class sqlalchemy_mate.engine_creator.EngineCreator(host=None, port=None, database=None, username=None, password=None)[source]

Tired of looking up docs on https://docs.sqlalchemy.org/en/latest/core/engines.html?

EngineCreator creates sqlalchemy engine in one line:

Example:

from sqlalchemy_mate import EngineCreator

# sqlite in memory
engine = EngineCreator.create_sqlite()

# connect to postgresql, credential stored at ``~/.db.json``
# content of ``.db.json``
{
    "mydb": {
        "host": "example.com",
        "port": 1234,
        "database": "test",
        "username": "admin",
        "password": "admin"
    },
    ...
}
engine = EngineCreator.from_home_db_json("mydb").create_postgresql()
property uri: str

Return sqlalchemy connect string URI.

classmethod from_json(json_file: str, json_path: Optional[str] = None, key_mapping: Optional[dict] = None) sqlalchemy_mate.engine_creator.EngineCreator[source]

Load connection credential from json file.

Parameters
  • json_file – str, path to json file

  • json_path – str, dot notation of the path to the credential dict.

  • key_mapping – dict, map ‘host’, ‘port’, ‘database’, ‘username’, ‘password’ to custom alias, for example {'host': 'h', 'port': 'p', 'database': 'db', 'username': 'user', 'password': 'pwd'}. This params are used to adapt any json data.

Return type

Returns

Example:

Your json file:

{
    "credentials": {
        "db1": {
            "h": "example.com",
            "p": 1234,
            "db": "test",
            "user": "admin",
            "pwd": "admin",
        },
        "db2": {
            ...
        }
    }
}

Usage:

cred = Credential.from_json(
    "path-to-json-file", "credentials.db1",
    dict(host="h", port="p", database="db", username="user", password="pwd")
)
classmethod from_home_db_json(identifier: str, key_mapping: Optional[dict] = None) sqlalchemy_mate.engine_creator.EngineCreator[source]

Read credential from $HOME/.db.json file.

Parameters
  • identifier (str) – str, database identifier.

  • key_mapping (Dict[str, str]) – dict

.db.json``:

{
    "identifier1": {
        "host": "example.com",
        "port": 1234,
        "database": "test",
        "username": "admin",
        "password": "admin",
    },
    "identifier2": {
        ...
    }
}
classmethod from_s3_json(bucket_name: str, key: str, json_path: Optional[str] = None, key_mapping: Optional[dict] = None, aws_profile: Optional[str] = None, aws_access_key_id: Optional[str] = None, aws_secret_access_key: Optional[str] = None, region_name: Optional[str] = None) sqlalchemy_mate.engine_creator.EngineCreator[source]

Load database credential from json on s3.

Parameters
  • bucket_name – str

  • key – str

  • aws_profile – if None, assume that you are using this from AWS cloud. (service on the same cloud doesn’t need profile name)

  • aws_access_key_id – str, not recommend to use

  • aws_secret_access_key – str, not recommend to use

  • region_name – str

classmethod from_env(prefix: str, kms_decrypt: bool = False, aws_profile: Optional[str] = None) sqlalchemy_mate.engine_creator.EngineCreator[source]

Load database credential from env variable.

  • host: ENV.{PREFIX}_HOST

  • port: ENV.{PREFIX}_PORT

  • database: ENV.{PREFIX}_DATABASE

  • username: ENV.{PREFIX}_USERNAME

  • password: ENV.{PREFIX}_PASSWORD

Parameters
  • prefix – str

  • kms_decrypt – bool

  • aws_profile – str

to_dict()[source]

Convert credentials into a dict.

create_engine(conn_str, **kwargs) sqlalchemy.engine.base.Engine[source]
Return type

Engine

classmethod create_sqlite(path=':memory:', **kwargs)[source]

Create sqlite engine.

class DialectAndDriver[source]

DB dialect and DB driver mapping.

create_postgresql(**kwargs)[source]
Return type

Engine

create_postgresql_psycopg2(**kwargs)[source]
Return type

Engine

create_postgresql_pg8000(**kwargs)[source]
Return type

Engine

create_postgresql_psycopg2cffi(**kwargs)[source]
Return type

Engine

create_postgresql_pypostgresql(**kwargs)[source]
Return type

Engine

create_mysql(**kwargs)[source]
Return type

Engine

create_mysql_mysqldb(**kwargs)[source]
Return type

Engine

create_mysql_mysqlconnector(**kwargs)[source]
Return type

Engine

create_mysql_oursql(**kwargs)[source]
Return type

Engine

create_mysql_pymysql(**kwargs)[source]
Return type

Engine

create_mysql_cymysql(**kwargs)[source]
Return type

Engine

create_oracle(**kwargs)[source]
Return type

Engine

create_oracle_cx_oracle(**kwargs)[source]
Return type

Engine

create_mssql_pyodbc(**kwargs)[source]
Return type

Engine

create_mssql_pymssql(**kwargs)[source]
Return type

Engine

create_redshift(**kwargs)[source]
Return type

Engine