@mikro-orm/seeder 7.0.0-dev.9 → 7.0.0-dev.90
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 +3 -2
- package/SeedManager.d.ts +10 -1
- package/SeedManager.js +32 -4
- package/package.json +5 -6
package/README.md
CHANGED
|
@@ -11,7 +11,6 @@ TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-or
|
|
|
11
11
|
[](https://discord.gg/w8bjxFHS7X)
|
|
12
12
|
[](https://www.npmjs.com/package/@mikro-orm/core)
|
|
13
13
|
[](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
|
|
14
|
-
[](https://codeclimate.com/github/mikro-orm/mikro-orm/maintainability)
|
|
15
14
|
[](https://github.com/mikro-orm/mikro-orm/actions?workflow=tests)
|
|
16
15
|
|
|
17
16
|
## 🤔 Unit of What?
|
|
@@ -141,7 +140,7 @@ There is also auto-generated [CHANGELOG.md](CHANGELOG.md) file based on commit m
|
|
|
141
140
|
- [Composite and Foreign Keys as Primary Key](https://mikro-orm.io/docs/composite-keys)
|
|
142
141
|
- [Filters](https://mikro-orm.io/docs/filters)
|
|
143
142
|
- [Using `QueryBuilder`](https://mikro-orm.io/docs/query-builder)
|
|
144
|
-
- [
|
|
143
|
+
- [Populating relations](https://mikro-orm.io/docs/populating-relations)
|
|
145
144
|
- [Property Validation](https://mikro-orm.io/docs/property-validation)
|
|
146
145
|
- [Lifecycle Hooks](https://mikro-orm.io/docs/events#hooks)
|
|
147
146
|
- [Vanilla JS Support](https://mikro-orm.io/docs/usage-with-js)
|
|
@@ -382,6 +381,8 @@ See also the list of contributors who [participated](https://github.com/mikro-or
|
|
|
382
381
|
|
|
383
382
|
Please ⭐️ this repository if this project helped you!
|
|
384
383
|
|
|
384
|
+
> If you'd like to support my open-source work, consider sponsoring me directly at [github.com/sponsors/b4nan](https://github.com/sponsors/b4nan).
|
|
385
|
+
|
|
385
386
|
## 📝 License
|
|
386
387
|
|
|
387
388
|
Copyright © 2018 [Martin Adámek](https://github.com/b4nan).
|
package/SeedManager.d.ts
CHANGED
|
@@ -7,11 +7,20 @@ export declare class SeedManager implements ISeedManager {
|
|
|
7
7
|
private readonly absolutePath;
|
|
8
8
|
constructor(em: EntityManager);
|
|
9
9
|
static register(orm: MikroORM): void;
|
|
10
|
+
/**
|
|
11
|
+
* Checks if `src` folder exists, it so, tries to adjust the migrations and seeders paths automatically to use it.
|
|
12
|
+
* If there is a `dist` or `build` folder, it will be used for the JS variant (`path` option), while the `src` folder will be
|
|
13
|
+
* used for the TS variant (`pathTs` option).
|
|
14
|
+
*
|
|
15
|
+
* If the default folder exists (e.g. `/migrations`), the config will respect that, so this auto-detection should not
|
|
16
|
+
* break existing projects, only help with the new ones.
|
|
17
|
+
*/
|
|
18
|
+
private detectSourceFolder;
|
|
10
19
|
seed(...classNames: Constructor<Seeder>[]): Promise<void>;
|
|
11
20
|
/**
|
|
12
21
|
* @internal
|
|
13
22
|
*/
|
|
14
23
|
seedString(...classNames: string[]): Promise<void>;
|
|
15
|
-
|
|
24
|
+
create(className: string): Promise<string>;
|
|
16
25
|
private generate;
|
|
17
26
|
}
|
package/SeedManager.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Utils, } from '@mikro-orm/core';
|
|
2
|
+
import { fs } from '@mikro-orm/core/fs-utils';
|
|
2
3
|
import { writeFile } from 'node:fs/promises';
|
|
3
|
-
import
|
|
4
|
+
import { glob } from 'tinyglobby';
|
|
4
5
|
export class SeedManager {
|
|
5
6
|
em;
|
|
6
7
|
config;
|
|
@@ -12,6 +13,7 @@ export class SeedManager {
|
|
|
12
13
|
this.options = this.config.get('seeder');
|
|
13
14
|
this.em = this.em.fork();
|
|
14
15
|
this.config.set('persistOnCreate', true);
|
|
16
|
+
this.detectSourceFolder();
|
|
15
17
|
/* v8 ignore next */
|
|
16
18
|
const key = (this.config.get('preferTs', Utils.detectTypeScriptSupport()) && this.options.pathTs) ? 'pathTs' : 'path';
|
|
17
19
|
this.absolutePath = Utils.absolutePath(this.options[key], this.config.get('baseDir'));
|
|
@@ -19,6 +21,32 @@ export class SeedManager {
|
|
|
19
21
|
static register(orm) {
|
|
20
22
|
orm.config.registerExtension('@mikro-orm/seeder', () => new SeedManager(orm.em));
|
|
21
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Checks if `src` folder exists, it so, tries to adjust the migrations and seeders paths automatically to use it.
|
|
26
|
+
* If there is a `dist` or `build` folder, it will be used for the JS variant (`path` option), while the `src` folder will be
|
|
27
|
+
* used for the TS variant (`pathTs` option).
|
|
28
|
+
*
|
|
29
|
+
* If the default folder exists (e.g. `/migrations`), the config will respect that, so this auto-detection should not
|
|
30
|
+
* break existing projects, only help with the new ones.
|
|
31
|
+
*/
|
|
32
|
+
detectSourceFolder() {
|
|
33
|
+
const baseDir = this.config.get('baseDir');
|
|
34
|
+
const defaultPath = './seeders';
|
|
35
|
+
if (!fs.pathExists(baseDir + '/src')) {
|
|
36
|
+
this.options.path ??= defaultPath;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const exists = fs.pathExists(`${baseDir}/${defaultPath}`);
|
|
40
|
+
const distDir = fs.pathExists(baseDir + '/dist');
|
|
41
|
+
const buildDir = fs.pathExists(baseDir + '/build');
|
|
42
|
+
// if neither `dist` nor `build` exist, we use the `src` folder as it might be a JS project without building, but with `src` folder
|
|
43
|
+
const path = distDir ? './dist' : (buildDir ? './build' : './src');
|
|
44
|
+
// only if the user did not provide any values and if the default path does not exist
|
|
45
|
+
if (!this.options.path && !this.options.pathTs && !exists) {
|
|
46
|
+
this.options.path = `${path}/seeders`;
|
|
47
|
+
this.options.pathTs = './src/seeders';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
22
50
|
async seed(...classNames) {
|
|
23
51
|
for (const SeederClass of classNames) {
|
|
24
52
|
const seeder = new SeederClass();
|
|
@@ -32,7 +60,7 @@ export class SeedManager {
|
|
|
32
60
|
*/
|
|
33
61
|
async seedString(...classNames) {
|
|
34
62
|
const path = `${this.absolutePath}/${this.options.glob}`;
|
|
35
|
-
const files = await
|
|
63
|
+
const files = await glob(path);
|
|
36
64
|
const classMap = new Map();
|
|
37
65
|
for (const path of files) {
|
|
38
66
|
const exports = await Utils.dynamicImport(path);
|
|
@@ -48,8 +76,8 @@ export class SeedManager {
|
|
|
48
76
|
await this.seed(seederClass);
|
|
49
77
|
}
|
|
50
78
|
}
|
|
51
|
-
async
|
|
52
|
-
|
|
79
|
+
async create(className) {
|
|
80
|
+
fs.ensureDir(this.absolutePath);
|
|
53
81
|
return this.generate(className);
|
|
54
82
|
}
|
|
55
83
|
async generate(className) {
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/seeder",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "7.0.0-dev.
|
|
4
|
+
"version": "7.0.0-dev.90",
|
|
5
5
|
"description": "Seeder package for MikroORM.",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./package.json": "./package.json",
|
|
8
8
|
".": "./index.js"
|
|
9
9
|
},
|
|
10
|
-
"typings": "index.d.ts",
|
|
11
10
|
"repository": {
|
|
12
11
|
"type": "git",
|
|
13
12
|
"url": "git+ssh://git@github.com/mikro-orm/mikro-orm.git"
|
|
@@ -40,7 +39,7 @@
|
|
|
40
39
|
},
|
|
41
40
|
"homepage": "https://mikro-orm.io",
|
|
42
41
|
"engines": {
|
|
43
|
-
"node": ">= 22.
|
|
42
|
+
"node": ">= 22.17.0"
|
|
44
43
|
},
|
|
45
44
|
"scripts": {
|
|
46
45
|
"build": "yarn clean && yarn compile && yarn copy",
|
|
@@ -52,12 +51,12 @@
|
|
|
52
51
|
"access": "public"
|
|
53
52
|
},
|
|
54
53
|
"dependencies": {
|
|
55
|
-
"
|
|
54
|
+
"tinyglobby": "0.2.15"
|
|
56
55
|
},
|
|
57
56
|
"devDependencies": {
|
|
58
|
-
"@mikro-orm/core": "^6.
|
|
57
|
+
"@mikro-orm/core": "^6.6.2"
|
|
59
58
|
},
|
|
60
59
|
"peerDependencies": {
|
|
61
|
-
"@mikro-orm/core": "7.0.0-dev.
|
|
60
|
+
"@mikro-orm/core": "7.0.0-dev.90"
|
|
62
61
|
}
|
|
63
62
|
}
|