| 1 |
.. _database: |
| 2 |
|
| 3 |
Database Wrapper |
| 4 |
================ |
| 5 |
|
| 6 |
The Peewee database wrapper provides a thin layer of integration between flask |
| 7 |
apps and the peewee orm. |
| 8 |
|
| 9 |
The database wrapper is important because it ensures that a database connection |
| 10 |
is created for every incoming request, and closed upon request completion. It |
| 11 |
also provides a subclass of ``Model`` which works with the database specified |
| 12 |
in your app's configuration. |
| 13 |
|
| 14 |
Most features of ``flask-peewee`` require a database wrapper, so you very likely |
| 15 |
always create one. |
| 16 |
|
| 17 |
The database wrapper reads its configuration from the Flask application. The |
| 18 |
configuration requires only two arguments, but any additional arguments will |
| 19 |
be passed to the database driver when connecting: |
| 20 |
|
| 21 |
`name` |
| 22 |
The name of the database to connect to (or filename if using sqlite3) |
| 23 |
|
| 24 |
`engine` |
| 25 |
The database driver to use, must be a subclass of ``peewee.Database``. |
| 26 |
|
| 27 |
.. code-block:: python |
| 28 |
|
| 29 |
from flask import Flask |
| 30 |
from peewee import * |
| 31 |
|
| 32 |
from flask_peewee.db import Database |
| 33 |
|
| 34 |
DATABASE = { |
| 35 |
'name': 'example.db', |
| 36 |
'engine': 'peewee.SqliteDatabase', |
| 37 |
} |
| 38 |
|
| 39 |
app = Flask(__name__) |
| 40 |
app.config.from_object(__name__) # Load database configuration from module. |
| 41 |
|
| 42 |
# Instantiate the db wrapper. |
| 43 |
db = Database(app) |
| 44 |
|
| 45 |
# Start creating models. |
| 46 |
class Blog(db.Model): |
| 47 |
name = CharField() |
| 48 |
# .. etc. |
| 49 |
|
| 50 |
You can also directly pass the Peewee database instance to the |
| 51 |
:py:class:`Database` helper: |
| 52 |
|
| 53 |
.. code-block:: python |
| 54 |
|
| 55 |
app = Flask(__name__) |
| 56 |
app.config.from_object(__name__) |
| 57 |
|
| 58 |
sqlite_db = SqliteDatabase('example.db') |
| 59 |
db = Database(app, sqlite_db) |
| 60 |
|
| 61 |
class Blob(db.Model): |
| 62 |
name = CharField() # ... |
| 63 |
|
| 64 |
The database initialization can be deferred in order to support more dynamic |
| 65 |
behavior: |
| 66 |
|
| 67 |
.. code-block:: python |
| 68 |
|
| 69 |
from flask_peewee.db import Database |
| 70 |
|
| 71 |
# Defer initialization but define our models. |
| 72 |
db = Database() |
| 73 |
|
| 74 |
class Blog(db.Model): |
| 75 |
name = CharField() |
| 76 |
# .. etc. |
| 77 |
|
| 78 |
# Some time later, we can: |
| 79 |
app = Flask(__name__) |
| 80 |
app.config.from_object(__name__) |
| 81 |
db.init_app(app) |
| 82 |
|
| 83 |
# Or we can also specify the database directly. |
| 84 |
app = Flask(__name__) |
| 85 |
|
| 86 |
sqlite_db = SqliteDatabase('example.db') |
| 87 |
db.init_app(app, sqlite_db) |
| 88 |
|
| 89 |
|
| 90 |
Other examples |
| 91 |
-------------- |
| 92 |
|
| 93 |
Connection pooled Postgres or MySQL: |
| 94 |
|
| 95 |
.. code-block:: python |
| 96 |
|
| 97 |
DATABASE = { |
| 98 |
'name': 'my_app', |
| 99 |
'engine': 'playhouse.pool.PooledPostgresqlDatabase', |
| 100 |
# 'engine': 'playhouse.pool.PooledMySQLDatabase', |
| 101 |
# 'user': 'postgres', |
| 102 |
# 'password': 'secret password', |
| 103 |
# 'host': '127.0.0.1', |
| 104 |
} |
| 105 |
|
| 106 |
To connect to Postgres / MySQL without pool: |
| 107 |
|
| 108 |
.. code-block:: python |
| 109 |
|
| 110 |
DATABASE = { |
| 111 |
'name': 'my_database', |
| 112 |
'engine': 'peewee.PostgresqlDatabase', |
| 113 |
# 'engine': 'peewee.MySQLDatabase', |
| 114 |
'user': 'db_user', |
| 115 |
'password': 'secret password', |
| 116 |
} |
| 117 |
|
| 118 |
We can specify the database directly, as well: |
| 119 |
|
| 120 |
.. code-block:: python |
| 121 |
|
| 122 |
pg_db = PostgresqlDatabase('pg_database') |
| 123 |
db = Database(app, pg_db) |