68 lines
2.7 KiB
JavaScript
68 lines
2.7 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.PilaoEnviar = exports.zp_enviar_registros = exports.zp_registrar_base_dados = void 0;
|
|
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
const p_respostas_1 = require("p-respostas");
|
|
const zod_1 = require("zod");
|
|
const variaveis_1 = require("./variaveis");
|
|
exports.zp_registrar_base_dados = zod_1.z.object({
|
|
tabela: zod_1.z.string(),
|
|
colunas: zod_1.z.array(zod_1.z.object({
|
|
coluna: zod_1.z.string(),
|
|
tipo: variaveis_1.z_tipo_coluna_base_dados,
|
|
})),
|
|
});
|
|
//enviar registros para base de dados
|
|
exports.zp_enviar_registros = zod_1.z.object({
|
|
tabela: zod_1.z.string(),
|
|
registros: zod_1.z.array(zod_1.z.record(zod_1.z.string(), zod_1.z.object({
|
|
valor: zod_1.z.any(),
|
|
tipo: variaveis_1.z_tipo_coluna_base_dados.optional().nullable(),
|
|
}))),
|
|
});
|
|
class ClassPilaoEnviar {
|
|
constructor({ conta, produto, emDesenvolvimento, ver_log, }) {
|
|
this.__registros = [];
|
|
this.__conta = conta;
|
|
this.__produto = produto;
|
|
this.__emDesenvolvimento = emDesenvolvimento;
|
|
this.__ver_log = ver_log;
|
|
}
|
|
tabela(tabela) {
|
|
this.__tabela = tabela;
|
|
return this;
|
|
}
|
|
adicionarRegistro(...registro) {
|
|
this.__registros.push(...registro);
|
|
return this;
|
|
}
|
|
async enviar() {
|
|
const registros = this.__registros;
|
|
const url = new URL(`${(0, variaveis_1.urlPilao)(this.__emDesenvolvimento).api}/enviar_registros/${this.__produto}/${this.__conta}`);
|
|
if (this.__ver_log)
|
|
console.log(`[PILÃO]: Enviando "${registros.length}" registros na tabela "${this.__tabela}" para "${url}".`);
|
|
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 = await (0, cross_fetch_1.default)(url.toString(), {
|
|
method: "POST",
|
|
body: JSON.stringify({ tabela: this.__tabela, registros: bloco }),
|
|
headers: { "Content-Type": "application/json" },
|
|
})
|
|
.then((r) => r.json())
|
|
.catch((e) => p_respostas_1.respostaComuns.erro("Erro ao enviar registros", [e.message]))
|
|
.then((r) => r);
|
|
if (resp.eErro) {
|
|
return resp;
|
|
}
|
|
}
|
|
return p_respostas_1.respostaComuns.valor(true);
|
|
}
|
|
}
|
|
const PilaoEnviar = (_) => new ClassPilaoEnviar(_);
|
|
exports.PilaoEnviar = PilaoEnviar;
|