Skip to content

Commit 5622304

Browse files
committed
improving engine docs
1 parent aacd02d commit 5622304

File tree

5 files changed

+152
-109
lines changed

5 files changed

+152
-109
lines changed

docs/src/piccolo/engines/index.rst

Lines changed: 9 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ It's important that each ``Table`` class knows which engine to use. There are
1010
two ways of doing this - setting it explicitly via the ``db`` argument, or
1111
letting Piccolo find it using ``engine_finder``.
1212

13+
-------------------------------------------------------------------------------
14+
1315
Explicit
1416
--------
1517

@@ -30,6 +32,7 @@ connect to a database.
3032
class MyTable(Table, db=DB):
3133
name = Varchar()
3234
35+
-------------------------------------------------------------------------------
3336

3437
engine_finder
3538
-------------
@@ -94,6 +97,8 @@ variable accordingly.
9497
9598
.. hint:: You can also specify sub modules, like `my_module.piccolo_conf`.
9699

100+
-------------------------------------------------------------------------------
101+
97102
.. _EngineTypes:
98103

99104
Engine types
@@ -103,83 +108,8 @@ Engine types
103108
production. It is the most feature complete.
104109

105110

106-
SQLiteEngine
107-
~~~~~~~~~~~~
108-
109-
.. code-block:: python
110-
111-
# piccolo_conf.py
112-
from piccolo.engine.sqlite import SQLiteEngine
113-
114-
115-
DB = SQLiteEngine(path='my_app.sqlite')
116-
117-
118-
PostgresEngine
119-
~~~~~~~~~~~~~~
120-
121-
.. code-block:: python
122-
123-
# piccolo_conf.py
124-
from piccolo.engine.postgres import PostgresEngine
125-
126-
127-
DB = PostgresEngine({
128-
'host': 'localhost',
129-
'database': 'my_app',
130-
'user': 'postgres',
131-
'password': ''
132-
})
133-
134-
Connection pool
135-
---------------
136-
137-
.. warning:: This is currently only available for Postgres.
138-
139-
140-
To use a connection pool, you need to first initialise it. The best place to do
141-
this is in the startup event handler of whichever web framework you are using.
142-
143-
Here's an example using Starlette. Notice that we also close the connection
144-
pool in the shutdown event handler.
145-
146-
.. code-block:: python
147-
148-
from piccolo.engine import from starlette.applications import Starlette
149-
from starlette.applications import Starlette
150-
151-
152-
app = Starlette()
153-
154-
155-
@app.on_event('startup')
156-
async def open_database_connection_pool():
157-
engine = engine_finder()
158-
await engine.start_connnection_pool()
159-
160-
161-
@app.on_event('shutdown')
162-
async def close_database_connection_pool():
163-
engine = engine_finder()
164-
await engine.close_connnection_pool()
165-
166-
.. hint:: Using a connection pool helps with performance, since connections
167-
are reused instead of being created for each query.
168-
169-
Once a connection pool has been started, the engine will use it for making
170-
queries.
171-
172-
.. hint:: If you're running several instances of an app on the same server,
173-
you may prefer an external connection pooler - like pgbouncer.
174-
175-
Configuration
176-
~~~~~~~~~~~~~
177-
178-
The connection pool uses the same configuration as your engine. You can also
179-
pass in additional parameters, which are passed to the underlying database
180-
adapter. Here's an example:
181-
182-
.. code-block:: python
111+
.. toctree::
112+
:maxdepth: 1
183113

