@mikro-orm/seeder 7.0.0-dev.77 → 7.0.0-dev.78

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/SeedManager.d.ts CHANGED
@@ -7,6 +7,15 @@ 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
package/SeedManager.js CHANGED
@@ -1,4 +1,5 @@
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
4
  import { glob } from 'tinyglobby';
4
5
  export class SeedManager {
@@ -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();
@@ -49,7 +77,7 @@ export class SeedManager {
49
77
  }
50
78
  }
51
79
  async create(className) {
52
- Utils.ensureDir(this.absolutePath);
80
+ fs.ensureDir(this.absolutePath);
53
81
  return this.generate(className);
54
82
  }
55
83
  async generate(className) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/seeder",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.77",
4
+ "version": "7.0.0-dev.78",
5
5
  "description": "Seeder package for MikroORM.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -57,6 +57,6 @@
57
57
  "@mikro-orm/core": "^6.6.1"
58
58
  },
59
59
  "peerDependencies": {
60
- "@mikro-orm/core": "7.0.0-dev.77"
60
+ "@mikro-orm/core": "7.0.0-dev.78"
61
61
  }
62
62
  }