DbAdapter Interface¶
The DbAdapterInterface class defines an interface to find, add, update and remove persistent database objects, while shielding the Flask-User code from the underlying implementation.
-
class
DbAdapterInterface
(app, db)¶ Define the DbAdapter interface to manage objects in various databases.
This interface supports session-based ODMs (
db.session.add()/commit()
) as well as object-based ODMs (object.save()
).Parameters: - app (Flask) – The Flask appliation instance.
- db – The object-database mapper instance.
-
__init__
(app, db)¶ Parameters: - app (Flask) – The Flask appliation instance.
- db – The object-database mapper instance.
-
add_object
(object)¶ Add a new object to the database.
Session-based ODMs would call something likedb.session.add(object)
.Object-based ODMs would call something likeobject.save()
.
-
commit
()¶ Save all modified session objects to the database.
Session-based ODMs would call something likedb.session.commit()
.Object-based ODMs would do nothing.
-
delete_object
(object)¶ Delete object from database.
-
find_objects
(ObjectClass, **kwargs)¶ Retrieve all objects of type
ObjectClass
, matching the specified filters in**kwargs
– case sensitive.
-
find_first_object
(ObjectClass, **kwargs)¶ Retrieve the first object of type
ObjectClass
, matching the specified filters in**kwargs
– case sensitive.
-
ifind_first_object
(ObjectClass, **kwargs)¶ Retrieve the first object of type
ObjectClass
, matching the specified filters in**kwargs
– case insensitive.If USER_IFIND_MODE is ‘nocase_collation’ this method maps to find_first_object().If USER_IFIND_MODE is ‘ifind’ this method performs a case insensitive find.
-
get_object
(ObjectClass, id)¶ Retrieve object of type
ObjectClass
byid
.Returns object on success.Returns None otherwise.
-
save_object
(object)¶ Save object to database.
Session-based ODMs would do nothing.Object-based ODMs would do something like object.save().
-
create_all_tables
()¶ Create database tables for all known database data-models.
-
drop_all_tables
()¶ Drop all tables.
Warning
ALL DATA WILL BE LOST. Use only for automated testing.
Implementing a CustomDbAdapter¶
You can write you own DbAdapter implementation by defining a CustomDbAdapter class and configure Flask-User to use this class like so:
# Define a CustomDbAdapter
from flask_user.db_adapters import DbAdapterInterface
class CustomDbAdapter(DbAdapterInterface):
...
# Setup Flask-User
user_manager = UserManager(app, db, User)
# Customize Flask-User
user_manager.db_adapter = CustomDbAdapter(app)
For an example, see the SQLDbAdapter() implementation.