maestro connection with microservices - clients and modules

This commit is contained in:
rodrigo.zamboni
2022-03-18 10:16:27 -03:00
parent e15a680b9f
commit 1523c6b4d1
25 changed files with 1668 additions and 639 deletions
+21 -16
View File
@@ -1,17 +1,22 @@
// import { join } from 'path';
// import { ClientOptions, Transport } from '@nestjs/microservices';
import { ClientOptions, Transport } from '@nestjs/microservices';
import { TransformationProtofile } from '@victorradael/protospack';
import { credentials } from '@grpc/grpc-js';
// export const TransformationsClientConfiguration: ClientOptions = {
// transport: Transport.GRPC,
// options: {
// //url: `${process.env.USERS_SVC_URL}:${process.env.USERS_SVC_PORT}`,
// url: `127.0.0.1:50054`,
// package: 'transformation',
// protoPath: join(__dirname, '..', 'proto', 'transformation.proto'),
// loader: {
// enums: String,
// objects: true,
// arrays: true,
// },
// },
// };
export class TransformationsClientConfiguration {
config(): ClientOptions {
return {
transport: Transport.GRPC,
options: {
url: process.env.TRFACTORY_URL,
package: 'transformation',
// credentials: credentials.createSsl(),
protoPath: TransformationProtofile,
loader: {
enums: String,
objects: true,
arrays: true,
},
},
};
}
};
+151 -57
View File
@@ -1,65 +1,159 @@
// import {
// Controller,
// Delete,
// Get,
// OnModuleInit,
// Param,
// Post,
// Put,
// } from '@nestjs/common';
// import {
// Client,
// ClientGrpc,
// MessagePattern,
// Payload,
// } from '@nestjs/microservices';
// import { TransformationsService } from './transformations.service';
// import { CreateTransformationDto } from './dto/create-transformation.dto';
// import { UpdateTransformationDto } from './dto/update-transformation.dto';
// import { ApiTags } from '@nestjs/swagger';
// import { GrpcClientConfiguration } from './client.config';
import {
Controller,
Delete,
Get,
Inject,
OnModuleInit,
Param,
Post,
Put,
} from '@nestjs/common';
import {
Client,
ClientGrpc,
MessagePattern,
Payload,
} from '@nestjs/microservices';
import { ApiTags } from '@nestjs/swagger';
import { TransformationsServiceInterface } from '@victorradael/protospack';
import { ICreateTransformationElement, ICreateTransformationsRequest, IdRequest, IUpdateTransformationRequest } from './interfaces';
// @Controller('transformation')
// export class TransformationsClientService implements OnModuleInit {
// @Client(GrpcClientConfiguration)
// private transformationServiceClient: ClientGrpc;
@Controller('transformation')
export class TransformationsClientService implements OnModuleInit {
private transformationService: TransformationsServiceInterface;
constructor(
@Inject('TRANSFORMATIONS_PACKAGE') private readonly grpcClient: ClientGrpc,
) {}
// private transformationService: TransformationsService;
onModuleInit() {
this.transformationService =
this.grpcClient.getService<TransformationsServiceInterface>(
'TransformationService',
);
}
// onModuleInit() {
// this.transformationService =
// this.transformationServiceClient.getService<TransformationsService>(
// 'TransformationService',
// );
// }
@Post()
async create(createTransformationsDto: ICreateTransformationsRequest) {
console.log('TransformationClientService', 'Create');
// @Post()
// create(@Payload() createTransformationDto: CreateTransformationDto[]) {
// return createTransformationDto.map((transf) =>
// this.transformationService.create(transf),
// );
// }
const createTransformationResponse = await new Promise((resolve, reject) => {
this.transformationService.create(createTransformationsDto).subscribe({
next(x) {
resolve(x);
},
error(err) {
console.log('Observable Error');
reject(err);
},
complete() {
console.log('done');
},
});
})
.then((res) => res)
.catch((err) => {
throw new Error(err);
});
return createTransformationResponse;
}
// @Get()
// list() {
// return this.transformationService.list();
// }
async findAll() {
console.log('TransformationClientService', 'FindAll');
// @Get(':id')
// show(@Payload() id: string) {
// return this.transformationService.show(id);
// }
const findAllTransformationResponse = await new Promise((resolve, reject) => {
this.transformationService.findAll({}).subscribe({
next(x) {
resolve(x);
},
error(err) {
console.log('Observable Error');
reject(err);
},
complete() {
console.log('done');
},
});
})
.then((res) => res)
.catch((err) => {
throw new Error(err);
});
// @Put(':id')
// update(
// @Param() id: string,
// @Payload() updateTransformationDto: UpdateTransformationDto,
// ) {
// return this.transformationService.update(id, updateTransformationDto);
// }
return findAllTransformationResponse;
}
// @Delete(':id')
// remove(@Param() id: string) {
// return this.transformationService.remove(id);
// }
// }
async findOne({id}: IdRequest) {
console.log('TransformationClientService', 'FindOne');
const findOneTransformationResponse = await new Promise((resolve, reject) => {
this.transformationService.findOne({id}).subscribe({
next(x) {
resolve(x);
},
error(err) {
console.log('Observable Error');
reject(err);
},
complete() {
console.log('done');
},
});
})
.then((res) => res)
.catch((err) => {
throw new Error(err);
});
return findOneTransformationResponse;
}
async update(updateTransformationDTO: IUpdateTransformationRequest) {
console.log('TransformationClientService', 'Update');
const updateTransformationResponse = await new Promise((resolve, reject) => {
this.transformationService.update(updateTransformationDTO).subscribe({
next(x) {
resolve(x);
},
error(err) {
console.log('Observable Error');
reject(err);
},
complete() {
console.log('done');
},
});
})
.then((res) => res)
.catch((err) => {
throw new Error(err);
});
return updateTransformationResponse;
}
async remove({id}: IdRequest) {
console.log('TransformationClientService', 'Remove');
const removeTransformationResponse = await new Promise((resolve, reject) => {
this.transformationService.remove({id}).subscribe({
next(x) {
resolve(x);
},
error(err) {
console.log('Observable Error');
reject(err);
},
complete() {
console.log('done');
},
});
})
.then((res) => res)
.catch((err) => {
throw new Error(err);
});
return removeTransformationResponse;
}
}
+31
View File
@@ -0,0 +1,31 @@
export interface ICreateTransformationsRequest {
transformations: ICreateTransformationElement[];
}
interface Param {
base_column?: string;
column_name?: string;
n_digits?: number;
start_index?: number;
randbelow?: number;
const?: string;
}
export interface ICreateTransformationElement {
type: string;
params: Param[];
input_source: string;
table: string;
}
export interface IdRequest{
id:string;
}
export interface IUpdateTransformationRequest {
created_at?: string;
id?: string;
client_id?: string;
params: Param[];
type?: string;
input_source?: string;
table?: string;
}