Overview
In traditional databases, you define tables with schemas. In SnackBase, you define Collections, which:- Store schema metadata in the
collectionstable - Create/update physical tables dynamically
- Auto-generate REST API endpoints
- Provide validation and type safety
- Support relationships between collections
- Protect sensitive data with PII masking
What is a Collection?
A Collection is a named data schema with fields, types, and configuration options.Collection Structure
Components of a Collection
Collection vs Table
Understanding the distinction is critical:The Confusion
How It Actually Works
When you create a collection namedposts:
- Schema Definition: Stored in
collectionstable (metadata) - Table Creation: Physical
col_poststable created (if doesn’t exist) - API Generation:
/api/v1/records/postsendpoints registered - Usage: All accounts use the same physical table
Table Naming Convention
Critical: Collection tables are prefixed withcol_ to avoid conflicts with system tables.
Why This Design?
Field Types
Collections support multiple field types with built-in validation.Available Field Types
Field Configuration
Each field type has specific configuration options:Text Field
Number Field
DateTime Field
Email Field
URL Field
JSON Field
Reference Field
File Field
Dynamic Table Generation
SnackBase dynamically creates and modifies database tables based on collection schemas.Table Creation Flow
Built-in Fields
Every collection table includes automatic fields you don’t need to define:Indexes and Constraints
SnackBase automatically creates:Auto-Generated APIs
Each collection automatically gets a complete REST API.Generated Endpoints
For a collection namedposts:
API Usage Examples
Query Filtering
List endpoints support powerful filtering:PII Masking
SnackBase provides automatic PII (Personally Identifiable Information) masking to protect sensitive user data.How PII Masking Works
PII fields are automatically masked for users who don’t have thepii_access group membership:
PII Mask Types
Configuring PII Fields
To enable PII masking on a field, use thepii_mask configuration:
Reference Fields
Reference fields allow you to create relationships between collections using foreign keys.Reference Field Configuration
on_delete Actions
Reference Field Example
Reference Validation
When creating or updating records with reference fields:Schema Evolution
Collections can evolve over time with schema updates.Supported Changes
Schema Update Flow
Schema Evolution Rules
Field Addition (Only supported operation):Best Practices
1. Naming Conventions
Use lowercase, plural names for collections:2. Field Naming
Use snake_case for field names:3. Use Appropriate Field Types
Choose the most specific type for your data:4. Use JSON for Flexible Data
For metadata or varying structures:5. Plan Schema Evolution
Design schemas with evolution in mind:- Only add new fields (deletion and type changes are not allowed)
- Use
required: falsefor fields that might be optional later - Document schema changes in migration revisions
- Test schema updates in development first
6. Use PII Masking for Sensitive Data
Protect user privacy with automatic PII masking:7. Choose Appropriate on_delete Actions
Select the right action for reference fields:8. Avoid Too Many Collections
Each collection creates a table. Consider:- Can related data be in the same collection?
- Would JSON fields work better for varying schemas?
- Do you really need separate tables?
blog_posts and news_posts, use posts with a category field.
Aggregation Queries
Collections support server-side aggregation via the/{collection}/aggregate endpoint:
Aggregations support
filter, group_by, and having clauses for powerful analytics without fetching raw records. See the API Reference for full details.