/** * @db4/core - Document Types and Conversion Functions * * Provides unified document types following mdxld conventions: * * **Compact Form**: `{$id, $type, $version, $createdAt, $updatedAt, ...data}` * - $-prefixed metadata inline with data * - Used for storage (DO SQLite, CDC events, R2) * - Efficient for database operations * * **Expanded Form**: `{id, type, version, createdAt, updatedAt, data}` * - Metadata separated from data * - Used for API responses * - Clear separation of concerns * * @packageDocumentation */ /** * System metadata fields for compact documents. * These are $-prefixed and stored inline with the document data. */ export declare const COMPACT_SYSTEM_FIELDS: readonly ["$id", "$type", "$version", "$createdAt", "$updatedAt"]; /** * Compact document with required $id and $type. * Additional data properties are stored inline. * * @example * ```typescript * const doc: CompactDocument = { * $id: 'user-123', * $type: 'User', * name: 'Alice', * email: 'alice@example.com', * }; * ``` */ export interface CompactDocument { /** Document identifier */ $id: string; /** Document type (collection/entity name) */ $type: string; /** Additional data properties */ [key: string]: unknown; } /** * Compact stored document with full metadata. * Extends CompactDocument with version and timestamps. * * @example * ```typescript * const stored: CompactStoredDocument = { * $id: 'user-123', * $type: 'User', * $version: 1, * $createdAt: 1705836000000, * $updatedAt: 1705836000000, * name: 'Alice', * }; * ``` */ export interface CompactStoredDocument extends CompactDocument { /** Document version (incremented on each update) */ $version: number; /** Creation timestamp (ms since epoch) */ $createdAt: number; /** Last update timestamp (ms since epoch) */ $updatedAt: number; } /** * Expanded document with id, type, and data separated. * Used for API responses where clear structure is preferred. * * @example * ```typescript * const doc: ExpandedDocument = { * id: 'user-123', * type: 'User', * data: { * name: 'Alice', * email: 'alice@example.com', * }, * }; * ``` */ export interface ExpandedDocument { /** Document identifier */ id: string; /** Document type (collection/entity name) */ type: string; /** Document data (all non-metadata fields) */ data: Record; } /** * Expanded stored document with full metadata. * Extends ExpandedDocument with version and timestamps. * * @example * ```typescript * const stored: ExpandedStoredDocument = { * id: 'user-123', * type: 'User', * version: 1, * createdAt: 1705836000000, * updatedAt: 1705836000000, * data: { name: 'Alice' }, * }; * ``` */ export interface ExpandedStoredDocument extends ExpandedDocument { /** Document version (incremented on each update) */ version: number; /** Creation timestamp (ms since epoch) */ createdAt: number; /** Last update timestamp (ms since epoch) */ updatedAt: number; } /** * Type guard for CompactDocument. * Checks for required $id and $type string fields. * * @param value - Value to check * @returns true if value is a CompactDocument */ export declare function isCompactDocument(value: unknown): value is CompactDocument; /** * Type guard for CompactStoredDocument. * Checks for all metadata fields in addition to CompactDocument requirements. * * @param value - Value to check * @returns true if value is a CompactStoredDocument */ export declare function isCompactStoredDocument(value: unknown): value is CompactStoredDocument; /** * Type guard for ExpandedDocument. * Checks for required id, type, and data object fields. * * @param value - Value to check * @returns true if value is an ExpandedDocument */ export declare function isExpandedDocument(value: unknown): value is ExpandedDocument; /** * Type guard for ExpandedStoredDocument. * Checks for all metadata fields in addition to ExpandedDocument requirements. * * @param value - Value to check * @returns true if value is an ExpandedStoredDocument */ export declare function isExpandedStoredDocument(value: unknown): value is ExpandedStoredDocument; /** * Convert a compact document to expanded form. * Extracts $-prefixed metadata and separates data into its own object. * * @param compact - Compact document to convert * @returns Expanded document * * @example * ```typescript * const compact = { $id: 'u1', $type: 'User', name: 'Alice' }; * const expanded = compactToExpanded(compact); * // { id: 'u1', type: 'User', data: { name: 'Alice' } } * ``` */ export declare function compactToExpanded(compact: CompactDocument): ExpandedDocument; export declare function compactToExpanded(compact: CompactStoredDocument): ExpandedStoredDocument; /** * Convert an expanded document to compact form. * Merges data into the document with $-prefixed metadata. * * @param expanded - Expanded document to convert * @returns Compact document * * @example * ```typescript * const expanded = { id: 'u1', type: 'User', data: { name: 'Alice' } }; * const compact = expandedToCompact(expanded); * // { $id: 'u1', $type: 'User', name: 'Alice' } * ``` */ export declare function expandedToCompact(expanded: ExpandedDocument): CompactDocument; export declare function expandedToCompact(expanded: ExpandedStoredDocument): CompactStoredDocument; /** * Extract data fields from a compact document. * Returns a new object with only the non-system fields. * * @param compact - Compact document * @returns Object with data fields only */ export declare function extractData(compact: CompactDocument): Record; /** * Check if a field name is a system field. * * @param field - Field name to check * @returns true if the field is a system field */ export declare function isSystemField(field: string): boolean; /** * Create a new CompactDocument with generated ID if not provided. * * @param doc - Document with optional $id * @param generateId - Function to generate an ID * @returns CompactDocument with guaranteed $id */ export declare function ensureId & { $id?: string; }>(doc: T, generateId: () => string): T & { $id: string; }; /** * Create a CompactStoredDocument from a CompactDocument. * Adds version and timestamp metadata. * * @param doc - Compact document to store * @param options - Storage options * @returns CompactStoredDocument with metadata */ export declare function createStoredDocument(doc: CompactDocument, options?: { version?: number; createdAt?: number; updatedAt?: number; }): CompactStoredDocument; /** * Update a CompactStoredDocument with new data. * Increments version and updates the updatedAt timestamp. * * @param existing - Existing stored document * @param changes - Changes to apply * @returns Updated CompactStoredDocument */ export declare function updateStoredDocument(existing: CompactStoredDocument, changes: Partial>): CompactStoredDocument; //# sourceMappingURL=document.d.ts.map