Skip to main content

Dynamic SQLAlchemy Metadata Reflection of One or More SQLALchemy Engines

Project description

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sa-metameta-0.0.1.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

sa_metameta-0.0.1-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file sa-metameta-0.0.1.tar.gz.

File metadata

  • Download URL: sa-metameta-0.0.1.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for sa-metameta-0.0.1.tar.gz
Algorithm Hash digest
SHA256 5a19140af4514a5bbfdc8cefef88763d59941506245e0cd8d126474087331c91
MD5 1659bdafa1480c4d58e766a7b5a53dfa
BLAKE2b-256 7e2fe5a763a3df301f23757626b615630246a3bd52aa711add133d9cffcf2a75

See more details on using hashes here.

File details

Details for the file sa_metameta-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: sa_metameta-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for sa_metameta-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 760a41b8f0741d5b3c334340f15483e95ff0a7500d62df132f8fcc9e30251916
MD5 598bedbddebe9fbc25656eb50973bb0b
BLAKE2b-256 6739eb667a778181c78a3f16c158ee361536b3bc83de3457dfa61a763830027b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page