xanascript 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/orm.js ADDED
@@ -0,0 +1,120 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+
4
+ export const TIPOS_MAP = {
5
+ TEXTO: "string",
6
+ NUMERO: "number",
7
+ BOOLEANO: "boolean",
8
+ DATA: "string",
9
+ QUALQUER: "any",
10
+ };
11
+
12
+ export function criarRepositorio(nomeTabela, props, diretorio) {
13
+ const dir = diretorio || process.cwd();
14
+ const dbFile = path.join(dir, `${nomeTabela}.json`);
15
+
16
+ let dados = [];
17
+ if (fs.existsSync(dbFile)) {
18
+ try {
19
+ dados = JSON.parse(fs.readFileSync(dbFile, "utf-8"));
20
+ } catch {
21
+ dados = [];
22
+ }
23
+ } else {
24
+ fs.writeFileSync(dbFile, "[]", "utf-8");
25
+ }
26
+
27
+ function salvar() {
28
+ fs.writeFileSync(dbFile, JSON.stringify(dados, null, 2), "utf-8");
29
+ }
30
+
31
+ function gerarId() {
32
+ if (dados.length === 0) return 1;
33
+ return Math.max(...dados.map(d => d.id || 0)) + 1;
34
+ }
35
+
36
+ function validar(entrada, parcial = false) {
37
+ const erros = [];
38
+ for (const p of props) {
39
+ const val = entrada[p.name];
40
+ if (val === undefined && !parcial) {
41
+ erros.push(`Campo "${p.name}" (${p.type}) é obrigatório`);
42
+ continue;
43
+ }
44
+ if (val === undefined) continue;
45
+
46
+ const tipoEsperado = TIPOS_MAP[p.type] || "any";
47
+ if (tipoEsperado === "string" && typeof val !== "string") {
48
+ erros.push(`Campo "${p.name}" espera TEXTO, recebeu ${typeof val}`);
49
+ } else if (tipoEsperado === "number" && typeof val !== "number") {
50
+ erros.push(`Campo "${p.name}" espera NUMERO, recebeu ${typeof val}`);
51
+ } else if (tipoEsperado === "boolean" && typeof val !== "boolean") {
52
+ erros.push(`Campo "${p.name}" espera BOOLEANO, recebeu ${typeof val}`);
53
+ }
54
+ }
55
+ return erros;
56
+ }
57
+
58
+ return {
59
+ criar(entrada) {
60
+ const erros = validar(entrada);
61
+ if (erros.length > 0) throw new Error("Erros de validação:\n" + erros.join("\n"));
62
+ const item = { id: gerarId(), ...entrada, criadoEm: new Date().toISOString() };
63
+ dados.push(item);
64
+ salvar();
65
+ return item;
66
+ },
67
+
68
+ listar() {
69
+ return [...dados];
70
+ },
71
+
72
+ buscar(id) {
73
+ return dados.find(d => d.id === id) || null;
74
+ },
75
+
76
+ atualizar(id, mudancas) {
77
+ const idx = dados.findIndex(d => d.id === id);
78
+ if (idx === -1) throw new Error(`Registro ${id} não encontrado em ${nomeTabela}`);
79
+ const erros = validar(mudancas, true);
80
+ if (erros.length > 0) throw new Error("Erros de validação:\n" + erros.join("\n"));
81
+ dados[idx] = { ...dados[idx], ...mudancas, atualizadoEm: new Date().toISOString() };
82
+ salvar();
83
+ return dados[idx];
84
+ },
85
+
86
+ deletar(id) {
87
+ const idx = dados.findIndex(d => d.id === id);
88
+ if (idx === -1) throw new Error(`Registro ${id} não encontrado em ${nomeTabela}`);
89
+ const removido = dados.splice(idx, 1)[0];
90
+ salvar();
91
+ return removido;
92
+ },
93
+
94
+ buscarOnde(filtro) {
95
+ return dados.filter(d => {
96
+ for (const [k, v] of Object.entries(filtro)) {
97
+ if (d[k] !== v) return false;
98
+ }
99
+ return true;
100
+ });
101
+ },
102
+
103
+ select(campos) {
104
+ return dados.map(d => {
105
+ const obj = {};
106
+ for (const c of campos) obj[c] = d[c];
107
+ return obj;
108
+ });
109
+ },
110
+
111
+ contar() {
112
+ return dados.length;
113
+ },
114
+
115
+ limpar() {
116
+ dados = [];
117
+ salvar();
118
+ },
119
+ };
120
+ }