UNPKG

@seedts/types

Version:

Core TypeScript types and interfaces for SeedTS

130 lines 3.59 kB
import type { SeedAdapter } from './adapter'; import type { ExecutionContext } from './execution-context'; /** * Conflict resolution strategies when seed data already exists */ export type ConflictStrategy = 'skip' | 'update' | 'error'; /** * Supported attribute types */ export type AttributeType = 'string' | 'number' | 'boolean' | 'date' | 'json' | 'bigint'; /** * Relation selection strategies */ export type RelationStrategy = 'random' | 'sequential' | 'round-robin'; /** * Generator function for attribute values */ export type AttributeGenerator<T = any> = (ctx: ExecutionContext) => T | Promise<T>; /** * Transformer function for attribute values */ export type AttributeTransformer<T = any> = (value: T, ctx: ExecutionContext) => T | Promise<T>; /** * Attribute definition */ export interface AttributeDefinition<T = any> { name: string; type?: AttributeType; value?: T; unique?: boolean; nullable?: boolean; autoIncrement?: boolean; default?: T | (() => T); generator?: AttributeGenerator<T>; relation?: RelationDefinition; dynamicRef?: boolean; transform?: AttributeTransformer<T>; } /** * Relation definition for foreign keys */ export interface RelationDefinition { from: string; select: string; strategy?: RelationStrategy; where?: (item: any) => boolean; } /** * Record definition for static data */ export interface RecordDefinition { attributes: Record<string, any>; } /** * Data transformation hook - modifies data before/after insertion */ export type DataTransformer<T = any> = (data: T[], ctx: ExecutionContext) => T[] | Promise<T[]>; /** * Action settings for executing entity seeding */ export interface ActionDefinition { count?: number; run?: boolean | ((ctx: ExecutionContext) => boolean | Promise<boolean>); condition?: (ctx: ExecutionContext) => boolean | Promise<boolean>; beforeInsert?: DataTransformer; afterInsert?: DataTransformer; entity: EntityDefinition; } /** * Entity definition - only responsible for data structure and relations */ export interface EntityDefinition { alias?: string; attributes: AttributeDefinition[]; records?: RecordDefinition[]; } /** * Seed metadata */ export interface SeedMetadata { createdAt: Date; recordCount: number; duration: number; errors?: Error[]; } /** * Seed lifecycle hooks */ export interface SeedHooks<T = any> { beforeInsert?: (data: T[]) => T[] | Promise<T[]>; afterInsert?: (data: T[]) => T[] | Promise<T[]>; onSuccess?: (data: T[], metadata: SeedMetadata) => void | Promise<void>; onError?: (error: Error) => void | Promise<void>; } /** * Seed definition */ export interface SeedDefinition<T = any> { name: string; adapter: SeedAdapter<T>; dependsOn: string[]; onConflict: ConflictStrategy; action?: ActionDefinition; entity: EntityDefinition; hooks?: SeedHooks<T>; metadata?: SeedMetadata; } /** * Seed execution options */ export interface SeedExecutionOptions { parallel?: boolean; transaction?: boolean; dryRun?: boolean; verbose?: boolean; beforeEach?: (seed: SeedDefinition) => void | Promise<void>; afterEach?: (seed: SeedDefinition, metadata: SeedMetadata) => void | Promise<void>; onError?: (seed: SeedDefinition, error: Error) => void | Promise<void>; } /** * Seed result after execution */ export interface SeedResult { name: string; success: boolean; recordCount: number; duration: number; error?: Error; } //# sourceMappingURL=seed-definition.d.ts.map