sa-metameta
Meta information of SQLAlchemy metadata by engine
Purpose
This module is designed to have a top-level class instance to which SQLAlchemy Engine instances can be registered as a MetaEngine. Each MetaEngine can then be probed directly to retrieve all tables by user schema. Thus an application can reference multiple databases and use the reflected Table instances to create queries for each.
This module has classes that will probe a RDBMS that supports the information_schema.schema view to get table information for each schema in a database. Currently, only PostgreSQL is fully supported.
This module will support psycopg2 and asyncpg.
This module requires SQLAlchemy >= v1.4 for database probing and Table class creation.
This module currently supports only table object creation.
Overview
After instantiating the MetaMeta class, an engine can be registered. Once that is done, the database can be probed for tables. Once that has completed successfully, the tables can be referenced starting from the MetaMeta instance to the engine, to the schema, and then to the table.
Example:
import sqlalchemy as sa
from sa_metameta import meta
engine = sa.create_engine("postgresql://postgres:pg_passwd@localhost:5432/my_database")
mm = meta.MetaMeta()
# This will use the database name from the URL as the attribute name
mm.register_engine(engine)
# This will probe all schemata in my_database and for each schema, the tables will be reflected.
mm.my_database.discover()
# now we can see what tables have been found by using list()
list(mm.my_database.public)
[
"table1",
"table2",
...
]
The engine, schema, and table can be referenced by dot or subscript notation:
engine = mm["my_database"]
schema = mm["my_database"]["public"]
table1 = mm["my_database"]["public"]["table1"]
# or
engine = mm.my_database
schema = mm.my_database.public
table1 = mm.my_database.public.table1
To reference columns, use the Table().c.column syntax.
query = sa.select(table1.c.label).filter(table1.c.quatloos > 200)
db = engine.session()
res = db.execute(query).all()
The engine can be exported as yaml or ddl. This will write a .yaml or a .sql file depending on the method called.
mm.my_database.as_yaml()
# "my_database.yaml"
mm.my_database.as_ddl()
# "my_database.sql"
Release files for sa-metameta 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sa-metameta-0.0.3.tar.gz | 13.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sa_metameta-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.2 kB
Release files / sa-metameta-0.0.3.tar.gz
| Download URL | sa-metameta-0.0.3.tar.gz |
|---|---|
| Size | 13.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
26d21bb71863c36b9d2ea0e14527d28c582c36fe6b9d9c83e6ec9aaca6229329
|
|
BLAKE2b-256 checksum How to use checksums |
04727b4fe497bf9c18a2b00dfbc638a672869965e8c716346b6da88f8f1a88e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.8
|
Release files / sa_metameta-0.0.3-py3-none-any.whl
| Download URL | sa_metameta-0.0.3-py3-none-any.whl |
|---|---|
| Size | 14.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0c3b9dee8adc509c32e72dd6c61616c9cccc5b2a68a4a5451fcca788396df894
|
|
BLAKE2b-256 checksum How to use checksums |
758f961e127ccfaccfed6358c0f264bfd8e85c0bd31d9e956b885a87625c4ac6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.8
|