Setting Up Firestore in Native Mode: A Detailed Guide
What is Firestore Native Mode?
Firestore Native mode is the recommended way to use Firestore. It’s a fully managed, serverless NoSQL database that automatically scales and provides real-time updates. It’s different from Datastore mode (the older version) and offers better performance and features.
Step-by-Step Setup
1. Access the Firestore Console
- Go to the Google Cloud Console
- Select your project
- In the left navigation menu, find “Firestore” under “Databases”
2. Create the Database
- Click “Create Database”
- You’ll see two options:
- Native mode (Recommended)
- Datastore mode (Legacy)
- Select “Native mode”
3. Choose a Location
You’ll need to select a location for your database. This is important because:
- It affects latency
- It can’t be changed later
- It should be close to your users
Common options:
us-central1
(Iowa) - Good default for USeurope-west1
(Belgium) - Good for Europeasia-east1
(Taiwan) - Good for Asia
4. Choose a Starting Mode
You’ll be asked to choose a starting mode:
- Production mode
- Starts with all security rules enabled
- Requires authentication
- Good for production applications
- Test mode
- Starts with open access
- Good for development
- Warning: Not secure for production
For development, you can start with “Test mode” and update the security rules later.
5. Security Rules
After creation, you should set up security rules. Here’s a basic example:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Allow read/write access to all users under any document
match /{document=**} {
allow read, write: if true; // WARNING: This is for development only!
}
}
}
For production, you should use more restrictive rules:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /tasks/{taskId} {
allow read: if request.auth != null;
allow write: if request.auth != null;
}
}
}
6. Verify Setup
To verify your setup, you can:
- Use the Firestore Console to create a test document
- Run a simple query using the Google Cloud Console
- Test with your application code
7. Cost Considerations
Firestore has a free tier that includes:
- 50,000 reads per day
- 20,000 writes per day
- 20,000 deletes per day
- 1GB of stored data
Beyond the free tier, you pay for:
- Document reads
- Document writes
- Document deletes
- Stored data
- Network egress
8. Best Practices
- Indexing
- Firestore automatically creates indexes for single-field queries
- For compound queries, you’ll need to create composite indexes
- You can create indexes in the Firebase Console or using the
gcloud
command
- Data Structure
- Design your data structure based on your query patterns
- Consider using subcollections for related data
- Keep documents small (under 1MB)
- Security
- Always use security rules
- Never leave your database in test mode for production
- Use authentication for all operations
9. Common Commands
# List all collections
gcloud firestore collections list
# Export data
gcloud firestore export gs://your-bucket
# Import data
gcloud firestore import gs://your-bucket
# Create an index
gcloud firestore indexes composite create --collection-group=tasks --field-config=field=title,order=ascending --field-config=field=created,order=descending
10. Monitoring
Set up monitoring in the Google Cloud Console:
- Go to “Monitoring”
- Create alerts for:
- High read/write operations
- Storage usage
- Error rates
11. Backup and Recovery
Firestore automatically backs up your data, but you can also:
- Export data to Cloud Storage
- Set up scheduled exports
- Use the Firebase Console to restore from backups