Giter Club home page Giter Club logo

easyasyncpg's Introduction

EasyAsyncPg

Wrapper for asyncpg that makes it easier to use and adds new functionalities

Introduction

EasyAsyncPg is a Python library that provides a convenient, high-level API to work with PostgreSQL databases using asyncpg. The library allows you to manage multiple database connections, transactional operations, and parameterized queries in an easy-to-use manner.

Features

  • Primary and Secondary Connections: Distribute database reads and writes efficiently.
  • Transaction Management: Easily begin, commit, or rollback transactions.
  • Safe Mode: Optionally enforce read-write separation.
  • Connection Weights: Specify the weight for secondary connections for custom load balancing.

Installation

To install EasyAsyncPg, you can use pip:

pip install easyasyncpg

or, if you use poetry:

poetry add easyasyncpg

Basic Usage

Initialization

from easyasyncpg import EasyAsyncPg

db = EasyAsyncPg(safe_mode=True)

Adding Connections

await db.add_connection(
    host="localhost",
    user="postgres",
    password="postgres",
    database="mydb",
    port=5432,
    role=Role.PRIMARY,
    weight=1,
)

Executing Queries

await db.execute("INSERT INTO users (name, age) VALUES (:name, :age)", {"name": "Alice", "age": 30})

Transaction Management

async def my_transaction(db):
    await db.execute("UPDATE users SET age=31 WHERE name='Alice'")
    
await db.execute_transaction(my_transaction)

API Reference

EasyAsyncPg

Methods

  • add_connection(...): Add a new database connection.
  • get_connections(): Get all active connections.
  • begin_transaction(): Begin a new transaction.
  • commit_transaction(): Commit the current transaction.
  • rollback_transaction(): Rollback the current transaction.
  • execute(...): Execute a SQL query.
  • execute_many(...): Execute a SQL query multiple times.
  • fetch_all(...): Fetch all rows from a SQL query.
  • fetch_one(...): Fetch one row from a SQL query.
  • fetch_val(...): Fetch a single value from a SQL query.
  • fetch_column(...): Fetch a single column from a SQL query.
  • fetch_key_pairs(...): Fetch key-value pairs from a SQL query.

Contributing

Feel free to open an issue or submit a pull request if you find any bugs or have suggestions for additional features.

License

MIT License. See LICENSE for more information.

easyasyncpg's People

Contributors

camcima avatar

Stargazers

Fernando Macedo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.