92 lines
3.5 KiB
JavaScript
92 lines
3.5 KiB
JavaScript
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(),
|
|
}))),
|
|
});
|
|
class ClassPilaoEnviar {
|
|
constructor({ conta, produto, emDesenvolvimento, ver_log, }) {
|
|
this.__registrosParaEnvio = [];
|
|
this.__codigosParaDeletar = [];
|
|
this.__conta = conta;
|
|
this.__produto = produto;
|
|
this.__emDesenvolvimento = emDesenvolvimento;
|
|
this.__ver_log = ver_log;
|
|
}
|
|
tabela(tabela) {
|
|
this.__tabela = tabela;
|
|
return this;
|
|
}
|
|
adicionarRegistroParaEnviar(...registro) {
|
|
this.__registrosParaEnvio.push(...registro);
|
|
return this;
|
|
}
|
|
async __salvar_enviar_registros() {
|
|
const registros = this.__registrosParaEnvio;
|
|
const url = new URL(`${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 node_fetch(url.toString(), {
|
|
method: "POST",
|
|
body: JSON.stringify({ tabela: this.__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);
|
|
}
|
|
async __salvar_deletar_registros() {
|
|
const codigos = [...this.__codigosParaDeletar];
|
|
const url = new URL(`${urlPilao(this.__emDesenvolvimento).api}/deletar_registros/${this.__produto}/${this.__conta}`);
|
|
const tamanhoBlocos = 1000;
|
|
while (codigos.length > 0) {
|
|
const bloco = codigos.splice(0, tamanhoBlocos);
|
|
const resp = await node_fetch(url.toString(), {
|
|
method: "POST",
|
|
body: JSON.stringify({ tabela: this.__tabela, codigos: 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;
|
|
}
|
|
}
|
|
this.__codigosParaDeletar;
|
|
return respostaComuns.valor(true);
|
|
}
|
|
async salvar() {
|
|
const re = await this.__salvar_enviar_registros();
|
|
if (re.eErro)
|
|
return re;
|
|
const rd = await this.__salvar_deletar_registros();
|
|
if (rd.eErro)
|
|
return rd;
|
|
return respostaComuns.valor(true);
|
|
}
|
|
}
|
|
export const PilaoEnviar = (_) => new ClassPilaoEnviar(_);
|