Skip to content

Instantly share code, notes, and snippets.

@xApnea
Last active June 19, 2020 21:15
Show Gist options
  • Save xApnea/9048bed7d5f79bb79df403b1c3f74876 to your computer and use it in GitHub Desktop.
Save xApnea/9048bed7d5f79bb79df403b1c3f74876 to your computer and use it in GitHub Desktop.
const faker = require('faker');
const db = require('./index.js');
const Product = require('./model.js');
const numOfDataPoints = 10;
var generateEntry = function(numOfVariations) {
var randomTitle = faker.commerce.productName(); //Fender Stratocaster
var randomDescription = faker.lorem.paragraph();
var variations = [];
for (var i = 0; i < numOfVariations; i++) {
var randomColor = faker.commerce.color(); // black
var randomCost = faker.commerce.price(); // 492.00
var randomImages = [
{src: faker.image.imageUrl(846, 1038)},
{src: faker.image.imageUrl(846, 1038)},
{src: faker.image.imageUrl(846, 1038)},
{src: faker.image.imageUrl(846, 1038)}
] // 4 random image urls
var variation = {
color: randomColor,
cost: randomCost,
images: randomImages
}
variations.push(variation);
}
var entry = {
title: randomTitle,
description: randomDescription,
variations : variations
}
return entry;
}
var seedDatabase = function() {
var sampleData = [];
for (var i = 0; i < numOfDataPoints; i++) {
var numOfVariations = Math.ceil((Math.random() * 4));
sampleData.push(generateEntry(numOfVariations));
}
Product.create(sampleData)
.then(() => db.disconnect());
}
seedDatabase();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment