Skip to main content
SnackBase’s Collections are the core abstraction for defining data schemas and generating APIs. This guide explains how collections work, the dynamic table system, and implications for developers.

Overview

In traditional databases, you define tables with schemas. In SnackBase, you define Collections, which:
  1. Store schema metadata in the collections table
  2. Create/update physical tables dynamically
  3. Auto-generate REST API endpoints
  4. Provide validation and type safety
  5. Support relationships between collections
  6. 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 named posts:
  1. Schema Definition: Stored in collections table (metadata)
  2. Table Creation: Physical col_posts table created (if doesn’t exist)
  3. API Generation: /api/v1/records/posts endpoints registered
  4. Usage: All accounts use the same physical table

Table Naming Convention

Critical: Collection tables are prefixed with col_ 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 named posts:

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 the pii_access group membership:

PII Mask Types

Configuring PII Fields

To enable PII masking on a field, use the pii_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

This creates a foreign key constraint:

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):
Field Deletion (NOT allowed):
Type Changes (NOT allowed):

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:
Store arbitrary data:

5. Plan Schema Evolution

Design schemas with evolution in mind:
  • Only add new fields (deletion and type changes are not allowed)
  • Use required: false for 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:
This ensures compliance with privacy regulations by default.

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?
Example: Instead of 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.

Public Collections

Collections can be configured for anonymous read access, allowing unauthenticated users to query records. This is useful for public-facing data like product listings, blog posts, or FAQ content.

Summary