Package: db-hooks
db_hooksExtremely beta sqlalchemy and toml based sql connection manager.config filedb_hookslooks for a local config file at./.databases.tomlin your working directory and then looks for a global config file using theappdirslibrary.You can edit or create this file in your$EDITORby runningdb_hooks edit. For me, this is vim. If no config exists, it will create one at the default location. In general, that location is one of the following:oslocationlinux~/.local/share/db_hooks/databases.tomlosx~/Library/Application Support/db_hooks/databases.tomlwindows 10~\AppData\local\jfhbrook\db_hooksThe format of this file is currently poorly-documented. The best place to get an idea of the full api isthe source code. Defaults for most things are sensible.connectionsKeys in the TOML file under the connections namespace are connection names.Connections names are nested under theconnectionsnamespace. Under those are key/value pairs for the various parameters in a connection. Connections support the following parameters:protocol- a supported protocol. Currently:postgres,mysqlandsqlite.username- the username to log into the database with.has_password- set this flag tofalsein order to disable automatic password prompting. Defaults totruefor all protocols where passwords are supported.host- the database host.port- the database port.database- the database name.password_cmd- a connection-specific override for thepassword_cmd. By default, a connection will use the globally-setpassword_cmd.For a very simple example, a local postgres connection might looks like this:[connections.pg_example]protocol="postgres"username="josh"host="localhost"port="5432"database="josh"passwordsdb_hooksincludes general purpose hooks for fetching passwords for connections, designed to be adaptable to multiple systems.The top-level config supports two relevant parameters:password_cmd- a shell snippet that will be ran in order to fetch the password. The command should print a string password to stdout.password_loader- configure the shell loader/parser used for running the command. defaults toshlex, but can be set tobashorpowershellin order to take advantage of a full shell language.These two parameters, plus thepassword_cmdoptional override, can be used to embed a small snippet that fetches the password.This is designed to allow the most flexibility and therefore will depend on your needs.For example, you may want to read passwords usingzenity:password_cmd="zenity --password"In Windows, you may want to use PowerShell to accomplish something similar:password_loader="powershell"password_cmd="(Get-Credential -Credential {name}).GetNetworkCredential().Password"cachingConnections in code are cached bydb_hooksusingcachetools. This means that if you callget_enginewith the same connection name twice that it will in most cases reuse an already-initialized engine, meaning that you will only be prompted for a password once.Caching supports four cache types:LRUCache,RRCache,TTLCacheandLFUCache. Each of these takes a collection of keyword args, which are passed to the corresponding contructors directly.The default isLRUCachewith amaxsizeof 512. This means that all connection info is cached indefinitely until over 512 distinct engines have been created, at which the least recently used engine is evicted.This API is relatively unstable and may change as thedb_hookssystem is implemented for other languages.in your python codeOnce you have a connection configured, you can load a sqlalchemy engine using
theget_engineAPI:fromdb_hooksimportget_engineengine=get_engine('pg_example')This will pull the connection, prompt for your password, and give you a configured sqlalchemy engine.the clidb_hooksexposes a CLI that will launch eitherpsql,mysqlorsqlitein a manner appropriate to the underlying connection. You can connect to this database by runningdb_hooks connect {your_connection_name_here}.bash completedb_hookshas support for shell tab-completion. You can enable it by runningeval $(_DB_HOOKS_COMPLETE=source db_hooks)for bash (the default shell for most Linux distributions and older versions of OSX) oreval $(_DB_HOOKS_COMPLETE=source_zsh db_hooks)(the default shell in new versions of OSX). PowerShell is unfortunately not supported. For more information, you canread Click's docs for autocompltion.licensingThis library is licensed under the Apache Software License. See the LICENSE and NOTICE files for details.