@mikro-orm/seeder 7.0.0-dev.33 → 7.0.0-dev.330
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 +7 -4
- package/SeedManager.d.ts +12 -5
- package/SeedManager.js +88 -30
- package/package.json +32 -36
package/README.md
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
<a href="https://mikro-orm.io"><img src="https://raw.githubusercontent.com/mikro-orm/mikro-orm/master/docs/static/img/logo-readme.svg?sanitize=true" alt="MikroORM" /></a>
|
|
3
3
|
</h1>
|
|
4
4
|
|
|
5
|
-
TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/) and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL
|
|
5
|
+
TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/) and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL, SQLite (including libSQL), MSSQL and Oracle databases.
|
|
6
6
|
|
|
7
7
|
> Heavily inspired by [Doctrine](https://www.doctrine-project.org/) and [Hibernate](https://hibernate.org/).
|
|
8
8
|
|
|
9
|
-
[](https://
|
|
10
|
-
[](https://
|
|
9
|
+
[](https://npmx.dev/package/@mikro-orm/core)
|
|
10
|
+
[](https://npmx.dev/package/@mikro-orm/core)
|
|
11
11
|
[](https://discord.gg/w8bjxFHS7X)
|
|
12
|
-
[](https://
|
|
12
|
+
[](https://npmx.dev/package/@mikro-orm/core)
|
|
13
13
|
[](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
|
|
14
14
|
[](https://github.com/mikro-orm/mikro-orm/actions?workflow=tests)
|
|
15
15
|
|
|
@@ -181,6 +181,7 @@ yarn add @mikro-orm/core @mikro-orm/mysql # for mysql/mariadb
|
|
|
181
181
|
yarn add @mikro-orm/core @mikro-orm/mariadb # for mysql/mariadb
|
|
182
182
|
yarn add @mikro-orm/core @mikro-orm/postgresql # for postgresql
|
|
183
183
|
yarn add @mikro-orm/core @mikro-orm/mssql # for mssql
|
|
184
|
+
yarn add @mikro-orm/core @mikro-orm/oracledb # for oracle
|
|
184
185
|
yarn add @mikro-orm/core @mikro-orm/sqlite # for sqlite
|
|
185
186
|
yarn add @mikro-orm/core @mikro-orm/libsql # for libsql
|
|
186
187
|
```
|
|
@@ -381,6 +382,8 @@ See also the list of contributors who [participated](https://github.com/mikro-or
|
|
|
381
382
|
|
|
382
383
|
Please ⭐️ this repository if this project helped you!
|
|
383
384
|
|
|
385
|
+
> If you'd like to support my open-source work, consider sponsoring me directly at [github.com/sponsors/b4nan](https://github.com/sponsors/b4nan).
|
|
386
|
+
|
|
384
387
|
## 📝 License
|
|
385
388
|
|
|
386
389
|
Copyright © 2018 [Martin Adámek](https://github.com/b4nan).
|
package/SeedManager.d.ts
CHANGED
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
import { type Constructor, type EntityManager, type ISeedManager, type MikroORM } from '@mikro-orm/core';
|
|
2
2
|
import type { Seeder } from './Seeder.js';
|
|
3
3
|
export declare class SeedManager implements ISeedManager {
|
|
4
|
-
private
|
|
5
|
-
private readonly config;
|
|
6
|
-
private readonly options;
|
|
7
|
-
private readonly absolutePath;
|
|
4
|
+
#private;
|
|
8
5
|
constructor(em: EntityManager);
|
|
6
|
+
private init;
|
|
9
7
|
static register(orm: MikroORM): void;
|
|
8
|
+
/**
|
|
9
|
+
* Checks if `src` folder exists, it so, tries to adjust the migrations and seeders paths automatically to use it.
|
|
10
|
+
* If there is a `dist` or `build` folder, it will be used for the JS variant (`path` option), while the `src` folder will be
|
|
11
|
+
* used for the TS variant (`pathTs` option).
|
|
12
|
+
*
|
|
13
|
+
* If the default folder exists (e.g. `/migrations`), the config will respect that, so this auto-detection should not
|
|
14
|
+
* break existing projects, only help with the new ones.
|
|
15
|
+
*/
|
|
16
|
+
private detectSourceFolder;
|
|
10
17
|
seed(...classNames: Constructor<Seeder>[]): Promise<void>;
|
|
11
18
|
/**
|
|
12
19
|
* @internal
|
|
13
20
|
*/
|
|
14
21
|
seedString(...classNames: string[]): Promise<void>;
|
|
15
|
-
|
|
22
|
+
create(className: string): Promise<string>;
|
|
16
23
|
private generate;
|
|
17
24
|
}
|
package/SeedManager.js
CHANGED
|
@@ -1,41 +1,97 @@
|
|
|
1
1
|
import { Utils, } from '@mikro-orm/core';
|
|
2
|
-
import { writeFile } from 'node:fs/promises';
|
|
3
|
-
import { glob } from 'tinyglobby';
|
|
4
2
|
export class SeedManager {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
#config;
|
|
4
|
+
#options;
|
|
5
|
+
#absolutePath;
|
|
6
|
+
#initialized = false;
|
|
7
|
+
#em;
|
|
9
8
|
constructor(em) {
|
|
10
|
-
this
|
|
11
|
-
this
|
|
12
|
-
this
|
|
13
|
-
this
|
|
14
|
-
this
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
this.#em = em;
|
|
10
|
+
this.#config = this.#em.config;
|
|
11
|
+
this.#options = this.#config.get('seeder');
|
|
12
|
+
this.#em = this.#em.fork();
|
|
13
|
+
this.#config.set('persistOnCreate', true);
|
|
14
|
+
}
|
|
15
|
+
async init() {
|
|
16
|
+
if (this.#initialized) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
this.#initialized = true;
|
|
20
|
+
if (!this.#options.seedersList) {
|
|
21
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
22
|
+
this.detectSourceFolder(fs);
|
|
23
|
+
/* v8 ignore next */
|
|
24
|
+
const key = this.#config.get('preferTs', Utils.detectTypeScriptSupport()) && this.#options.pathTs ? 'pathTs' : 'path';
|
|
25
|
+
this.#absolutePath = fs.absolutePath(this.#options[key], this.#config.get('baseDir'));
|
|
26
|
+
}
|
|
18
27
|
}
|
|
19
28
|
static register(orm) {
|
|
20
29
|
orm.config.registerExtension('@mikro-orm/seeder', () => new SeedManager(orm.em));
|
|
21
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Checks if `src` folder exists, it so, tries to adjust the migrations and seeders paths automatically to use it.
|
|
33
|
+
* If there is a `dist` or `build` folder, it will be used for the JS variant (`path` option), while the `src` folder will be
|
|
34
|
+
* used for the TS variant (`pathTs` option).
|
|
35
|
+
*
|
|
36
|
+
* If the default folder exists (e.g. `/migrations`), the config will respect that, so this auto-detection should not
|
|
37
|
+
* break existing projects, only help with the new ones.
|
|
38
|
+
*/
|
|
39
|
+
detectSourceFolder(fs) {
|
|
40
|
+
const baseDir = this.#config.get('baseDir');
|
|
41
|
+
const defaultPath = './seeders';
|
|
42
|
+
if (!fs.pathExists(baseDir + '/src')) {
|
|
43
|
+
this.#options.path ??= defaultPath;
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const exists = fs.pathExists(`${baseDir}/${defaultPath}`);
|
|
47
|
+
const distDir = fs.pathExists(baseDir + '/dist');
|
|
48
|
+
const buildDir = fs.pathExists(baseDir + '/build');
|
|
49
|
+
// if neither `dist` nor `build` exist, we use the `src` folder as it might be a JS project without building, but with `src` folder
|
|
50
|
+
const path = distDir ? './dist' : buildDir ? './build' : './src';
|
|
51
|
+
// only if the user did not provide any values and if the default path does not exist
|
|
52
|
+
if (!this.#options.path && !this.#options.pathTs && !exists) {
|
|
53
|
+
this.#options.path = `${path}/seeders`;
|
|
54
|
+
this.#options.pathTs = './src/seeders';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
22
57
|
async seed(...classNames) {
|
|
23
58
|
for (const SeederClass of classNames) {
|
|
24
59
|
const seeder = new SeederClass();
|
|
25
|
-
await seeder.run(this
|
|
26
|
-
await this
|
|
27
|
-
this
|
|
60
|
+
await seeder.run(this.#em);
|
|
61
|
+
await this.#em.flush();
|
|
62
|
+
this.#em.clear();
|
|
28
63
|
}
|
|
29
64
|
}
|
|
30
65
|
/**
|
|
31
66
|
* @internal
|
|
32
67
|
*/
|
|
33
68
|
async seedString(...classNames) {
|
|
34
|
-
|
|
35
|
-
|
|
69
|
+
if (this.#options.seedersList) {
|
|
70
|
+
const classMap = new Map();
|
|
71
|
+
for (const entry of this.#options.seedersList) {
|
|
72
|
+
if (typeof entry === 'function') {
|
|
73
|
+
classMap.set(entry.name, entry);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
classMap.set(entry.name, entry.class);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const className of classNames) {
|
|
80
|
+
const seederClass = classMap.get(className);
|
|
81
|
+
if (!seederClass) {
|
|
82
|
+
throw new Error(`Seeder class ${className} not found in seedersList`);
|
|
83
|
+
}
|
|
84
|
+
await this.seed(seederClass);
|
|
85
|
+
}
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
await this.init();
|
|
89
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
90
|
+
const path = fs.normalizePath(this.#absolutePath, this.#options.glob);
|
|
91
|
+
const files = fs.glob(path).sort();
|
|
36
92
|
const classMap = new Map();
|
|
37
|
-
for (const
|
|
38
|
-
const exports = await
|
|
93
|
+
for (const filePath of files) {
|
|
94
|
+
const exports = await fs.dynamicImport(filePath);
|
|
39
95
|
for (const name of Object.keys(exports)) {
|
|
40
96
|
classMap.set(name, exports[name]);
|
|
41
97
|
}
|
|
@@ -43,20 +99,22 @@ export class SeedManager {
|
|
|
43
99
|
for (const className of classNames) {
|
|
44
100
|
const seederClass = classMap.get(className);
|
|
45
101
|
if (!seederClass) {
|
|
46
|
-
throw new Error(`Seeder class ${className} not found in ${
|
|
102
|
+
throw new Error(`Seeder class ${className} not found in ${fs.relativePath(path, process.cwd())}`);
|
|
47
103
|
}
|
|
48
104
|
await this.seed(seederClass);
|
|
49
105
|
}
|
|
50
106
|
}
|
|
51
|
-
async
|
|
52
|
-
|
|
53
|
-
|
|
107
|
+
async create(className) {
|
|
108
|
+
await this.init();
|
|
109
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
110
|
+
fs.ensureDir(this.#absolutePath);
|
|
111
|
+
return this.generate(fs, className);
|
|
54
112
|
}
|
|
55
|
-
async generate(className) {
|
|
56
|
-
const fileName = `${this
|
|
57
|
-
const filePath = `${this
|
|
113
|
+
async generate(fs, className) {
|
|
114
|
+
const fileName = `${this.#options.fileName(className)}.${this.#options.emit}`;
|
|
115
|
+
const filePath = `${this.#absolutePath}/${fileName}`;
|
|
58
116
|
let ret = '';
|
|
59
|
-
if (this
|
|
117
|
+
if (this.#options.emit === 'ts') {
|
|
60
118
|
ret += `import type { EntityManager } from '@mikro-orm/core';\n`;
|
|
61
119
|
ret += `import { Seeder } from '@mikro-orm/seeder';\n\n`;
|
|
62
120
|
ret += `export class ${className} extends Seeder {\n\n`;
|
|
@@ -72,7 +130,7 @@ export class SeedManager {
|
|
|
72
130
|
ret += `}\n`;
|
|
73
131
|
ret += `exports.${className} = ${className};\n`;
|
|
74
132
|
}
|
|
75
|
-
await writeFile(filePath, ret, { flush: true });
|
|
133
|
+
await fs.writeFile(filePath, ret, { flush: true });
|
|
76
134
|
return filePath;
|
|
77
135
|
}
|
|
78
136
|
}
|
package/package.json
CHANGED
|
@@ -1,63 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/seeder",
|
|
3
|
-
"
|
|
4
|
-
"version": "7.0.0-dev.33",
|
|
3
|
+
"version": "7.0.0-dev.330",
|
|
5
4
|
"description": "Seeder package for MikroORM.",
|
|
6
|
-
"exports": {
|
|
7
|
-
"./package.json": "./package.json",
|
|
8
|
-
".": "./index.js"
|
|
9
|
-
},
|
|
10
|
-
"typings": "index.d.ts",
|
|
11
|
-
"repository": {
|
|
12
|
-
"type": "git",
|
|
13
|
-
"url": "git+ssh://git@github.com/mikro-orm/mikro-orm.git"
|
|
14
|
-
},
|
|
15
5
|
"keywords": [
|
|
16
|
-
"
|
|
6
|
+
"data-mapper",
|
|
7
|
+
"ddd",
|
|
8
|
+
"entity",
|
|
9
|
+
"identity-map",
|
|
10
|
+
"javascript",
|
|
11
|
+
"js",
|
|
12
|
+
"mariadb",
|
|
13
|
+
"mikro-orm",
|
|
17
14
|
"mongo",
|
|
18
15
|
"mongodb",
|
|
19
16
|
"mysql",
|
|
20
|
-
"
|
|
17
|
+
"orm",
|
|
21
18
|
"postgresql",
|
|
19
|
+
"seeder",
|
|
22
20
|
"sqlite",
|
|
23
21
|
"sqlite3",
|
|
24
22
|
"ts",
|
|
25
23
|
"typescript",
|
|
26
|
-
"
|
|
27
|
-
"javascript",
|
|
28
|
-
"entity",
|
|
29
|
-
"ddd",
|
|
30
|
-
"mikro-orm",
|
|
31
|
-
"unit-of-work",
|
|
32
|
-
"data-mapper",
|
|
33
|
-
"identity-map",
|
|
34
|
-
"seeder"
|
|
24
|
+
"unit-of-work"
|
|
35
25
|
],
|
|
36
|
-
"
|
|
37
|
-
"license": "MIT",
|
|
26
|
+
"homepage": "https://mikro-orm.io",
|
|
38
27
|
"bugs": {
|
|
39
28
|
"url": "https://github.com/mikro-orm/mikro-orm/issues"
|
|
40
29
|
},
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"author": "Wybren Kortstra",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+ssh://git@github.com/mikro-orm/mikro-orm.git"
|
|
44
35
|
},
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"copy": "node ../../scripts/copy.mjs"
|
|
36
|
+
"type": "module",
|
|
37
|
+
"exports": {
|
|
38
|
+
"./package.json": "./package.json",
|
|
39
|
+
".": "./index.js"
|
|
50
40
|
},
|
|
51
41
|
"publishConfig": {
|
|
52
42
|
"access": "public"
|
|
53
43
|
},
|
|
54
|
-
"
|
|
55
|
-
"
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "yarn compile && yarn copy",
|
|
46
|
+
"clean": "yarn run -T rimraf ./dist",
|
|
47
|
+
"compile": "yarn run -T tsc -p tsconfig.build.json",
|
|
48
|
+
"copy": "node ../../scripts/copy.mjs"
|
|
56
49
|
},
|
|
57
50
|
"devDependencies": {
|
|
58
|
-
"@mikro-orm/core": "^6.
|
|
51
|
+
"@mikro-orm/core": "^6.6.9"
|
|
59
52
|
},
|
|
60
53
|
"peerDependencies": {
|
|
61
|
-
"@mikro-orm/core": "7.0.0-dev.
|
|
54
|
+
"@mikro-orm/core": "7.0.0-dev.330"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">= 22.17.0"
|
|
62
58
|
}
|
|
63
59
|
}
|