Skip to content

Commit

Permalink
feature: revision module countries core
Browse files Browse the repository at this point in the history
  • Loading branch information
ldiego73 committed May 18, 2020
1 parent 72aab2d commit 27a65bb
Show file tree
Hide file tree
Showing 49 changed files with 2,999 additions and 3,147 deletions.
File renamed without changes.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Architecture diagrams

![Concentric Layers](images/architecture-2.png)

![Dependency Flow](images/architecture-3.png)

## Project structure

This project is structured by libraries and modules
Expand Down
Binary file added images/architecture-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"version": "1.0.0",
"packages": [
"libs/*",
"modules/**"
],
"version": "1.0.0"
"npmClient": "yarn"
}
1 change: 0 additions & 1 deletion links.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
https://herbertograca.com/2017/11/16/explicit-architecture-01-ddd-hexagonal-onion-clean-cqrs-how-i-put-it-all-together/#application-core-organisation
https://github.com/stemmlerjs/white-label/tree/master/src
https://khalilstemmler.com/articles/enterprise-typescript-nodejs/application-layer-use-cases/
https://github.com/sckv/almeriajs-ddd/
https://github.com/kgrzybek/modular-monolith-with-ddd/tree/master/src
https://github.com/ardalis/ddd-guestbook/tree/master/src/CleanArchitecture.Infrastructure
https://github.com/EduardoPires/EquinoxProject/tree/master/src
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion modules/countries/app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
reports/
lib/
lib/
.env
1 change: 1 addition & 0 deletions modules/countries/config/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
reports/
lib/
.env
26 changes: 25 additions & 1 deletion modules/countries/config/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
import { resolve } from 'path';
import { config } from 'dotenv';

config({ path: resolve(__dirname, '.env') });
export interface CountryDbEnv {
name: string;
type: string;
storage: string;
}

export interface CountryEnv {
db: CountryDbEnv;
production: boolean;
}

export const readEnv = (name: string = '.env'): CountryEnv => {
const path = resolve(process.cwd(), name);

config({ path });

return {
db: {
name: process.env.DB_NAME as string,
type: process.env.DB_TYPE as string,
storage: process.env.DB_STORAGE as string,
},
production: process.env.NODE_ENV === 'production',
};
};
10 changes: 1 addition & 9 deletions modules/countries/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
import './env';

export const DB = {
NAME: process.env.DB_NAME,
TYPE: process.env.DB_TYPE,
STORAGE: process.env.DB_STORAGE,
};

export const PRODUCTION = process.env.NODE_ENV === 'production';
export * from './env';
1 change: 1 addition & 0 deletions modules/countries/core/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
reports/
lib/
.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { UseCase, UseCaseUnexpectedError } from '@micro/kernel';
import { Either, Result } from '@micro/kernel';
import { CountryRepository, Iso, Country } from '@domain/index';
import { IsoInvalidError, CountryInvalidError } from '@domain/errors/index';
import { CountryRepositoryImpl } from '@infraestructure/repositories/index';
import { CountryDto } from '@application/dtos/index';
import { CountryAlreadyExistsError } from './create-country.error';

Expand All @@ -13,10 +12,10 @@ type Response<T> = Either<

