Skip to content

Commit

Permalink
feat: seeder with non default export
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Nov 25, 2023
1 parent 6f91abb commit b16f8b2
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 30 deletions.
7 changes: 3 additions & 4 deletions src/seeder/executor.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { isObject } from 'locter';
import type { ObjectLiteral } from 'rapiq';
import { DataSource, MssqlParameter, Table } from 'typeorm';
import type { DataSourceOptions, QueryRunner } from 'typeorm';
import { MssqlParameter, Table } from 'typeorm';
import type { DataSource, DataSourceOptions, QueryRunner } from 'typeorm';
import type { MongoQueryRunner } from 'typeorm/driver/mongodb/MongoQueryRunner';
import { setDataSource } from '../data-source';
import { useEnv } from '../env';
import { adjustFilePaths, readTSConfig, resolveFilePath } from '../utils';
import type { TSConfig } from '../utils';
import { SeederEntity } from './entity';
import { prepareSeederFactories, useSeederFactoryManager } from './factory';
import { SeederExecutorOptions } from './type';
import type { SeederOptions, SeederPrepareElement } from './type';
import type { SeederExecutorOptions, SeederOptions, SeederPrepareElement } from './type';
import { prepareSeederSeeds } from './utils';

export class SeederExecutor {
Expand Down
40 changes: 24 additions & 16 deletions src/seeder/utils/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getModuleExport, load } from 'locter';
import { load } from 'locter';
import path from 'node:path';
import type { SeederConstructor, SeederPrepareElement } from '../type';
import { resolveFilePaths, resolveFilePatterns } from './file-path';
Expand Down Expand Up @@ -27,28 +27,36 @@ export async function prepareSeederSeeds(

for (let i = 0; i < seedFiles.length; i++) {
const moduleExports = await load(seedFiles[i]);
const moduleDefault = getModuleExport(moduleExports);

if (moduleDefault.value) {
const item = moduleDefault.value as SeederConstructor;
let clazzConstructor : SeederConstructor | undefined;

const exportKeys = Object.keys(moduleExports);
for (let j = 0; j < exportKeys.length; j++) {
const moduleExport = moduleExports[exportKeys[j]];
if (
typeof moduleExport === 'function' &&
moduleExport.prototype
) {
clazzConstructor = moduleExport;
}
}

if (clazzConstructor) {
const fileName = path.basename(seedFiles[i]);
const filePath = seedFiles[i];
const match = fileName.match(/^([0-9]{13,})-(.*)$/);

let timestamp : number | undefined;
if (match) {
items.push({
constructor: item,
timestamp: parseInt(match[1], 10),
fileName,
filePath,
});
} else {
items.push({
constructor: item,
fileName,
filePath,
});
timestamp = parseInt(match[1], 10);
}

items.push({
constructor: clazzConstructor,
fileName,
filePath,
...(timestamp ? { timestamp } : {}),
});
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/data/entity/role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Role {
@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;
}
22 changes: 22 additions & 0 deletions test/data/seed/role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { DataSource } from 'typeorm';
import type { Seeder, SeederFactoryManager } from '../../../src';
import { Role } from '../entity/role';

export class RoleSeeder implements Seeder {
track = true;

public async run(
dataSource: DataSource,
_factoryManager: SeederFactoryManager,
) : Promise<unknown> {
const repository = dataSource.getRepository(Role);

await repository.insert([
{
name: 'admin',
},
]);

return undefined;
}
}
3 changes: 2 additions & 1 deletion test/data/typeorm/data-source.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { DataSourceOptions } from 'typeorm';
import { DataSource } from 'typeorm';
import path from 'node:path';
import { Role } from '../entity/role';
import { User } from '../entity/user';
import type { SeederOptions } from '../../../src';

export const options : DataSourceOptions & SeederOptions = {
type: 'better-sqlite3',
entities: [User],
entities: [Role, User],
database: path.join(__dirname, 'db.sqlite'),
factories: ['test/data/factory/**/*.{ts,.js}'],
seeds: ['test/data/seed/**/*.{ts,js}'],
Expand Down
14 changes: 5 additions & 9 deletions test/unit/seeder/seeder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { DataSource } from 'typeorm';
import {
SeederExecutor,
runSeeder,
runSeeders,
useDataSource,
} from '../../../src';
import type { SeederEntity } from '../../../src';
import { User } from '../../data/entity/user';
Expand All @@ -11,20 +11,20 @@ import '../../data/factory/user';
import UserSeeder from '../../data/seed/user';

describe('src/seeder/index.ts', () => {
let dataSource : DataSource;

beforeEach(async () => {
await setupTestDatabase();
dataSource = await setupTestDatabase();
});

afterEach(async () => {
await destroyTestDatabase();
});

it('should seed with data-source options', async () => {
const dataSource = await useDataSource();

const executor = new SeederExecutor(dataSource);
let output = await executor.execute();
expect(output.length).toEqual(1);
expect(output.length).toEqual(2);

output = await executor.execute();
expect(output.length).toEqual(0);
Expand All @@ -37,8 +37,6 @@ describe('src/seeder/index.ts', () => {
});

it('should seed with explicit definitions', async () => {
const dataSource = await useDataSource();

await runSeeders(dataSource, {
seeds: [UserSeeder],
});
Expand All @@ -51,8 +49,6 @@ describe('src/seeder/index.ts', () => {
});

it('should seed with explicit definition', async () => {
const dataSource = await useDataSource();

const response = await runSeeder(dataSource, UserSeeder);
expect(response).toBeDefined();

Expand Down

0 comments on commit b16f8b2

Please sign in to comment.