Skip to content

Commit

Permalink
S01E12
Browse files Browse the repository at this point in the history
  • Loading branch information
ansipunk committed Mar 4, 2024
1 parent 09c1b2b commit f39f5eb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
23 changes: 20 additions & 3 deletions databases/backends/psycopg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import typing

import orjson
import psycopg
import psycopg.adapt
import psycopg.types
Expand All @@ -20,15 +19,33 @@
TransactionBackend,
)

try:
import orjson

def load(data):
return orjson.loads(data)

def dump(data):
return orjson.dumps(data)

except ImportError:
import json

def load(data):
return json.loads(data.decode("utf-8"))

def dump(data):
return json.dumps(data).encode("utf-8")


class JsonLoader(psycopg.adapt.Loader):
def load(self, data):
return orjson.loads(data)
return load(data)


class JsonDumper(psycopg.adapt.Dumper):
def dump(self, data):
return orjson.dumps(data)
return dump(data)


class PsycopgBackend(DatabaseBackend):
Expand Down
2 changes: 1 addition & 1 deletion databases/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Database:
"postgresql+asyncpg": "databases.backends.asyncpg:AsyncpgBackend",
"postgresql+psycopg": "databases.backends.psycopg:PsycopgBackend",
"mysql": "databases.backends.mysql:MySQLBackend",
"mysql+aiomysql": "databases.backends.asyncmy:MySQLBackend",
"mysql+aiomysql": "databases.backends.mysql:MySQLBackend",
"mysql+asyncmy": "databases.backends.asyncmy:AsyncMyBackend",
"sqlite": "databases.backends.sqlite:SQLiteBackend",
"sqlite+aiosqlite": "databases.backends.sqlite:SQLiteBackend",
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
-e .

# Speedups
orjson==3.9.15

# Async database drivers
asyncmy==0.2.9
aiomysql==0.2.0
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def get_packages(package):
"psycopg3": ["psycopg", "psycopg-pool"],
"sqlite": ["aiosqlite"],
"aiosqlite": ["aiosqlite"],
"orjson": ["orjson"],
},
classifiers=[
"Development Status :: 3 - Alpha",
Expand Down

0 comments on commit f39f5eb

Please sign in to comment.