forked from w3tecch/typeorm-seeding
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adapt use seeders to new structure
BREAKING CHANGE `useSeeders` change its definition and is now incompatible with previous version
- Loading branch information
1 parent
aecf7b4
commit 5bca471
Showing
5 changed files
with
36 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class DefaultSeederNotDefinedError extends Error { | ||
constructor() { | ||
super(`Default seeder is not defined.`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import type { Connection } from 'typeorm' | ||
import type { ClassConstructor } from './types' | ||
|
||
export abstract class Seeder { | ||
abstract run(connection: Connection): Promise<void> | ||
|
||
protected async call(connection: Connection, seeders: ClassConstructor<Seeder>[]): Promise<void> { | ||
for (const seeder of seeders) { | ||
await new seeder().run(connection) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,23 @@ | ||
import { configureConnection, fetchConnection, getConnectionOptions } from './connection' | ||
import { SeederImportationError } from './errors/SeederImportationError' | ||
import { configureConnection, fetchConnection } from './connection' | ||
import { Seeder } from './seeder' | ||
import type { ConnectionConfiguration } from './types' | ||
import { calculateFilePaths } from './utils/fileHandling' | ||
import type { ClassConstructor, ConnectionConfiguration } from './types' | ||
|
||
export async function useSeeders(entrySeeders: ClassConstructor<Seeder> | ClassConstructor<Seeder>[]): Promise<void> | ||
export async function useSeeders( | ||
executeSeeders?: boolean, | ||
options?: Partial<ConnectionConfiguration>, | ||
): Promise<Seeder[]> | ||
export async function useSeeders( | ||
executeSeeders?: boolean, | ||
seeders?: string[], | ||
options?: Partial<ConnectionConfiguration>, | ||
): Promise<Seeder[]> | ||
entrySeeders: ClassConstructor<Seeder> | ClassConstructor<Seeder>[], | ||
customOptions: Partial<ConnectionConfiguration>, | ||
): Promise<void> | ||
|
||
export async function useSeeders( | ||
executeSeeders?: boolean, | ||
seedersOrOptions?: string[] | Partial<ConnectionConfiguration>, | ||
options?: Partial<ConnectionConfiguration>, | ||
) { | ||
const shouldExecuteSeeders = Boolean(executeSeeders) | ||
const seeders = Array.isArray(seedersOrOptions) ? seedersOrOptions : undefined | ||
const customOptions = Array.isArray(seedersOrOptions) ? options : seedersOrOptions | ||
entrySeeders: ClassConstructor<Seeder> | ClassConstructor<Seeder>[], | ||
customOptions?: Partial<ConnectionConfiguration>, | ||
): Promise<void> { | ||
if (customOptions) configureConnection(customOptions) | ||
|
||
configureConnection(customOptions) | ||
const option = await getConnectionOptions() | ||
const connection = await fetchConnection() | ||
|
||
let seederFiles = calculateFilePaths(option.seeders) | ||
if (seeders) { | ||
const seedersDesired = calculateFilePaths(seeders) | ||
seederFiles = seederFiles.filter((factoryFile) => seedersDesired.includes(factoryFile)) | ||
const seeders = Array.isArray(entrySeeders) ? entrySeeders : [entrySeeders] | ||
for (const seeder of seeders) { | ||
await new seeder().run(connection) | ||
} | ||
|
||
let seedersImported: Seeder[] | ||
try { | ||
seedersImported = await Promise.all(seederFiles.map((seederFile) => import(seederFile))) | ||
.then((elementsImported) => elementsImported.flatMap((e) => Object.values(e))) | ||
.then((elems) => elems.map((elem) => new elem()).filter((elem) => elem instanceof Seeder) as Seeder[]) | ||
|
||
if (shouldExecuteSeeders) { | ||
const connection = await fetchConnection() | ||
for (const seeder of seedersImported) { | ||
seeder.run(connection) | ||
} | ||
} | ||
} catch (error: any) { | ||
throw new SeederImportationError(error.message) | ||
} | ||
|
||
return seedersImported | ||
} |