import { credentials, loadPackageDefinition } from "@grpc/grpc-js";
import { loadSync } from "@grpc/proto-loader";
import moment from "moment";

// Ler as definições do protobuffer
const tasksDefs = loadSync("./tasks.proto"); 
const tasksProto = loadPackageDefinition(tasksDefs);

const clientGRPC = new tasksProto.TaskService('127.0.0.1:5050', credentials.createInsecure());

clientGRPC.findAll({}, (err, tasks) => {
  if (err){
    console.log(err);
    return;
  }

  console.log(tasks)
});

const obj = {id: 2, title: 'picolo', type: 'teste', created_at: moment().format()}
clientGRPC.insertOne(obj, (err, task) => {
  if (err){
    console.log(err);
    return;
  }

  console.log(task)  
})