# Moose / Migrate / Lifecycle Documentation – TypeScript ## Included Files 1. moose/migrate/lifecycle/lifecycle.mdx ## LifeCycle Management Source: moose/migrate/lifecycle/lifecycle.mdx Control how Moose manages database and streaming resources when your code changes # LifeCycle Management ## Overview The `LifeCycle` enum controls how Moose manages the lifecycle of database/streaming resources when your code changes. This feature gives you fine-grained control over whether Moose automatically updates your database schema or leaves it under external/manual control. ## LifeCycle Modes ### `FULLY_MANAGED` (Default) This is the default behavior where Moose has complete control over your database resources. When you change your data models, Moose will automatically: - Add new columns or tables - Remove columns or tables that no longer exist in your code - Modify existing column types and constraints This mode can perform destructive operations. Data may be lost if you remove fields from your data models or if you perform operations that require a destroy and recreate to be effective, like changing the `orderByFields` field . ```ts filename="FullyManagedExample.ts" copy interface UserData { id: string; name: string; email: string; } // Default behavior - fully managed const userTable = new OlapTable("users"); // Explicit fully managed configuration const explicitTable = new OlapTable("users", { orderByFields: ["id"], lifeCycle: LifeCycle.FULLY_MANAGED }); ``` ### `DELETION_PROTECTED` This mode allows Moose to automatically add new database structures but prevents it from removing existing ones. Perfect for production environments where you want to evolve your schema safely without risking data loss. **What Moose will do:** - Add new columns, tables - Modify column types (if compatible) - Update non-destructive configurations **What Moose won't do:** - Drop columns or tables - Perform destructive schema changes ```ts filename="DeletionProtectedExample.ts" copy interface ProductEvent { id: string; productId: string; timestamp: Date; action: string; } const productAnalytics = new IngestPipeline("product_analytics", { table: { orderByFields: ["timestamp", "productId"], engine: ClickHouseEngines.ReplacingMergeTree, }, stream: { parallelism: 4, }, ingestApi: true, // automatically applied to the table and stream lifeCycle: LifeCycle.DELETION_PROTECTED }); ``` ### `EXTERNALLY_MANAGED` This mode tells Moose to completely hands-off your resources. You become responsible for creating and managing the database schema. This is useful when: - You have existing database tables managed by another team - You're integrating with another system (e.g. PeerDB) - You have strict database change management processes With externally managed resources, you must ensure your database schema matches your data models exactly, or you may encounter runtime errors. ```ts filename="ExternallyManagedExample.ts" copy interface ExternalUserData { userId: Key; fullName: string; emailAddress: string; createdAt: Date; } // Connect to existing database table const legacyUserTable = new OlapTable("legacy_users", { lifeCycle: LifeCycle.EXTERNALLY_MANAGED }); // Connect to existing Kafka topic const legacyStream = new Stream("legacy_user_stream", { lifeCycle: LifeCycle.EXTERNALLY_MANAGED, destination: legacyUserTable }); ```