Skip to content

Commit

Permalink
S01E12
Browse files Browse the repository at this point in the history
  • Loading branch information
ansipunk committed Mar 3, 2024
1 parent 09c1b2b commit e7358af
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 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
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 e7358af

Please sign in to comment.