Skip to content

Commit

Permalink
feat: adapt use seeders to new structure
Browse files Browse the repository at this point in the history
BREAKING CHANGE `useSeeders` change its definition and is now incompatible with previous version
  • Loading branch information
jorgebodega committed Jan 6, 2022
1 parent aecf7b4 commit 5bca471
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 47 deletions.
10 changes: 9 additions & 1 deletion src/connection/getConnectionOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConnectionOptionsReader } from 'typeorm'
import { DefaultSeederNotDefinedError } from '../errors/DefaultSeederNotDefinedError'
import type { ConnectionOptions } from '../types'
import { ConnectionConfigurationManager } from './ConnectionConfigurationManager'

Expand All @@ -11,10 +12,17 @@ export async function getConnectionOptions(): Promise<ConnectionOptions> {

const options = (await connectionReader.get(connection)) as ConnectionOptions

const seedersFromEnv = process.env.TYPEORM_SEEDING_SEEDS
const seedersFromEnv = process.env.TYPEORM_SEEDING_SEEDERS
const defaultSeederFromEnv = process.env.TYPEORM_SEEDING_DEFAULT_SEEDER
const defaultSeeder = defaultSeederFromEnv || options.defaultSeeder

if (!defaultSeeder) {
throw new DefaultSeederNotDefinedError()
}

return {
...options,
seeders: seedersFromEnv ? [seedersFromEnv] : options.seeders || [],
defaultSeeder,
}
}
5 changes: 5 additions & 0 deletions src/errors/DefaultSeederNotDefinedError.ts
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.`)
}
}
7 changes: 7 additions & 0 deletions src/seeder.ts
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)
}
}
}
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type { ConnectionOptions as TypeORMConnectionOptions } from 'typeorm'

/**
* Constructor of the seed class
*/
export type ClassConstructor<T> = new () => T

export type ConnectionOptions = TypeORMConnectionOptions & {
seeders: string[]
defaultSeeder: string
}

export type ConnectionConfiguration = {
Expand Down
57 changes: 14 additions & 43 deletions src/useSeeders.ts
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
}

0 comments on commit 5bca471

Please sign in to comment.