Last active
December 5, 2018 19:04
-
-
Save novium/a841412902e2343dee02153829e715eb to your computer and use it in GitHub Desktop.
Mock data
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
let faker = require('faker'); | |
let mysql = require('mysql'); | |
//faker.locale = 'sv'; | |
let connection = mysql.createConnection({ | |
host: 'localhost', | |
user: 'root', | |
password: '', | |
database: 'store' | |
});/* | |
let connection = mysql.createConnection({ | |
host: 'back.db1.course.it.uu.se', | |
user: 'fall18_it9', | |
password: 'il25rl0L', | |
database: 'fall18_project_it9' | |
});*/ | |
connection.connect(); | |
connection.beginTransaction((err) => { | |
for(let i = 0; i < 20; i++) { | |
connection.query( | |
'INSERT INTO addresses \ | |
(country, zip, city, street_address) VALUES \ | |
(?, ?, ?, ?)', | |
['SWE', faker.address.zipCode(), faker.address.city(), faker.address.streetAddress()] | |
); | |
connection.query( | |
'INSERT INTO users \ | |
(pin, first_name, last_name, address_id) VALUES \ | |
(?, ?, ?, LAST_INSERT_ID())', | |
[ssn(), faker.name.firstName(), faker.name.lastName()] | |
); | |
} | |
connection.query( | |
'INSERT INTO departments \ | |
(title, description, parent_id) VALUES \ | |
(?, ?, NULL)', | |
['Store', 'Welcome to the store!'] | |
); | |
connection.query( | |
'INSERT INTO departments \ | |
(title, description, parent_id) VALUES \ | |
(?, ?, ?)', | |
['Rooter', faker.lorem.sentences(), 1] | |
); | |
connection.query( | |
'INSERT INTO departments \ | |
(title, description, parent_id) VALUES \ | |
(?, ?, ?)', | |
['Anti-Rooter', faker.lorem.sentences(), 1] | |
); | |
// CREATE LEAF DEPARTMENTS | |
for(let i = 0; i < 4; i++) { | |
connection.query( | |
'INSERT INTO departments \ | |
(title, description) VALUES \ | |
(?, ?)', | |
[faker.commerce.productAdjective() + ' ' + faker.commerce.color() + ' ' + faker.commerce.department(), faker.lorem.sentences()] | |
); | |
connection.query('UPDATE departments \ | |
SET parent_id = 2 \ | |
WHERE id = LAST_INSERT_ID()', | |
); // RAND() * (LAST_INSERT_ID() - 2) + 1 | |
} | |
for(let i = 0; i < 4; i++) { | |
connection.query( | |
'INSERT INTO departments \ | |
(title, description) VALUES \ | |
(?, ?)', | |
[faker.commerce.productAdjective() + ' ' + faker.commerce.color() + ' ' + faker.commerce.department(), faker.lorem.sentences()] | |
); | |
connection.query( | |
'UPDATE departments \ | |
SET parent_id = 3 \ | |
WHERE id = LAST_INSERT_ID()', | |
); | |
} | |
// CREATE PRODUCTS FOR LEAF DEPS | |
for(let i = 0; i < 30; i++) { | |
connection.query( | |
'SELECT id \ | |
FROM departments \ | |
WHERE parent_id IS NOT NULL AND parent_id != 1 \ | |
ORDER BY RAND() \ | |
LIMIT 1', | |
(error, result, fields) => { | |
const depID = result[0].id; | |
connection.query( | |
'INSERT INTO products \ | |
(title, description, price, quantity, deleted, tax, discount, featured, department_id) VALUES \ | |
(?, ?, ?, ?, ?, ?, ?, ?, ?)', | |
[faker.commerce.productName(), faker.lorem.sentence(), faker.commerce.price(), 6, false, 20, 0, false, depID] | |
); | |
} | |
); | |
} | |
// Create keywords | |
for(let i = 0; i < 5; i++) { | |
connection.query( | |
'INSERT INTO keywords \ | |
(title) VALUES \ | |
(?)', | |
[faker.commerce.productMaterial()] | |
); | |
} | |
connection.commit((err) => { | |
// CREATE REVIEWS | |
for(let i = 0; i < 3; i++) { | |
connection.query( | |
'INSERT INTO reviews \ | |
(review, grade, user_id, product_id) VALUES \ | |
(?, ?, ?, ?)', | |
[faker.lorem.paragraph(), Math.round((Math.random() * 50 + 1)) / 10, Math.random() * 19 + 1, Math.random() * 29 + 1] | |
); | |
} | |
// Add keywords to products | |
for(let i = 0; i < 30; i++) { | |
connection.query( | |
'INSERT INTO products_keywords \ | |
(keyword_id, product_id) VALUES \ | |
(?, ?), (?, ?)', | |
[Math.random() * 4 + 1, i+1, Math.random() * 4 + 1, i+1] | |
); | |
} | |
connection.query('INSERT INTO orders \ | |
(payment_reference, tracking_number, updated_at, ordered_at, address_id, user_id, state) \ | |
VALUES (?, ?, ?, ?, ?, ?, ?)', | |
['0DFM288NUJK', '4428839UDK23', '2018-11-28', '2018-11-26', 1, 1, 'new']); | |
connection.query('INSERT INTO departments_attributes (logo, welcome_text, department_id) VALUES (?, ?, ?)', | |
['http://lorempixel.com/128/128/technics/', 'Some snazzy welcome text for the store!', 1]); | |
}); | |
}); | |
function ssn() { | |
let ssn = ""; | |
let possible = "0123456789"; | |
for (let i = 0; i < 10; i++) | |
ssn += possible.charAt(Math.floor(Math.random() * possible.length)); | |
return ssn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
npm install mysql faker
node mock.js