Skip to content

Commit e7358af

Browse files
author
ansipunk
committed
S01E12
1 parent 09c1b2b commit e7358af

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

databases/backends/psycopg.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import typing
22

3-
import orjson
43
import psycopg
54
import psycopg.adapt
65
import psycopg.types
@@ -20,15 +19,33 @@
2019
TransactionBackend,
2120
)
2221

22+
try:
23+
import orjson
24+
25+
def load(data):
26+
return orjson.loads(data)
27+
28+
def dump(data):
29+
return orjson.dumps(data)
30+
31+
except ImportError:
32+
import json
33+
34+
def load(data):
35+
return json.loads(data.decode("utf-8"))
36+
37+
def dump(data):
38+
return json.dumps(data).encode("utf-8")
39+
2340

2441
class JsonLoader(psycopg.adapt.Loader):
2542
def load(self, data):
26-
return orjson.loads(data)
43+
return load(data)
2744

2845

2946
class JsonDumper(psycopg.adapt.Dumper):
3047
def dump(self, data):
31-
return orjson.dumps(data)
48+
return dump(data)
3249

3350

3451
class PsycopgBackend(DatabaseBackend):

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
-e .
22

3+
# Speedups
4+
orjson==3.9.15
5+
36
# Async database drivers
47
asyncmy==0.2.9
58
aiomysql==0.2.0

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def get_packages(package):
5858
"psycopg3": ["psycopg", "psycopg-pool"],
5959
"sqlite": ["aiosqlite"],
6060
"aiosqlite": ["aiosqlite"],
61+
"orjson": ["orjson"],
6162
},
6263
classifiers=[
6364
"Development Status :: 3 - Alpha",

0 commit comments

Comments
 (0)