export class CreateCountryUseCase
implements UseCase<CountryDto, Response<any>> {
private countryRepository: CountryRepository;
private repository: CountryRepository;

constructor() {
this.countryRepository = new CountryRepositoryImpl();
constructor(repository: CountryRepository) {
this.repository = repository;
}

async execute(request: CountryDto): Promise<Response<any>> {
Expand All @@ -40,14 +39,14 @@ export class CreateCountryUseCase
}

const country: Country = countryOrError.value;
const countryAlreadyExists = await this.countryRepository.exists(iso.value);
const countryAlreadyExists = await this.repository.exists(iso.value);

if (countryAlreadyExists) {
return Result.fail(CountryAlreadyExistsError.create(iso.value, null));
}

try {
await this.countryRepository.create(country);
await this.repository.create(country);
} catch (err) {
return Result.fail(UseCaseUnexpectedError.create(err));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export * from './create-country.error';

import { CountryRepositoryImpl } from '@infraestructure/repositories/index';
import { CreateCountryUseCase } from './create-country.use-case';
const createCountryUseCase = new CreateCountryUseCase();

const repository = new CountryRepositoryImpl();
const createCountryUseCase = new CreateCountryUseCase(repository);

export * from './create-country.error';
export * from './create-country.use-case';
export { createCountryUseCase };
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { UseCase, UseCaseUnexpectedError } from '@micro/kernel';
import { Either, Result } from '@micro/kernel';
import { CountryRepository, Iso } from '@domain/index';
import { IsoInvalidError } from '@domain/errors/index';
import { CountryRepositoryImpl } from '@infraestructure/repositories/index';
import { IsoDto } from '@application/dtos/index';

type Response<T> = Either<IsoInvalidError | UseCaseUnexpectedError, T>;

export class DeleteCountryUseCase implements UseCase<IsoDto, Response<any>> {
private countryRepository: CountryRepository;
private repository: CountryRepository;

constructor() {
this.countryRepository = new CountryRepositoryImpl();
constructor(repository: CountryRepository) {
this.repository = repository;
}

async execute(request: IsoDto): Promise<Response<any>> {
Expand All @@ -24,7 +23,7 @@ export class DeleteCountryUseCase implements UseCase<IsoDto, Response<any>> {
const iso = isoOrError.value;

try {
await this.countryRepository.delete(iso.value);
await this.repository.delete(iso.value);
} catch (err) {
return Result.fail(UseCaseUnexpectedError.create(err));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { CountryRepositoryImpl } from '@infraestructure/repositories/index';
import { DeleteCountryUseCase } from './delete.use-case';

const deleteCountryUseCase = new DeleteCountryUseCase();
const repository = new CountryRepositoryImpl();
const deleteCountryUseCase = new DeleteCountryUseCase(repository);

export * from './delete.use-case';
export { deleteCountryUseCase };
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { UseCase, UseCaseUnexpectedError } from '@micro/kernel';
import { Either, Result } from '@micro/kernel';
import { CountryRepository, Iso, Country } from '@domain/index';
import { IsoInvalidError } from '@domain/errors/index';
import { CountryRepositoryImpl } from '@infraestructure/repositories/index';
import { IsoDto, CountryDto } from '@application/dtos/index';
import { Transform } from '@micro/kernel/lib/application';
import { CountryTransform } from '@application/transforms/index';
Expand All @@ -11,12 +10,12 @@ type Response<T> = Either<IsoInvalidError | UseCaseUnexpectedError, T>;

export class FindCountryUseCase
implements UseCase<IsoDto, Response<CountryDto>> {
private countryRepository: CountryRepository;
private countryTransform: Transform<Country, CountryDto>;
private repository: CountryRepository;
private transform: Transform<Country, CountryDto>;

constructor() {
this.countryRepository = new CountryRepositoryImpl();
this.countryTransform = new CountryTransform();
constructor(repository: CountryRepository) {
this.repository = repository;
this.transform = new CountryTransform();
}

async execute(request: IsoDto): Promise<Response<CountryDto>> {
Expand All @@ -29,8 +28,8 @@ export class FindCountryUseCase
const iso = isoOrError.value;

try {
const country = await this.countryRepository.findByIso(iso.value);
return Result.ok(this.countryTransform.toDto(country));
const country = await this.repository.findByIso(iso.value);
return Result.ok(this.transform.toDto(country));
} catch (err) {
return Result.fail(UseCaseUnexpectedError.create(err));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { CountryRepositoryImpl } from '@infraestructure/repositories/index';
import { FindCountryUseCase } from './find-country.use-case';

const findCountryUseCase = new FindCountryUseCase();
const repository = new CountryRepositoryImpl();
const findCountryUseCase = new FindCountryUseCase(repository);

export * from './find-country.use-case';
export { findCountryUseCase };
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { CountryRepositoryImpl } from '@infraestructure/repositories/index';
import { ListCountryUseCase } from './list-country.use-case';

const listCountryUseCase = new ListCountryUseCase();
const repository = new CountryRepositoryImpl();
const listCountryUseCase = new ListCountryUseCase(repository);

export * from './list-country.use-case';
export { listCountryUseCase };
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { UseCase, UseCaseUnexpectedError } from '@micro/kernel';
import { Either, Result } from '@micro/kernel';
import { CountryRepository, Country } from '@domain/index';
import { IsoInvalidError, CountryInvalidError } from '@domain/errors/index';
import { CountryRepositoryImpl } from '@infraestructure/repositories/index';
import { IsoDto, CountryDto } from '@application/dtos/index';
import { Transform } from '@micro/kernel/lib/application';
import { CountryTransform } from '@application/transforms/index';
Expand All @@ -11,18 +10,18 @@ type Response<T> = Either<IsoInvalidError | CountryInvalidError, T>;

export class ListCountryUseCase
implements UseCase<IsoDto, Response<CountryDto[]>> {
private countryRepository: CountryRepository;
private countryTransform: Transform<Country, CountryDto>;
private repository: CountryRepository;
private transform: Transform<Country, CountryDto>;

constructor() {
this.countryRepository = new CountryRepositoryImpl();
this.countryTransform = new CountryTransform();
constructor(repository: CountryRepository) {
this.repository = repository;
this.transform = new CountryTransform();
}

async execute(): Promise<Response<CountryDto[]>> {
try {
const countries = await this.countryRepository.findAll();
return Result.ok(this.countryTransform.toColletion!(countries));
const countries = await this.repository.findAll();
return Result.ok(this.transform.toColletion!(countries));
} catch (err) {
return Result.fail(UseCaseUnexpectedError.create(err));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export * from './update-country.error';

import { CountryRepositoryImpl } from '@infraestructure/repositories/index';
import { UpdateCountryUseCase } from './update-country.use-case';
const updateCountryUseCase = new UpdateCountryUseCase();

const repository = new CountryRepositoryImpl();
const updateCountryUseCase = new UpdateCountryUseCase(repository);

export * from './update-country.error';
export * from './update-country.use-case';
export { updateCountryUseCase };
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { UseCase, UseCaseUnexpectedError } from '@micro/kernel';
import { Either, Result } from '@micro/kernel';
import { CountryRepository, Iso, Country } from '@domain/index';
import { IsoInvalidError, CountryInvalidError } from '@domain/errors';
import { CountryRepositoryImpl } from '@infraestructure/repositories/index';
import { CountryDto } from '@application/dtos';
import { CountryNotExistsError } from './update-country.error';

Expand All @@ -13,10 +12,10 @@ type Response<T> = Either<

export class UpdateCountryUseCase
implements UseCase<CountryDto, Response<any>> {
private countryRepository: CountryRepository;
private repository: CountryRepository;

constructor() {
this.countryRepository = new CountryRepositoryImpl();
constructor(repository: CountryRepository) {
this.repository = repository;
}

async execute(request: CountryDto): Promise<Response<any>> {
Expand All @@ -40,14 +39,14 @@ export class UpdateCountryUseCase
}

const country: Country = countryOrError.value;
const countryAlreadyExists = await this.countryRepository.exists(iso.value);
const countryAlreadyExists = await this.repository.exists(iso.value);

if (!countryAlreadyExists) {
return Result.fail(CountryNotExistsError.create(iso.value, null));
}

try {
await this.countryRepository.update(country);
await this.repository.update(country);
} catch (err) {
return Result.fail(UseCaseUnexpectedError.create(err));
}
Expand Down
1 change: 0 additions & 1 deletion modules/countries/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './infraestructure';
export * from './domain';
export * from './application';
12 changes: 7 additions & 5 deletions modules/countries/core/src/infraestructure/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Sequelize, Dialect } from 'sequelize';
import { DB, PRODUCTION } from '@micro/country-config';
import { readEnv } from '@micro/country-config';
import { Logger } from '@micro/logger';

const l = Logger.create('database');
const env = readEnv();

export const db = new Sequelize({
database: DB.NAME,
dialect: DB.TYPE as Dialect,
storage: DB.STORAGE,
database: env.db.name,
dialect: env.db.type as Dialect,
storage: env.db.storage,
});

if (!PRODUCTION) {
if (!env.production) {
l.info('Creando...');
db.sync({ force: true }).then(() => {
l.info('Tables created!!!');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ CountryModel.init(
id: {
type: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
Expand Down
2 changes: 1 addition & 1 deletion modules/countries/core/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const aliases = Object.keys(tsconfig.compilerOptions.paths).reduce(
module.exports = {
target: 'node',
entry: entries,
mode: 'production',
mode: 'development',
module: {
rules: [
{
Expand Down
12 changes: 12 additions & 0 deletions modules/countries/interfaces/console/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false
3 changes: 2 additions & 1 deletion modules/countries/interfaces/console/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
reports/
lib/
lib/
.env
7 changes: 7 additions & 0 deletions modules/countries/interfaces/console/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"endOfLine": "auto",
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5"
}
4 changes: 4 additions & 0 deletions modules/countries/interfaces/console/@types/package.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.json' {
const value: any;
export default value;
}
Loading

0 comments on commit 27a65bb

Please sign in to comment.