import uuid from "uuid/v4"; import models from "../../models"; const _createCar = (brandId, active, year, done) => { models.Cars.create({ name: uuid(), year, active, brandId }) .then(item => { done(null, item); }) .catch(done); }; const _createBrand = done => { models.Brands.create({ name: uuid() }) .then(item => { done(null, item); }) .catch(done); }; const _createBrands = (total, done) => { const created = []; const next = () => { if (total === created.length) { done(null, created); } }; for (let i = 0; i < total; i += 1) { _createBrand((err, res) => { if (!err) { created.push(res); next(); } }); } }; const _truncateCars = (tables, done) => { if (tables.indexOf("Cars") === -1) { done(null); return true; } models.Cars.destroy({ where: {} }) .then(() => done(null)) .catch(err => done(err)); return true; }; const _truncateBrands = (tables, done) => { if (tables.indexOf("Brands") === -1) { done(null); return true; } models.Brands.destroy({ where: {} }) .then(() => done(null)) .catch(err => done(err)); return true; }; const _truncate = (tables, done) => { _truncateCars(tables, errCars => { if (errCars) { done(errCars); } else { _truncateBrands(tables, errBrands => { done(errBrands); }); } }); }; export const createBrand = _createBrand; export const createBrands = _createBrands; export const createCar = _createCar; export const truncate = _truncate;