UNPKG

@t1mmen/srtd

Version:

Supabase Repeatable Template Definitions (srtd): 🪄 Live-reloading SQL templates for Supabase DX. Make your database changes reviewable and migrations maintainable! 🚀

121 lines (120 loc) • 4.07 kB
/** * MigrationBuilder Service - Generates Supabase migration files from templates * Pure service that takes template content and metadata and produces formatted migration files */ import type { BuildLog, CLIConfig } from '../types.js'; export interface TemplateMetadata { name: string; templatePath: string; relativePath: string; content: string; hash: string; lastBuildAt?: string; } export interface MigrationOptions { force?: boolean; wrapInTransaction?: boolean; bundleMode?: boolean; templateName?: string; } export interface MigrationResult { fileName: string; filePath: string; content: string; timestamp: string; /** The new lastTimestamp to store in the build log */ newLastTimestamp: string; } export interface BundleMigrationResult { fileName: string; filePath: string; content: string; timestamp: string; /** The new lastTimestamp to store in the build log */ newLastTimestamp: string; includedTemplates: string[]; } export interface MigrationBuilderConfig { baseDir: string; templateDir: string; migrationDir: string; migrationPrefix?: string; migrationFilename?: string; banner?: string; footer?: string; wrapInTransaction?: boolean; } export declare class MigrationBuilder { private config; constructor(config: MigrationBuilderConfig); /** * Generate a migration file from a single template. * Note: Caller is responsible for updating the build log with newLastTimestamp. */ generateMigration(template: TemplateMetadata, buildLog: BuildLog, options?: MigrationOptions): MigrationResult; /** * Generate a bundled migration file from multiple templates. * Note: Caller is responsible for updating the build log with newLastTimestamp. */ generateBundledMigration(templates: TemplateMetadata[], buildLog: BuildLog, options?: MigrationOptions): BundleMigrationResult; /** * Format migration content with headers, footers, and transaction wrapping */ private formatMigrationContent; /** * Create MigrationBuilder from CLI config */ static fromConfig(config: CLIConfig, baseDir: string): MigrationBuilder; /** * Generate migration file path for a template */ getMigrationPath(templateName: string, timestamp: string): string; /** * Generate absolute migration file path for a template */ getAbsoluteMigrationPath(templateName: string, timestamp: string): string; /** * Validate migration configuration */ validateConfig(): { valid: boolean; errors: string[]; }; /** * Validate that a migration path stays within the migration directory * Prevents path traversal attacks via malicious template patterns */ private validateMigrationPath; /** * Write migration file to disk */ writeMigration(migrationResult: MigrationResult): Promise<string>; /** * Write bundled migration file to disk */ writeBundledMigration(migrationResult: BundleMigrationResult): Promise<string>; /** * Generate and write migration file in one operation. * Note: Caller is responsible for updating the build log with result.newLastTimestamp. */ generateAndWriteMigration(template: TemplateMetadata, buildLog: BuildLog, options?: MigrationOptions): Promise<{ result: MigrationResult; filePath: string; }>; /** * Generate and write bundled migration file in one operation. * Note: Caller is responsible for updating the build log with result.newLastTimestamp. */ generateAndWriteBundledMigration(templates: TemplateMetadata[], buildLog: BuildLog, options?: MigrationOptions): Promise<{ result: BundleMigrationResult; filePath: string; }>; /** * Check if migration file already exists */ migrationExists(fileName: string): Promise<boolean>; /** * Get current configuration */ getConfig(): Readonly<MigrationBuilderConfig>; }