|
8 | 8 |
|
9 | 9 | # pgmq-sqlalchemy
|
10 | 10 |
|
11 |
| -Python client using **sqlalchemy ORM** for the PGMQ Postgres extension. |
| 11 | +More flexible [PGMQ Postgres extension](https://github.com/tembo-io/pgmq) Python client that using **sqlalchemy ORM**, supporting both **async** and **sync** `engines`, `sessionmakers` or built from `dsn`. |
| 12 | + |
| 13 | +## Table of Contents |
| 14 | + |
| 15 | +* [pgmq-sqlalchemy](#pgmq-sqlalchemy) |
| 16 | + * [Features](#features) |
| 17 | + * [Installation](#installation) |
| 18 | + * [Getting Started](#getting-started) |
| 19 | + * [Postgres Setup](#postgres-setup) |
| 20 | + * [Usage](#usage) |
| 21 | + * [Issue/ Contributing / Development](#issue-contributing--development) |
| 22 | + * [TODO](#todo) |
12 | 23 |
|
13 |
| -支援 **SQLAlchemy ORM** 的 Python 客戶端 <br> |
14 |
| -用於 [PGMQ Postgres 插件](https://github.com/tembo-io/pgmq) 。 |
15 | 24 |
|
16 | 25 | ## Features
|
17 | 26 |
|
18 |
| -- 支援 **async** 和 **sync** `engines`、`sessionmakers`,或由 `dsn` 構建。 |
19 |
| -- 支援所有 sqlalchemy 支持的 postgres DBAPIs。 |
20 |
| - > 例如:`psycopg`, `psycopg2`, `asyncpg` |
21 |
| - > 可見 [SQLAlchemy Postgresql Dialects](https://docs.sqlalhttps://docs.sqlalchemy.org/en/20/dialects/postgresql.html) |
| 27 | +- Supports **async** and **sync** `engines` and `sessionmakers`, or built from `dsn`. |
| 28 | +- **Automatically** creates `pgmq` (or `pg_partman`) extension on the database if not exists. |
| 29 | +- Supports **all postgres DBAPIs supported by sqlalchemy**. |
| 30 | + > e.g. `psycopg`, `psycopg2`, `asyncpg` .. <br> |
| 31 | + > See [SQLAlchemy Postgresql Dialects](https://docs.sqlalhttps://docs.sqlalchemy.org/en/20/dialects/postgresql.html) |
| 32 | +
|
| 33 | +## Installation |
| 34 | + |
| 35 | +Install with pip: |
| 36 | + |
| 37 | +```bash |
| 38 | +pip install pgmq-sqlalchemy |
| 39 | +``` |
| 40 | + |
| 41 | +Install with additional DBAPIs packages: |
| 42 | + |
| 43 | +```bash |
| 44 | +pip install pgmq-sqlalchemy[psycopg2] |
| 45 | +pip install pgmq-sqlalchemy[asyncpg] |
| 46 | +# pip install pgmq-sqlalchemy[postgres-python-driver] |
| 47 | +``` |
| 48 | + |
| 49 | +## Getting Started |
| 50 | + |
| 51 | +### Postgres Setup |
| 52 | + |
| 53 | +Prerequisites: **Postgres** with **PGMQ** extension installed. <br> |
| 54 | +For quick setup: |
| 55 | +```bash |
| 56 | +docker run -d --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 quay.io/tembo/pg16-pgmq:latest |
| 57 | +``` |
| 58 | +> For more information, see [PGMQ](https://github.com/tembo-io/pgmq) |
| 59 | +
|
| 60 | +### Usage |
| 61 | + |
| 62 | +> [!NOTE] |
| 63 | +> Check [pgmq-sqlalchemy Document](https://pgmq-sqlalchemy-python.readthedocs.io/en/latest/) for more examples and detailed usage. |
| 64 | +
|
| 65 | + |
| 66 | +For `dispatcher.py`: |
| 67 | +```python |
| 68 | +from typing import List |
| 69 | +from pgmq_sqlalchemy import PGMQueue |
| 70 | + |
| 71 | +postgres_dsn = 'postgresql://postgres:postgres@localhost:5432/postgres' |
| 72 | + |
| 73 | +pgmq = PGMQueue(dsn=postgres_dsn) |
| 74 | +pgmq.create_queue('my_queue') |
| 75 | + |
| 76 | +msg = {'key': 'value', 'key2': 'value2'} |
| 77 | +msg_id:int = pgmq.send('my_queue', msg) |
| 78 | + |
| 79 | +# could also send a list of messages |
| 80 | +msg_ids:List[int] = pgmq.send_batch('my_queue', [msg, msg]) |
| 81 | +``` |
| 82 | + |
| 83 | +For `consumer.py`: |
| 84 | +```python |
| 85 | +from pgmq_sqlalchemy import PGMQueue |
| 86 | +from pgmq_sqlalchemy.schema import Message |
| 87 | + |
| 88 | +postgres_dsn = 'postgresql://postgres:postgres@localhost:5432/postgres' |
| 89 | + |
| 90 | +pgmq = PGMQueue(dsn=postgres_dsn) |
| 91 | + |
| 92 | +# read a single message |
| 93 | +msg:Message = pgmq.read('my_queue') |
| 94 | + |
| 95 | +# read a batch of messages |
| 96 | +msgs:List[Message] = pgmq.read_batch('my_queue', 10) |
| 97 | +``` |
| 98 | + |
| 99 | +For `monitor.py`: |
| 100 | +```python |
| 101 | +from pgmq_sqlalchemy import PGMQueue |
| 102 | +from pgmq_sqlalchemy.schema import QueueMetrics |
| 103 | + |
| 104 | +postgres_dsn = 'postgresql://postgres:postgres@localhost:5432/postgres' |
| 105 | + |
| 106 | +pgmq = PGMQueue(dsn=postgres_dsn) |
| 107 | + |
| 108 | +# get queue metrics |
| 109 | +metrics:QueueMetrics = pgmq.metrics('my_queue') |
| 110 | +print(metrics.queue_length) |
| 111 | +print(metrics.total_messages) |
| 112 | +``` |
| 113 | + |
| 114 | +## Issue/ Contributing / Development |
| 115 | + |
| 116 | +Welcome to open an issue or pull request ! <br> |
| 117 | +See [`Development` on Online Document](https://pgmq-sqlalchemy-python.readthedocs.io/en/latest/) or [CONTRIBUTING.md](.github/CONTRIBUTING.md) for more information. |
22 | 118 |
|
| 119 | +## TODO |
23 | 120 |
|
| 121 | +- [ ] Add **time-based** partition option and validation to `create_partitioned_queue` method. |
| 122 | +- [ ] Read(single/batch) Archive Table ( `read_archive` method ) |
| 123 | +- [ ] Detach Archive Table ( `detach_archive` method ) |
| 124 | +- [ ] Add `set_vt` utils method. |
0 commit comments