Created
November 12, 2021 21:39
-
-
Save HallexCosta/dfe210c3ad45974b34d0a61deb994dd7 to your computer and use it in GitHub Desktop.
Factory create SQL statments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// i will use playcode.io | |
// depedencies -> moment and faker | |
function getRandomInt(min, max) { | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive | |
} | |
class Depto { | |
constructor() { | |
this.nome = faker.name.jobTitle() | |
} | |
} | |
class Curso { | |
constructor() { | |
this.nome = faker.name.jobArea() | |
this.ch = moment().format('HH') | |
} | |
} | |
class Funcionario { | |
constructor() { | |
const number = getRandomInt(1, 3) | |
this.cpf = Number(`${number}${number}${number}`) | |
this.nome = faker.name.jobArea() | |
this.cod_depto = 1 | |
} | |
} | |
class Capacitacao { | |
constructor() { | |
this.data_inicio = moment().format('YYYY-MM-DD') | |
this.data_fim = moment().format('YYYY-MM-DD') | |
this.cod_curso = 1 | |
const number = getRandomInt(1, 3) | |
this.cpf_func = Number(`${number}${number}${number}`) | |
} | |
} | |
createInsertStatments(Depto, 2) | |
createInsertStatments(Curso, 3) | |
createInsertStatments(Funcionario, 3) | |
createInsertStatments(Capacitacao, 7) | |
function createInsertStatments(model, repeatLoop = 1) { | |
const data = [] | |
const sqls = [] | |
for (let i = 1; i <= repeatLoop; i++) { | |
const modelCopy = new (model)() | |
data.push({...modelCopy}) | |
let sql = "insert into " + model.name.toLowerCase() + " (" + Object.keys(modelCopy).join(", ") + ") " | |
sql += "values ('" + Object.values(modelCopy).join("', '") + "');" | |
console.log(sql) | |
sqls.push(sql) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment