drivers/dist-import/pilao-de-dados/_enviar_registros.js
2024-06-28 14:03:35 -03:00

49 lines
2.3 KiB
JavaScript

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import node_fetch from "cross-fetch";
import { respostaComuns } from "p-respostas";
import { z } from "zod";
import { urlPilao, z_tipo_coluna_base_dados, } from "./variaveis";
export const zp_registrar_base_dados = z.object({
tabela: z.string(),
colunas: z.array(z.object({
coluna: z.string(),
tipo: z_tipo_coluna_base_dados,
})),
});
//enviar registros para base de dados
export const zp_enviar_registros = z.object({
tabela: z.string(),
registros: z.array(z.record(z.string(), z.object({
valor: z.any(),
tipo: z_tipo_coluna_base_dados.optional().nullable(),
}))),
});
export const enviar_registros = ({ conta, produto, emDesenvolvimento }) => (_a) => __awaiter(void 0, [_a], void 0, function* ({ registros, tabela, }) {
const url = new URL(`${urlPilao(emDesenvolvimento).api}/${Object.keys({ enviar_registros })[0]}/${produto}/${conta}`);
const tamanhoBlocos = 1000;
while (registros.length > 0) {
const bloco = registros
.splice(0, tamanhoBlocos)
.map((r) => Object.fromEntries(Object.entries(r).map(([k, v]) => [k, v === undefined ? null : v])));
const resp = yield node_fetch(url.toString(), {
method: "POST",
body: JSON.stringify({ tabela, registros: bloco }),
headers: { "Content-Type": "application/json" },
})
.then((r) => r.json())
.catch((e) => respostaComuns.erro("Erro ao enviar registros", [e.message]))
.then((r) => r);
if (resp.eErro) {
return resp;
}
}
return respostaComuns.valor(true);
});