Skip to content

Commit 08a90d3

Browse files
committed
Refatora gerador de dados
1 parent af0c762 commit 08a90d3

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

gerar-dados/index.js

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ const { faker } = require('@faker-js/faker');
22

33
const lojas = 20, produtos = 200, vendas = 1000
44

5-
// Função auxiliar para gerar uma string formatada para o insert
6-
function formatInsert(table, columns, values) {
5+
function formatInsert(tableName, columns, values) {
76
const newValues = values.map(val => {
87
if(typeof val !== 'string')
98
return val
@@ -13,10 +12,9 @@ function formatInsert(table, columns, values) {
1312
return quotes ? `'${val}'` : val
1413
})
1514

16-
return `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${newValues.join(', ')});`;
15+
return `INSERT INTO ${tableName} (${columns.join(', ')}) VALUES (${newValues.join(', ')});`;
1716
}
1817

19-
// Gerar inserts para a tabela cliente
2018
function generateClienteInserts(numInserts) {
2119
const inserts = [];
2220

@@ -29,7 +27,7 @@ function generateClienteInserts(numInserts) {
2927
const insert = formatInsert('cliente', ['nome', 'cpf', 'cidade_id', 'data_nascimento'], [
3028
`'${nome}'`,
3129
`'${cpf}'`,
32-
`${cidade_id}`,
30+
cidade_id,
3331
`'${data_nascimento}'`
3432
]);
3533

@@ -39,7 +37,6 @@ function generateClienteInserts(numInserts) {
3937
return inserts;
4038
}
4139

42-
// Gerar inserts para a tabela loja
4340
function generateLojaInserts() {
4441
const inserts = [];
4542

@@ -48,7 +45,7 @@ function generateLojaInserts() {
4845
const data_inauguracao = faker.date.birthdate().toISOString().split('T')[0];
4946

5047
const insert = formatInsert('loja', ['cidade_id', 'data_inauguracao'], [
51-
`${cidade_id}`,
48+
cidade_id,
5249
`'${data_inauguracao}'`
5350
]);
5451

@@ -58,7 +55,6 @@ function generateLojaInserts() {
5855
return inserts;
5956
}
6057

61-
// Gerar inserts para a tabela funcionario
6258
function generateFuncionarioInserts(numInserts) {
6359
const inserts = [];
6460

@@ -71,7 +67,7 @@ function generateFuncionarioInserts(numInserts) {
7167
const insert = formatInsert('funcionario', ['nome', 'cpf', 'loja_id', 'data_nascimento'], [
7268
`'${nome}'`,
7369
`'${cpf}'`,
74-
`${loja_id}`,
70+
loja_id,
7571
`'${data_nascimento}'`
7672
]);
7773

@@ -81,7 +77,6 @@ function generateFuncionarioInserts(numInserts) {
8177
return inserts;
8278
}
8379

84-
// Gerar inserts para a tabela marca
8580
function generateMarcaInserts(numInserts) {
8681
const inserts = [];
8782

@@ -95,7 +90,6 @@ function generateMarcaInserts(numInserts) {
9590
return inserts;
9691
}
9792

98-
// Gerar inserts para a tabela produto
9993
function generateProdutoInserts() {
10094
const inserts = [];
10195

@@ -106,8 +100,8 @@ function generateProdutoInserts() {
106100

107101
const insert = formatInsert('produto', ['nome', 'marca_id', 'valor'], [
108102
`'${nome}'`,
109-
`${marca_id}`,
110-
`${valor}`
103+
marca_id,
104+
valor
111105
]);
112106

113107
inserts.push(insert);
@@ -116,7 +110,6 @@ function generateProdutoInserts() {
116110
return inserts;
117111
}
118112

119-
// Gerar inserts para a tabela estoque
120113
function generateEstoqueInserts() {
121114
const inserts = [];
122115
const quant = 10000;
@@ -136,7 +129,6 @@ function generateEstoqueInserts() {
136129
return inserts;
137130
}
138131

139-
// Gerar inserts para a tabela venda
140132
function generateVendaInserts() {
141133
const inserts = [];
142134

@@ -146,9 +138,9 @@ function generateVendaInserts() {
146138
const funcionario_id = faker.number.int({ min: 1, max: 50 });
147139

148140
const insert = formatInsert('venda', ['loja_id', 'cliente_id', 'funcionario_id'], [
149-
`${loja_id}`,
150-
`${cliente_id}`,
151-
`${funcionario_id}`
141+
loja_id,
142+
cliente_id,
143+
funcionario_id
152144
]);
153145

154146
inserts.push(insert);
@@ -157,7 +149,6 @@ function generateVendaInserts() {
157149
return inserts;
158150
}
159151

160-
// Gerar inserts para a tabela item_venda
161152
function generateItemVendaInserts() {
162153
const inserts = [];
163154

@@ -174,10 +165,10 @@ function generateItemVendaInserts() {
174165
const valor = faker.number.int({ min: 10, max: 100 });
175166

176167
const insert = formatInsert('item_venda', ['venda_id', 'produto_id', 'quant', 'valor'], [
177-
`${venda_id}`,
178-
`${produto_id}`,
179-
`${quant}`,
180-
`${valor}`
168+
venda_id,
169+
produto_id,
170+
quant,
171+
valor
181172
]);
182173

183174
inserts.push(insert);

0 commit comments

Comments
 (0)