forked from w3tecch/typeorm-seeding
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add lazyattribute and subfactory as options in attribs
- Loading branch information
1 parent
4a2ce08
commit 48a3630
Showing
7 changed files
with
70 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import type { FactorizedAttrs, LazyAttributeCallback } from './types' | ||
import type { FactorizedAttr, LazyAttributeCallback } from './types' | ||
|
||
export class LazyAttribute<T> { | ||
constructor(private callback: LazyAttributeCallback<T>) {} | ||
export class LazyAttribute<T, V> { | ||
constructor(private callback: LazyAttributeCallback<T, V>) {} | ||
|
||
resolve(entity: T): FactorizedAttrs<T> { | ||
resolve(entity: T): FactorizedAttr<V> { | ||
return this.callback(entity) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { Factory } from './factory' | ||
import type { Constructable, FactorizedAttrs } from './types' | ||
|
||
export class Subfactory<T> { | ||
private factoryInstance: Factory<T> | ||
private values?: Partial<FactorizedAttrs<T>> | ||
private count?: number | ||
|
||
constructor(factory: Constructable<Factory<T>>) | ||
constructor(factory: Constructable<Factory<T>>, values?: Partial<FactorizedAttrs<T>>) | ||
constructor(factory: Constructable<Factory<T>>, count?: number) | ||
constructor(factory: Constructable<Factory<T>>, values?: Partial<FactorizedAttrs<T>>, count?: number) | ||
|
||
constructor( | ||
factory: Constructable<Factory<T>>, | ||
countOrValues?: Partial<FactorizedAttrs<T>> | number, | ||
count?: number, | ||
) { | ||
this.factoryInstance = new factory() | ||
this.values = typeof countOrValues === 'number' ? undefined : countOrValues | ||
this.count = typeof countOrValues === 'number' ? countOrValues : count | ||
} | ||
|
||
create() { | ||
if (this.count) { | ||
return this.factoryInstance.createMany(this.count, this.values) | ||
} | ||
|
||
return this.factoryInstance.create(this.values) | ||
} | ||
|
||
make() { | ||
if (this.count) { | ||
return this.factoryInstance.makeMany(this.count, this.values) | ||
} | ||
|
||
return this.factoryInstance.make(this.values) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,7 @@ export class User { | |
|
||
@Column() | ||
lastName!: string | ||
|
||
@Column() | ||
phone!: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
import faker from '@faker-js/faker' | ||
import { Factory } from '../../src/factory' | ||
import { LazyAttribute } from '../../src/lazyAttribute' | ||
import { Subfactory } from '../../src/subfactory' | ||
import { Pet } from '../entities/Pet.entity' | ||
import { UserFactory } from './User.factory' | ||
|
||
export class PetFactory extends Factory<Pet> { | ||
protected entity = Pet | ||
protected attrs = { | ||
name: faker.name.findName(), | ||
lastName: async () => faker.name.findName(), | ||
// owner: new UserFactory() as any, | ||
owner: new LazyAttribute( | ||
(instance: Pet) => | ||
new Subfactory(UserFactory, { | ||
name: `${instance.name} owner`, | ||
}), | ||
), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import faker from '@faker-js/faker' | ||
import { Factory } from '../../src/factory' | ||
import { LazyAttribute } from '../../src/lazyAttribute' | ||
import { Subfactory } from '../../src/subfactory' | ||
import { User } from '../entities/User.entity' | ||
import { PetFactory } from './Pet.factory' | ||
|
||
export class UserFactory extends Factory<User> { | ||
protected entity = User | ||
protected attrs = { | ||
name: faker.name.findName(), | ||
lastName: () => faker.name.lastName(), | ||
lastName: new LazyAttribute((instance: User) => faker.name.lastName()), | ||
phone: new LazyAttribute((instance: User) => faker.random.number()), | ||
} | ||
} |