Skip to content

Commit

Permalink
fix: attrs failing with lazy instances
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebodega committed Feb 9, 2022
1 parent 3efe94e commit c8ddda8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export abstract class Factory<T> {
*/
async create(overrideParams: Partial<FactorizedAttrs<T>> = {}, saveOptions?: SaveOptions): Promise<T> {
const attrs = { ...this.attrs, ...overrideParams }
const entity = await this.makeEntity(attrs, true)
const preloadedAttrs = Object.entries(attrs).filter(([, value]) => !(value instanceof LazyInstanceAttribute))

const entity = await this.makeEntity(Object.fromEntries(preloadedAttrs) as FactorizedAttrs<T>, true)

const em = (await fetchConnection()).createEntityManager()
const savedEntity = await em.save<T>(entity, saveOptions)
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export * from './lazyAttribute'
export * from './instanceAttribute'
export * from './lazyInstanceAttribute'
export * from './seeder'
export * from './subfactory'
export * from './types'
export * from './useSeeders'
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export type ConnectionConfiguration = {
export type Constructable<T> = new () => T
export type FactorizedAttr<V> = V | (() => V | Promise<V>) | Subfactory<V extends Array<infer U> ? U : V>
export type FactorizedAttrs<T> = {
[K in keyof Partial<T>]: FactorizedAttr<T[K]> | LazyAttribute<T, T[K]>
[K in keyof Partial<T>]: FactorizedAttr<T[K]> | LazyAttribute<T, FactorizedAttr<T[K]>>
}
export type LazyAttributeCallback<T, V> = (entity: T) => FactorizedAttr<V>
export type LazyAttributeCallback<T, V> = (entity: T) => V

0 comments on commit c8ddda8

Please sign in to comment.