@joakimbugge/typeorm-seeder 0.5.0 → 0.6.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/README.md +85 -9
- package/dist/index.cjs +41 -37
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +77 -46
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +77 -46
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +38 -34
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -31,16 +31,19 @@ interface SeedContext {
|
|
|
31
31
|
* Factory callback passed to `@Seed`. Receives the seed context and the partially built entity.
|
|
32
32
|
*
|
|
33
33
|
* Properties are seeded sequentially in declaration order, so any property declared above the
|
|
34
|
-
* current one is already set on `self` and can be read to derive the current value
|
|
34
|
+
* current one is already set on `self` and can be read to derive the current value.
|
|
35
|
+
*
|
|
36
|
+
* Annotate `self` with the entity class to get full type inference — TypeScript infers
|
|
37
|
+
* `TEntity` from the annotation, so no cast is needed:
|
|
35
38
|
*
|
|
36
39
|
* @example
|
|
37
40
|
* @Seed(() => faker.date.past())
|
|
38
41
|
* beginDate!: Date
|
|
39
42
|
*
|
|
40
|
-
* @Seed((_, self) => faker.date.future({ refDate:
|
|
43
|
+
* @Seed((_, self: MyEntity) => faker.date.future({ refDate: self.beginDate }))
|
|
41
44
|
* endDate!: Date
|
|
42
45
|
*/
|
|
43
|
-
type SeedFactory<T = unknown> = (context: SeedContext, self:
|
|
46
|
+
type SeedFactory<T = unknown, TEntity = any> = (context: SeedContext, self: TEntity) => T | Promise<T>;
|
|
44
47
|
/** Options for the `@Seed` decorator. */
|
|
45
48
|
interface SeedOptions {
|
|
46
49
|
/**
|
|
@@ -98,51 +101,103 @@ declare function Seed(options: SeedOptions): PropertyDecorator;
|
|
|
98
101
|
* @Seed(async ({ dataSource }) => dataSource.getRepository(Role).findOneByOrFail({ name: 'admin' }))
|
|
99
102
|
* role!: Role
|
|
100
103
|
*/
|
|
101
|
-
declare function Seed(factory: SeedFactory): PropertyDecorator;
|
|
104
|
+
declare function Seed<TEntity = any>(factory: SeedFactory<unknown, TEntity>): PropertyDecorator;
|
|
102
105
|
/** Marks a property with a factory callback and additional options. */
|
|
103
|
-
declare function Seed(factory: SeedFactory, options: SeedOptions): PropertyDecorator;
|
|
106
|
+
declare function Seed<TEntity = any>(factory: SeedFactory<unknown, TEntity>, options: SeedOptions): PropertyDecorator;
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/seed/creator.d.ts
|
|
109
|
+
/**
|
|
110
|
+
* Options for {@link create} and {@link createMany} on the single-class form.
|
|
111
|
+
* Extends {@link SeedContext} with a typed `values` override map.
|
|
112
|
+
*/
|
|
113
|
+
interface CreateOptions<T extends EntityInstance> extends SeedContext {
|
|
114
|
+
/**
|
|
115
|
+
* Property values to apply after all `@Seed` factories have run.
|
|
116
|
+
* Wins unconditionally — factories still execute but their output is overwritten.
|
|
117
|
+
* Also works for properties that have no `@Seed` decorator.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* const user = await dataSource.getRepository(User).findOneByOrFail({ name: 'Alice' })
|
|
121
|
+
* const post = await seed(Post).create({ values: { author: user } })
|
|
122
|
+
*/
|
|
123
|
+
values?: Partial<T>;
|
|
124
|
+
}
|
|
125
|
+
/** Options for {@link createMany}. Extends {@link SeedContext} with a required instance count. */
|
|
126
|
+
interface CreateManyOptions<T extends EntityInstance = EntityInstance> extends SeedContext {
|
|
127
|
+
count: number;
|
|
128
|
+
values?: Partial<T>;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Creates one entity instance in memory without persisting it.
|
|
132
|
+
*
|
|
133
|
+
* When passed an array of classes, relation seeding is disabled by default
|
|
134
|
+
* (pass `relations: true` in the context to override). Returns a tuple of
|
|
135
|
+
* instances in the same order as the input array.
|
|
136
|
+
*/
|
|
137
|
+
declare function create<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options?: CreateOptions<T>): Promise<T>;
|
|
138
|
+
declare function create<T extends readonly EntityConstructor[]>(EntityClasses: [...T], context?: SeedContext): Promise<MapToInstances<T>>;
|
|
139
|
+
/**
|
|
140
|
+
* Creates multiple entity instances in memory without persisting them.
|
|
141
|
+
*
|
|
142
|
+
* When passed an array of classes, returns a tuple of arrays — one per class — each
|
|
143
|
+
* containing `count` instances. Relation seeding is disabled by default for the
|
|
144
|
+
* array variant; pass `relations: true` in the options to override.
|
|
145
|
+
*/
|
|
146
|
+
declare function createMany<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: CreateManyOptions<T>): Promise<T[]>;
|
|
147
|
+
declare function createMany<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: CreateManyOptions): Promise<MapToInstanceArrays<T>>;
|
|
104
148
|
//#endregion
|
|
105
149
|
//#region src/seed/persist.d.ts
|
|
106
|
-
/** Options for {@link
|
|
107
|
-
interface
|
|
150
|
+
/** Options for {@link save}. Extends {@link SeedContext} with a required DataSource. */
|
|
151
|
+
interface SaveOptions<T extends EntityInstance = EntityInstance> extends SeedContext {
|
|
108
152
|
dataSource: DataSource;
|
|
153
|
+
/**
|
|
154
|
+
* Property values to apply to each entity after seeding and before persisting.
|
|
155
|
+
* Wins unconditionally over `@Seed` factory output — the factory still runs,
|
|
156
|
+
* but its result is overwritten. Also works for properties that have no `@Seed`
|
|
157
|
+
* decorator at all.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* const users = await dataSource.getRepository(User).find()
|
|
161
|
+
* const user = faker.helpers.arrayElement(users)
|
|
162
|
+
* await seed(Booking).saveMany(10, { dataSource, values: { user } })
|
|
163
|
+
*/
|
|
164
|
+
values?: Partial<T>;
|
|
109
165
|
}
|
|
110
|
-
/** Options for {@link
|
|
111
|
-
interface
|
|
166
|
+
/** Options for {@link saveMany}. Extends {@link SaveOptions} with a required instance count. */
|
|
167
|
+
interface SaveManyOptions<T extends EntityInstance = EntityInstance> extends SaveOptions<T> {
|
|
112
168
|
count: number;
|
|
113
169
|
}
|
|
114
170
|
/**
|
|
115
171
|
* Creates and persists a seed entity and all its seeded relations.
|
|
116
|
-
* Delegates to {@link saveManySeed} with `count: 1` and unwraps the result.
|
|
117
172
|
*/
|
|
118
|
-
declare function
|
|
173
|
+
declare function save<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: SaveOptions<T>): Promise<T>;
|
|
119
174
|
/**
|
|
120
175
|
* Creates and persists one instance of each entity class in the array.
|
|
121
176
|
* Relation seeding is disabled by default; pass `relations: true` to override.
|
|
122
177
|
*/
|
|
123
|
-
declare function
|
|
178
|
+
declare function save<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: SaveOptions): Promise<MapToInstances<T>>;
|
|
124
179
|
/**
|
|
125
180
|
* Creates and persists multiple seed entities of the same class.
|
|
126
|
-
* Applies the same logic as {@link
|
|
181
|
+
* Applies the same logic as {@link save} for each entity.
|
|
127
182
|
*/
|
|
128
|
-
declare function
|
|
183
|
+
declare function saveMany<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: SaveManyOptions<T>): Promise<T[]>;
|
|
129
184
|
/**
|
|
130
185
|
* Creates and persists multiple instances of each entity class in the array.
|
|
131
186
|
* Relation seeding is disabled by default; pass `relations: true` to override.
|
|
132
187
|
*/
|
|
133
|
-
declare function
|
|
188
|
+
declare function saveMany<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: SaveManyOptions): Promise<MapToInstanceArrays<T>>;
|
|
134
189
|
//#endregion
|
|
135
190
|
//#region src/seed/builder.d.ts
|
|
136
191
|
/** Seed builder for a single entity class. Returned by {@link seed} when passed one class. */
|
|
137
192
|
interface SingleSeed<T extends EntityInstance> {
|
|
138
193
|
/** Creates a single instance in memory without persisting. */
|
|
139
|
-
create(context?:
|
|
194
|
+
create(context?: CreateOptions<T>): Promise<T>;
|
|
140
195
|
/** Creates and persists a single instance. */
|
|
141
|
-
save(options:
|
|
196
|
+
save(options: SaveOptions<T>): Promise<T>;
|
|
142
197
|
/** Creates multiple instances in memory without persisting. */
|
|
143
|
-
createMany(count: number, context?:
|
|
198
|
+
createMany(count: number, context?: CreateOptions<T>): Promise<T[]>;
|
|
144
199
|
/** Creates and persists multiple instances. */
|
|
145
|
-
saveMany(count: number, options:
|
|
200
|
+
saveMany(count: number, options: SaveOptions<T>): Promise<T[]>;
|
|
146
201
|
}
|
|
147
202
|
/**
|
|
148
203
|
* Seed builder for multiple entity classes. Returned by {@link seed} when passed an array.
|
|
@@ -153,11 +208,11 @@ interface MultiSeed<T extends readonly EntityConstructor[]> {
|
|
|
153
208
|
/** Creates one instance of each class in memory without persisting. */
|
|
154
209
|
create(context?: SeedContext): Promise<MapToInstances<T>>;
|
|
155
210
|
/** Creates and persists one instance of each class. */
|
|
156
|
-
save(options:
|
|
211
|
+
save(options: SaveOptions): Promise<MapToInstances<T>>;
|
|
157
212
|
/** Creates `count` instances of each class in memory without persisting. */
|
|
158
213
|
createMany(count: number, context?: SeedContext): Promise<MapToInstanceArrays<T>>;
|
|
159
214
|
/** Creates and persists `count` instances of each class. */
|
|
160
|
-
saveMany(count: number, options:
|
|
215
|
+
saveMany(count: number, options: SaveOptions): Promise<MapToInstanceArrays<T>>;
|
|
161
216
|
}
|
|
162
217
|
/**
|
|
163
218
|
* Entry point for creating and persisting seed data.
|
|
@@ -184,30 +239,6 @@ interface MultiSeed<T extends readonly EntityConstructor[]> {
|
|
|
184
239
|
declare function seed<T extends EntityInstance>(EntityClass: EntityConstructor<T>): SingleSeed<T>;
|
|
185
240
|
declare function seed<T extends readonly EntityConstructor[]>(EntityClasses: [...T]): MultiSeed<T>;
|
|
186
241
|
//#endregion
|
|
187
|
-
//#region src/seed/creator.d.ts
|
|
188
|
-
/** Options for {@link createManySeed}. Extends {@link SeedContext} with a required instance count. */
|
|
189
|
-
interface CreateManySeedOptions extends SeedContext {
|
|
190
|
-
count: number;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Creates one entity instance in memory without persisting it.
|
|
194
|
-
*
|
|
195
|
-
* When passed an array of classes, relation seeding is disabled by default
|
|
196
|
-
* (pass `relations: true` in the context to override). Returns a tuple of
|
|
197
|
-
* instances in the same order as the input array.
|
|
198
|
-
*/
|
|
199
|
-
declare function createSeed<T extends EntityInstance>(EntityClass: EntityConstructor<T>, context?: SeedContext): Promise<T>;
|
|
200
|
-
declare function createSeed<T extends readonly EntityConstructor[]>(EntityClasses: [...T], context?: SeedContext): Promise<MapToInstances<T>>;
|
|
201
|
-
/**
|
|
202
|
-
* Creates multiple entity instances in memory without persisting them.
|
|
203
|
-
*
|
|
204
|
-
* When passed an array of classes, returns a tuple of arrays — one per class — each
|
|
205
|
-
* containing `count` instances. Relation seeding is disabled by default for the
|
|
206
|
-
* array variant; pass `relations: true` in the options to override.
|
|
207
|
-
*/
|
|
208
|
-
declare function createManySeed<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: CreateManySeedOptions): Promise<T[]>;
|
|
209
|
-
declare function createManySeed<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: CreateManySeedOptions): Promise<MapToInstanceArrays<T>>;
|
|
210
|
-
//#endregion
|
|
211
242
|
//#region src/seeder/decorator.d.ts
|
|
212
243
|
/**
|
|
213
244
|
* Interface that seeder classes must implement.
|
|
@@ -285,5 +316,5 @@ interface RunSeedersOptions extends SeedContext {
|
|
|
285
316
|
*/
|
|
286
317
|
declare function runSeeders(seeders: SeederCtor[], options?: RunSeedersOptions): Promise<void>;
|
|
287
318
|
//#endregion
|
|
288
|
-
export { type
|
|
319
|
+
export { type CreateManyOptions, type CreateOptions, type EntityConstructor, type EntityInstance, type RunSeedersOptions, type SaveManyOptions, type SaveOptions, Seed, type SeedContext, type SeedEntry, type SeedFactory, type SeedOptions, Seeder, type SeederCtor, type SeederInterface, type SeederOptions, create, createMany, runSeeders, save, saveMany, seed };
|
|
289
320
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/seed/registry.ts","../src/seed/decorator.ts","../src/seed/
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/seed/registry.ts","../src/seed/decorator.ts","../src/seed/creator.ts","../src/seed/persist.ts","../src/seed/builder.ts","../src/seeder/decorator.ts","../src/seeder/runner.ts"],"mappings":";;;;KAGY,cAAA;AAAZ;AAAA,KAGY,iBAAA,WAA4B,cAAA,GAAiB,cAAA,cAA4B,CAAA;;UAGpE,WAAA;EANS;AAG1B;;;;;;;;EAaE,UAAA,GAAa,UAAA;EAbyB;;;;;AAGxC;;;EAmBE,SAAA;AAAA;;;;;AAmBF;;;;;;;;;;;;KAAY,WAAA,gCACV,OAAA,EAAS,WAAA,EACT,IAAA,EAAM,OAAA,KACH,CAAA,GAAI,OAAA,CAAQ,CAAA;;UAGA,WAAA;EAJf;;;;EASA,KAAA;AAAA;AAAA,UAGe,SAAA;EACf,WAAA;;EAEA,OAAA,EAAS,WAAA;EACT,OAAA,EAAS,WAAA;AAAA;AAAA,KAGC,cAAA,oBAAkC,iBAAA,oBAChC,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,iBAAA,YAA6B,CAAA;AAAA,KAGhD,mBAAA,oBAAuC,iBAAA,oBACrC,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,iBAAA,YAA6B,CAAA;;;;;AAtE5D;;;;;AAGA;;;iBCOgB,IAAA,CAAA,GAAQ,iBAAA;;;;;;;;;;;ADJxB;iBCgBgB,IAAA,CAAK,OAAA,EAAS,WAAA,GAAc,iBAAA;;;;;;;;ADsB5C;;;;;;;;;iBCLgB,IAAA,eAAA,CAAoB,OAAA,EAAS,WAAA,UAAqB,OAAA,IAAW,iBAAA;;iBAE7D,IAAA,eAAA,CACd,OAAA,EAAS,WAAA,UAAqB,OAAA,GAC9B,OAAA,EAAS,WAAA,GACR,iBAAA;;;;;AD5CH;;UEWiB,aAAA,WAAwB,cAAA,UAAw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AA;AAAA,iBAC9E,IAAA,oBAAwB,iBAAA,GAAA,CAAqB,aAAA,MAAmB,CAAA,IAAK,SAAA,CAAU,CAAA;;;;;AJ9D/F;;;;;UKOiB,eAAA;EACf,GAAA,CAAI,OAAA,EAAS,WAAA,GAAc,OAAA;AAAA;;UAIZ,aAAA;ELToE;;;;;EKenF,YAAA,cAA0B,eAAA;AAAA;;;ALZ5B;;;;;;;;;AAsCA;;;iBKTgB,MAAA,CAAO,OAAA,GAAS,aAAA,GAAqB,cAAA;;;;KChCzC,UAAA,aAAuB,eAAA;;UAGlB,iBAAA,SAA0B,WAAA;ENNjB;;AAG1B;;;;EMUE,OAAA;ENVmF;EMYnF,QAAA,IAAY,MAAA,EAAQ,UAAA,YAAsB,OAAA;ENZ0C;EMcpF,OAAA,IAAW,MAAA,EAAQ,UAAA,EAAY,UAAA,oBAA8B,OAAA;ENdvB;EMgBtC,OAAA,IAAW,MAAA,EAAQ,UAAA,EAAY,KAAA,qBAA0B,OAAA;ENhB0B;EMkBnF,IAAA,IAAQ,MAAA,EAAQ,UAAA,eAAyB,OAAA;AAAA;ANf3C;;;;;;;;;AAsCA;;;;;;;;;AAtCA,iBMqFsB,UAAA,CACpB,OAAA,EAAS,UAAA,IACT,OAAA,GAAS,iBAAA,GACR,OAAA"}
|
package/dist/index.d.mts
CHANGED
|
@@ -31,16 +31,19 @@ interface SeedContext {
|
|
|
31
31
|
* Factory callback passed to `@Seed`. Receives the seed context and the partially built entity.
|
|
32
32
|
*
|
|
33
33
|
* Properties are seeded sequentially in declaration order, so any property declared above the
|
|
34
|
-
* current one is already set on `self` and can be read to derive the current value
|
|
34
|
+
* current one is already set on `self` and can be read to derive the current value.
|
|
35
|
+
*
|
|
36
|
+
* Annotate `self` with the entity class to get full type inference — TypeScript infers
|
|
37
|
+
* `TEntity` from the annotation, so no cast is needed:
|
|
35
38
|
*
|
|
36
39
|
* @example
|
|
37
40
|
* @Seed(() => faker.date.past())
|
|
38
41
|
* beginDate!: Date
|
|
39
42
|
*
|
|
40
|
-
* @Seed((_, self) => faker.date.future({ refDate:
|
|
43
|
+
* @Seed((_, self: MyEntity) => faker.date.future({ refDate: self.beginDate }))
|
|
41
44
|
* endDate!: Date
|
|
42
45
|
*/
|
|
43
|
-
type SeedFactory<T = unknown> = (context: SeedContext, self:
|
|
46
|
+
type SeedFactory<T = unknown, TEntity = any> = (context: SeedContext, self: TEntity) => T | Promise<T>;
|
|
44
47
|
/** Options for the `@Seed` decorator. */
|
|
45
48
|
interface SeedOptions {
|
|
46
49
|
/**
|
|
@@ -98,51 +101,103 @@ declare function Seed(options: SeedOptions): PropertyDecorator;
|
|
|
98
101
|
* @Seed(async ({ dataSource }) => dataSource.getRepository(Role).findOneByOrFail({ name: 'admin' }))
|
|
99
102
|
* role!: Role
|
|
100
103
|
*/
|
|
101
|
-
declare function Seed(factory: SeedFactory): PropertyDecorator;
|
|
104
|
+
declare function Seed<TEntity = any>(factory: SeedFactory<unknown, TEntity>): PropertyDecorator;
|
|
102
105
|
/** Marks a property with a factory callback and additional options. */
|
|
103
|
-
declare function Seed(factory: SeedFactory, options: SeedOptions): PropertyDecorator;
|
|
106
|
+
declare function Seed<TEntity = any>(factory: SeedFactory<unknown, TEntity>, options: SeedOptions): PropertyDecorator;
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/seed/creator.d.ts
|
|
109
|
+
/**
|
|
110
|
+
* Options for {@link create} and {@link createMany} on the single-class form.
|
|
111
|
+
* Extends {@link SeedContext} with a typed `values` override map.
|
|
112
|
+
*/
|
|
113
|
+
interface CreateOptions<T extends EntityInstance> extends SeedContext {
|
|
114
|
+
/**
|
|
115
|
+
* Property values to apply after all `@Seed` factories have run.
|
|
116
|
+
* Wins unconditionally — factories still execute but their output is overwritten.
|
|
117
|
+
* Also works for properties that have no `@Seed` decorator.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* const user = await dataSource.getRepository(User).findOneByOrFail({ name: 'Alice' })
|
|
121
|
+
* const post = await seed(Post).create({ values: { author: user } })
|
|
122
|
+
*/
|
|
123
|
+
values?: Partial<T>;
|
|
124
|
+
}
|
|
125
|
+
/** Options for {@link createMany}. Extends {@link SeedContext} with a required instance count. */
|
|
126
|
+
interface CreateManyOptions<T extends EntityInstance = EntityInstance> extends SeedContext {
|
|
127
|
+
count: number;
|
|
128
|
+
values?: Partial<T>;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Creates one entity instance in memory without persisting it.
|
|
132
|
+
*
|
|
133
|
+
* When passed an array of classes, relation seeding is disabled by default
|
|
134
|
+
* (pass `relations: true` in the context to override). Returns a tuple of
|
|
135
|
+
* instances in the same order as the input array.
|
|
136
|
+
*/
|
|
137
|
+
declare function create<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options?: CreateOptions<T>): Promise<T>;
|
|
138
|
+
declare function create<T extends readonly EntityConstructor[]>(EntityClasses: [...T], context?: SeedContext): Promise<MapToInstances<T>>;
|
|
139
|
+
/**
|
|
140
|
+
* Creates multiple entity instances in memory without persisting them.
|
|
141
|
+
*
|
|
142
|
+
* When passed an array of classes, returns a tuple of arrays — one per class — each
|
|
143
|
+
* containing `count` instances. Relation seeding is disabled by default for the
|
|
144
|
+
* array variant; pass `relations: true` in the options to override.
|
|
145
|
+
*/
|
|
146
|
+
declare function createMany<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: CreateManyOptions<T>): Promise<T[]>;
|
|
147
|
+
declare function createMany<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: CreateManyOptions): Promise<MapToInstanceArrays<T>>;
|
|
104
148
|
//#endregion
|
|
105
149
|
//#region src/seed/persist.d.ts
|
|
106
|
-
/** Options for {@link
|
|
107
|
-
interface
|
|
150
|
+
/** Options for {@link save}. Extends {@link SeedContext} with a required DataSource. */
|
|
151
|
+
interface SaveOptions<T extends EntityInstance = EntityInstance> extends SeedContext {
|
|
108
152
|
dataSource: DataSource;
|
|
153
|
+
/**
|
|
154
|
+
* Property values to apply to each entity after seeding and before persisting.
|
|
155
|
+
* Wins unconditionally over `@Seed` factory output — the factory still runs,
|
|
156
|
+
* but its result is overwritten. Also works for properties that have no `@Seed`
|
|
157
|
+
* decorator at all.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* const users = await dataSource.getRepository(User).find()
|
|
161
|
+
* const user = faker.helpers.arrayElement(users)
|
|
162
|
+
* await seed(Booking).saveMany(10, { dataSource, values: { user } })
|
|
163
|
+
*/
|
|
164
|
+
values?: Partial<T>;
|
|
109
165
|
}
|
|
110
|
-
/** Options for {@link
|
|
111
|
-
interface
|
|
166
|
+
/** Options for {@link saveMany}. Extends {@link SaveOptions} with a required instance count. */
|
|
167
|
+
interface SaveManyOptions<T extends EntityInstance = EntityInstance> extends SaveOptions<T> {
|
|
112
168
|
count: number;
|
|
113
169
|
}
|
|
114
170
|
/**
|
|
115
171
|
* Creates and persists a seed entity and all its seeded relations.
|
|
116
|
-
* Delegates to {@link saveManySeed} with `count: 1` and unwraps the result.
|
|
117
172
|
*/
|
|
118
|
-
declare function
|
|
173
|
+
declare function save<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: SaveOptions<T>): Promise<T>;
|
|
119
174
|
/**
|
|
120
175
|
* Creates and persists one instance of each entity class in the array.
|
|
121
176
|
* Relation seeding is disabled by default; pass `relations: true` to override.
|
|
122
177
|
*/
|
|
123
|
-
declare function
|
|
178
|
+
declare function save<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: SaveOptions): Promise<MapToInstances<T>>;
|
|
124
179
|
/**
|
|
125
180
|
* Creates and persists multiple seed entities of the same class.
|
|
126
|
-
* Applies the same logic as {@link
|
|
181
|
+
* Applies the same logic as {@link save} for each entity.
|
|
127
182
|
*/
|
|
128
|
-
declare function
|
|
183
|
+
declare function saveMany<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: SaveManyOptions<T>): Promise<T[]>;
|
|
129
184
|
/**
|
|
130
185
|
* Creates and persists multiple instances of each entity class in the array.
|
|
131
186
|
* Relation seeding is disabled by default; pass `relations: true` to override.
|
|
132
187
|
*/
|
|
133
|
-
declare function
|
|
188
|
+
declare function saveMany<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: SaveManyOptions): Promise<MapToInstanceArrays<T>>;
|
|
134
189
|
//#endregion
|
|
135
190
|
//#region src/seed/builder.d.ts
|
|
136
191
|
/** Seed builder for a single entity class. Returned by {@link seed} when passed one class. */
|
|
137
192
|
interface SingleSeed<T extends EntityInstance> {
|
|
138
193
|
/** Creates a single instance in memory without persisting. */
|
|
139
|
-
create(context?:
|
|
194
|
+
create(context?: CreateOptions<T>): Promise<T>;
|
|
140
195
|
/** Creates and persists a single instance. */
|
|
141
|
-
save(options:
|
|
196
|
+
save(options: SaveOptions<T>): Promise<T>;
|
|
142
197
|
/** Creates multiple instances in memory without persisting. */
|
|
143
|
-
createMany(count: number, context?:
|
|
198
|
+
createMany(count: number, context?: CreateOptions<T>): Promise<T[]>;
|
|
144
199
|
/** Creates and persists multiple instances. */
|
|
145
|
-
saveMany(count: number, options:
|
|
200
|
+
saveMany(count: number, options: SaveOptions<T>): Promise<T[]>;
|
|
146
201
|
}
|
|
147
202
|
/**
|
|
148
203
|
* Seed builder for multiple entity classes. Returned by {@link seed} when passed an array.
|
|
@@ -153,11 +208,11 @@ interface MultiSeed<T extends readonly EntityConstructor[]> {
|
|
|
153
208
|
/** Creates one instance of each class in memory without persisting. */
|
|
154
209
|
create(context?: SeedContext): Promise<MapToInstances<T>>;
|
|
155
210
|
/** Creates and persists one instance of each class. */
|
|
156
|
-
save(options:
|
|
211
|
+
save(options: SaveOptions): Promise<MapToInstances<T>>;
|
|
157
212
|
/** Creates `count` instances of each class in memory without persisting. */
|
|
158
213
|
createMany(count: number, context?: SeedContext): Promise<MapToInstanceArrays<T>>;
|
|
159
214
|
/** Creates and persists `count` instances of each class. */
|
|
160
|
-
saveMany(count: number, options:
|
|
215
|
+
saveMany(count: number, options: SaveOptions): Promise<MapToInstanceArrays<T>>;
|
|
161
216
|
}
|
|
162
217
|
/**
|
|
163
218
|
* Entry point for creating and persisting seed data.
|
|
@@ -184,30 +239,6 @@ interface MultiSeed<T extends readonly EntityConstructor[]> {
|
|
|
184
239
|
declare function seed<T extends EntityInstance>(EntityClass: EntityConstructor<T>): SingleSeed<T>;
|
|
185
240
|
declare function seed<T extends readonly EntityConstructor[]>(EntityClasses: [...T]): MultiSeed<T>;
|
|
186
241
|
//#endregion
|
|
187
|
-
//#region src/seed/creator.d.ts
|
|
188
|
-
/** Options for {@link createManySeed}. Extends {@link SeedContext} with a required instance count. */
|
|
189
|
-
interface CreateManySeedOptions extends SeedContext {
|
|
190
|
-
count: number;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Creates one entity instance in memory without persisting it.
|
|
194
|
-
*
|
|
195
|
-
* When passed an array of classes, relation seeding is disabled by default
|
|
196
|
-
* (pass `relations: true` in the context to override). Returns a tuple of
|
|
197
|
-
* instances in the same order as the input array.
|
|
198
|
-
*/
|
|
199
|
-
declare function createSeed<T extends EntityInstance>(EntityClass: EntityConstructor<T>, context?: SeedContext): Promise<T>;
|
|
200
|
-
declare function createSeed<T extends readonly EntityConstructor[]>(EntityClasses: [...T], context?: SeedContext): Promise<MapToInstances<T>>;
|
|
201
|
-
/**
|
|
202
|
-
* Creates multiple entity instances in memory without persisting them.
|
|
203
|
-
*
|
|
204
|
-
* When passed an array of classes, returns a tuple of arrays — one per class — each
|
|
205
|
-
* containing `count` instances. Relation seeding is disabled by default for the
|
|
206
|
-
* array variant; pass `relations: true` in the options to override.
|
|
207
|
-
*/
|
|
208
|
-
declare function createManySeed<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: CreateManySeedOptions): Promise<T[]>;
|
|
209
|
-
declare function createManySeed<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: CreateManySeedOptions): Promise<MapToInstanceArrays<T>>;
|
|
210
|
-
//#endregion
|
|
211
242
|
//#region src/seeder/decorator.d.ts
|
|
212
243
|
/**
|
|
213
244
|
* Interface that seeder classes must implement.
|
|
@@ -285,5 +316,5 @@ interface RunSeedersOptions extends SeedContext {
|
|
|
285
316
|
*/
|
|
286
317
|
declare function runSeeders(seeders: SeederCtor[], options?: RunSeedersOptions): Promise<void>;
|
|
287
318
|
//#endregion
|
|
288
|
-
export { type
|
|
319
|
+
export { type CreateManyOptions, type CreateOptions, type EntityConstructor, type EntityInstance, type RunSeedersOptions, type SaveManyOptions, type SaveOptions, Seed, type SeedContext, type SeedEntry, type SeedFactory, type SeedOptions, Seeder, type SeederCtor, type SeederInterface, type SeederOptions, create, createMany, runSeeders, save, saveMany, seed };
|
|
289
320
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/seed/registry.ts","../src/seed/decorator.ts","../src/seed/
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/seed/registry.ts","../src/seed/decorator.ts","../src/seed/creator.ts","../src/seed/persist.ts","../src/seed/builder.ts","../src/seeder/decorator.ts","../src/seeder/runner.ts"],"mappings":";;;;KAGY,cAAA;AAAZ;AAAA,KAGY,iBAAA,WAA4B,cAAA,GAAiB,cAAA,cAA4B,CAAA;;UAGpE,WAAA;EANS;AAG1B;;;;;;;;EAaE,UAAA,GAAa,UAAA;EAbyB;;;;;AAGxC;;;EAmBE,SAAA;AAAA;;;;;AAmBF;;;;;;;;;;;;KAAY,WAAA,gCACV,OAAA,EAAS,WAAA,EACT,IAAA,EAAM,OAAA,KACH,CAAA,GAAI,OAAA,CAAQ,CAAA;;UAGA,WAAA;EAJf;;;;EASA,KAAA;AAAA;AAAA,UAGe,SAAA;EACf,WAAA;;EAEA,OAAA,EAAS,WAAA;EACT,OAAA,EAAS,WAAA;AAAA;AAAA,KAGC,cAAA,oBAAkC,iBAAA,oBAChC,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,iBAAA,YAA6B,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AAS,UAAA,IACT,OAAA,GAAS,iBAAA,GACR,OAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -68,7 +68,7 @@ function getClassHierarchy(target) {
|
|
|
68
68
|
* 3. Bare relation decorators (`@Seed()` without a factory) — skipped when `relations` is `false`,
|
|
69
69
|
* and also skipped for any related class already present in the ancestor chain (circular guard).
|
|
70
70
|
*/
|
|
71
|
-
async function
|
|
71
|
+
async function createOne(EntityClass, context) {
|
|
72
72
|
const instance = new EntityClass();
|
|
73
73
|
const ancestors = getAncestors(context);
|
|
74
74
|
const childContext = withAncestor(context, EntityClass);
|
|
@@ -85,7 +85,7 @@ async function createOneSeed(EntityClass, context) {
|
|
|
85
85
|
if (seededProperties.has(embedded.propertyName)) continue;
|
|
86
86
|
const EmbeddedClass = embedded.type();
|
|
87
87
|
if (getSeeds(EmbeddedClass).length > 0) {
|
|
88
|
-
record[embedded.propertyName] = await
|
|
88
|
+
record[embedded.propertyName] = await createOne(EmbeddedClass, context);
|
|
89
89
|
seededProperties.add(embedded.propertyName);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
@@ -96,38 +96,39 @@ async function createOneSeed(EntityClass, context) {
|
|
|
96
96
|
if (!relation || typeof relation.type !== "function") continue;
|
|
97
97
|
const RelatedClass = relation.type();
|
|
98
98
|
if (ancestors.has(RelatedClass)) continue;
|
|
99
|
-
if (relation.relationType === "one-to-many" || relation.relationType === "many-to-many") record[propertyKey] = await
|
|
99
|
+
if (relation.relationType === "one-to-many" || relation.relationType === "many-to-many") record[propertyKey] = await createMany(RelatedClass, {
|
|
100
100
|
count: options.count ?? 1,
|
|
101
101
|
...childContext
|
|
102
102
|
});
|
|
103
|
-
else record[propertyKey] = await
|
|
103
|
+
else record[propertyKey] = await createOne(RelatedClass, childContext);
|
|
104
104
|
seededProperties.add(propertyKey);
|
|
105
105
|
}
|
|
106
106
|
return instance;
|
|
107
107
|
}
|
|
108
|
-
async function
|
|
108
|
+
async function create(classOrClasses, options = {}) {
|
|
109
109
|
if (Array.isArray(classOrClasses)) {
|
|
110
110
|
const effectiveContext = {
|
|
111
111
|
relations: false,
|
|
112
|
-
...
|
|
112
|
+
...options
|
|
113
113
|
};
|
|
114
|
-
return await Promise.all(classOrClasses.map((cls) =>
|
|
114
|
+
return await Promise.all(classOrClasses.map((cls) => createOne(cls, effectiveContext)));
|
|
115
115
|
}
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return entity;
|
|
116
|
+
const { values, ...context } = options;
|
|
117
|
+
const instance = await createOne(classOrClasses, context);
|
|
118
|
+
if (values) Object.assign(instance, values);
|
|
119
|
+
return instance;
|
|
121
120
|
}
|
|
122
|
-
async function
|
|
121
|
+
async function createMany(classOrClasses, { count, values, ...context }) {
|
|
123
122
|
if (Array.isArray(classOrClasses)) {
|
|
124
123
|
const effectiveContext = {
|
|
125
124
|
relations: false,
|
|
126
125
|
...context
|
|
127
126
|
};
|
|
128
|
-
return await Promise.all(classOrClasses.map((cls) => Promise.all(Array.from({ length: count }, () =>
|
|
127
|
+
return await Promise.all(classOrClasses.map((cls) => Promise.all(Array.from({ length: count }, () => createOne(cls, effectiveContext)))));
|
|
129
128
|
}
|
|
130
|
-
|
|
129
|
+
const instances = await Promise.all(Array.from({ length: count }, () => createOne(classOrClasses, context)));
|
|
130
|
+
if (values) instances.forEach((e) => Object.assign(e, values));
|
|
131
|
+
return instances;
|
|
131
132
|
}
|
|
132
133
|
//#endregion
|
|
133
134
|
//#region src/seed/persist.ts
|
|
@@ -177,40 +178,43 @@ function enableCascadeInsert(EntityClass, dataSource) {
|
|
|
177
178
|
function restoreCascade(states) {
|
|
178
179
|
for (const { relation, original } of states) relation.isCascadeInsert = original;
|
|
179
180
|
}
|
|
180
|
-
async function
|
|
181
|
+
async function save(classOrClasses, options) {
|
|
181
182
|
if (Array.isArray(classOrClasses)) {
|
|
182
183
|
const effectiveOptions = {
|
|
183
184
|
relations: false,
|
|
184
185
|
...options,
|
|
185
186
|
count: 1
|
|
186
187
|
};
|
|
187
|
-
return await Promise.all(classOrClasses.map((cls) =>
|
|
188
|
+
return await Promise.all(classOrClasses.map((cls) => saveBatch(cls, effectiveOptions).then(([entity]) => entity)));
|
|
188
189
|
}
|
|
189
|
-
const [entity] = await
|
|
190
|
+
const [entity] = await saveBatch(classOrClasses, {
|
|
190
191
|
...options,
|
|
191
192
|
count: 1
|
|
192
193
|
});
|
|
193
194
|
return entity;
|
|
194
195
|
}
|
|
195
|
-
async function
|
|
196
|
+
async function saveMany(classOrClasses, options) {
|
|
196
197
|
if (Array.isArray(classOrClasses)) {
|
|
197
198
|
const effectiveOptions = {
|
|
198
199
|
relations: false,
|
|
199
200
|
...options
|
|
200
201
|
};
|
|
201
|
-
return await Promise.all(classOrClasses.map((cls) =>
|
|
202
|
+
return await Promise.all(classOrClasses.map((cls) => saveBatch(cls, effectiveOptions)));
|
|
202
203
|
}
|
|
203
|
-
return await
|
|
204
|
+
return await saveBatch(classOrClasses, options);
|
|
204
205
|
}
|
|
205
206
|
/**
|
|
206
|
-
* Creates and persists `count` instances of a single entity class
|
|
207
|
+
* Creates and persists `count` instances of a single entity class in one batched
|
|
208
|
+
* `repository.save()` call. Batching is intentional — it is more efficient than
|
|
209
|
+
* saving each instance individually, as TypeORM can consolidate the inserts.
|
|
210
|
+
*
|
|
207
211
|
* Enables cascade inserts on every entity class in the object graph before saving,
|
|
208
212
|
* then restores the original flags — regardless of whether the save succeeds or fails.
|
|
209
213
|
*/
|
|
210
|
-
async function
|
|
214
|
+
async function saveBatch(EntityClass, options) {
|
|
211
215
|
const { count, dataSource } = options;
|
|
212
216
|
if (count === 0) return [];
|
|
213
|
-
const entities = await
|
|
217
|
+
const entities = await createMany(EntityClass, options);
|
|
214
218
|
const visited = /* @__PURE__ */ new Set();
|
|
215
219
|
const states = entities.flatMap((entity) => collectEntityClasses(entity, visited)).flatMap((cls) => enableCascadeInsert(cls, dataSource));
|
|
216
220
|
try {
|
|
@@ -225,13 +229,13 @@ function seed(classOrClasses) {
|
|
|
225
229
|
if (Array.isArray(classOrClasses)) {
|
|
226
230
|
const classes = classOrClasses;
|
|
227
231
|
return {
|
|
228
|
-
create: (context) =>
|
|
229
|
-
save: (options) =>
|
|
230
|
-
createMany: (count, context) =>
|
|
232
|
+
create: (context) => create(classes, context),
|
|
233
|
+
save: (options) => save(classes, options),
|
|
234
|
+
createMany: (count, context) => createMany(classes, {
|
|
231
235
|
count,
|
|
232
236
|
...context
|
|
233
237
|
}),
|
|
234
|
-
saveMany: (count, options) =>
|
|
238
|
+
saveMany: (count, options) => saveMany(classes, {
|
|
235
239
|
count,
|
|
236
240
|
...options
|
|
237
241
|
})
|
|
@@ -239,13 +243,13 @@ function seed(classOrClasses) {
|
|
|
239
243
|
}
|
|
240
244
|
const EntityClass = classOrClasses;
|
|
241
245
|
return {
|
|
242
|
-
create: (
|
|
243
|
-
save: (options) =>
|
|
244
|
-
createMany: (count,
|
|
246
|
+
create: (options) => create(EntityClass, options),
|
|
247
|
+
save: (options) => save(EntityClass, options),
|
|
248
|
+
createMany: (count, options) => createMany(EntityClass, {
|
|
245
249
|
count,
|
|
246
|
-
...
|
|
250
|
+
...options
|
|
247
251
|
}),
|
|
248
|
-
saveMany: (count, options) =>
|
|
252
|
+
saveMany: (count, options) => saveMany(EntityClass, {
|
|
249
253
|
count,
|
|
250
254
|
...options
|
|
251
255
|
})
|
|
@@ -354,6 +358,6 @@ async function runSeeders(seeders, options = {}) {
|
|
|
354
358
|
}
|
|
355
359
|
}
|
|
356
360
|
//#endregion
|
|
357
|
-
export { Seed, Seeder,
|
|
361
|
+
export { Seed, Seeder, create, createMany, runSeeders, save, saveMany, seed };
|
|
358
362
|
|
|
359
363
|
//# sourceMappingURL=index.mjs.map
|