Created
August 18, 2018 19:10
-
-
Save coderdiaz/53d85998f4b25dc0b43b988ea079431e to your computer and use it in GitHub Desktop.
Facker SQL for Sprotify
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
/* | |
* Getting Started | |
* $ yarn add mysql faker moment | |
* $ npm i -S mysql faker moment | |
* $ node index.js | |
*/ | |
const mysql = require('mysql') | |
const faker = require('faker') | |
const moment = require('moment') | |
// Connection database | |
const connection = mysql.createConnection({ | |
host: '127.0.0.1', | |
user: 'root', | |
password: 'root', | |
database: 'mydb' | |
}) | |
// Conneting to mysql | |
connection.connect((err, args) => { | |
if (err) console.log(err) | |
console.log('Connected!') | |
}) | |
const debutDate = moment(faker.date.recent()).format('YYYY/MM/DD') | |
// Query for create artists | |
const queryCreateArtistas = ` | |
INSERT INTO artistas(nombre, debut, valoracion, bio) | |
VALUES("${faker.name.findName()}", "${debutDate}", ${faker.random.number(10)}, "${faker.lorem.words(50)}") | |
` | |
// Query for create genres | |
const queryCreateGeneros = (i, productName) => ` | |
INSERT INTO generos(idgeneros, nombre) | |
VALUES ("${productName}") | |
` | |
const queryCreateCanciones = (titulo, anio, duracion, url) => ` | |
INSERT INTO canciones(titulo, anio, duracion, formato, url, artistas_idartistas, generos_idgeneros) | |
VALUES( | |
"${titulo}", | |
"${anio}", | |
"${duracion}", | |
"mp3", | |
"${url}", | |
${faker.random.number({ min: 1, max: 300 })}, | |
${faker.random.number({ min: 1, max: 300 })} | |
) | |
` | |
// Logic | |
for (let i = 1; i <= 300; i++) { | |
connection.query(queryCreateCanciones(faker.company.companyName(), faker.random.number({ min: 1920, max: 2018 }), "3:00", faker.internet.url()), (err, results, fields) => { | |
if (err) console.log(err) | |
console.log(results) | |
}) | |
} | |
// Closing connection | |
connection.end() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment