Skip to content

Commit

Permalink
fix: load seeders classes using typeorm (#324) (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
alumni authored Oct 4, 2024
1 parent d0c7bf1 commit a2590cc
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 54 deletions.
13 changes: 2 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
"bin": {
"typeorm-seeding": "dist/cli.js"
},
"contributors": [
"Jorge Bodega <[email protected]> (https://github.com/jorgebodega)"
],
"contributors": ["Jorge Bodega <[email protected]> (https://github.com/jorgebodega)"],
"dependencies": {
"chalk": "4.1.2",
"commander": "12.1.0",
"glob": "11.0.0",
"ora": "5.4.1",
"tslib": "2.7.0"
},
Expand All @@ -18,7 +15,6 @@
"@biomejs/biome": "1.9.2",
"@faker-js/faker": "8.4.1",
"@jorgebodega/typeorm-factory": "1.4.0",
"@types/glob": "8.1.0",
"@types/jest": "29.5.13",
"@types/node": "20.16.8",
"jest": "29.7.0",
Expand All @@ -32,12 +28,7 @@
"engines": {
"node": ">=18 <19 || >=20"
},
"keywords": [
"typeorm",
"seed",
"seeding",
"orm"
],
"keywords": ["typeorm", "seed", "seeding", "orm"],
"license": "MIT",
"main": "dist/index.js",
"name": "@jorgebodega/typeorm-seeding",
Expand Down
19 changes: 0 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 a2590cc

Please sign in to comment.