melhorias de tipagem
This commit is contained in:
parent
1d5d3a48b4
commit
afa28a0699
32 changed files with 435 additions and 486 deletions
69
src/pilao-de-dados/_enviar_registros.ts
Normal file
69
src/pilao-de-dados/_enviar_registros.ts
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import node_fetch from "cross-fetch"
|
||||
import type { tipoResposta } from "p-respostas"
|
||||
import { respostaComuns } from "p-respostas"
|
||||
import { z } from "zod"
|
||||
import {
|
||||
PREFIXO,
|
||||
baseUrlPilao,
|
||||
z_tipo_coluna_base_dados,
|
||||
type zp_produto_conta,
|
||||
} 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 }: z.infer<typeof zp_produto_conta>) =>
|
||||
async ({
|
||||
registros,
|
||||
tabela,
|
||||
}: z.infer<typeof zp_enviar_registros>): Promise<tipoResposta<true>> => {
|
||||
const url = new URL(
|
||||
`${baseUrlPilao(
|
||||
emDesenvolvimento,
|
||||
)}${PREFIXO}/${Object.keys({ enviar_registros })[0]}/${produto}/${conta}`,
|
||||
)
|
||||
|
||||
const tamanhoBlocos = 1000
|
||||
|
||||
while (registros.length > 0) {
|
||||
const bloco = registros.splice(0, tamanhoBlocos)
|
||||
const resp = await 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 as tipoResposta<true>)
|
||||
|
||||
if (resp.eErro) {
|
||||
return resp
|
||||
}
|
||||
}
|
||||
|
||||
return respostaComuns.valor(true)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue