Skip to content

Commit 4956def

Browse files
committed
Grammar and spelling improvements
"First of all, so sorry for my poor english. I will be so happy, if someone pushes a PR correcting all my english mistakes. Anyway I will try to do my best."
1 parent 8ab05b1 commit 4956def

File tree

3 files changed

+64
-64
lines changed

3 files changed

+64
-64
lines changed

README.md

+46-46
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,42 @@ FastAPI integration for AsyncPG
44

55
## Narrative
66

7-
First of all, so sorry for my poor english. I will be so happy,
8-
if someone pushes a PR correcting all my english mistakes. Anyway
7+
First of all, I am so sorry for my poor English. I will be so happy
8+
if someone pushes a PR correcting all my English mistakes. Anyway,
99
I will try to do my best.
1010

11-
Looking at fastapi ecosystem seems like everyone is trying to integrate
12-
fastapi with orms, but from my experience working with raw
11+
Looking at the fastapi ecosystem seems like everyone is trying to
12+
integrate fastapi with orms, but from my experience working with raw
1313
sql I'm so productive.
1414

15-
If you think a bit around, your real model layer, is the schema on your
16-
db (you can add abastractions on top of it), but what ends
17-
is your data, and these are tables, columns and rows.
15+
If you think a little, your real model layer is the schema of your
16+
db. You can add abstractions on top of it, but what matters in the
17+
end is your data. The tables, columns and rows.
1818

1919
Also, sql, it's one of the best things I learned
2020
because it's something that always is there.
2121

22-
On another side, postgresql it's robust and rock solid,
23-
thousands of projects depend on it, and use it as their storage layer.
24-
AsyncPG it's a crazy fast postgresql driver
25-
written from scratch.
22+
Another note, postgresql is robust and rock solid.
23+
Thousands of projects depend on it and use it as their storage layer.
24+
AsyncPG is a crazy fast postgresql driver written from scratch.
2625

2726
FastAPI seems like a clean, and developer productive approach to web
2827
frameworks. It's crazy how well it integrates with OpenAPI,
29-
and how easy makes things to a developer to move on.
28+
and how easy it makes things for a developer.
3029

3130
## Integration
3231

33-
fastapi_asyncpg trys to integrate fastapi and asyncpg in an idiomatic way.
34-
fastapi_asyncpg when configured exposes two injectable providers to
35-
fastapi path functions, can use:
32+
fastapi_asyncpg tries to integrate fastapi and asyncpg in an idiomatic way.
33+
fastapi_asyncpg when configured exposes two injectable providers that
34+
fastapi path functions can use:
3635

37-
- `db.connection` : it's just a raw connection picked from the pool,
38-
that it's auto released when pathfunction ends, this is mostly
39-
merit of the DI system around fastapi.
36+
- `db.connection`: a raw connection picked from the pool, that is
37+
auto-released when path function ends. This is mostly thanks to the
38+
DI system around fastapi.
4039

41-
- `db.transaction`: the same, but wraps the pathfuncion on a transaction
42-
this is more or less the same than the `atomic` decorator from Django.
43-
also `db.atomic` it's aliased
40+
- `db.transaction`: the same, but wraps the path funcion in a transaction
41+
this is more or less the same as the `atomic` decorator from Django.
42+
It is also aliased as `db.atomic`
4443

4544
```python
4645
from fastapi import FastAPI
@@ -69,35 +68,36 @@ async def mutate_something_compled(db=Depends(db.atomic))
6968
# if something fails, everyting is rolleback, you know all or nothing
7069
```
7170

72-
And there's also an `initialization` callable on the main factory function.
73-
That can be used like in flask to initialize whatever you need on the db.
74-
The `initialization` is called right after asyncpg stablishes a connection,
71+
There's also an `initialization` callable on the main factory function.
72+
This can be used as in flask to do whatever initializion you need.
73+
`initialization` is called right after asyncpg stablishes a connection,
7574
and before the app fully boots. (Some projects use this as a poor migration
76-
runner, not the best practice if you are deploying multiple
75+
runner, but this is not the best practice if you are deploying multiple
7776
instances of the app).
7877

7978
## Testing
8079

