Skip to content

Commit

Permalink
feat: separate different types of lazy attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebodega committed Feb 9, 2022
1 parent 2b2883b commit 3efe94e
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ tmp
old

### Arch linux error ###
~
~
.vscode/
24 changes: 0 additions & 24 deletions .vscode/launch.json

This file was deleted.

3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

22 changes: 16 additions & 6 deletions src/factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SaveOptions } from 'typeorm'
import { fetchConnection } from './connection'
import { LazyAttribute } from './lazyAttribute'
import { InstanceAttribute } from './instanceAttribute'
import { LazyInstanceAttribute } from './lazyInstanceAttribute'
import { Subfactory } from './subfactory'
import type { Constructable, FactorizedAttrs } from './types'

Expand Down Expand Up @@ -38,11 +39,11 @@ export abstract class Factory<T> {
const attrs = { ...this.attrs, ...overrideParams }
const entity = await this.makeEntity(attrs, true)

const connection = await fetchConnection()
const savedEntity = await connection.createEntityManager().save<T>(entity, saveOptions)
await this.applyLazyAttributes(savedEntity, attrs, true)
const em = (await fetchConnection()).createEntityManager()
const savedEntity = await em.save<T>(entity, saveOptions)

return savedEntity
await this.applyLazyAttributes(savedEntity, attrs, true)
return em.save<T>(savedEntity, saveOptions)
}

/**
Expand All @@ -69,13 +70,22 @@ export abstract class Factory<T> {
}),
)

await Promise.all(
Object.entries(attrs).map(async ([key, value]) => {
if (value instanceof InstanceAttribute) {
const newAttrib = value.resolve(entity)
Object.assign(entity, { [key]: await Factory.resolveValue(newAttrib, shouldPersist) })
}
}),
)

return entity
}

private async applyLazyAttributes(entity: T, attrs: FactorizedAttrs<T>, shouldPersist: boolean) {
await Promise.all(
Object.entries(attrs).map(async ([key, value]) => {
if (value instanceof LazyAttribute) {
if (value instanceof LazyInstanceAttribute) {
const newAttrib = value.resolve(entity)
Object.assign(entity, { [key]: await Factory.resolveValue(newAttrib, shouldPersist) })
}
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from './connection'
export * from './factory'
export * from './lazyAttribute'
export * from './instanceAttribute'
export * from './lazyInstanceAttribute'
export * from './seeder'
export * from './types'
export * from './useSeeders'
3 changes: 3 additions & 0 deletions src/instanceAttribute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LazyAttribute } from './lazyAttribute'

export class InstanceAttribute<T, V> extends LazyAttribute<T, V> {}
2 changes: 1 addition & 1 deletion src/lazyAttribute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FactorizedAttr, LazyAttributeCallback } from './types'

export class LazyAttribute<T, V> {
export abstract class LazyAttribute<T, V> {
constructor(private callback: LazyAttributeCallback<T, V>) {}

resolve(entity: T): FactorizedAttr<V> {
Expand Down
3 changes: 3 additions & 0 deletions src/lazyInstanceAttribute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LazyAttribute } from './lazyAttribute'

export class LazyInstanceAttribute<T, V> extends LazyAttribute<T, V> {}
9 changes: 5 additions & 4 deletions test/factories/Pet.factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import faker from '@faker-js/faker'
import { Factory } from '../../src/factory'
import { LazyAttribute } from '../../src/lazyAttribute'
import { InstanceAttribute } from '../../src/instanceAttribute'
import { LazyInstanceAttribute } from '../../src/lazyInstanceAttribute'
import { Subfactory } from '../../src/subfactory'
import type { FactorizedAttrs } from '../../src/types'
import { Pet } from '../entities/Pet.entity'
Expand All @@ -11,9 +12,9 @@ export class PetFactory extends Factory<Pet> {
protected attrs: FactorizedAttrs<Pet> = {
name: faker.name.findName(),
lastName: async () => faker.name.findName(),
address: new LazyAttribute((instance) => async () => `${instance.name.toLowerCase()} address`),
email: new LazyAttribute((instance) => `${instance.name.toLowerCase()}@example.com`),
owner: new LazyAttribute(
address: new InstanceAttribute((instance) => async () => `${instance.name.toLowerCase()} address`),
email: new InstanceAttribute((instance) => `${instance.name.toLowerCase()}@example.com`),
owner: new LazyInstanceAttribute(
(instance) =>
new Subfactory(UserFactory, {
name: () => `${instance.name} owner`,
Expand Down
8 changes: 4 additions & 4 deletions test/factories/User.factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import faker from '@faker-js/faker'
import { faker } from '@faker-js/faker'
import { Factory } from '../../src/factory'
import { LazyAttribute } from '../../src/lazyAttribute'
import { InstanceAttribute } from '../../src/instanceAttribute'
import { Subfactory } from '../../src/subfactory'
import type { FactorizedAttrs } from '../../src/types'
import { User } from '../entities/User.entity'
Expand All @@ -11,8 +11,8 @@ export class UserFactory extends Factory<User> {
protected attrs: FactorizedAttrs<User> = {
name: faker.name.findName(),
lastName: async () => faker.name.findName(),
address: new LazyAttribute((instance) => async () => `${instance.name.toLowerCase()} address`),
email: new LazyAttribute((instance) => `${instance.name.toLowerCase()}@example.com`),
address: new InstanceAttribute((instance) => async () => `${instance.name.toLowerCase()} address`),
email: new InstanceAttribute((instance) => `${instance.name.toLowerCase()}@example.com`),
pets: new Subfactory(PetFactory, 2),
}
}
2 changes: 1 addition & 1 deletion test/seeders/User.seeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export class UserSeeder extends Seeder {
async run(connection: Connection) {
await new UserFactory().createMany(10)

await this.call(connection, [PetSeeder])
await this.call(connection, [])
}
}

0 comments on commit 3efe94e

Please sign in to comment.