Skip to content

Commit

Permalink
fix: load seeders classes using typeorm (jorgebodega#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
alumni committed Sep 26, 2024
1 parent d0c7bf1 commit 7b79a3c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 24 deletions.
6 changes: 2 additions & 4 deletions src/commands/seed.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SeederExecutionError } from "../errors/SeederExecutionError";
import { useDataSource, useSeeders } from "../helpers";
import type { Seeder } from "../seeder";
import type { Constructable, SeedCommandArguments } from "../types";
import { calculateFilePath, loadDataSource, loadSeeders } from "../utils";
import { loadDataSource, loadSeeders } from "../utils";

async function run(paths: string[]) {
const opts = seedCommand.opts<SeedCommandArguments>();
Expand All @@ -32,9 +32,7 @@ async function run(paths: string[]) {
spinner.start("Importing seeders");
let seeders!: Constructable<Seeder>[];
try {
const seederFiles = paths.flatMap(calculateFilePath);

seeders = await loadSeeders(seederFiles);
seeders = await loadSeeders(dataSource, paths);

spinner.succeed("Seeder imported");
} catch (error: unknown) {
Expand Down
20 changes: 6 additions & 14 deletions src/utils/commandUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DataSource } from "typeorm";
import { importClassesFromDirectories } from "typeorm/util/DirectoryExportedClassesLoader";
import { CommandUtils as TypeormCommandUtils } from "typeorm/commands/CommandUtils";
import { Seeder } from "../seeder";
import type { Constructable } from "../types";
Expand All @@ -7,22 +8,13 @@ export async function loadDataSource(dataSourceFilePath: string): Promise<DataSo
return TypeormCommandUtils.loadDataSource(dataSourceFilePath);
}

export async function loadSeeders(seederPaths: string[]): Promise<Constructable<Seeder>[]> {
const seederFileExports = (await Promise.all(seederPaths.map((seederFile) => import(seederFile))))
.map((seederExport) => seederExport.default?.default ?? seederExport.default)
.filter((seederExport) => Boolean(seederExport));
export async function loadSeeders(dataSource: DataSource, seederPaths: string[]): Promise<Constructable<Seeder>[]> {
const seederFileExports = await importClassesFromDirectories(dataSource.logger, seederPaths);

if (seederFileExports.length === 0) {
throw new Error("No default seeders found");
}
const seeders = seederFileExports.filter((seeder) => seeder.prototype instanceof Seeder) as Constructable<Seeder>[];

const seeders: Constructable<Seeder>[] = [];
for (const fileExport in seederFileExports) {
const seederExport = seederFileExports[fileExport];
const instance = new seederExport();
if (instance instanceof Seeder) {
seeders.push(seederExport);
}
if (seeders.length === 0) {
throw new Error("No default seeders found");
}

return seeders;
Expand Down
5 changes: 0 additions & 5 deletions src/utils/fileHandling.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./commandUtils";
export * from "./fileHandling";

0 comments on commit 7b79a3c

Please sign in to comment.