81-
For testing we use [pytest-docker-fixtures](https://pypi.org/project/pytest-docker-fixtures/), it requires docker on the host machine or on whatever CI you use
82-
(seems like works as expected with github actions)
80+
For testing we use [pytest-docker-fixtures](https://pypi.org/project/pytest-docker-fixtures/),
81+
it requires docker on the host machine or on whatever CI you use
82+
(it works as expected with github actions)
8383

84-
It works, creating a container for the session and exposing it as pytest fixture.
84+
It creates a container for the session and exposes it as pytest fixture.
8585
It's a good practice to run tests with a real database, and
86-
pytest-docker-fixtures make it's so easy. As a bonus, all fixtures run on a CI.
87-
We use Jenkins witht docker and docker, but also seems like travis and github actions
88-
also work.
86+
pytest-docker-fixtures makes it so easy. As a bonus, all fixtures run in CI.
87+
We use Jenkins with docker, and docker, but it seems that travis and github
88+
actions also work.
8989

9090
The fixture needs to be added to the pytest plugins `conftest.py` file.
9191

92-
on conftest.py
92+
in conftest.py
9393

9494
```python
9595
pytest_plugins = [
9696
"pytest_docker_fixtures",
9797
]
9898
```
9999

100-
With this in place, we can just yield a pg fixture
100+
With this in place, we can yield a pg fixture
101101

102102
```python
103103
from pytest_docker_fixtures import images
@@ -149,9 +149,9 @@ async def test_something(asgi_app):
149149
res = await client.request("/")
150150
```
151151

152-
Anyway if the application will grow, to multiples subpackages,
153-
and apps, we trend to build the main app as a factory, that
154-
creates it, something like:
152+
If the application grows to multiples subpackages and
153+
apps, we can build the main app as a factory. Something
154+
like:
155155

156156
```python
157157
from fastapi_asyncpg import configure_asyncpg
@@ -168,7 +168,7 @@ def make_asgi_app(settings):
168168
return app
169169
````
170170

171-
Then on the fixture, we just need, to factorze and app from our function
171+
Then in the fixture, we just need to factorize an app from our function
172172

173173
```python
174174

@@ -184,7 +184,7 @@ async def asgi_app(pg)
184184
host, port = pg
185185
dsn = f"postgresql://postgres@{host}:{port}/test_db"
186186
app = make_asgi_app({"dsn": dsn})
187-
# ther's a pointer on the pool into app.state
187+
# there's a pointer to the pool into app.state
188188
yield app
189189

190190
async def test_something(asgi_app):
@@ -193,7 +193,7 @@ async def test_something(asgi_app):
193193
async with db.pool.acquire() as db:
194194
# setup your test state
195195

196-
# this context manager handlers lifespan events
196+
# this context manager handles lifespan events
197197
async with TestClient(app) as client:
198198
res = await client.request("/")
199199

@@ -202,16 +202,16 @@ async def test_something(asgi_app):
202202
There's also another approach exposed and used on [tests](tests/test_db.py),
203203
that exposes a single connection to the test and rolls back changes on end.
204204
We use this approach on a large project (500 tables per schema and
205-
multiples schemas), and seems like it speeds up a bit test creation.
206-
This approach is what [Databases](https://www.encode.io/databases/) it's using.
207-
Feel free to follow the tests to see if it feets better.
205+
multiples schemas), and seems like it speeds up test creation a bit.
206+
This approach is what [Databases](https://www.encode.io/databases/) is using.
207+
Feel free to follow the tests to see if it feels better.
208208

209209
## Extras
210210

211-
There are some utility functions I daily use with asyncpg that helps me
212-
speed up some sql operations like, they are all on sql.py, and mostly are
213-
self documented. They are in use on tests.
211+
There are some utility functions I use daily with asyncpg that help me
212+
speed up some sql operations. They are all in sql.py, and are mostly
213+
self documented. They are in use in tests.
214214

215215
### Authors
216216

217-
`fastapi_asyncpg` was written by `Jordi collell <[email protected]>`\_.
217+
`fastapi_asyncpg` was written by `Jordi Collell <[email protected]>`\_.

fastapi_asyncpg/__init__.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ def __init__(
2323
"""This is the entry point to configure an asyncpg pool with fastapi.
2424
2525
Arguments
26-
app: The fastapp application that we use to store the pool
27-
and bind to it's initialitzation events
28-
dsn: A postgresql desn like postgresql://user:password@postgresql:5432/db
26+
app: The fastapi application that we use to store the pool
27+
and bind to its initialization events
28+
dsn: A postgresql dsn like postgresql://user:password@postgresql:5432/db
2929
init_db: Optional callable that receives a db connection,
30-
for doing an initialitzation of it
31-
pool: This is used for testing to skip the pool initialitzation
32-
an just use the SingleConnectionTestingPool
33-
**options: connection options to directly pass to asyncpg driver
30+
for initialitzation
31+
pool: This is used for testing to skip the pool intitialization
32+
and just use the SingleConnectionTestingPool
33+
**options: connection options to directly pass to the asyncpg driver
3434
see: https://magicstack.github.io/asyncpg/current/api/index.html#connection-pools
3535
"""
3636
self.app = app
@@ -42,7 +42,7 @@ def __init__(
4242
self.app.router.add_event_handler("shutdown", self.on_disconnect)
4343

4444
async def on_connect(self):
45-
"""handler called during initialitzation of asgi app, that connects to
45+
"""handler called during intitialization of asgi app, that connects to
4646
the db"""
4747
# if the pool is comming from outside (tests), don't connect it
4848
if self._pool:
@@ -70,8 +70,8 @@ def pool(self):
7070

7171
async def connection(self):
7272
"""
73-
A ready to use connection Dependency just usable
74-
on your path functions that gets a connection from the pool
73+
A ready-to-use connection Dependency usable in your path
74+
functions that gets a connection from the pool
7575
Example:
7676
db = configure_asyncpg(app, "dsn://")
7777
@app.get("/")
@@ -83,14 +83,14 @@ async def get_content(db = Depens(db.connection)):
8383

8484
async def transaction(self):
8585
"""
86-
A ready to use transaction Dependecy just usable on a path function
86+
A ready to use transaction Dependecy usable in a path function
8787
Example:
8888
db = configure_asyncpg(app, "dsn://")
8989
@app.get("/")
9090
async def get_content(db = Depens(db.transaction)):
9191
await db.execute("insert into keys values (1, 2)")
9292
await db.execute("insert into keys values (1, 2)")
93-
All view function executed, are wrapped inside a postgresql transaction
93+
All view functions executed are wrapped inside a postgresql transaction
9494
"""
9595
async with self.pool.acquire() as db:
9696
txn = db.transaction()
@@ -108,7 +108,7 @@ async def get_content(db = Depens(db.transaction)):
108108

109109
class SingleConnectionTestingPool:
110110
"""A fake pool that simulates pooling, but runs on
111-
a single transaction that it's rolled back after
111+
a single transaction. The transaction is rolled back after
112112
each test.
113113
With some large schemas this seems to be faster than
114114
the other approach
@@ -157,9 +157,9 @@ async def create_pool_test(
157157
initialize: typing.Callable = None,
158158
add_logger_postgres: bool = False,
159159
):
160-
"""This part is only used for testing,
161-
we create a fake "pool" that just starts a connecion,
162-
that does a transaction inside it"""
160+
"""This part is only used for testing.
161+
We create a fake "pool" that just starts a connection
162+
and does a transaction inside it"""
163163
conn = await asyncpg.connect(dsn=dsn)
164164
pool = SingleConnectionTestingPool(
165165
conn, initialize=initialize, add_logger_postgres=add_logger_postgres

fastapi_asyncpg/sql.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async def delete(db, table, condition, args=None):
5959
def query_to_json(query, name):
6060
"""This query is useful to fetch a complex join
6161
with some aggregations as a single blob, and later,
62-
just hydrate it without having to iterate over the resultset
62+
hydrate it without having to iterate over the resultset
6363
6464
.. Example:
6565
SELECT
@@ -74,7 +74,7 @@ def query_to_json(query, name):
7474
WHERE user_id = ANY($1)
7575
GROUP BY u.user_id;
7676
77-
This query will fetch a list of users, and aggregate it's
77+
This query will fetch a list of users, and aggregate its
7878
scopes as an array of dicts
7979
"""
8080

0 commit comments

Comments
 (0)