@mikro-orm/seeder 7.0.4-dev.9 → 7.0.4
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/Factory.d.ts +38 -38
- package/Factory.js +70 -69
- package/README.md +1 -1
- package/SeedManager.d.ts +20 -20
- package/SeedManager.js +123 -124
- package/Seeder.d.ts +8 -4
- package/Seeder.js +7 -7
- package/package.json +3 -3
package/Factory.d.ts
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
1
|
import type { EntityData, EntityManager, Constructor } from '@mikro-orm/core';
|
|
2
2
|
/** Base class for entity factories used in seeding. Provides methods to create and persist test entities. */
|
|
3
3
|
export declare abstract class Factory<TEntity extends object, TInput = EntityData<TEntity>> {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
4
|
+
protected readonly em: EntityManager;
|
|
5
|
+
abstract readonly model: Constructor<TEntity>;
|
|
6
|
+
private eachFunction?;
|
|
7
|
+
constructor(em: EntityManager);
|
|
8
|
+
protected abstract definition(input?: TInput): EntityData<TEntity>;
|
|
9
|
+
/**
|
|
10
|
+
* Make a single entity instance, without persisting it.
|
|
11
|
+
* @param input Object specifying what default attributes of the entity factory should be overridden
|
|
12
|
+
*/
|
|
13
|
+
makeEntity(input?: TInput, index?: number): TEntity;
|
|
14
|
+
/**
|
|
15
|
+
* Make a single entity and persist (not flush)
|
|
16
|
+
* @param input Object specifying what default attributes of the entity factory should be overridden
|
|
17
|
+
*/
|
|
18
|
+
makeOne(input?: TInput): TEntity;
|
|
19
|
+
/**
|
|
20
|
+
* Make multiple entities and then persist them (not flush)
|
|
21
|
+
* @param amount Number of entities that should be generated
|
|
22
|
+
* @param input Object specifying what default attributes of the entity factory should be overridden
|
|
23
|
+
*/
|
|
24
|
+
make(amount: number, input?: TInput): TEntity[];
|
|
25
|
+
/**
|
|
26
|
+
* Create (and flush) a single entity
|
|
27
|
+
* @param input Object specifying what default attributes of the entity factory should be overridden
|
|
28
|
+
*/
|
|
29
|
+
createOne(input?: TInput): Promise<TEntity>;
|
|
30
|
+
/**
|
|
31
|
+
* Create (and flush) multiple entities
|
|
32
|
+
* @param amount Number of entities that should be generated
|
|
33
|
+
* @param input Object specifying what default attributes of the entity factory should be overridden
|
|
34
|
+
*/
|
|
35
|
+
create(amount: number, input?: TInput): Promise<TEntity[]>;
|
|
36
|
+
/**
|
|
37
|
+
* Set a function that is applied to each entity before it is returned
|
|
38
|
+
* In case of `createOne` or `create` it is applied before the entity is persisted
|
|
39
|
+
* @param eachFunction The function that is applied on every entity
|
|
40
|
+
*/
|
|
41
|
+
each(eachFunction: (entity: TEntity, index: number) => void): this;
|
|
42
42
|
}
|
package/Factory.js
CHANGED
|
@@ -1,72 +1,73 @@
|
|
|
1
1
|
/** Base class for entity factories used in seeding. Provides methods to create and persist test entities. */
|
|
2
2
|
export class Factory {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
3
|
+
em;
|
|
4
|
+
eachFunction;
|
|
5
|
+
constructor(em) {
|
|
6
|
+
this.em = em;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Make a single entity instance, without persisting it.
|
|
10
|
+
* @param input Object specifying what default attributes of the entity factory should be overridden
|
|
11
|
+
*/
|
|
12
|
+
makeEntity(input, index = 0) {
|
|
13
|
+
const data =
|
|
14
|
+
this.definition.length === 0
|
|
15
|
+
? {
|
|
16
|
+
...this.definition(),
|
|
17
|
+
...input,
|
|
18
|
+
}
|
|
19
|
+
: this.definition(input);
|
|
20
|
+
const entity = this.em.create(this.model, data, { persist: false });
|
|
21
|
+
this.eachFunction?.(entity, index);
|
|
22
|
+
return entity;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Make a single entity and persist (not flush)
|
|
26
|
+
* @param input Object specifying what default attributes of the entity factory should be overridden
|
|
27
|
+
*/
|
|
28
|
+
makeOne(input) {
|
|
29
|
+
const entity = this.makeEntity(input);
|
|
30
|
+
this.em.persist(entity);
|
|
31
|
+
return entity;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Make multiple entities and then persist them (not flush)
|
|
35
|
+
* @param amount Number of entities that should be generated
|
|
36
|
+
* @param input Object specifying what default attributes of the entity factory should be overridden
|
|
37
|
+
*/
|
|
38
|
+
make(amount, input) {
|
|
39
|
+
const entities = [...Array(amount)].map((_, index) => {
|
|
40
|
+
return this.makeEntity(input, index);
|
|
41
|
+
});
|
|
42
|
+
this.em.persist(entities);
|
|
43
|
+
return entities;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Create (and flush) a single entity
|
|
47
|
+
* @param input Object specifying what default attributes of the entity factory should be overridden
|
|
48
|
+
*/
|
|
49
|
+
async createOne(input) {
|
|
50
|
+
const entity = this.makeOne(input);
|
|
51
|
+
await this.em.flush();
|
|
52
|
+
return entity;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Create (and flush) multiple entities
|
|
56
|
+
* @param amount Number of entities that should be generated
|
|
57
|
+
* @param input Object specifying what default attributes of the entity factory should be overridden
|
|
58
|
+
*/
|
|
59
|
+
async create(amount, input) {
|
|
60
|
+
const entities = this.make(amount, input);
|
|
61
|
+
await this.em.flush();
|
|
62
|
+
return entities;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Set a function that is applied to each entity before it is returned
|
|
66
|
+
* In case of `createOne` or `create` it is applied before the entity is persisted
|
|
67
|
+
* @param eachFunction The function that is applied on every entity
|
|
68
|
+
*/
|
|
69
|
+
each(eachFunction) {
|
|
70
|
+
this.eachFunction = eachFunction;
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
72
73
|
}
|
package/README.md
CHANGED
|
@@ -133,7 +133,7 @@ const author = await em.findOneOrFail(Author, 1, {
|
|
|
133
133
|
populate: ['books'],
|
|
134
134
|
});
|
|
135
135
|
author.name = 'Jon Snow II';
|
|
136
|
-
author.books.getItems().forEach(book => book.title += ' (2nd ed.)');
|
|
136
|
+
author.books.getItems().forEach(book => (book.title += ' (2nd ed.)'));
|
|
137
137
|
author.books.add(orm.em.create(Book, { title: 'New Book', author }));
|
|
138
138
|
|
|
139
139
|
// Flush computes change sets and executes them in a single transaction
|
package/SeedManager.d.ts
CHANGED
|
@@ -2,24 +2,24 @@ import { type Constructor, type EntityManager, type ISeedManager, type MikroORM
|
|
|
2
2
|
import type { Seeder } from './Seeder.js';
|
|
3
3
|
/** Manages discovery and execution of database seeders. */
|
|
4
4
|
export declare class SeedManager implements ISeedManager {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
5
|
+
#private;
|
|
6
|
+
constructor(em: EntityManager);
|
|
7
|
+
private init;
|
|
8
|
+
static register(orm: MikroORM): void;
|
|
9
|
+
/**
|
|
10
|
+
* Checks if `src` folder exists, it so, tries to adjust the migrations and seeders paths automatically to use it.
|
|
11
|
+
* If there is a `dist` or `build` folder, it will be used for the JS variant (`path` option), while the `src` folder will be
|
|
12
|
+
* used for the TS variant (`pathTs` option).
|
|
13
|
+
*
|
|
14
|
+
* If the default folder exists (e.g. `/migrations`), the config will respect that, so this auto-detection should not
|
|
15
|
+
* break existing projects, only help with the new ones.
|
|
16
|
+
*/
|
|
17
|
+
private detectSourceFolder;
|
|
18
|
+
seed(...classNames: Constructor<Seeder>[]): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
seedString(...classNames: string[]): Promise<void>;
|
|
23
|
+
create(className: string): Promise<string>;
|
|
24
|
+
private generate;
|
|
25
25
|
}
|
package/SeedManager.js
CHANGED
|
@@ -1,137 +1,136 @@
|
|
|
1
|
-
import { Utils
|
|
1
|
+
import { Utils } from '@mikro-orm/core';
|
|
2
2
|
/** Manages discovery and execution of database seeders. */
|
|
3
3
|
export class SeedManager {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
#config;
|
|
5
|
+
#options;
|
|
6
|
+
#absolutePath;
|
|
7
|
+
#initialized = false;
|
|
8
|
+
#em;
|
|
9
|
+
constructor(em) {
|
|
10
|
+
this.#em = em;
|
|
11
|
+
this.#config = this.#em.config;
|
|
12
|
+
this.#options = this.#config.get('seeder');
|
|
13
|
+
this.#em = this.#em.fork();
|
|
14
|
+
this.#config.set('persistOnCreate', true);
|
|
15
|
+
}
|
|
16
|
+
async init() {
|
|
17
|
+
if (this.#initialized) {
|
|
18
|
+
return;
|
|
15
19
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
/* v8 ignore next */
|
|
25
|
-
const key = this.#config.get('preferTs', Utils.detectTypeScriptSupport()) && this.#options.pathTs ? 'pathTs' : 'path';
|
|
26
|
-
this.#absolutePath = fs.absolutePath(this.#options[key], this.#config.get('baseDir'));
|
|
27
|
-
}
|
|
20
|
+
this.#initialized = true;
|
|
21
|
+
if (!this.#options.seedersList) {
|
|
22
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
23
|
+
this.detectSourceFolder(fs);
|
|
24
|
+
/* v8 ignore next */
|
|
25
|
+
const key =
|
|
26
|
+
this.#config.get('preferTs', Utils.detectTypeScriptSupport()) && this.#options.pathTs ? 'pathTs' : 'path';
|
|
27
|
+
this.#absolutePath = fs.absolutePath(this.#options[key], this.#config.get('baseDir'));
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
}
|
|
30
|
+
static register(orm) {
|
|
31
|
+
orm.config.registerExtension('@mikro-orm/seeder', () => new SeedManager(orm.em));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Checks if `src` folder exists, it so, tries to adjust the migrations and seeders paths automatically to use it.
|
|
35
|
+
* If there is a `dist` or `build` folder, it will be used for the JS variant (`path` option), while the `src` folder will be
|
|
36
|
+
* used for the TS variant (`pathTs` option).
|
|
37
|
+
*
|
|
38
|
+
* If the default folder exists (e.g. `/migrations`), the config will respect that, so this auto-detection should not
|
|
39
|
+
* break existing projects, only help with the new ones.
|
|
40
|
+
*/
|
|
41
|
+
detectSourceFolder(fs) {
|
|
42
|
+
const baseDir = this.#config.get('baseDir');
|
|
43
|
+
const defaultPath = './seeders';
|
|
44
|
+
if (!fs.pathExists(baseDir + '/src')) {
|
|
45
|
+
this.#options.path ??= defaultPath;
|
|
46
|
+
return;
|
|
31
47
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const baseDir = this.#config.get('baseDir');
|
|
42
|
-
const defaultPath = './seeders';
|
|
43
|
-
if (!fs.pathExists(baseDir + '/src')) {
|
|
44
|
-
this.#options.path ??= defaultPath;
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
const exists = fs.pathExists(`${baseDir}/${defaultPath}`);
|
|
48
|
-
const distDir = fs.pathExists(baseDir + '/dist');
|
|
49
|
-
const buildDir = fs.pathExists(baseDir + '/build');
|
|
50
|
-
// if neither `dist` nor `build` exist, we use the `src` folder as it might be a JS project without building, but with `src` folder
|
|
51
|
-
const path = distDir ? './dist' : buildDir ? './build' : './src';
|
|
52
|
-
// only if the user did not provide any values and if the default path does not exist
|
|
53
|
-
if (!this.#options.path && !this.#options.pathTs && !exists) {
|
|
54
|
-
this.#options.path = `${path}/seeders`;
|
|
55
|
-
this.#options.pathTs = './src/seeders';
|
|
56
|
-
}
|
|
48
|
+
const exists = fs.pathExists(`${baseDir}/${defaultPath}`);
|
|
49
|
+
const distDir = fs.pathExists(baseDir + '/dist');
|
|
50
|
+
const buildDir = fs.pathExists(baseDir + '/build');
|
|
51
|
+
// if neither `dist` nor `build` exist, we use the `src` folder as it might be a JS project without building, but with `src` folder
|
|
52
|
+
const path = distDir ? './dist' : buildDir ? './build' : './src';
|
|
53
|
+
// only if the user did not provide any values and if the default path does not exist
|
|
54
|
+
if (!this.#options.path && !this.#options.pathTs && !exists) {
|
|
55
|
+
this.#options.path = `${path}/seeders`;
|
|
56
|
+
this.#options.pathTs = './src/seeders';
|
|
57
57
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
}
|
|
59
|
+
async seed(...classNames) {
|
|
60
|
+
for (const SeederClass of classNames) {
|
|
61
|
+
const seeder = new SeederClass();
|
|
62
|
+
await seeder.run(this.#em);
|
|
63
|
+
await this.#em.flush();
|
|
64
|
+
this.#em.clear();
|
|
65
65
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
for (const className of classNames) {
|
|
81
|
-
const seederClass = classMap.get(className);
|
|
82
|
-
if (!seederClass) {
|
|
83
|
-
throw new Error(`Seeder class ${className} not found in seedersList`);
|
|
84
|
-
}
|
|
85
|
-
await this.seed(seederClass);
|
|
86
|
-
}
|
|
87
|
-
return;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
async seedString(...classNames) {
|
|
71
|
+
if (this.#options.seedersList) {
|
|
72
|
+
const classMap = new Map();
|
|
73
|
+
for (const entry of this.#options.seedersList) {
|
|
74
|
+
if (typeof entry === 'function') {
|
|
75
|
+
classMap.set(entry.name, entry);
|
|
76
|
+
} else {
|
|
77
|
+
classMap.set(entry.name, entry.class);
|
|
88
78
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
for (const filePath of files) {
|
|
95
|
-
const exports = await fs.dynamicImport(filePath);
|
|
96
|
-
for (const name of Object.keys(exports)) {
|
|
97
|
-
classMap.set(name, exports[name]);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
for (const className of classNames) {
|
|
101
|
-
const seederClass = classMap.get(className);
|
|
102
|
-
if (!seederClass) {
|
|
103
|
-
throw new Error(`Seeder class ${className} not found in ${fs.relativePath(path, process.cwd())}`);
|
|
104
|
-
}
|
|
105
|
-
await this.seed(seederClass);
|
|
79
|
+
}
|
|
80
|
+
for (const className of classNames) {
|
|
81
|
+
const seederClass = classMap.get(className);
|
|
82
|
+
if (!seederClass) {
|
|
83
|
+
throw new Error(`Seeder class ${className} not found in seedersList`);
|
|
106
84
|
}
|
|
85
|
+
await this.seed(seederClass);
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
107
88
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
89
|
+
await this.init();
|
|
90
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
91
|
+
const path = fs.normalizePath(this.#absolutePath, this.#options.glob);
|
|
92
|
+
const files = fs.glob(path).sort();
|
|
93
|
+
const classMap = new Map();
|
|
94
|
+
for (const filePath of files) {
|
|
95
|
+
const exports = await fs.dynamicImport(filePath);
|
|
96
|
+
for (const name of Object.keys(exports)) {
|
|
97
|
+
classMap.set(name, exports[name]);
|
|
98
|
+
}
|
|
113
99
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
100
|
+
for (const className of classNames) {
|
|
101
|
+
const seederClass = classMap.get(className);
|
|
102
|
+
if (!seederClass) {
|
|
103
|
+
throw new Error(`Seeder class ${className} not found in ${fs.relativePath(path, process.cwd())}`);
|
|
104
|
+
}
|
|
105
|
+
await this.seed(seederClass);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async create(className) {
|
|
109
|
+
await this.init();
|
|
110
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
111
|
+
fs.ensureDir(this.#absolutePath);
|
|
112
|
+
return this.generate(fs, className);
|
|
113
|
+
}
|
|
114
|
+
async generate(fs, className) {
|
|
115
|
+
const fileName = `${this.#options.fileName(className)}.${this.#options.emit}`;
|
|
116
|
+
const filePath = `${this.#absolutePath}/${fileName}`;
|
|
117
|
+
let ret = '';
|
|
118
|
+
if (this.#options.emit === 'ts') {
|
|
119
|
+
ret += `import type { EntityManager } from '@mikro-orm/core';\n`;
|
|
120
|
+
ret += `import { Seeder } from '@mikro-orm/seeder';\n\n`;
|
|
121
|
+
ret += `export class ${className} extends Seeder {\n\n`;
|
|
122
|
+
ret += ` async run(em: EntityManager): Promise<void> {}\n\n`;
|
|
123
|
+
ret += `}\n`;
|
|
124
|
+
} else {
|
|
125
|
+
ret += `'use strict';\n`;
|
|
126
|
+
ret += `Object.defineProperty(exports, '__esModule', { value: true });\n`;
|
|
127
|
+
ret += `const { Seeder } = require('@mikro-orm/seeder');\n\n`;
|
|
128
|
+
ret += `class ${className} extends Seeder {\n\n`;
|
|
129
|
+
ret += ` async run(em: EntityManager): Promise<void> {}\n\n`;
|
|
130
|
+
ret += `}\n`;
|
|
131
|
+
ret += `exports.${className} = ${className};\n`;
|
|
136
132
|
}
|
|
133
|
+
await fs.writeFile(filePath, ret, { flush: true });
|
|
134
|
+
return filePath;
|
|
135
|
+
}
|
|
137
136
|
}
|
package/Seeder.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import type { Dictionary, EntityManager } from '@mikro-orm/core';
|
|
2
2
|
/** Base class for database seeders. Extend this class and implement `run()` to populate the database with data. */
|
|
3
3
|
export declare abstract class Seeder<T extends Dictionary = Dictionary> {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
abstract run(em: EntityManager, context?: T): void | Promise<void>;
|
|
5
|
+
protected call(
|
|
6
|
+
em: EntityManager,
|
|
7
|
+
seeders: {
|
|
8
|
+
new (): Seeder;
|
|
9
|
+
}[],
|
|
10
|
+
context?: T,
|
|
11
|
+
): Promise<void>;
|
|
8
12
|
}
|
package/Seeder.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/** Base class for database seeders. Extend this class and implement `run()` to populate the database with data. */
|
|
2
2
|
export class Seeder {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
3
|
+
async call(em, seeders, context = {}) {
|
|
4
|
+
for (const Seeder of seeders) {
|
|
5
|
+
const fork = em.fork();
|
|
6
|
+
const instance = new Seeder();
|
|
7
|
+
await instance.run(fork, context);
|
|
8
|
+
await fork.flush();
|
|
10
9
|
}
|
|
10
|
+
}
|
|
11
11
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/seeder",
|
|
3
|
-
"version": "7.0.4
|
|
3
|
+
"version": "7.0.4",
|
|
4
4
|
"description": "Seeder package for MikroORM.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"data-mapper",
|
|
@@ -48,10 +48,10 @@
|
|
|
48
48
|
"copy": "node ../../scripts/copy.mjs"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@mikro-orm/core": "^7.0.
|
|
51
|
+
"@mikro-orm/core": "^7.0.4"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
|
-
"@mikro-orm/core": "7.0.4
|
|
54
|
+
"@mikro-orm/core": "7.0.4"
|
|
55
55
|
},
|
|
56
56
|
"engines": {
|
|
57
57
|
"node": ">= 22.17.0"
|