Data Storage
Plash provides a straightforward approach to persistent data storage for your applications. This explainer will help you understand how data storage works and best practices for managing your application data.
The data/
Directory
The most important concept to understand is the special data/
directory:
- Files in the
data/
directory are preserved between deployments - All other files in your project are replaced with each new deployment
This behavior allows you to update your application code while preserving your database and other persistent data.
Best Practices for Database Storage
Use SQLite
For most applications, we recommend using SQLite for persistent storage:
# Example of configuring a SQLite database path
= "data/prod.db" DB_PATH
By placing your SQLite database file in the data/
directory, you ensure it persists between deployments.
Deployment Considerations
When you run plash_deploy
, Plash will preserve only the contents of the data/
directory on the server.
data/
directory in your deployment files
If you upload files in a local data/
directory, they will overwrite any existing files with the same names in your production environment. Consider using environment variables to manage different database paths for development and production.
Environment-Based Configuration
Use the PLASH_PRODUCTION
environment variable (automatically set to 1
in your Plash environment) to modify your app’s behavior:
import os
= "data/prod.db" if os.getenv("PLASH_PRODUCTION") else "local.db" DB_PATH
Backing Up Your Data
You can download all your application files (including your data) by using:
plash_download
For critical applications, consider setting up automated backups. For example, using a cron job:
# Example cron job for daily backups
0 0 * * * plash_download --path /path/to/project --save_path /path/to/backups/$(date +\%Y\%m\%d)
Clearing Production Data
If you need to completely reset your application’s data, you can delete and recreate the application. This will permanently remove all associated data, so ensure you have backups if needed.