Package: pg-alchemy-kit
Documentation forPGLibraryIntroductionThePGlibrary offers a suite of utilities to manage and interact with PostgreSQL databases using SQLAlchemy. It provides functionality such as initializing the database engine, creating tables, and managing sessions.Installationpip3 install pg-alchemy-kitor if you preferpoetry add pg-alchemy-kitUsage1. InitializationTo initialize the PostgreSQL utility class, use:frompg_alchemy_kitimportPGdb=PG()db.initialize(url="postgresql://username:password@localhost:5432/mydatabase")url(Optional): The connection string for the PostgreSQL database. If not provided, the default is derived fromget_engine_url(), which uses the following environment variables:PG_USER: The username for the database.PG_PASSWORD: The password for the database.PG_HOST: The host for the database.PG_PORT: The port for the database.PG_DB: The name of the database.2. Creating TablesTo create tables in your PostgreSQL database:fromyour_orm_moduleimportBaseModel1,BaseModel2# Create tables for the models in the provided listdb.create_tables([BaseModel1,BaseModel2])Bases: A list of SQLAlchemy base models.schemas: A list of schema names. Default is["public"].3. Managing SessionsContext ManagerUse theget_session_ctx()to manage your session using a context manager:withdb.get_session_ctx()assession:# Use session for database operations here...GeneratorYou can also useget_session()to get a session:session=next(db.get_session())4. Closing the ConnectionAfter all operations, ensure you close the database connection:db.close()LoggingThePGclass sets up a logger to capture messages. If you want to use your own logger, pass it during initialization:importlogginglogger=logging.getLogger('my_custom_logger')db.initialize(url="postgresql://username:password@localhost:5432/mydatabase",logger=logger)Documentation forPGUtilsClassIntroductionPGUtilsis a utility class that provides various database-related methods for performing CRUD operations, transforming SQL statements, and managing connections.InitializationBefore usingPGUtils, it should be initialized:fromyour_module_pathimportPGUtilslogger=logging.getLogger('my_custom_logger')db_utils=PGUtils(logger)logger: A logging instance to capture any log messages.Setting up a session:After initializing, you should set up a session for further operations:session=db.get_session_ctx()# Get this from the PG classdb_utils.initialize(session)Methods1. SQL ExecutionSelect QueryTo select records from the database:results=db_utils.select(session,"SELECT * FROM your_table WHERE condition=:condition",{'condition':value})Insert QueryTo insert records:status=db_utils.insert(session,"INSERT INTO your_table(column) VALUES (:value)",{'value':value})Delete QueryTo delete records:status=db_utils.delete(session,"DELETE FROM your_table WHERE condition=:condition",{'condition':value})Execute QueryTo execute any SQL:status=db_utils.execute(session,"YOUR SQL QUERY HERE")Update QueryTo update records:status=db_utils.update(session,ModelClass,{'key':key_value},{'column_to_update':new_value})2. ORM OperationsInsert ORM RecordTo insert a single ORM record:record=db_utils.insert_orm(session,ModelClass,{'column':value})Bulk Insert ORM RecordsTo insert multiple ORM records:ids,records=db_utils.bulk_insert_orm(session,ModelClass,[{'column1':value1},{'column2':value2}])Insert ORM on ConflictTo insert ORM records with conflict handling:db_utils.insert_orm_on_conflict(session,ModelClass,[{'column':value}])Delete ORM RecordsTo delete ORM records:status=db_utils.delete_orm(session,ModelClass,[uuid1,uuid2])Get UUIDTo get the UUID of a record:record_uuid=db_utils.get_uuid(session,ModelClass,{'column':value})3. UtilitiesWrap SQL to JSONTo wrap a SQL statement such that its result is returned as a JSON array:json_sql=PGUtils.wrap_to_json("YOUR SQL QUERY HERE")4. Engine ManagementGet EngineTo get an engine:engine=get_engine("postgresql://username:password@localhost:5432/mydatabase")Get Engine URLTo get an engine URL:url=get_engine_url(connection_type="postgresql",pg_username="username",pg_password="password",pg_host="localhost",pg_port="5432",pg_db="mydatabase")ConclusionThe combination ofPGandPGUtilsoffers a robust solution for database operations with PostgreSQL using SQLAlchemy. Whether you're using raw SQL or ORM models, these classes simplify your database tasks. Always ensure to handle exceptions and roll back sessions where necessary to maintain data integrity.