Skip to content

Commit

Permalink
fix: generic type for seeder meta
Browse files Browse the repository at this point in the history
* fix: adjust type of meta to never in order to override it with a specific type

* fix: add a Meta type with default value unknown
  • Loading branch information
ckaeslin authored Jan 17, 2023
1 parent d2d4919 commit f82c655
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/seeder/factory/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { SeederFactory } from './module';
export class SeederFactoryManager {
public readonly items : Record<string, SeederFactoryConfig> = {};

set<O extends Record<string, any>>(
set<O extends Record<string, any>, Meta = unknown>(
entity: ObjectType<O> | EntitySchema<O>,
factoryFn: FactoryCallback<O>,
factoryFn: FactoryCallback<O, Meta>,
) : SeederFactoryConfig {
const name = getEntityName(entity);

Expand All @@ -20,9 +20,9 @@ export class SeederFactoryManager {
return this.items[name];
}

get<O extends Record<string, any>>(
get<O extends Record<string, any>, Meta = unknown>(
entity: ObjectType<O> | EntitySchema<O>,
) : SeederFactory<O> {
) : SeederFactory<O, Meta> {
const name = getEntityName(entity);

if (!hasOwnProperty(this.items, name)) {
Expand Down
10 changes: 5 additions & 5 deletions src/seeder/factory/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import { hasOwnProperty } from '../../utils';
import { useDataSource } from '../../data-source';
import { isPromise } from '../utils';

export class SeederFactory<O extends Record<string, any>> {
public readonly context: SeederFactoryContext<O>;
export class SeederFactory<O extends Record<string, any>, Meta = unknown> {
public readonly context: SeederFactoryContext<O, Meta>;

public meta: unknown;
public meta: Meta;

// --------------------------------------------------------------

constructor(context: SeederFactoryContext<O>) {
constructor(context: SeederFactoryContext<O, Meta>) {
this.context = context;
}

// --------------------------------------------------------------

public setMeta(value: unknown) {
public setMeta(value: Meta) {
this.meta = value;

return this;
Expand Down
8 changes: 4 additions & 4 deletions src/seeder/factory/type.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Faker } from '@faker-js/faker';
import { EntitySchema, ObjectType } from 'typeorm';

export type FactoryCallback<O> = (faker: Faker, meta: unknown) => O | Promise<O>;
export type FactoryCallback<O, Meta = unknown> = (faker: Faker, meta: Meta) => O | Promise<O>;

export type SeederFactoryConfig = {
factoryFn: FactoryCallback<any>,
factoryFn: FactoryCallback<any, any>,
entity: ObjectType<any> | EntitySchema<any>
};

export type SeederFactoryContext<O> = {
export type SeederFactoryContext<O, Meta = unknown> = {
name: string,
entity: ObjectType<O> | EntitySchema<O>,
factoryFn: FactoryCallback<O>
factoryFn: FactoryCallback<O, Meta>
};
4 changes: 2 additions & 2 deletions src/seeder/factory/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export function useSeederFactoryManager() {
return instance;
}

export function setSeederFactory<O extends Record<string, any>>(
export function setSeederFactory<O extends Record<string, any>, Meta = unknown>(
entity: ObjectType<O> | EntitySchema<O>,
factoryFn: FactoryCallback<O>,
factoryFn: FactoryCallback<O, Meta>,
) : SeederFactoryConfig {
const manager = useSeederFactoryManager();
return manager.set(entity, factoryFn);
Expand Down

0 comments on commit f82c655

Please sign in to comment.