Skip to content

Commit 58a2321

Browse files
authored
Doc improvements (#1962)
* Remove CHANGELOG from search results * Simplify the section on why tortoise was built * Improve databases and router pages
1 parent 8ee43fa commit 58a2321

File tree

6 files changed

+69
-60
lines changed

6 files changed

+69
-60
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.. _changelog:
2+
:no-search:
23

34
=========
45
Changelog

README.rst

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,9 @@ Tortoise ORM supports CPython 3.9 and later for SQLite, MySQL, PostgreSQL, Micro
3131
Why was Tortoise ORM built?
3232
---------------------------
3333

34-
Python has many existing and mature ORMs, unfortunately they are designed with an opposing paradigm of how I/O gets processed.
35-
``asyncio`` is relatively new technology that has a very different concurrency model, and the largest change is regarding how I/O is handled.
34+
Tortoise ORM was built to provide a lightweight, async-native Object-Relational Mapper for Python with a familiar Django-like API.
3635

37-
However, Tortoise ORM is not the first attempt of building an ``asyncio`` ORM. While there are many cases of developers attempting to map synchronous Python ORMs to the async world, initial attempts did not have a clean API.
38-
39-
Hence we started Tortoise ORM.
40-
41-
Tortoise ORM is designed to be functional, yet familiar, to ease the migration of developers wishing to switch to ``asyncio``.
42-
43-
It also performs well when compared to other Python ORMs. In `our benchmarks <https://github.com/tortoise/orm-benchmarks>`_, where we measure different read and write operations (rows/sec, more is better), it's trading places with Pony ORM:
36+
Tortoise ORM performs well when compared to other Python ORMs. In `our benchmarks <https://github.com/tortoise/orm-benchmarks>`_, where we measure different read and write operations (rows/sec, more is better), it's trading places with Pony ORM:
4437

4538
.. image:: https://raw.githubusercontent.com/tortoise/tortoise-orm/develop/docs/ORM_Perf.png
4639
:target: https://github.com/tortoise/orm-benchmarks

docs/databases.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ To use, please ensure that corresponding asyncio driver is installed.
1818
DB_URL
1919
======
2020

21-
Tortoise supports specifying Database configuration in a URL form.
22-
23-
The form is:
21+
Tortoise supports specifying Database configuration in a URL form. The form is:
2422

2523
:samp:`{DB_TYPE}://{USERNAME}:{PASSWORD}@{HOST}:{PORT}/{DB_NAME}?{PARAM1}=value&{PARAM2}=value`
2624

docs/index.rst

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,9 @@ Introduction
1818
Why was Tortoise ORM built?
1919
---------------------------
2020

21-
Python has many existing and mature ORMs, unfortunately they are designed with an opposing paradigm of how I/O gets processed.
22-
``asyncio`` is relatively new technology that has a different concurrency model, and the largest change is regarding how I/O is handled.
21+
Tortoise ORM was built to provide a lightweight, async-native Object-Relational Mapper for Python with a familiar Django-like API.
2322

24-
However, Tortoise ORM is not first attempt of building ``asyncio`` ORM, there are many cases of developers attempting to map synchronous python ORMs to the async world, initial attempts did not have a clean API.
25-
26-
Hence we started Tortoise ORM.
27-
28-
Tortoise ORM is designed to be functional, yet familiar, to ease the migration of developers wishing to switch to ``asyncio``.
29-
30-
It also performs well when compared to other Python ORMs. In `our benchmarks <https://github.com/tortoise/orm-benchmarks>`_, where we measure different read and write operations (rows/sec, more is better), it's trading places with Pony ORM:
23+
Tortoise ORM performs well when compared to other Python ORMs. In `our benchmarks <https://github.com/tortoise/orm-benchmarks>`_, where we measure different read and write operations (rows/sec, more is better), it's trading places with Pony ORM:
3124

3225
.. image:: ORM_Perf.png
3326
:target: https://github.com/tortoise/orm-benchmarks

docs/router.rst

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
Router
55
======
66

7-
The easiest way to use multiple databases is to set up a database routing scheme. The default routing scheme ensures that objects remain 'sticky' to their original database (i.e., an object retrieved from the foo database will be saved on the same database). The default routing scheme ensures that if a database isn't specified, all queries fall back to the default database.
7+
The easiest way to use multiple databases is to set up a database routing scheme. The default routing scheme ensures that objects remain 'sticky' to their original database (i.e., an object retrieved from the foo database will be saved to the same database). The default routing scheme also ensures that if a database isn't specified, all queries fall back to the default database.
88

99
Usage
1010
=====
1111

1212
Define Router
1313
-------------
1414

15-
Define a router is simple, you need just write a class that has `db_for_read` and `db_for_write` methods.
15+
Defining a router is simple - you just need to write a class that has `db_for_read` and `db_for_write` methods.
1616

1717
.. code-block:: python3
1818
@@ -23,19 +23,22 @@ Define a router is simple, you need just write a class that has `db_for_read` an
2323
def db_for_write(self, model: Type[Model]):
2424
return "master"
2525
26-
The two methods return a connection string defined in configuration.
26+
Both methods return a connection identifier that must be defined in the Tortoise configuration.
2727

28-
Config Router
29-
-------------
28+
Configure Router
29+
----------------
3030

31-
Just put it in configuration of tortoise or in `Tortoise.init` method.
31+
Simply include the router in your Tortoise configuration or pass it to the `Tortoise.init` method.
3232

3333
.. code-block:: python3
3434
35-
config = {
36-
"connections": {"master": "sqlite:///tmp/test.db", "slave": "sqlite:///tmp/test.db"},
35+
CONFIG = {
36+
"connections": {
37+
"master": "sqlite:///tmp/m.db",
38+
"slave": "sqlite:///tmp/s.db",
39+
},
3740
"apps": {
38-
"models": {
41+
"app": {
3942
"models": ["__main__"],
4043
"default_connection": "master",
4144
}
@@ -44,9 +47,7 @@ Just put it in configuration of tortoise or in `Tortoise.init` method.
4447
"use_tz": False,
4548
"timezone": "UTC",
4649
}
47-
await Tortoise.init(config=config)
48-
# or
49-
routers = config.pop('routers')
50-
await Tortoise.init(config=config, routers=routers)
50+
await Tortoise.init(config=CONFIG)
51+
5152
52-
After that, all `select` operations will use `slave` connection, all `create/update/delete` operations will use `master` connection.
53+
With this configuration, all `select` operations will use the `slave` connection, while all `create/update/delete` operations will use the `master` connection.

docs/setup.rst

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,78 @@ Set up
44

55
.. _init_app:
66

7-
Init app
8-
========
7+
Initialize Your Application
8+
===========================
99

10-
After you defined all your models, tortoise needs you to init them, in order to create backward relations between models and match your db client with appropriate models.
10+
After defining all your models, Tortoise ORM requires initialization to create backward relations between models and match your database client with the appropriate models.
1111

12-
You can do it like this:
12+
You can initialize your application like this:
1313

1414
.. code-block:: python3
1515
1616
from tortoise import Tortoise
1717
1818
async def init():
1919
# Here we create a SQLite DB using file "db.sqlite3"
20-
# also specify the app name of "models"
21-
# which contain models from "app.models"
20+
# and specify the app name "models"
21+
# which contains models from "app.models"
2222
await Tortoise.init(
2323
db_url='sqlite://db.sqlite3',
24-
modules={'models': ['app.models']}
24+
modules={'app': ['app.models']}
2525
)
2626
# Generate the schema
2727
await Tortoise.generate_schemas()
28-
29-
30-
Here we create connection to SQLite database client and then we discover & initialize models.
3128
32-
``generate_schema`` generates schema on empty database, you shouldn't run it on every app init, run it just once, maybe out of your main code.
33-
There is also the option when generating the schemas to set the ``safe`` parameter to ``True`` which will only insert the tables if they don't already exist.
3429
35-
If you define the variable ``__models__`` in the ``app.models`` module (or wherever you specify to load your models from), ``generate_schema`` will use that list, rather than automatically finding models for you.
30+
This example creates a connection to a SQLite database client and then discovers and initializes your models. The example configures the ``app`` application to load models from the ``app.models`` module. In this example it is a single application, but you can use multiple applications to group models.
3631

37-
.. _cleaningup:
32+
The ``generate_schemas()`` method generates the database schema on an empty database. When generating schemas, you can set the ``safe`` parameter to ``True``, which will only create tables if they don't already exist:
33+
34+
.. code-block:: python3
35+
36+
await Tortoise.generate_schemas(safe=True)
3837
39-
The Importance of cleaning up
40-
=============================
38+
See :ref:`migration` for schema migration tools.
4139

42-
Tortoise ORM will keep connections open to external Databases. As an ``asyncio`` Python library, it needs to have the connections closed properly or the Python interpreter may still wait for the completion of said connections.
40+
If you define a ``__models__`` variable in your ``app.models`` module (or wherever you specify to load your models from), ``generate_schemas()`` will use that list instead of automatically discovering models.
4341

44-
To ensure connections are closed please ensure that ``Tortoise.close_connections()`` is called:
42+
Another way of initializing the application is to use the ``Tortoise.init()`` method with the ``config`` parameter.
4543

4644
.. code-block:: python3
4745
48-
await Tortoise.close_connections()
46+
CONFIG = {
47+
"connections": {
48+
"default": {
49+
"engine": "tortoise.backends.sqlite",
50+
"credentials": {"file_path": "default.sqlite3"},
51+
},
52+
"another_conn": {
53+
"engine": "tortoise.backends.sqlite",
54+
"credentials": {"file_path": "another_conn.sqlite3"},
55+
},
56+
},
57+
"apps": {
58+
"app": {"models": ["app.models"], "default_connection": "default"},
59+
"another_app": {"models": ["another_app.models"], "default_connection": "another_conn"},
60+
},
61+
}
62+
63+
await Tortoise.init(config=CONFIG)
64+
4965
50-
The small helper function ``tortoise.run_async()`` will ensure that connections are closed.
66+
This way of initializing the application is useful when you want to configure different databases for different applications. This also allows to configure connection routing, see :ref:`router` for more details. Also check out :ref:`contrib_fastapi`, :ref:`contrib_sanic` and documentation on other integrations.
5167

52-
Reference
53-
=========
68+
.. _cleaningup:
69+
70+
The Importance of Cleaning Up
71+
=================================
72+
73+
Tortoise ORM maintains open connections to external databases. As an ``asyncio``-based Python library, these connections must be closed properly, or the Python interpreter may continue waiting for their completion.
74+
75+
To ensure connections are properly closed, make sure to call ``Tortoise.close_connections()``:
5476

55-
.. automodule:: tortoise
56-
:members:
57-
:undoc-members:
77+
.. code-block:: python3
78+
79+
await Tortoise.close_connections()
5880
81+
The helper function ``tortoise.run_async()`` automatically ensures that connections are closed when your application terminates.

0 commit comments

Comments
 (0)