forked from 05bit/peewee-async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeewee_asyncext.py
63 lines (49 loc) · 2.31 KB
/
peewee_asyncext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
peewee-async
============
Asynchronous interface for `peewee`_ ORM powered by `asyncio`_:
https://github.com/05bit/peewee-async
.. _peewee: https://github.com/coleifer/peewee
.. _asyncio: https://docs.python.org/3/library/asyncio.html
Licensed under The MIT License (MIT)
Copyright (c) 2014, Alexey Kinev <[email protected]>
"""
from peewee_async import AsyncPostgresqlMixin, PooledAsyncConnection
import playhouse.postgres_ext as ext
class PostgresqlExtDatabase(AsyncPostgresqlMixin, ext.PostgresqlExtDatabase):
"""PosgreSQL database extended driver providing **single drop-in sync**
connection and **single async connection** interface.
See also:
https://peewee.readthedocs.org/en/latest/peewee/playhouse.html#PostgresqlExtDatabase
"""
def __init__(self, database, threadlocals=True, autocommit=True,
fields=None, ops=None, autorollback=True, **kwargs):
super().__init__(database, threadlocals=True, autocommit=autocommit,
fields=fields, ops=ops, autorollback=autorollback,
**kwargs)
async_kwargs = self.connect_kwargs.copy()
async_kwargs.update({
'enable_json': True,
'enable_hstore': self.register_hstore,
})
self.init_async(**async_kwargs)
class PooledPostgresqlExtDatabase(AsyncPostgresqlMixin, ext.PostgresqlExtDatabase):
"""PosgreSQL database extended driver providing **single drop-in sync**
connection and **async connections pool** interface.
:param max_connections: connections pool size
See also:
https://peewee.readthedocs.org/en/latest/peewee/playhouse.html#PostgresqlExtDatabase
"""
def __init__(self, database, threadlocals=True, autocommit=True,
fields=None, ops=None, autorollback=True, max_connections=20,
**kwargs):
super().__init__(database, threadlocals=True, autocommit=autocommit,
fields=fields, ops=ops, autorollback=autorollback,
**kwargs)
async_kwargs = self.connect_kwargs.copy()
async_kwargs.update({
'enable_json': True,
'enable_hstore': self.register_hstore,
})
self.init_async(conn_cls=PooledAsyncConnection, minsize=1,
maxsize=max_connections, **async_kwargs)