Skip to main content

Overview

The Media Library API (db.media) provides methods for managing media assets including images, videos, documents, and audio files. All media metadata is stored in localStorage.

Methods

getAll

Retrieves all media assets from the library.
db.media.getAll(): MediaAsset[]
return
MediaAsset[]
Array of all media assets in the library
const assets = db.media.getAll();
console.log(`Total assets: ${assets.length}`);

// Filter by type
const images = assets.filter(a => a.type === 'image');
const videos = assets.filter(a => a.type === 'video');

add

Adds a new media asset to the library.
db.media.add(asset: Omit<MediaAsset, 'id' | 'uploadedAt'>): MediaAsset[]
asset
Omit<MediaAsset, 'id' | 'uploadedAt'>
required
Media asset data without id and uploadedAt (auto-generated)
return
MediaAsset[]
Updated array of all media assets
const newImage = db.media.add({
  name: 'hero-background.jpg',
  type: 'image',
  url: 'https://images.unsplash.com/photo-...',
  size: '2.4 MB',
  dimensions: '1920x1080',
  tags: ['hero', 'background', 'home']
});

delete

Deletes a media asset by ID.
db.media.delete(id: string): MediaAsset[]
id
string
required
The ID of the media asset to delete
return
MediaAsset[]
Updated array of remaining media assets
db.media.delete('media_123');
console.log('Asset deleted successfully');

Searches media assets by query and/or type.
db.media.search(query: string, type?: string): MediaAsset[]
query
string
required
Search term to match against name and tags
type
string
Filter by asset type: ‘image’, ‘video’, ‘document’, ‘audio’, or ‘all’
return
MediaAsset[]
Array of matching media assets
// Search all assets
const results = db.media.search('meditación');
console.log(`Found ${results.length} assets`);

Media Asset Properties

MediaAsset Interface

interface MediaAsset {
  id: string;              // Auto-generated
  name: string;            // Filename
  type: 'image' | 'video' | 'document' | 'audio';
  url: string;             // Public URL or path
  size: string;            // Human-readable size
  dimensions?: string;     // For images/videos (e.g., "1920x1080")
  uploadedAt: string;      // Auto-generated ISO date
  tags: string[];          // Searchable tags
  folderId?: string;       // Optional folder organization
}

Asset Types

Image

  • Type: image
  • Formats: JPG, PNG, GIF, WebP, SVG
  • Use cases: Hero backgrounds, blog images, gallery items
  • Should include: dimensions

Video

  • Type: video
  • Formats: MP4, WebM, MOV
  • Use cases: Hero videos, tutorial content, event recordings
  • Should include: dimensions

Document

  • Type: document
  • Formats: PDF, DOC, DOCX, TXT
  • Use cases: Downloadable guides, resources, forms

Audio

  • Type: audio
  • Formats: MP3, WAV, OGG
  • Use cases: Guided meditations, podcasts, audio content

Usage Patterns

Image Picker Component

const ImagePicker: React.FC<{ onSelect: (url: string) => void }> = ({ onSelect }) => {
  const images = db.media.search('', 'image');
  
  return (
    <div className="grid grid-cols-4 gap-2">
      {images.map(img => (
        <img
          key={img.id}
          src={img.url}
          alt={img.name}
          onClick={() => onSelect(img.url)}
          className="cursor-pointer hover:opacity-75"
        />
      ))}
    </div>
  );
};

Media Reference in Events

// Store reference to media asset, not duplicate
const event: CalendarEvent = {
  // ... other fields
  mediaRefs: [
    { mediaAssetId: 'media_123', label: 'Guía de Meditación' },
    { mediaAssetId: 'media_456', label: 'Agenda del Retiro' }
  ]
};

// Resolve references
const resolveMediaRefs = (refs: MeetingMediaRef[]) => {
  const allMedia = db.media.getAll();
  return refs.map(ref => {
    const asset = allMedia.find(m => m.id === ref.mediaAssetId);
    return { ...ref, asset };
  });
};

Tag Management

// Get all unique tags
const getAllTags = () => {
  const allMedia = db.media.getAll();
  const tagSet = new Set<string>();
  allMedia.forEach(asset => {
    asset.tags.forEach(tag => tagSet.add(tag));
  });
  return Array.from(tagSet).sort();
};

// Get assets by tag
const getAssetsByTag = (tag: string) => {
  return db.media.getAll().filter(asset => asset.tags.includes(tag));
};

Storage Key

  • Key: cafh_media_v1
  • Location: localStorage
  • Format: JSON array of MediaAsset objects

Build docs developers (and LLMs) love