Database
Panther currently built-in supports 2 database (MongoDB
, PantherDB
), but you can define your own custom database connection and queries too.
Structure#
DATABASE = {
'engine': {
'class': 'address of engine',
'arg1': 'value1',
...
},
'query': 'address of query class'
}
Notes#
- Built-in supported engines:
panther.db.connections.PantherDBConnection
panther.db.connections.MongoDBConnection
engine
values, other than theclass
will be passed to theclass.__init__()
query
is not required when you are using default supported engines, but you can customize it
PantherDB#
DATABASE = {
'engine': {
'class': 'panther.db.connections.PantherDBConnection',
'path': BASE_DIR / 'database.pdb',
'encryption': True
}
}
Notes#
path
is not required, but you can customize the directory and file of your databaseencryption
is not required and default isFalse
- The
cryptography
package is required if you setencryption
toTrue
MongoDB#
DATABASE = {
'engine': {
'class': 'panther.db.connections.MongoDBConnection',
'host': 'mongodb://127.0.0.1:27017/database_name'
}
}
Notes#
- The parameters are same as
pymongo.MongoClient
[Reference]
How it works?#
-
Panther creates a database connection depends on
DATABASE
block you defined inconfigs
-
You can access to this connection with your
models
, or direct access fromfrom panther.db.connections import db
-
Now we are going to create a new API which uses
PantherDB
and creating aBook
-
Create
Book
model inapp/models.py
from panther.db import Model class Book(Model): title: str description: str pages_count: int
-
Add
book
url inapp/urls.py
that points tobook_api()
... from app.apis import book_api urls = { 'book/': book_api, }
-
Create
book_api()
inapp/apis.py
from panther import status from panther.app import API from panther.response import Response @API() async def book_api(): ... return Response(status_code=status.HTTP_201_CREATED)
-
Now we should use the Panther ODM to create a book, it's based on mongo queries, for creation we use
insert_one
like this:from panther import status from panther.app import API from panther.response import Response from app.models import Book @API() async def book_api(): Book.insert_one( title='Python', description='Python is good.', pages_count=10 ) return Response(status_code=status.HTTP_201_CREATED)
In next step we are going to explain more about Panther ODM