Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const inventoryData = require("../seed_data/inventories");
  2. const warehouseData = require("../seed_data/warehouses");
  3.  
  4. exports.seed = function(knex) {
  5.   // Deletes ALL existing entries
  6.   return knex("warehouses")
  7.     .del()
  8.     .then(function() {
  9.       // Inserts seed entries
  10.       return knex("warehouses").insert(warehouseData);
  11.     })
  12.     .then(() => {
  13.       return knex("inventories").del();
  14.     })
  15.     .then(() => {
  16.       // Inserts seed entries
  17.       return knex("warehouses")
  18.         .pluck("id")
  19.         .then(warehouseIds => {
  20.           return warehouseIds;
  21.         });
  22.     })
  23.     .then(warehouseIds => {
  24.       const inventoryDataWithWarehouseIds = inventoryData.map(inventory => {
  25.         inventory.warehouse_id =
  26.           warehouseIds[Math.floor(Math.random() * warehouseIds.length)];
  27.         return inventory;
  28.       });
  29.       return knex("inventories").insert(inventoryDataWithWarehouseIds);
  30.     });
  31. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement