plantr 1.0.1 → 2.1.0
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/LICENSE +21 -0
- package/dist/index.d.ts +23 -20
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +906 -235
- package/dist/src/cli.d.ts +23 -0
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/src/create-seeder-instance.d.ts +73 -83
- package/dist/src/create-seeder-instance.d.ts.map +1 -0
- package/dist/src/define-seeder.d.ts +4 -3
- package/dist/src/define-seeder.d.ts.map +1 -0
- package/dist/src/dependency-resolver.d.ts +18 -2
- package/dist/src/dependency-resolver.d.ts.map +1 -0
- package/dist/src/formatting.d.ts +31 -12
- package/dist/src/formatting.d.ts.map +1 -0
- package/dist/src/lazy-faker.d.ts +12 -0
- package/dist/src/lazy-faker.d.ts.map +1 -0
- package/dist/src/run-seeders.d.ts +57 -17
- package/dist/src/run-seeders.d.ts.map +1 -0
- package/dist/src/seeding-state.d.ts +17 -2
- package/dist/src/seeding-state.d.ts.map +1 -0
- package/dist/src/step.d.ts +97 -15
- package/dist/src/step.d.ts.map +1 -0
- package/dist/src/types.d.ts +132 -29
- package/dist/src/types.d.ts.map +1 -0
- package/dist/state.d.ts +12 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +20 -0
- package/index.ts +60 -24
- package/package.json +20 -10
- package/readme.md +169 -49
- package/src/cli.ts +251 -0
- package/src/create-seeder-instance.ts +254 -173
- package/src/define-seeder.ts +14 -7
- package/src/dependency-resolver.ts +59 -30
- package/src/formatting.ts +142 -39
- package/src/lazy-faker.ts +46 -0
- package/src/run-seeders.ts +413 -123
- package/src/seeding-state.ts +32 -5
- package/src/step.ts +247 -46
- package/src/types.ts +184 -32
- package/state.ts +11 -0
- package/dist/cli.d.ts +0 -17
- package/dist/cli.js +0 -59
- package/dist/index-a1s5r901.js +0 -4
|
@@ -1,62 +1,98 @@
|
|
|
1
1
|
import { expect } from 'bun:test'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
3
|
+
import type { Faker } from '@faker-js/faker'
|
|
4
|
+
import { type RunSeedersCliOptions, runCli } from './cli.js'
|
|
5
|
+
import { createDefineSeeder } from './define-seeder.js'
|
|
6
|
+
import { getDefaultFaker, lazyFaker } from './lazy-faker.js'
|
|
7
|
+
import { runSeedersCore } from './run-seeders.js'
|
|
8
|
+
import { isSeedingActive } from './seeding-state.js'
|
|
9
|
+
import { createStepRunner, type SeederCache } from './step.js'
|
|
9
10
|
import type {
|
|
10
11
|
BaseSeederContext,
|
|
12
|
+
MaybePromise,
|
|
13
|
+
Prettify,
|
|
11
14
|
RunSeedersOptions,
|
|
15
|
+
RunSeedersResult,
|
|
12
16
|
Seeder,
|
|
13
|
-
SeederAny,
|
|
14
17
|
SeederConfig,
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
SeederName,
|
|
19
|
+
ValidateSeeders,
|
|
20
|
+
} from './types.js'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Full context type resolved from `BaseSeederContext` plus the extra properties
|
|
24
|
+
* returned by `config.context`. Any base key returned by `context` overrides the default.
|
|
25
|
+
*/
|
|
26
|
+
export type ResolvedContext<TExtra extends object = Record<never, never>> = Prettify<
|
|
27
|
+
Omit<BaseSeederContext, keyof TExtra> & TExtra
|
|
28
|
+
>
|
|
17
29
|
|
|
18
30
|
/**
|
|
19
31
|
* Configuration for creating a seeder instance.
|
|
20
|
-
*
|
|
32
|
+
* `context` is optional and its returned properties are automatically merged onto `BaseSeederContext`.
|
|
21
33
|
*/
|
|
22
|
-
export type SeederInstanceConfig<
|
|
34
|
+
export type SeederInstanceConfig<TExtra extends object = Record<never, never>> = {
|
|
23
35
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
36
|
+
* Optional factory function that extends the base context (`faker`, `step`, `expect`).
|
|
37
|
+
* Returned properties are merged onto `base` automatically — no need to spread `...base`,
|
|
38
|
+
* though spreading or overriding keys (such as `faker`) also works.
|
|
39
|
+
* Called once per run, after `onBeforeAll`.
|
|
26
40
|
*
|
|
27
41
|
* @example
|
|
28
42
|
* ```typescript
|
|
29
|
-
* context: (
|
|
30
|
-
* ...base,
|
|
31
|
-
* prisma,
|
|
32
|
-
* myHelper: () => { ... }
|
|
33
|
-
* })
|
|
43
|
+
* context: () => ({ prisma })
|
|
34
44
|
* ```
|
|
35
45
|
*/
|
|
36
|
-
context
|
|
46
|
+
context?: ((base: BaseSeederContext) => MaybePromise<TExtra>) | undefined
|
|
37
47
|
|
|
38
48
|
/**
|
|
39
49
|
* Optional cache for step caching.
|
|
40
|
-
* Provide get/set
|
|
50
|
+
* Provide `get` / `set` (and optionally `getAll` for single-query preloading).
|
|
41
51
|
*
|
|
42
52
|
* @example
|
|
43
53
|
* ```typescript
|
|
44
54
|
* cache: {
|
|
45
|
-
* get: async (key) => {
|
|
46
|
-
* const cached = await prisma.seedCache.findUnique({ where: { key } })
|
|
47
|
-
* return cached?.output as T | undefined
|
|
48
|
-
* },
|
|
55
|
+
* get: async (key) => (await prisma.seedCache.findUnique({ where: { key } }))?.output,
|
|
49
56
|
* set: async (key, value) => {
|
|
50
|
-
*
|
|
51
|
-
*
|
|
57
|
+
* const output = value as Prisma.InputJsonValue
|
|
58
|
+
* await prisma.seedCache.upsert({ where: { key }, create: { key, output }, update: { output } })
|
|
59
|
+
* },
|
|
52
60
|
* }
|
|
53
61
|
* ```
|
|
54
62
|
*/
|
|
55
|
-
cache?: SeederCache
|
|
63
|
+
cache?: SeederCache | undefined
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Optional: default maximum number of independent seeders to run concurrently (default: 1).
|
|
67
|
+
* Applies to both `runSeeders` and `runSeedersCli` unless overridden per run or via `--concurrency`.
|
|
68
|
+
* Set to a positive integer (e.g. `4`) or `Infinity` to run independent DAG branches in parallel.
|
|
69
|
+
*/
|
|
70
|
+
concurrency?: number | undefined
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Optional: custom `Faker` instance (e.g. a non-English locale like `fakerDE` or `fakerNB_NO`).
|
|
74
|
+
* Defaults to the lazy-loaded English locale (`@faker-js/faker/locale/en`).
|
|
75
|
+
*/
|
|
76
|
+
faker?: Faker | undefined
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Optional: the database URL this instance writes to, e.g. `() => process.env.DATABASE_URL`.
|
|
80
|
+
* The banner prints its host and database (never credentials), and a run refuses a
|
|
81
|
+
* host that is not local unless it passes `allowRemote` (CLI: `--allow-remote`).
|
|
82
|
+
* Return undefined to skip the check.
|
|
83
|
+
*/
|
|
84
|
+
target?: (() => string | undefined) | undefined
|
|
56
85
|
|
|
57
86
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
87
|
+
* Optional: seed faker before each seeder with this number plus a hash of the
|
|
88
|
+
* seeder's name, so every run generates the same data and adding a seeder does
|
|
89
|
+
* not shift the others'. Works safely with parallel `concurrency` via isolated Faker instances.
|
|
90
|
+
*/
|
|
91
|
+
fakerSeed?: number | undefined
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Called before all seeders run, before the context factory.
|
|
95
|
+
* Use for setup like connecting to databases, or to refuse a run.
|
|
60
96
|
*
|
|
61
97
|
* @example
|
|
62
98
|
* ```typescript
|
|
@@ -65,10 +101,11 @@ export type SeederInstanceConfig<TContext extends BaseSeederContext> = {
|
|
|
65
101
|
* }
|
|
66
102
|
* ```
|
|
67
103
|
*/
|
|
68
|
-
onBeforeAll?: () => void |
|
|
104
|
+
onBeforeAll?: (() => MaybePromise<void>) | undefined
|
|
69
105
|
|
|
70
106
|
/**
|
|
71
|
-
* Called after all seeders complete (success or failure)
|
|
107
|
+
* Called after all seeders complete (success or failure), also when the context
|
|
108
|
+
* factory throws. Receives the created context (or `undefined` if the context factory threw).
|
|
72
109
|
* Use for cleanup like disconnecting from databases.
|
|
73
110
|
*
|
|
74
111
|
* @example
|
|
@@ -78,15 +115,18 @@ export type SeederInstanceConfig<TContext extends BaseSeederContext> = {
|
|
|
78
115
|
* }
|
|
79
116
|
* ```
|
|
80
117
|
*/
|
|
81
|
-
onAfterAll?:
|
|
118
|
+
onAfterAll?:
|
|
119
|
+
| ((ctx: NoInfer<ResolvedContext<TExtra>> | undefined) => MaybePromise<void>)
|
|
120
|
+
| undefined
|
|
82
121
|
}
|
|
83
122
|
|
|
84
123
|
/**
|
|
85
124
|
* Result type for the seeder instance
|
|
86
125
|
*/
|
|
87
|
-
export type SeederInstance<TContext extends BaseSeederContext> = {
|
|
126
|
+
export type SeederInstance<TContext extends object = BaseSeederContext> = {
|
|
88
127
|
/**
|
|
89
|
-
* Define a new seeder with typed dependencies
|
|
128
|
+
* Define a new seeder with typed dependencies.
|
|
129
|
+
* `dependsOn` only accepts seeders whose context is compatible with this instance.
|
|
90
130
|
*
|
|
91
131
|
* @example
|
|
92
132
|
* ```typescript
|
|
@@ -104,27 +144,39 @@ export type SeederInstance<TContext extends BaseSeederContext> = {
|
|
|
104
144
|
defineSeeder: <
|
|
105
145
|
TName extends string,
|
|
106
146
|
TOutput,
|
|
107
|
-
const TDeps extends readonly
|
|
147
|
+
const TDeps extends readonly Seeder<TContext, string, unknown, string>[] = readonly [],
|
|
108
148
|
>(
|
|
109
149
|
config: SeederConfig<TContext, TName, TOutput, TDeps>,
|
|
110
|
-
) => Seeder<TContext, TName, TOutput
|
|
150
|
+
) => Seeder<TContext, TName, Awaited<TOutput>, SeederName<TDeps[number]>>
|
|
111
151
|
|
|
112
152
|
/**
|
|
113
153
|
* Run an array of seeders in dependency order.
|
|
114
154
|
* Automatically sets seeding state to active during execution.
|
|
115
155
|
*
|
|
156
|
+
* - Enforces context compatibility across seeders at compile time
|
|
157
|
+
* - Flags missing dependencies at compile time when passing known seeders
|
|
158
|
+
* - Autocompletes and checks `only` and `fresh` against the registered seeder names
|
|
159
|
+
* - Returns strongly typed `outputs` and discriminated-union `results`
|
|
160
|
+
*
|
|
116
161
|
* @param seeders - Array of seeders to run
|
|
117
162
|
* @param options - Optional runner options
|
|
118
|
-
* @returns Object containing results and
|
|
163
|
+
* @returns Object containing `success`, typed `results`, and typed `outputs`
|
|
164
|
+
* @throws Before anything runs, on an invalid registry (duplicate name, missing
|
|
165
|
+
* dependency, cycle), an unknown name in `only`/`fresh`, invalid `concurrency`, or a refused `target`
|
|
119
166
|
*/
|
|
120
|
-
runSeeders:
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
167
|
+
runSeeders: <
|
|
168
|
+
const TSeeders extends readonly Seeder<TContext, string, unknown, string>[],
|
|
169
|
+
const TOptions extends RunSeedersOptions<SeederName<TSeeders[number]>> | undefined = undefined,
|
|
170
|
+
>(
|
|
171
|
+
seeders: TSeeders & ValidateSeeders<TSeeders>,
|
|
172
|
+
options?: TOptions,
|
|
173
|
+
) => Promise<RunSeedersResult<TSeeders, TOptions>>
|
|
124
174
|
|
|
125
175
|
/**
|
|
126
|
-
* Run seeders as a CLI command.
|
|
127
|
-
*
|
|
176
|
+
* Run seeders as a CLI command: parses the arguments, runs, and exits with 0 or 1.
|
|
177
|
+
* Seeder names (positional or `--only a,b`) select seeders plus their dependencies;
|
|
178
|
+
* `--fresh`, `--concurrency`, `--continue-on-failure`, `--allow-remote`, `--quiet`,
|
|
179
|
+
* `--dry-run`, `--list`, and `--help` map to the runner options. Unknown flags and names exit 1.
|
|
128
180
|
*
|
|
129
181
|
* @example
|
|
130
182
|
* ```typescript
|
|
@@ -134,7 +186,10 @@ export type SeederInstance<TContext extends BaseSeederContext> = {
|
|
|
134
186
|
* }
|
|
135
187
|
* ```
|
|
136
188
|
*/
|
|
137
|
-
runSeedersCli:
|
|
189
|
+
runSeedersCli: <const TSeeders extends readonly Seeder<TContext, string, unknown, string>[]>(
|
|
190
|
+
seeders: TSeeders & ValidateSeeders<TSeeders>,
|
|
191
|
+
options?: RunSeedersCliOptions,
|
|
192
|
+
) => Promise<void>
|
|
138
193
|
|
|
139
194
|
/**
|
|
140
195
|
* Check if seeding is currently active.
|
|
@@ -151,159 +206,175 @@ export type SeederInstance<TContext extends BaseSeederContext> = {
|
|
|
151
206
|
isSeedingActive: () => boolean
|
|
152
207
|
}
|
|
153
208
|
|
|
209
|
+
type FakerConstructor = new (options: { locale: Faker['rawDefinitions'] }) => Faker
|
|
210
|
+
|
|
211
|
+
/** Create an isolated Faker instance using the same locale definitions as `baseFaker` */
|
|
212
|
+
function cloneFaker(baseFaker: Faker): Faker {
|
|
213
|
+
const Ctor = baseFaker.constructor as FakerConstructor | undefined
|
|
214
|
+
if (typeof Ctor === 'function' && 'rawDefinitions' in baseFaker && baseFaker.rawDefinitions) {
|
|
215
|
+
return new Ctor({ locale: baseFaker.rawDefinitions })
|
|
216
|
+
}
|
|
217
|
+
return baseFaker
|
|
218
|
+
}
|
|
219
|
+
|
|
154
220
|
/**
|
|
155
|
-
* Create a seeder instance with
|
|
221
|
+
* Create a seeder instance with an optional custom context.
|
|
156
222
|
*
|
|
157
223
|
* This is the main entry point for the seeder library. The library provides
|
|
158
|
-
* a base context with faker
|
|
159
|
-
*
|
|
224
|
+
* a base context with `faker`, `step`, and `expect`. Consumers can extend it
|
|
225
|
+
* without needing explicit generics or manual `...base` spreads.
|
|
160
226
|
*
|
|
161
|
-
* The seeding state is automatically managed - set to active
|
|
162
|
-
*
|
|
227
|
+
* The seeding state is automatically managed - set to active after validation
|
|
228
|
+
* succeeds and cleared when the run completes.
|
|
163
229
|
*
|
|
164
230
|
* @example
|
|
165
231
|
* ```typescript
|
|
166
232
|
* import { createSeederInstance } from 'plantr'
|
|
167
233
|
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
* context: (base) => ({
|
|
172
|
-
* ...base,
|
|
173
|
-
* prisma,
|
|
174
|
-
* }),
|
|
175
|
-
* cache: {
|
|
176
|
-
* get: async (key) => {
|
|
177
|
-
* const cached = await prisma.seedCache.findUnique({ where: { key } })
|
|
178
|
-
* return cached?.output
|
|
179
|
-
* },
|
|
180
|
-
* set: async (key, value) => {
|
|
181
|
-
* await prisma.seedCache.create({ data: { key, output: value } })
|
|
182
|
-
* }
|
|
183
|
-
* },
|
|
234
|
+
* export const { defineSeeder, runSeeders, runSeedersCli } = createSeederInstance({
|
|
235
|
+
* context: () => ({ prisma }),
|
|
236
|
+
* target: () => process.env.DATABASE_URL,
|
|
184
237
|
* })
|
|
185
|
-
*
|
|
186
|
-
* export { defineSeeder, isSeedingActive }
|
|
187
|
-
*
|
|
188
|
-
* // In your message/queue system:
|
|
189
|
-
* if (isSeedingActive()) {
|
|
190
|
-
* console.log('Skipping side effect during seeding')
|
|
191
|
-
* return
|
|
192
|
-
* }
|
|
193
|
-
*
|
|
194
|
-
* // Later...
|
|
195
|
-
* await runSeeders([seeder1, seeder2])
|
|
196
238
|
* ```
|
|
197
239
|
*/
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
240
|
+
type FakerSlot = {
|
|
241
|
+
name: string
|
|
242
|
+
isolated: boolean
|
|
243
|
+
instance?: Faker
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function createSeederInstance<TExtra extends object = Record<never, never>>(
|
|
247
|
+
config?: SeederInstanceConfig<TExtra>,
|
|
248
|
+
): SeederInstance<ResolvedContext<TExtra>> {
|
|
249
|
+
type TContext = ResolvedContext<TExtra>
|
|
250
|
+
|
|
201
251
|
const defineSeeder = createDefineSeeder<TContext>()
|
|
252
|
+
const customFaker = config?.faker && config.faker !== lazyFaker ? config.faker : undefined
|
|
253
|
+
const resolveBaseFaker = (): Faker => customFaker ?? getDefaultFaker()
|
|
254
|
+
const fakerSeed = config?.fakerSeed
|
|
255
|
+
const defaultConcurrency = config?.concurrency
|
|
202
256
|
|
|
203
|
-
const
|
|
204
|
-
seeders: readonly
|
|
257
|
+
const runInternal = async (
|
|
258
|
+
seeders: readonly Seeder<TContext, string, unknown, string>[],
|
|
205
259
|
options?: RunSeedersOptions,
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
260
|
+
bannerName?: string,
|
|
261
|
+
) => {
|
|
262
|
+
// One step runner and faker storage per run, so cache keys, duplicate checks, and scopes are isolated
|
|
263
|
+
const stepRunner = createStepRunner({
|
|
264
|
+
cache: config?.cache,
|
|
265
|
+
log: options?.verbose ?? true,
|
|
266
|
+
})
|
|
267
|
+
const fakerStorage = new AsyncLocalStorage<FakerSlot>()
|
|
268
|
+
const resolveActiveFaker = (): Faker => {
|
|
269
|
+
const slot = fakerStorage.getStore()
|
|
270
|
+
if (!slot) return resolveBaseFaker()
|
|
271
|
+
if (slot.instance) return slot.instance
|
|
272
|
+
const base = resolveBaseFaker()
|
|
273
|
+
if (!slot.isolated) {
|
|
274
|
+
slot.instance = base
|
|
275
|
+
return base
|
|
276
|
+
}
|
|
277
|
+
const cloned = cloneFaker(base)
|
|
278
|
+
if (fakerSeed !== undefined) {
|
|
279
|
+
cloned.seed(fakerSeed + hashName(slot.name))
|
|
280
|
+
}
|
|
281
|
+
slot.instance = cloned
|
|
282
|
+
return cloned
|
|
213
283
|
}
|
|
214
|
-
const ctx = await config.context(baseContext)
|
|
215
284
|
|
|
216
|
-
//
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
setSeedingActive(true)
|
|
223
|
-
if (config.onBeforeAll) {
|
|
224
|
-
await config.onBeforeAll()
|
|
225
|
-
}
|
|
285
|
+
// Lazy proxy: neither loads faker nor clones per-seeder Faker until a seeder accesses ctx.faker
|
|
286
|
+
const scopedFaker: Faker = new Proxy({} as Faker, {
|
|
287
|
+
get(_target, prop) {
|
|
288
|
+
const active = resolveActiveFaker()
|
|
289
|
+
const value = Reflect.get(active, prop, active)
|
|
290
|
+
return typeof value === 'function' && prop !== 'constructor' ? value.bind(active) : value
|
|
226
291
|
},
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
await config.onAfterAll()
|
|
231
|
-
}
|
|
292
|
+
set(_target, prop, value) {
|
|
293
|
+
const active = resolveActiveFaker()
|
|
294
|
+
return Reflect.set(active, prop, value, active)
|
|
232
295
|
},
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
options?: { name?: string },
|
|
248
|
-
): Promise<void> => {
|
|
249
|
-
const args = process.argv.slice(2)
|
|
250
|
-
const name = options?.name ?? 'Seeders'
|
|
251
|
-
|
|
252
|
-
// Show help if requested
|
|
253
|
-
if (args.includes('--help') || args.includes('-h')) {
|
|
254
|
-
console.log(`
|
|
255
|
-
${colorize('Usage:', 'cyan', 'bold')} bun run seed [options]
|
|
256
|
-
|
|
257
|
-
${colorize('Options:', 'white', 'bold')}
|
|
258
|
-
--help, -h Show this help message
|
|
259
|
-
--dry-run Show execution order without running seeders
|
|
296
|
+
has(_target, prop) {
|
|
297
|
+
return Reflect.has(resolveActiveFaker(), prop)
|
|
298
|
+
},
|
|
299
|
+
ownKeys() {
|
|
300
|
+
return Reflect.ownKeys(resolveActiveFaker())
|
|
301
|
+
},
|
|
302
|
+
getOwnPropertyDescriptor(_target, prop) {
|
|
303
|
+
const desc = Reflect.getOwnPropertyDescriptor(resolveActiveFaker(), prop)
|
|
304
|
+
return desc ? { ...desc, configurable: true } : undefined
|
|
305
|
+
},
|
|
306
|
+
getPrototypeOf() {
|
|
307
|
+
return Reflect.getPrototypeOf(resolveActiveFaker())
|
|
308
|
+
},
|
|
309
|
+
})
|
|
260
310
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
311
|
+
const mergedOptions: RunSeedersOptions =
|
|
312
|
+
defaultConcurrency !== undefined && options?.concurrency === undefined
|
|
313
|
+
? { ...options, concurrency: defaultConcurrency }
|
|
314
|
+
: (options ?? {})
|
|
265
315
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
316
|
+
return runSeedersCore(
|
|
317
|
+
seeders,
|
|
318
|
+
{
|
|
319
|
+
context: async () => {
|
|
320
|
+
const base: BaseSeederContext = {
|
|
321
|
+
faker: scopedFaker,
|
|
322
|
+
step: stepRunner.step,
|
|
323
|
+
expect,
|
|
324
|
+
}
|
|
325
|
+
if (!config?.context) return base as unknown as TContext
|
|
326
|
+
const extra = await config.context(base)
|
|
327
|
+
return extra === base
|
|
328
|
+
? (base as unknown as TContext)
|
|
329
|
+
: ({ ...base, ...extra } as unknown as TContext)
|
|
330
|
+
},
|
|
331
|
+
preloadCache: config?.cache?.getAll ? () => stepRunner.preload() : undefined,
|
|
332
|
+
flushCache: config?.cache?.setMany ? () => stepRunner.flush() : undefined,
|
|
333
|
+
createSeederScope: ({ name, fresh, concurrent }) => {
|
|
334
|
+
const stepScope = stepRunner.createScope({
|
|
335
|
+
seederName: name,
|
|
336
|
+
fresh,
|
|
337
|
+
prefix: concurrent ? name : undefined,
|
|
338
|
+
})
|
|
339
|
+
const slot: FakerSlot = {
|
|
340
|
+
name,
|
|
341
|
+
isolated: fakerSeed !== undefined || concurrent,
|
|
342
|
+
}
|
|
271
343
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
344
|
+
return {
|
|
345
|
+
record: stepScope.record,
|
|
346
|
+
run: <T>(fn: () => Promise<T>) =>
|
|
347
|
+
stepRunner.runWithScope(stepScope, () => fakerStorage.run(slot, fn)),
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
bannerName,
|
|
351
|
+
target: config?.target,
|
|
352
|
+
onBeforeAll: config?.onBeforeAll,
|
|
353
|
+
onAfterAll: config?.onAfterAll,
|
|
354
|
+
},
|
|
355
|
+
mergedOptions,
|
|
356
|
+
)
|
|
357
|
+
}
|
|
277
358
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const seederName = order[i]
|
|
283
|
-
if (!seederName) continue
|
|
284
|
-
const seeder = seederMap.get(seederName)
|
|
285
|
-
const deps = seeder?.dependsOn ?? []
|
|
286
|
-
const depsStr =
|
|
287
|
-
deps.length > 0 ? ` ${colorize(`(depends on: ${deps.join(', ')})`, 'dim')}` : ''
|
|
288
|
-
console.log(` ${i + 1}. ${seederName}${depsStr}`)
|
|
289
|
-
}
|
|
290
|
-
console.log('')
|
|
291
|
-
process.exit(0)
|
|
292
|
-
} catch (error) {
|
|
293
|
-
console.error(colorize('Failed to resolve dependencies:', 'red'))
|
|
294
|
-
console.error(` ${error instanceof Error ? error.message : String(error)}`)
|
|
295
|
-
process.exit(1)
|
|
296
|
-
}
|
|
297
|
-
}
|
|
359
|
+
const runSeeders = ((
|
|
360
|
+
seeders: readonly Seeder<TContext, string, unknown, string>[],
|
|
361
|
+
options?: RunSeedersOptions,
|
|
362
|
+
) => runInternal(seeders, options)) as SeederInstance<TContext>['runSeeders']
|
|
298
363
|
|
|
299
|
-
|
|
300
|
-
|
|
364
|
+
const runSeedersCli: SeederInstance<TContext>['runSeedersCli'] = async (seeders, options) => {
|
|
365
|
+
const bannerName = options?.name ?? 'Seeders'
|
|
366
|
+
const code = await runCli(
|
|
367
|
+
seeders,
|
|
368
|
+
(s, opts) =>
|
|
369
|
+
runInternal(s as readonly Seeder<TContext, string, unknown, string>[], opts, bannerName),
|
|
370
|
+
{
|
|
371
|
+
name: bannerName,
|
|
372
|
+
argv: options?.argv ?? process.argv.slice(2),
|
|
373
|
+
target: config?.target,
|
|
374
|
+
defaultConcurrency,
|
|
375
|
+
},
|
|
301
376
|
)
|
|
302
|
-
|
|
303
|
-
printResults: true,
|
|
304
|
-
verbose: true,
|
|
305
|
-
})
|
|
306
|
-
process.exit(success ? 0 : 1)
|
|
377
|
+
process.exit(code)
|
|
307
378
|
}
|
|
308
379
|
|
|
309
380
|
return {
|
|
@@ -313,3 +384,13 @@ Runs all ${seeders.length} seeders in dependency order.
|
|
|
313
384
|
isSeedingActive,
|
|
314
385
|
}
|
|
315
386
|
}
|
|
387
|
+
|
|
388
|
+
/** Small stable string hash (FNV-1a), so a seeder's faker seed depends only on its name */
|
|
389
|
+
function hashName(name: string): number {
|
|
390
|
+
let hash = 0x811c9dc5
|
|
391
|
+
for (let i = 0; i < name.length; i++) {
|
|
392
|
+
hash ^= name.charCodeAt(i)
|
|
393
|
+
hash = Math.imul(hash, 0x01000193)
|
|
394
|
+
}
|
|
395
|
+
return hash >>> 0
|
|
396
|
+
}
|
package/src/define-seeder.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { MaybePromise, Seeder, SeederConfig, SeederName } from './types.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Create a typed seeder definition
|
|
4
|
+
* Create a typed seeder definition factory bound to `TContext`.
|
|
5
5
|
*
|
|
6
6
|
* This is the core factory that creates a seeder from a configuration.
|
|
7
7
|
* The seeder can then be passed to `runSeeders` for execution.
|
|
@@ -30,17 +30,24 @@ export function createDefineSeeder<TContext>() {
|
|
|
30
30
|
return function defineSeeder<
|
|
31
31
|
TName extends string,
|
|
32
32
|
TOutput,
|
|
33
|
-
const TDeps extends readonly
|
|
34
|
-
>(
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
const TDeps extends readonly Seeder<TContext, string, unknown, string>[] = readonly [],
|
|
34
|
+
>(
|
|
35
|
+
config: SeederConfig<TContext, TName, TOutput, TDeps>,
|
|
36
|
+
): Seeder<TContext, TName, Awaited<TOutput>, SeederName<TDeps[number]>> {
|
|
37
|
+
// Extract and deduplicate names from seeder objects at runtime
|
|
38
|
+
const dependsOnNames = [
|
|
39
|
+
...new Set((config.dependsOn ?? []).map((seeder) => seeder.name)),
|
|
40
|
+
] as SeederName<TDeps[number]>[]
|
|
37
41
|
|
|
38
42
|
return {
|
|
39
43
|
name: config.name,
|
|
40
44
|
description: config.description,
|
|
41
45
|
dependsOn: dependsOnNames,
|
|
42
46
|
// Cast is safe because the deps object will have the correct shape at runtime
|
|
43
|
-
run: config.run as (
|
|
47
|
+
run: config.run as (
|
|
48
|
+
ctx: TContext,
|
|
49
|
+
deps: Record<string, unknown>,
|
|
50
|
+
) => MaybePromise<Awaited<TOutput>>,
|
|
44
51
|
}
|
|
45
52
|
}
|
|
46
53
|
}
|