You can use the internal API to load fixtures within your own application code. This involves using Loader, Resolver, Builder, and fixturesIterator from the typeorm-fixtures-cli/dist package.
import * as path from 'path';
import { Builder, fixturesIterator, Loader, Parser, Resolver } from 'typeorm-fixtures-cli/dist';
import { createConnection, getRepository } from 'typeorm';
import { CommandUtils } from 'typeorm/commands/CommandUtils';
const loadFixtures = async (fixturesPath: string) => {
let dataSource: DataSource | undefined = undefined;
try {
dataSource = await CommandUtils.loadDataSource(dataSourcePath);
await dataSource.initialize();
await dataSource.synchronize(true);
const loader = new Loader();
loader.load(path.resolve(fixturesPath));
const resolver = new Resolver();
const fixtures = resolver.resolve(loader.fixtureConfigs);
const builder = new Builder(connection, new Parser(), false);
for (const fixture of fixturesIterator(fixtures)) {
const entity: any = await builder.build(fixture);
await dataSource.getRepository(fixture.entity).save(entity);
}
} catch (err) {
throw err;
} finally {
if (dataSource) {
await dataSource.destroy();
}
}
};
loadFixtures('./fixtures');