SHARE
TWEET

Untitled

a guest Aug 18th, 2018 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Getting Started
  3.  * $ yarn add mysql faker moment
  4.  * $ npm i -S mysql faker moment
  5.  * $ node index.js
  6.  */
  7.  
  8. const mysql = require('mysql')
  9. const faker = require('faker')
  10. const moment = require('moment')
  11.  
  12. // Connection database
  13. const connection = mysql.createConnection({
  14.   host: '127.0.0.1',
  15.   user: 'root',
  16.   password: 'root',
  17.   database: 'mydb'
  18. })
  19.  
  20. // Conneting to mysql
  21. connection.connect((err, args) => {
  22.   if (err) console.log(err)
  23.   console.log('Connected!')
  24. })
  25.  
  26. const debutDate = moment(faker.date.recent()).format('YYYY/MM/DD')
  27.  
  28. // Query for create artists
  29. const queryCreateArtistas = `
  30. INSERT INTO artistas(nombre, debut, valoracion, bio)
  31. VALUES("${faker.name.findName()}", "${debutDate}", ${faker.random.number(10)}, "${faker.lorem.words(50)}")
  32. `
  33.  
  34. // Query for create genres
  35. const queryCreateGeneros = (i, productName) => `
  36.   INSERT INTO generos(idgeneros, nombre)
  37.   VALUES ("${productName}")
  38. `
  39.  
  40. const queryCreateCanciones = (titulo, anio, duracion, url) => `
  41.   INSERT INTO canciones(titulo, anio, duracion, formato, url, artistas_idartistas, generos_idgeneros)
  42.   VALUES(
  43.     "${titulo}",
  44.     "${anio}",
  45.     "${duracion}",
  46.     "mp3",
  47.     "${url}",
  48.     ${faker.random.number({┬ámin: 1, max: 300 })},
  49.     ${faker.random.number({ min: 1, max: 300 })}
  50.   )
  51. `
  52.  
  53. // Logic
  54. for (let i = 1; i <= 300; i++) {
  55.   connection.query(queryCreateCanciones(faker.company.companyName(), faker.random.number({ min: 1920, max: 2018 }), "3:00", faker.internet.url()), (err, results, fields) => {
  56.     if (err) console.log(err)
  57.     console.log(results)
  58.   })
  59. }
  60.  
  61. // Closing connection
  62. connection.end()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top