From f82c65505fb9e8ee48a81e1d55c9504795116274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=A4slin?= Date: Tue, 17 Jan 2023 11:02:28 +0100 Subject: [PATCH] fix: generic type for seeder meta * 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 --- src/seeder/factory/manager.ts | 8 ++++---- src/seeder/factory/module.ts | 10 +++++----- src/seeder/factory/type.ts | 8 ++++---- src/seeder/factory/utils.ts | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/seeder/factory/manager.ts b/src/seeder/factory/manager.ts index eaf8b866..66d26e79 100644 --- a/src/seeder/factory/manager.ts +++ b/src/seeder/factory/manager.ts @@ -6,9 +6,9 @@ import { SeederFactory } from './module'; export class SeederFactoryManager { public readonly items : Record = {}; - set>( + set, Meta = unknown>( entity: ObjectType | EntitySchema, - factoryFn: FactoryCallback, + factoryFn: FactoryCallback, ) : SeederFactoryConfig { const name = getEntityName(entity); @@ -20,9 +20,9 @@ export class SeederFactoryManager { return this.items[name]; } - get>( + get, Meta = unknown>( entity: ObjectType | EntitySchema, - ) : SeederFactory { + ) : SeederFactory { const name = getEntityName(entity); if (!hasOwnProperty(this.items, name)) { diff --git a/src/seeder/factory/module.ts b/src/seeder/factory/module.ts index 31465a39..f465973b 100644 --- a/src/seeder/factory/module.ts +++ b/src/seeder/factory/module.ts @@ -5,20 +5,20 @@ import { hasOwnProperty } from '../../utils'; import { useDataSource } from '../../data-source'; import { isPromise } from '../utils'; -export class SeederFactory> { - public readonly context: SeederFactoryContext; +export class SeederFactory, Meta = unknown> { + public readonly context: SeederFactoryContext; - public meta: unknown; + public meta: Meta; // -------------------------------------------------------------- - constructor(context: SeederFactoryContext) { + constructor(context: SeederFactoryContext) { this.context = context; } // -------------------------------------------------------------- - public setMeta(value: unknown) { + public setMeta(value: Meta) { this.meta = value; return this; diff --git a/src/seeder/factory/type.ts b/src/seeder/factory/type.ts index 7c73a355..3be5c77b 100644 --- a/src/seeder/factory/type.ts +++ b/src/seeder/factory/type.ts @@ -1,15 +1,15 @@ import { Faker } from '@faker-js/faker'; import { EntitySchema, ObjectType } from 'typeorm'; -export type FactoryCallback = (faker: Faker, meta: unknown) => O | Promise; +export type FactoryCallback = (faker: Faker, meta: Meta) => O | Promise; export type SeederFactoryConfig = { - factoryFn: FactoryCallback, + factoryFn: FactoryCallback, entity: ObjectType | EntitySchema }; -export type SeederFactoryContext = { +export type SeederFactoryContext = { name: string, entity: ObjectType | EntitySchema, - factoryFn: FactoryCallback + factoryFn: FactoryCallback }; diff --git a/src/seeder/factory/utils.ts b/src/seeder/factory/utils.ts index 74e08609..c38ebbd1 100644 --- a/src/seeder/factory/utils.ts +++ b/src/seeder/factory/utils.ts @@ -14,9 +14,9 @@ export function useSeederFactoryManager() { return instance; } -export function setSeederFactory>( +export function setSeederFactory, Meta = unknown>( entity: ObjectType | EntitySchema, - factoryFn: FactoryCallback, + factoryFn: FactoryCallback, ) : SeederFactoryConfig { const manager = useSeederFactoryManager(); return manager.set(entity, factoryFn);