Database Storage (DBStorage)¶
DBStorage stores file bytes directly in the database alongside the File record's data field. This is the default storage backend — no additional infrastructure or packages are needed.
When to Use¶
- Prototyping and development
- Small files (configs, text documents, small images)
- When you want everything in a single database with no external dependencies
Not recommended for large files or high-traffic scenarios — every file read/write goes through the database.
Configuration¶
DBStorage has no constructor parameters — it works out of the box:
from amsdal_models.storage.backends.db import DBStorage
storage = DBStorage()
Since it's the default backend, you don't need to configure anything unless you've changed DEFAULT_FILE_STORAGE to something else.
Explicitly setting as default¶
Via environment variable:
export AMSDAL_DEFAULT_FILE_STORAGE='amsdal_models.storage.backends.db.DBStorage'
Or via code:
from amsdal.storages import set_default_storage
from amsdal_models.storage.backends.db import DBStorage
set_default_storage(DBStorage())
Per-field usage¶
from amsdal_models.storage.backends.db import DBStorage
from amsdal.models.core.file import File
from amsdal_models.classes.fields.file import FileField
from amsdal_models.classes.model import Model
class MyModel(Model):
attachment: File = FileField(storage=DBStorage())
How It Works¶
When a File is saved with DBStorage:
- On save — file bytes are read from the source stream and stored in the
Filemodel'sdatafield (a binary column in the database). - On open — bytes are read from the
datafield and returned as an in-memoryBytesIOstream. - On delete — the
datafield is cleared. - URL — returns a placeholder
db://<filename>URL (not publicly accessible).
DBStorage sets keeps_local_copy = True, which tells the framework to keep the data field populated and not clear it after persistence.
Async Support¶
All operations have async counterparts (asave, aopen, adelete, aexists, aurl) that work identically to the sync versions.
Limitations¶
- No public URLs —
url()returnsdb://filename, which is not a real URL. If you need publicly accessible file links, use FileSystemStorage or S3Storage. - Database size — every file adds its full byte content to the database. Large files will bloat your database.
- Performance — reading large files goes through the database driver, which is slower than reading from disk or S3.