184-
# To increase the number of connections available:
185-
await engine.start_connnection_pool(max_size=20)
114+
./sqlite_engine
115+
./postgres_engine
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
PostgresEngine
2+
==============
3+
4+
Configuration
5+
-------------
6+
7+
.. code-block:: python
8+
9+
# piccolo_conf.py
10+
from piccolo.engine.postgres import PostgresEngine
11+
12+
13+
DB = PostgresEngine(config={
14+
'host': 'localhost',
15+
'database': 'my_app',
16+
'user': 'postgres',
17+
'password': ''
18+
})
19+
20+
config
21+
~~~~~~
22+
23+
The config dictionary is passed directly to the underlying database adapter,
24+
asyncpg. See the `asyncpg docs <https://magicstack.github.io/asyncpg/current/api/index.html#connection>`_
25+
to learn more.
26+
27+
-------------------------------------------------------------------------------
28+
29+
Connection pool
30+
---------------
31+
32+
To use a connection pool, you need to first initialise it. The best place to do
33+
this is in the startup event handler of whichever web framework you are using.
34+
35+
Here's an example using Starlette. Notice that we also close the connection
36+
pool in the shutdown event handler.
37+
38+
.. code-block:: python
39+
40+
from piccolo.engine import from starlette.applications import Starlette
41+
from starlette.applications import Starlette
42+
43+
44+
app = Starlette()
45+
46+
47+
@app.on_event('startup')
48+
async def open_database_connection_pool():
49+
engine = engine_finder()
50+
await engine.start_connnection_pool()
51+
52+
53+
@app.on_event('shutdown')
54+
async def close_database_connection_pool():
55+
engine = engine_finder()
56+
await engine.close_connnection_pool()
57+
58+
.. hint:: Using a connection pool helps with performance, since connections
59+
are reused instead of being created for each query.
60+
61+
Once a connection pool has been started, the engine will use it for making
62+
queries.
63+
64+
.. hint:: If you're running several instances of an app on the same server,
65+
you may prefer an external connection pooler - like pgbouncer.
66+
67+
Configuration
68+
~~~~~~~~~~~~~
69+
70+
The connection pool uses the same configuration as your engine. You can also
71+
pass in additional parameters, which are passed to the underlying database
72+
adapter. Here's an example:
73+
74+
.. code-block:: python
75+
76+
# To increase the number of connections available:
77+
await engine.start_connnection_pool(max_size=20)
78+
79+
-------------------------------------------------------------------------------
80+
81+
Source
82+
------
83+
84+
.. currentmodule:: piccolo.engine.postgres
85+
86+
.. autoclass:: PostgresEngine
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
SQLiteEngine
2+
============
3+
4+
Configuration
5+
-------------
6+
7+
The ``SQLiteEngine`` is very simple - just specify a file path. The database
8+
file will be created automatically if it doesn't exist.
9+
10+
.. code-block:: python
11+
12+
# piccolo_conf.py
13+
from piccolo.engine.sqlite import SQLiteEngine
14+
15+
16+
DB = SQLiteEngine(path='my_app.sqlite')
17+
18+
-------------------------------------------------------------------------------
19+
20+
Source
21+
------
22+
23+
.. currentmodule:: piccolo.engine.sqlite
24+
25+
.. autoclass:: SQLiteEngine

piccolo/engine/postgres.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,32 @@ async def __aexit__(self, exception_type, exception, traceback):
193193

194194

195195
class PostgresEngine(Engine):
196+
"""
197+
Used to connect to Postgresql.
198+
199+
:param config:
200+
The config dictionary is passed to the underlying database adapter,
201+
asyncpg. Common arguments you're likely to need are:
202+
203+
* host
204+
* port
205+
* user
206+
* password
207+
* database
208+
209+
For example, ``{'host': 'localhost', 'port': 5432}``.
210+
211+
To see all available options:
212+
213+
* https://magicstack.github.io/asyncpg/current/api/index.html#connection
214+
215+
:param extensions:
216+
When the engine starts, it will try and create these extensions
217+
in Postgres.
218+
219+
""" # noqa: E501
196220

197-
__slots__ = ("config", "pool")
221+
__slots__ = ("config", "extensions", "pool", "transaction_connection")
198222

199223
engine_type = "postgres"
200224
min_version_number = 9.6
@@ -204,28 +228,6 @@ def __init__(
204228
config: t.Dict[str, t.Any],
205229
extensions: t.Sequence[str] = ["uuid-ossp"],
206230
) -> None:
207-
"""
208-
:param config:
209-
The config dictionary is passed to the underlying database adapter,
210-
asyncpg. Common arguments you're likely to need are:
211-
212-
* host
213-
* port
214-
* user
215-
* password
216-
* database
217-
218-
For example, ``{'host': 'localhost', 'port': 5432}``.
219-
220-
To see all available options:
221-
222-
* https://magicstack.github.io/asyncpg/current/api/index.html#connection
223-
224-
:param extensions:
225-
When the engine starts, it will try and create these extensions
226-
in Postgres.
227-
228-
""" # noqa: E501
229231
self.config = config
230232
self.extensions = extensions
231233
self.pool: t.Optional[Pool] = None

piccolo/engine/sqlite.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@ def dict_factory(cursor, row) -> t.Dict:
294294

295295

296296
class SQLiteEngine(Engine):
297+
"""
298+
Any connection kwargs are passed into the database adapter.
299+
300+
See here for more info:
301+
https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
302+
303+
"""
297304

298305
__slots__ = ("connection_kwargs",)
299306

@@ -307,13 +314,6 @@ def __init__(
307314
isolation_level=None,
308315
**connection_kwargs,
309316
) -> None:
310-
"""
311-
Any connection kwargs are passed into the database adapter.
312-
313-
See here for more info:
314-
https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
315-
316-
"""
317317
connection_kwargs.update(
318318
{
319319
"database": path,

0 commit comments

Comments
 (0)