@joakimbugge/typeorm-seeder 0.4.1 → 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 +42 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +88 -45
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +88 -45
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +39 -35
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -27,8 +27,23 @@ interface SeedContext {
|
|
|
27
27
|
*/
|
|
28
28
|
relations?: boolean;
|
|
29
29
|
}
|
|
30
|
-
/**
|
|
31
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Factory callback passed to `@Seed`. Receives the seed context and the partially built entity.
|
|
32
|
+
*
|
|
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.
|
|
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:
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* @Seed(() => faker.date.past())
|
|
41
|
+
* beginDate!: Date
|
|
42
|
+
*
|
|
43
|
+
* @Seed((_, self: MyEntity) => faker.date.future({ refDate: self.beginDate }))
|
|
44
|
+
* endDate!: Date
|
|
45
|
+
*/
|
|
46
|
+
type SeedFactory<T = unknown, TEntity = any> = (context: SeedContext, self: TEntity) => T | Promise<T>;
|
|
32
47
|
/** Options for the `@Seed` decorator. */
|
|
33
48
|
interface SeedOptions {
|
|
34
49
|
/**
|
|
@@ -86,51 +101,103 @@ declare function Seed(options: SeedOptions): PropertyDecorator;
|
|
|
86
101
|
* @Seed(async ({ dataSource }) => dataSource.getRepository(Role).findOneByOrFail({ name: 'admin' }))
|
|
87
102
|
* role!: Role
|
|
88
103
|
*/
|
|
89
|
-
declare function Seed(factory: SeedFactory): PropertyDecorator;
|
|
104
|
+
declare function Seed<TEntity = any>(factory: SeedFactory<unknown, TEntity>): PropertyDecorator;
|
|
90
105
|
/** Marks a property with a factory callback and additional options. */
|
|
91
|
-
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>>;
|
|
92
148
|
//#endregion
|
|
93
149
|
//#region src/seed/persist.d.ts
|
|
94
|
-
/** Options for {@link
|
|
95
|
-
interface
|
|
150
|
+
/** Options for {@link save}. Extends {@link SeedContext} with a required DataSource. */
|
|
151
|
+
interface SaveOptions<T extends EntityInstance = EntityInstance> extends SeedContext {
|
|
96
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>;
|
|
97
165
|
}
|
|
98
|
-
/** Options for {@link
|
|
99
|
-
interface
|
|
166
|
+
/** Options for {@link saveMany}. Extends {@link SaveOptions} with a required instance count. */
|
|
167
|
+
interface SaveManyOptions<T extends EntityInstance = EntityInstance> extends SaveOptions<T> {
|
|
100
168
|
count: number;
|
|
101
169
|
}
|
|
102
170
|
/**
|
|
103
171
|
* Creates and persists a seed entity and all its seeded relations.
|
|
104
|
-
* Delegates to {@link saveManySeed} with `count: 1` and unwraps the result.
|
|
105
172
|
*/
|
|
106
|
-
declare function
|
|
173
|
+
declare function save<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: SaveOptions<T>): Promise<T>;
|
|
107
174
|
/**
|
|
108
175
|
* Creates and persists one instance of each entity class in the array.
|
|
109
176
|
* Relation seeding is disabled by default; pass `relations: true` to override.
|
|
110
177
|
*/
|
|
111
|
-
declare function
|
|
178
|
+
declare function save<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: SaveOptions): Promise<MapToInstances<T>>;
|
|
112
179
|
/**
|
|
113
180
|
* Creates and persists multiple seed entities of the same class.
|
|
114
|
-
* Applies the same logic as {@link
|
|
181
|
+
* Applies the same logic as {@link save} for each entity.
|
|
115
182
|
*/
|
|
116
|
-
declare function
|
|
183
|
+
declare function saveMany<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: SaveManyOptions<T>): Promise<T[]>;
|
|
117
184
|
/**
|
|
118
185
|
* Creates and persists multiple instances of each entity class in the array.
|
|
119
186
|
* Relation seeding is disabled by default; pass `relations: true` to override.
|
|
120
187
|
*/
|
|
121
|
-
declare function
|
|
188
|
+
declare function saveMany<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: SaveManyOptions): Promise<MapToInstanceArrays<T>>;
|
|
122
189
|
//#endregion
|
|
123
190
|
//#region src/seed/builder.d.ts
|
|
124
191
|
/** Seed builder for a single entity class. Returned by {@link seed} when passed one class. */
|
|
125
192
|
interface SingleSeed<T extends EntityInstance> {
|
|
126
193
|
/** Creates a single instance in memory without persisting. */
|
|
127
|
-
create(context?:
|
|
194
|
+
create(context?: CreateOptions<T>): Promise<T>;
|
|
128
195
|
/** Creates and persists a single instance. */
|
|
129
|
-
save(options:
|
|
196
|
+
save(options: SaveOptions<T>): Promise<T>;
|
|
130
197
|
/** Creates multiple instances in memory without persisting. */
|
|
131
|
-
createMany(count: number, context?:
|
|
198
|
+
createMany(count: number, context?: CreateOptions<T>): Promise<T[]>;
|
|
132
199
|
/** Creates and persists multiple instances. */
|
|
133
|
-
saveMany(count: number, options:
|
|
200
|
+
saveMany(count: number, options: SaveOptions<T>): Promise<T[]>;
|
|
134
201
|
}
|
|
135
202
|
/**
|
|
136
203
|
* Seed builder for multiple entity classes. Returned by {@link seed} when passed an array.
|
|
@@ -141,11 +208,11 @@ interface MultiSeed<T extends readonly EntityConstructor[]> {
|
|
|
141
208
|
/** Creates one instance of each class in memory without persisting. */
|
|
142
209
|
create(context?: SeedContext): Promise<MapToInstances<T>>;
|
|
143
210
|
/** Creates and persists one instance of each class. */
|
|
144
|
-
save(options:
|
|
211
|
+
save(options: SaveOptions): Promise<MapToInstances<T>>;
|
|
145
212
|
/** Creates `count` instances of each class in memory without persisting. */
|
|
146
213
|
createMany(count: number, context?: SeedContext): Promise<MapToInstanceArrays<T>>;
|
|
147
214
|
/** Creates and persists `count` instances of each class. */
|
|
148
|
-
saveMany(count: number, options:
|
|
215
|
+
saveMany(count: number, options: SaveOptions): Promise<MapToInstanceArrays<T>>;
|
|
149
216
|
}
|
|
150
217
|
/**
|
|
151
218
|
* Entry point for creating and persisting seed data.
|
|
@@ -172,30 +239,6 @@ interface MultiSeed<T extends readonly EntityConstructor[]> {
|
|
|
172
239
|
declare function seed<T extends EntityInstance>(EntityClass: EntityConstructor<T>): SingleSeed<T>;
|
|
173
240
|
declare function seed<T extends readonly EntityConstructor[]>(EntityClasses: [...T]): MultiSeed<T>;
|
|
174
241
|
//#endregion
|
|
175
|
-
//#region src/seed/creator.d.ts
|
|
176
|
-
/** Options for {@link createManySeed}. Extends {@link SeedContext} with a required instance count. */
|
|
177
|
-
interface CreateManySeedOptions extends SeedContext {
|
|
178
|
-
count: number;
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Creates one entity instance in memory without persisting it.
|
|
182
|
-
*
|
|
183
|
-
* When passed an array of classes, relation seeding is disabled by default
|
|
184
|
-
* (pass `relations: true` in the context to override). Returns a tuple of
|
|
185
|
-
* instances in the same order as the input array.
|
|
186
|
-
*/
|
|
187
|
-
declare function createSeed<T extends EntityInstance>(EntityClass: EntityConstructor<T>, context?: SeedContext): Promise<T>;
|
|
188
|
-
declare function createSeed<T extends readonly EntityConstructor[]>(EntityClasses: [...T], context?: SeedContext): Promise<MapToInstances<T>>;
|
|
189
|
-
/**
|
|
190
|
-
* Creates multiple entity instances in memory without persisting them.
|
|
191
|
-
*
|
|
192
|
-
* When passed an array of classes, returns a tuple of arrays — one per class — each
|
|
193
|
-
* containing `count` instances. Relation seeding is disabled by default for the
|
|
194
|
-
* array variant; pass `relations: true` in the options to override.
|
|
195
|
-
*/
|
|
196
|
-
declare function createManySeed<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: CreateManySeedOptions): Promise<T[]>;
|
|
197
|
-
declare function createManySeed<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: CreateManySeedOptions): Promise<MapToInstanceArrays<T>>;
|
|
198
|
-
//#endregion
|
|
199
242
|
//#region src/seeder/decorator.d.ts
|
|
200
243
|
/**
|
|
201
244
|
* Interface that seeder classes must implement.
|
|
@@ -273,5 +316,5 @@ interface RunSeedersOptions extends SeedContext {
|
|
|
273
316
|
*/
|
|
274
317
|
declare function runSeeders(seeders: SeederCtor[], options?: RunSeedersOptions): Promise<void>;
|
|
275
318
|
//#endregion
|
|
276
|
-
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 };
|
|
277
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,UAAwB,WAAA;EFXvC;;AAG1B;;;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AAwB,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
|
@@ -27,8 +27,23 @@ interface SeedContext {
|
|
|
27
27
|
*/
|
|
28
28
|
relations?: boolean;
|
|
29
29
|
}
|
|
30
|
-
/**
|
|
31
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Factory callback passed to `@Seed`. Receives the seed context and the partially built entity.
|
|
32
|
+
*
|
|
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.
|
|
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:
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* @Seed(() => faker.date.past())
|
|
41
|
+
* beginDate!: Date
|
|
42
|
+
*
|
|
43
|
+
* @Seed((_, self: MyEntity) => faker.date.future({ refDate: self.beginDate }))
|
|
44
|
+
* endDate!: Date
|
|
45
|
+
*/
|
|
46
|
+
type SeedFactory<T = unknown, TEntity = any> = (context: SeedContext, self: TEntity) => T | Promise<T>;
|
|
32
47
|
/** Options for the `@Seed` decorator. */
|
|
33
48
|
interface SeedOptions {
|
|
34
49
|
/**
|
|
@@ -86,51 +101,103 @@ declare function Seed(options: SeedOptions): PropertyDecorator;
|
|
|
86
101
|
* @Seed(async ({ dataSource }) => dataSource.getRepository(Role).findOneByOrFail({ name: 'admin' }))
|
|
87
102
|
* role!: Role
|
|
88
103
|
*/
|
|
89
|
-
declare function Seed(factory: SeedFactory): PropertyDecorator;
|
|
104
|
+
declare function Seed<TEntity = any>(factory: SeedFactory<unknown, TEntity>): PropertyDecorator;
|
|
90
105
|
/** Marks a property with a factory callback and additional options. */
|
|
91
|
-
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>>;
|
|
92
148
|
//#endregion
|
|
93
149
|
//#region src/seed/persist.d.ts
|
|
94
|
-
/** Options for {@link
|
|
95
|
-
interface
|
|
150
|
+
/** Options for {@link save}. Extends {@link SeedContext} with a required DataSource. */
|
|
151
|
+
interface SaveOptions<T extends EntityInstance = EntityInstance> extends SeedContext {
|
|
96
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>;
|
|
97
165
|
}
|
|
98
|
-
/** Options for {@link
|
|
99
|
-
interface
|
|
166
|
+
/** Options for {@link saveMany}. Extends {@link SaveOptions} with a required instance count. */
|
|
167
|
+
interface SaveManyOptions<T extends EntityInstance = EntityInstance> extends SaveOptions<T> {
|
|
100
168
|
count: number;
|
|
101
169
|
}
|
|
102
170
|
/**
|
|
103
171
|
* Creates and persists a seed entity and all its seeded relations.
|
|
104
|
-
* Delegates to {@link saveManySeed} with `count: 1` and unwraps the result.
|
|
105
172
|
*/
|
|
106
|
-
declare function
|
|
173
|
+
declare function save<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: SaveOptions<T>): Promise<T>;
|
|
107
174
|
/**
|
|
108
175
|
* Creates and persists one instance of each entity class in the array.
|
|
109
176
|
* Relation seeding is disabled by default; pass `relations: true` to override.
|
|
110
177
|
*/
|
|
111
|
-
declare function
|
|
178
|
+
declare function save<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: SaveOptions): Promise<MapToInstances<T>>;
|
|
112
179
|
/**
|
|
113
180
|
* Creates and persists multiple seed entities of the same class.
|
|
114
|
-
* Applies the same logic as {@link
|
|
181
|
+
* Applies the same logic as {@link save} for each entity.
|
|
115
182
|
*/
|
|
116
|
-
declare function
|
|
183
|
+
declare function saveMany<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: SaveManyOptions<T>): Promise<T[]>;
|
|
117
184
|
/**
|
|
118
185
|
* Creates and persists multiple instances of each entity class in the array.
|
|
119
186
|
* Relation seeding is disabled by default; pass `relations: true` to override.
|
|
120
187
|
*/
|
|
121
|
-
declare function
|
|
188
|
+
declare function saveMany<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: SaveManyOptions): Promise<MapToInstanceArrays<T>>;
|
|
122
189
|
//#endregion
|
|
123
190
|
//#region src/seed/builder.d.ts
|
|
124
191
|
/** Seed builder for a single entity class. Returned by {@link seed} when passed one class. */
|
|
125
192
|
interface SingleSeed<T extends EntityInstance> {
|
|
126
193
|
/** Creates a single instance in memory without persisting. */
|
|
127
|
-
create(context?:
|
|
194
|
+
create(context?: CreateOptions<T>): Promise<T>;
|
|
128
195
|
/** Creates and persists a single instance. */
|
|
129
|
-
save(options:
|
|
196
|
+
save(options: SaveOptions<T>): Promise<T>;
|
|
130
197
|
/** Creates multiple instances in memory without persisting. */
|
|
131
|
-
createMany(count: number, context?:
|
|
198
|
+
createMany(count: number, context?: CreateOptions<T>): Promise<T[]>;
|
|
132
199
|
/** Creates and persists multiple instances. */
|
|
133
|
-
saveMany(count: number, options:
|
|
200
|
+
saveMany(count: number, options: SaveOptions<T>): Promise<T[]>;
|
|
134
201
|
}
|
|
135
202
|
/**
|
|
136
203
|
* Seed builder for multiple entity classes. Returned by {@link seed} when passed an array.
|
|
@@ -141,11 +208,11 @@ interface MultiSeed<T extends readonly EntityConstructor[]> {
|
|
|
141
208
|
/** Creates one instance of each class in memory without persisting. */
|
|
142
209
|
create(context?: SeedContext): Promise<MapToInstances<T>>;
|
|
143
210
|
/** Creates and persists one instance of each class. */
|
|
144
|
-
save(options:
|
|
211
|
+
save(options: SaveOptions): Promise<MapToInstances<T>>;
|
|
145
212
|
/** Creates `count` instances of each class in memory without persisting. */
|
|
146
213
|
createMany(count: number, context?: SeedContext): Promise<MapToInstanceArrays<T>>;
|
|
147
214
|
/** Creates and persists `count` instances of each class. */
|
|
148
|
-
saveMany(count: number, options:
|
|
215
|
+
saveMany(count: number, options: SaveOptions): Promise<MapToInstanceArrays<T>>;
|
|
149
216
|
}
|
|
150
217
|
/**
|
|
151
218
|
* Entry point for creating and persisting seed data.
|
|
@@ -172,30 +239,6 @@ interface MultiSeed<T extends readonly EntityConstructor[]> {
|
|
|
172
239
|
declare function seed<T extends EntityInstance>(EntityClass: EntityConstructor<T>): SingleSeed<T>;
|
|
173
240
|
declare function seed<T extends readonly EntityConstructor[]>(EntityClasses: [...T]): MultiSeed<T>;
|
|
174
241
|
//#endregion
|
|
175
|
-
//#region src/seed/creator.d.ts
|
|
176
|
-
/** Options for {@link createManySeed}. Extends {@link SeedContext} with a required instance count. */
|
|
177
|
-
interface CreateManySeedOptions extends SeedContext {
|
|
178
|
-
count: number;
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Creates one entity instance in memory without persisting it.
|
|
182
|
-
*
|
|
183
|
-
* When passed an array of classes, relation seeding is disabled by default
|
|
184
|
-
* (pass `relations: true` in the context to override). Returns a tuple of
|
|
185
|
-
* instances in the same order as the input array.
|
|
186
|
-
*/
|
|
187
|
-
declare function createSeed<T extends EntityInstance>(EntityClass: EntityConstructor<T>, context?: SeedContext): Promise<T>;
|
|
188
|
-
declare function createSeed<T extends readonly EntityConstructor[]>(EntityClasses: [...T], context?: SeedContext): Promise<MapToInstances<T>>;
|
|
189
|
-
/**
|
|
190
|
-
* Creates multiple entity instances in memory without persisting them.
|
|
191
|
-
*
|
|
192
|
-
* When passed an array of classes, returns a tuple of arrays — one per class — each
|
|
193
|
-
* containing `count` instances. Relation seeding is disabled by default for the
|
|
194
|
-
* array variant; pass `relations: true` in the options to override.
|
|
195
|
-
*/
|
|
196
|
-
declare function createManySeed<T extends EntityInstance>(EntityClass: EntityConstructor<T>, options: CreateManySeedOptions): Promise<T[]>;
|
|
197
|
-
declare function createManySeed<T extends readonly EntityConstructor[]>(EntityClasses: [...T], options: CreateManySeedOptions): Promise<MapToInstanceArrays<T>>;
|
|
198
|
-
//#endregion
|
|
199
242
|
//#region src/seeder/decorator.d.ts
|
|
200
243
|
/**
|
|
201
244
|
* Interface that seeder classes must implement.
|
|
@@ -273,5 +316,5 @@ interface RunSeedersOptions extends SeedContext {
|
|
|
273
316
|
*/
|
|
274
317
|
declare function runSeeders(seeders: SeederCtor[], options?: RunSeedersOptions): Promise<void>;
|
|
275
318
|
//#endregion
|
|
276
|
-
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 };
|
|
277
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,CAAA;AAAA,KAGhD,mBAAA,oBAAuC,iBAAA,oBACrC,CAA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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);
|
|
@@ -78,14 +78,14 @@ async function createOneSeed(EntityClass, context) {
|
|
|
78
78
|
const record = instance;
|
|
79
79
|
for (const { propertyKey, factory } of getSeeds(EntityClass)) {
|
|
80
80
|
if (!factory) continue;
|
|
81
|
-
record[propertyKey] = await factory(context);
|
|
81
|
+
record[propertyKey] = await factory(context, instance);
|
|
82
82
|
seededProperties.add(propertyKey);
|
|
83
83
|
}
|
|
84
84
|
for (const embedded of storage.filterEmbeddeds(EntityClass)) {
|
|
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
|