plantr 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts ADDED
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Generic seeder types - context-agnostic
3
+ *
4
+ * The library provides a base context with faker, step, and expect.
5
+ * Consumers can extend this with their own context (e.g. prisma, custom helpers).
6
+ */
7
+
8
+ import type { expect } from 'bun:test'
9
+ import type { faker } from '@faker-js/faker'
10
+ import type { StepFn } from './step'
11
+
12
+ /**
13
+ * Base seeder context provided by the library.
14
+ * Contains faker for generating test data, step for workflow organization,
15
+ * and expect for assertions.
16
+ */
17
+ export type BaseSeederContext = {
18
+ /** Faker instance for generating test data */
19
+ faker: typeof faker
20
+ /** Step function for organizing seeder workflow */
21
+ step: StepFn
22
+ /** Assertion function from bun:test for verifying data */
23
+ expect: typeof expect
24
+ }
25
+
26
+ /** Extract the output type from a Seeder */
27
+ // biome-ignore lint/suspicious/noExplicitAny: needs any to match any context type
28
+ export type SeederOutput<T> = T extends Seeder<any, string, infer O> ? O : never
29
+
30
+ /** Extract the name from a Seeder */
31
+ // biome-ignore lint/suspicious/noExplicitAny: needs any to match any context type
32
+ export type SeederName<T> = T extends Seeder<any, infer N, unknown> ? N : never
33
+
34
+ /** Build a typed deps object from an array of seeder dependencies */
35
+ export type DepsFromSeeders<T extends readonly SeederAny[]> = {
36
+ [K in T[number] as SeederName<K>]: SeederOutput<K>
37
+ }
38
+
39
+ /** Configuration for creating a seeder */
40
+ export type SeederConfig<
41
+ TContext,
42
+ TName extends string,
43
+ TOutput,
44
+ TDeps extends readonly SeederAny[] = readonly [],
45
+ > = {
46
+ /** Unique name for the seeder */
47
+ name: TName
48
+ /** Optional description of what the seeder does */
49
+ description?: string
50
+ /** Seeder objects that must run before this one - provides typed outputs */
51
+ dependsOn?: TDeps
52
+ /** The seeding function that creates data */
53
+ run: (
54
+ ctx: TContext,
55
+ deps: TDeps extends readonly never[] ? Record<string, never> : DepsFromSeeders<TDeps>,
56
+ ) => Promise<TOutput>
57
+ }
58
+
59
+ /** A seeder instance ready for execution */
60
+ export type Seeder<TContext, TName extends string = string, TOutput = unknown> = {
61
+ /** Unique name for the seeder */
62
+ name: TName
63
+ /** Optional description of what the seeder does */
64
+ description?: string
65
+ /** Names of seeders that must run before this one (extracted from dependsOn) */
66
+ dependsOn: readonly string[]
67
+ /** The seeding function that creates data */
68
+ run: (ctx: TContext, deps: Record<string, unknown>) => Promise<TOutput>
69
+ }
70
+
71
+ /** Type-erased seeder for collections (can hold any seeder type) */
72
+ // biome-ignore lint/suspicious/noExplicitAny: SeederAny needs 'any' to work without explicit context type
73
+ export type SeederAny = Seeder<any, string, unknown>
74
+
75
+ /** Status of a seeder execution */
76
+ export type SeederStatus = 'pending' | 'running' | 'completed' | 'failed' | 'skipped'
77
+
78
+ /** Result of running a seeder */
79
+ export type SeederResult = {
80
+ name: string
81
+ status: SeederStatus
82
+ durationMs?: number
83
+ output?: unknown
84
+ error?: Error
85
+ }
86
+
87
+ /** Options for the seeder runner */
88
+ export type RunSeedersOptions = {
89
+ /** Whether to print the results table (default: true) */
90
+ printResults?: boolean
91
+ /** Whether to print verbose output (default: true) */
92
+ verbose?: boolean
93
+ /**
94
+ * Whether to continue running seeders after a failure (default: false).
95
+ * When true, seeders that depend on a failed seeder will be skipped,
96
+ * but independent seeders will still run.
97
+ */
98
+ continueOnFailure?: boolean
99
+ }