Content Adapters Reference
Content adapters provide the storage layer for the content system, abstracting different storage mechanisms behind a common interface. This reference documents the adapter system, its core interfaces, and available implementations.
Adapter System Overview
The adapter system follows these core principles:
- Common Interface: All adapters implement the same
ContentAdapter
interface - Storage Abstraction: Each adapter abstracts a specific storage mechanism
- URI-Based Addressing: Content is identified by URIs across all adapters
- Environment Awareness: Adapters are selected based on the runtime environment
- Factory Pattern: Adapters are created through factory functions
Adapter Selection
The content system automatically selects appropriate adapters based on:
- Runtime environment (Node.js vs. browser)
- URI scheme (file:, http:, memory:, etc.)
- Configuration options
- Feature requirements
Core Adapter Types
The content system includes several categories of adapters:
Environment-Specific Adapters
- Node.js Adapters: Designed for server environments
FileSystemAdapter
: Local filesystem storageWatcherAdapter
: Filesystem watching capabilities
- Browser Adapters: Designed for client environments
IndexedDBAdapter
: Persistent browser storageLocalStorageAdapter
: Simple key-value browser storageServiceWorkerAdapter
: Offline-capable browser storage
Environment-Agnostic Adapters
- Common Adapters: Work in any environment
MemoryAdapter
: In-memory content storageHttpAdapter
: Remote content via HTTP/HTTPSCacheAdapter
: Content caching layer
Special-Purpose Adapters
- Composite Adapters: Combine multiple adapters
FallbackAdapter
: Try primary adapter, fall back to secondaryMirrorAdapter
: Write to multiple adapters simultaneously
- Transformation Adapters: Modify content during operations
TransformAdapter
: Apply transformations during read/writeValidationAdapter
: Validate content during operations
Adapter Factory System
Adapters are created using factory functions:
typescript
import { createAdapter, createFileSystemAdapter } from '@lib/content/adapters'
// Generic adapter creation
const adapter = createAdapter({
read: async uri => {
/* ... */
},
write: async (uri, content) => {
/* ... */
},
delete: async uri => {
/* ... */
},
list: async pattern => {
/* ... */
},
// Optional methods
watch: (pattern, callback) => {
/* ... */
},
exists: async uri => {
/* ... */
},
})
// Specific adapter creation
const fsAdapter = createFileSystemAdapter({
basePath: '/content',
caseSensitive: true,
})
Adapter Interface
All adapters implement the ContentAdapter
interface:
typescript
interface ContentAdapter {
// Required methods
read(uri: string, options?: ReadOptions): Promise<Content>
write(uri: string, content: Content, options?: WriteOptions): Promise<void>
delete(uri: string, options?: DeleteOptions): Promise<void>
list(pattern?: string, options?: ListOptions): Promise<string[]>
// Optional methods
watch?(
pattern: string,
callback: WatchCallback,
options?: WatchOptions
): Unsubscribe
exists?(uri: string, options?: ExistsOptions): Promise<boolean>
// Resources
events?: EventEmitter
dispose?(): Promise<void>
}
Adapter Metadata
Adapters expose metadata about their capabilities:
typescript
interface AdapterMetadata {
id: string
name: string
description?: string
schemes: string[]
capabilities: {
read: boolean
write: boolean
delete: boolean
list: boolean
watch: boolean
exists: boolean
transactions: boolean
}
performance: {
readLatency: 'low' | 'medium' | 'high'
writeLatency: 'low' | 'medium' | 'high'
concurrency: number
}
constraints: {
maxContentSize?: number
supportedContentTypes?: string[]
requiresAuthentication?: boolean
maxConcurrentOperations?: number
}
}
Adapter Error Handling
Adapters throw standardized error types:
ContentNotFoundError
: Content not found at the specified URIContentAccessError
: Permission or access issuesContentValidationError
: Content failed validationContentOperationError
: Generic operation failuresContentAdapterError
: Adapter-specific issues