Compare commits
1
Commits
v1.1.0
..
49e8de3b1a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
49e8de3b1a |
@@ -10,18 +10,18 @@ a sibling of the `mongo` lib. Class is **`PsqlDB`**.
|
||||
`requirements.txt`:
|
||||
|
||||
```
|
||||
psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v1.0.0
|
||||
psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.7
|
||||
```
|
||||
|
||||
Direct:
|
||||
|
||||
```bash
|
||||
pip install "psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v1.0.0"
|
||||
pip install "psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.7"
|
||||
```
|
||||
|
||||
Pulls `asyncpg`.
|
||||
|
||||
Drop the `@v1.0.0` suffix from the line above to install the latest unpinned.
|
||||
Drop the `@v0.1.7` suffix from the line above to install the latest unpinned.
|
||||
|
||||
## The two-layer API
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "psql"
|
||||
version = "1.1.0"
|
||||
version = "1.0.0"
|
||||
description = "async postgres wrapper over asyncpg: two-layer API (friendly verbs + raw escape hatch), fail-loud, config-free"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
|
||||
+17
-20
@@ -34,11 +34,8 @@ placeholders / injection safety:
|
||||
|
||||
errors (FAIL LOUD - unlike the mongo lib's swallow-and-default):
|
||||
every method catches the driver error (asyncpg.PostgresError / InterfaceError, OSError
|
||||
on connection loss) and re-raises it - the raised exception IS the signal, and the
|
||||
caller (which alone knows fatal-vs-routine) decides and logs. the wrapped method itself
|
||||
logs only at DEBUG (with the traceback) so a failure isn't reported twice (raise XOR
|
||||
log). a None/[] return is only ever a real result (no row, empty table) - never a
|
||||
swallowed failure. for anything
|
||||
on connection loss), logs via getLogger(__name__), and re-raises. a None/[] return is
|
||||
only ever a real result (no row, empty table) - never a swallowed failure. for anything
|
||||
not wrapped, use the raw `.pool` property (the asyncpg.Pool).
|
||||
"""
|
||||
|
||||
@@ -132,7 +129,7 @@ class PsqlDB:
|
||||
pool = await asyncpg.create_pool(**self._config)
|
||||
await pool.fetchval("SELECT 1")
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.connect() failed", exc_info=True)
|
||||
log.exception("psql.connect() failed")
|
||||
if pool is not None:
|
||||
await pool.close()
|
||||
raise
|
||||
@@ -161,7 +158,7 @@ class PsqlDB:
|
||||
try:
|
||||
await self._pool.close()
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.close()", exc_info=True)
|
||||
log.exception("psql.close()")
|
||||
raise
|
||||
finally:
|
||||
self._pool = None
|
||||
@@ -194,7 +191,7 @@ class PsqlDB:
|
||||
try:
|
||||
return await self.pool.execute(query, *params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.execute(): %s", query, exc_info=True)
|
||||
log.exception("psql.execute(): %s", query)
|
||||
raise
|
||||
|
||||
async def fetch(self, query: str, *params: Any) -> List[dict]:
|
||||
@@ -202,7 +199,7 @@ class PsqlDB:
|
||||
try:
|
||||
rows = await self.pool.fetch(query, *params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.fetch(): %s", query, exc_info=True)
|
||||
log.exception("psql.fetch(): %s", query)
|
||||
raise
|
||||
return [_row_to_dict(r) for r in rows]
|
||||
|
||||
@@ -214,7 +211,7 @@ class PsqlDB:
|
||||
try:
|
||||
row = await self.pool.fetchrow(query, *params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.fetchone(): %s", query, exc_info=True)
|
||||
log.exception("psql.fetchone(): %s", query)
|
||||
raise
|
||||
return _row_to_dict(row) if row is not None else None
|
||||
|
||||
@@ -223,7 +220,7 @@ class PsqlDB:
|
||||
try:
|
||||
return await self.pool.fetchval(query, *params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.fetchval(): %s", query, exc_info=True)
|
||||
log.exception("psql.fetchval(): %s", query)
|
||||
raise
|
||||
|
||||
def transaction(self):
|
||||
@@ -247,7 +244,7 @@ class PsqlDB:
|
||||
try:
|
||||
await self.pool.execute(f"CREATE DATABASE {_quote_ident(name)}")
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.create_database(%s)", name, exc_info=True)
|
||||
log.exception("psql.create_database(%s)", name)
|
||||
raise
|
||||
|
||||
async def create_table(self, name: str, schema: Dict[str, str]) -> None:
|
||||
@@ -262,7 +259,7 @@ class PsqlDB:
|
||||
try:
|
||||
await self.pool.execute(f"CREATE TABLE IF NOT EXISTS {_quote_ident(name)} ({cols})")
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.create_table(%s)", name, exc_info=True)
|
||||
log.exception("psql.create_table(%s)", name)
|
||||
raise
|
||||
|
||||
async def drop(self, name: str, *, table: bool = True) -> None:
|
||||
@@ -271,7 +268,7 @@ class PsqlDB:
|
||||
try:
|
||||
await self.pool.execute(f"DROP {kind} IF EXISTS {_quote_ident(name)}")
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.drop(%s)", name, exc_info=True)
|
||||
log.exception("psql.drop(%s)", name)
|
||||
raise
|
||||
|
||||
async def insert(self, table: str, values: Dict[str, Any]) -> int:
|
||||
@@ -287,7 +284,7 @@ class PsqlDB:
|
||||
try:
|
||||
status = await self.pool.execute(query, *values.values())
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.insert(%s)", table, exc_info=True)
|
||||
log.exception("psql.insert(%s)", table)
|
||||
raise
|
||||
return _status_count(status)
|
||||
|
||||
@@ -302,7 +299,7 @@ class PsqlDB:
|
||||
try:
|
||||
rows = await self.pool.fetch(query, *params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.get(%s)", table, exc_info=True)
|
||||
log.exception("psql.get(%s)", table)
|
||||
raise
|
||||
return [_row_to_dict(r) for r in rows]
|
||||
|
||||
@@ -313,7 +310,7 @@ class PsqlDB:
|
||||
try:
|
||||
row = await self.pool.fetchrow(query, *params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.get_one(%s)", table, exc_info=True)
|
||||
log.exception("psql.get_one(%s)", table)
|
||||
raise
|
||||
return _row_to_dict(row) if row is not None else None
|
||||
|
||||
@@ -327,7 +324,7 @@ class PsqlDB:
|
||||
try:
|
||||
status = await self.pool.execute(query, *params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.delete(%s)", table, exc_info=True)
|
||||
log.exception("psql.delete(%s)", table)
|
||||
raise
|
||||
return _status_count(status)
|
||||
|
||||
@@ -338,7 +335,7 @@ class PsqlDB:
|
||||
try:
|
||||
return bool(await self.pool.fetchval(query, *params))
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.exists(%s)", table, exc_info=True)
|
||||
log.exception("psql.exists(%s)", table)
|
||||
raise
|
||||
|
||||
async def upsert(self, table: str, values: Dict[str, Any], conflict: Sequence[str]) -> int:
|
||||
@@ -360,7 +357,7 @@ class PsqlDB:
|
||||
try:
|
||||
status = await self.pool.execute(query, *values.values())
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("psql.upsert(%s)", table, exc_info=True)
|
||||
log.exception("psql.upsert(%s)", table)
|
||||
raise
|
||||
return _status_count(status)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user