Skip to content

Commit ac65f0f

Browse files
committed
add faker generator and settings
1 parent 8f0ea2f commit ac65f0f

File tree

6 files changed

+35
-3
lines changed

6 files changed

+35
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { faker } from '@faker-js/faker';
2+
import { AbstractGenerator } from './generators';
3+
4+
export class FakerGenerator extends AbstractGenerator<string> {
5+
public validate(): boolean {
6+
if (this.column.template === undefined)
7+
throw new Error(`faker template required for type faker: ${this.table.name}.${this.column.name}`);
8+
return true;
9+
}
10+
11+
public generate(rowIndex: number, row: { [key: string]: any; }): string {
12+
faker.locale = this.column.locale || 'en';
13+
return faker.fake(this.column.template!);
14+
}
15+
}

src/generation/generators/generators.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export enum Generators {
1313
values = 'values',
1414
foreignKey = 'foreignKey',
1515
function = 'function',
16+
faker = 'faker',
1617
}
1718

1819
export abstract class AbstractGenerator<T>{
@@ -22,7 +23,7 @@ export abstract class AbstractGenerator<T>{
2223
protected column: CustomizedColumn,
2324
) { }
2425

25-
public static validate(table: CustomizedTable, column: CustomizedColumn): boolean { return true };
26+
public static validate(table: CustomizedTable, column: CustomizedColumn): boolean { return true; };
2627

2728
public async init(): Promise<AbstractGenerator<T>> { return this; }
2829

src/generation/generators/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CustomizedColumn, CustomizedTable } from '../../schema/customized-schem
44
import { BitGenerator } from './bit.generator';
55
import { BooleanGenerator } from './boolean.generator';
66
import { DateGenerator } from './date.generator';
7+
import { FakerGenerator } from './faker.generator';
78
import { ForeignKeyGenerator } from './foreignkey.generator';
89
import { FunctionGenerator } from './function.generator';
910
import { AbstractGenerator, Generators } from './generators';
@@ -55,6 +56,9 @@ export class GeneratorBuilder {
5556
case Generators.function:
5657
FunctionGenerator.validate(table, column);
5758
break;
59+
case Generators.faker:
60+
FakerGenerator.validate(table, column);
61+
break;
5862
case Generators.none:
5963
default:
6064
throw new Error(`No generator defined for column: ${table.name}.${column.name}`);
@@ -98,6 +102,9 @@ export class GeneratorBuilder {
98102
case Generators.function:
99103
generator = new FunctionGenerator(this.random, this.table, column);
100104
break;
105+
case Generators.faker:
106+
generator = new FakerGenerator(this.random, this.table, column);
107+
break;
101108
case Generators.none:
102109
default:
103110
throw new Error(`No generator defined for column: ${this.table.name}.${column.name}`);

src/schema/custom-schema.class.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ export class CustomTable {
8383
addLines?: number;
8484
@IsBoolean()
8585
disableTriggers?: boolean;
86+
@IsString()
87+
template?: any;
8688
}
8789

8890
type CustomColumn = { name: string; } & Partial<Column>;

src/schema/customized-schema.class.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export class CustomizedSchema extends CustomSchema {
7878
if (customColumn.unique !== undefined) customizedColumnBuilder.set('unique', customColumn.unique);
7979
if (customColumn.unsigned !== undefined) customizedColumnBuilder.set('unsigned', customColumn.unsigned);
8080
if (customColumn.customFunction !== undefined) customizedColumnBuilder.set('customFunction', customColumn.customFunction);
81+
if (customColumn.template !== undefined) customizedColumnBuilder.set('template', customColumn.template);
8182
if (customColumn?.foreignKey) {
8283
customizedColumnBuilder.set('foreignKey', customColumn.foreignKey);
8384
customizedTable.referencedTables.push(customColumn.foreignKey.table);

src/schema/schema.class.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { IsArray, ValidateNested, IsString, IsBoolean, IsNumber, IsOptional, validateOrReject } from 'class-validator';
2-
import { Type, plainToClass, classToPlain } from 'class-transformer';
1+
import { classToPlain, plainToClass, Type } from 'class-transformer';
2+
import { IsArray, IsBoolean, IsNumber, IsOptional, IsString, ValidateNested, validateOrReject } from 'class-validator';
33
import { Generators } from '../generation/generators/generators';
44

55
export class Schema {
@@ -82,4 +82,10 @@ export class Column {
8282
@IsOptional()
8383
@IsString()
8484
customFunction?: string;
85+
@IsOptional()
86+
@IsString()
87+
template?: string;
88+
@IsOptional()
89+
@IsString()
90+
locale?: string;
8591
}

0 commit comments

Comments
 (0)