generated from AnderGI/typescript-ddd-esm-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client TypeORM repository + Tests added
- Loading branch information
Showing
26 changed files
with
205 additions
and
33 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
5 changes: 3 additions & 2 deletions
5
src/apps/backoffice/backend/dependency-injection/application.json
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,8 +1,9 @@ | ||
{ | ||
"imports": [ | ||
{ "resource": "./contexts/backoffice/shared/application.json" }, | ||
{ "resource": "./shared/application.json" }, | ||
{ "resource": "./apps/application.json" } | ||
{ "resource": "./apps/application.json" }, | ||
{ "resource": "./contexts/backoffice/shared/application.json" }, | ||
{ "resource": "./contexts/backoffice/client/application.json" } | ||
|
||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
src/apps/backoffice/backend/dependency-injection/contexts/backoffice/client/application.json
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,8 @@ | ||
{ | ||
"services": { | ||
"backoffice.client.ClientRepository": { | ||
"class": "../../../../../../../contexts/backoffice/client/infrastructure/persistence/typeorm/TypeOrmClientRepository", | ||
"arguments": ["@backoffice.shared.TypeOrmClientFactory"] | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions
43
src/contexts/backoffice/client/infrastructure/persistence/typeorm/ClientEntity.entity.ts
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,43 @@ | ||
import { EntitySchema } from 'typeorm'; | ||
|
||
import { ValueObjectTransformer } from '../../../../../shared/infrastructure/persistence/typeorm/ValueObjectTransformer'; | ||
import Client from '../../../domain/Client'; | ||
import ClientCompany from '../../../domain/ClientCompany'; | ||
import ClientEmail from '../../../domain/ClientEmail'; | ||
import ClientId from '../../../domain/ClientId'; | ||
import ClientName from '../../../domain/ClientName'; | ||
import ClientPhone from '../../../domain/ClientPhone'; | ||
import ClientPosition from '../../../domain/ClientPosition'; | ||
|
||
export const ClientEntity = new EntitySchema<Client>({ | ||
name: 'Client', | ||
tableName: 'clients', | ||
target: Client, | ||
columns: { | ||
id: { | ||
type: String, | ||
primary: true, | ||
transformer: ValueObjectTransformer(ClientId) | ||
}, | ||
name: { | ||
type: String, | ||
transformer: ValueObjectTransformer(ClientName) | ||
}, | ||
email: { | ||
type: String, | ||
transformer: ValueObjectTransformer(ClientEmail) | ||
}, | ||
phone: { | ||
type: String, | ||
transformer: ValueObjectTransformer(ClientPhone) | ||
}, | ||
company: { | ||
type: String, | ||
transformer: ValueObjectTransformer(ClientCompany) | ||
}, | ||
position: { | ||
type: String, | ||
transformer: ValueObjectTransformer(ClientPosition) | ||
} | ||
} | ||
}); |
43 changes: 43 additions & 0 deletions
43
src/contexts/backoffice/client/infrastructure/persistence/typeorm/TypeOrmClientRepository.ts
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,43 @@ | ||
import { DataSource, EntitySchema, Repository } from 'typeorm'; | ||
|
||
import { Nullable } from '../../../../../../shared/domain/Nullable'; | ||
import Client from '../../../domain/Client'; | ||
import ClientRepository from '../../../domain/ClientRepository'; | ||
import { ClientEntity } from './ClientEntity.entity'; | ||
|
||
export class TypeOrmClientRepository implements ClientRepository { | ||
constructor(private readonly _client: Promise<DataSource>) {} | ||
|
||
public async save(client: Client): Promise<void> { | ||
return this.persist(client); | ||
} | ||
|
||
public async search(_: Client): Promise<Nullable<Client>> { | ||
const repository = await this.repository(); | ||
const retrievedUser = await repository.findOne({ | ||
where: { | ||
id: _.id.value | ||
} | ||
}); | ||
|
||
return retrievedUser; | ||
} | ||
|
||
public entitySchema(): EntitySchema<Client> { | ||
return ClientEntity; | ||
} | ||
|
||
private async client(): Promise<DataSource> { | ||
return this._client; | ||
} | ||
|
||
private async repository(): Promise<Repository<Client>> { | ||
return (await this.client()).getRepository(this.entitySchema()); | ||
} | ||
|
||
private async persist(aggregateRoot: Client): Promise<void> { | ||
const repository = await this.repository(); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
await repository.save(aggregateRoot); | ||
} | ||
} |
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
8 changes: 4 additions & 4 deletions
8
...lication/register/ClientRegistrar.test.ts → ...lication/register/ClientRegistrar.test.ts
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
2 changes: 1 addition & 1 deletion
2
...n/register/RegisterClientCommandMother.ts → ...n/register/RegisterClientCommandMother.ts
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as fs from 'faker'; | ||
|
||
import RegisterClientCommand from '../../../../../src/contexts/backoffice/client/application/register/RegisterClientCommand'; | ||
import Client from '../../../../../src/contexts/backoffice/client/domain/Client'; | ||
|
||
export class ClientMother { | ||
static create(): Client { | ||
const primitives = { | ||
id: fs.datatype.uuid(), | ||
name: fs.name.firstName(), | ||
email: fs.internet.email(), | ||
phone: fs.phone.phoneNumber(), | ||
company: fs.company.companyName(), | ||
position: fs.name.jobType() | ||
}; | ||
|
||
return Client.fromPrimitives(primitives); | ||
} | ||
|
||
static fromCommand(_: RegisterClientCommand): Client { | ||
const { data } = _; | ||
const primitives = { | ||
id: data.id, | ||
name: data.name, | ||
email: data.email, | ||
phone: data.phone, | ||
company: data.company, | ||
position: data.position | ||
}; | ||
|
||
return Client.fromPrimitives(primitives); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...ce/clients/domain/MockClientRepository.ts → ...ice/client/domain/MockClientRepository.ts
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
26 changes: 26 additions & 0 deletions
26
tests/contexts/backoffice/client/infrastructure/persistence/ClientRepositoryArrenger.ts
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,26 @@ | ||
import ClientRepository from '../../../../../../src/contexts/backoffice/client/domain/ClientRepository'; | ||
import { EnvironmentArranger } from '../../../../shared/infrastructure/arranger/EnvironmentArranger'; | ||
import { ClientMother } from '../../domain/ClientMother'; | ||
|
||
export default class ClientRepositoryArrenger { | ||
constructor( | ||
private readonly repository: ClientRepository, | ||
private readonly environmentArranger: EnvironmentArranger | ||
) {} | ||
|
||
public async saveClient(): Promise<void> { | ||
await this.cleanFirst(); | ||
const client = ClientMother.create(); | ||
await this.repository.save(client); | ||
await this.cleanEnd(); | ||
} | ||
|
||
private async cleanFirst(): Promise<void> { | ||
await this.environmentArranger.clean(); | ||
} | ||
|
||
private async cleanEnd(): Promise<void> { | ||
await this.environmentArranger.clean(); | ||
await this.environmentArranger.close(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tests/contexts/backoffice/client/infrastructure/persistence/ClientRepositoryFactory.ts
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,8 @@ | ||
import container from '../../../../../../src/apps/backoffice/backend/dependency-injection'; | ||
import ClientRepository from '../../../../../../src/contexts/backoffice/client/domain/ClientRepository'; | ||
|
||
export default class ClientRepositoryFactory { | ||
public static getRepository(): ClientRepository { | ||
return container.get('backoffice.client.ClientRepository'); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/contexts/backoffice/client/infrastructure/persistence/TypeOrmClientRepository.test.ts
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,15 @@ | ||
import EnvironmentArrengerFactory from '../../../../shared/infrastructure/arranger/EnvironmentArrengerFactory'; | ||
import ClientRepositoryArrenger from './ClientRepositoryArrenger'; | ||
import ClientRepositoryFactory from './ClientRepositoryFactory'; | ||
|
||
describe('TypeOrmClientRepository', () => { | ||
describe('#save', () => { | ||
it('should upsert a client', async () => { | ||
const arrenger = new ClientRepositoryArrenger( | ||
ClientRepositoryFactory.getRepository(), | ||
EnvironmentArrengerFactory.getArranger() | ||
); | ||
await arrenger.saveClient(); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.