#!/usr/bin/env node const path = require('path') require('dotenv').config({path: path.join(__dirname, '../.env')}) const pg = require('../db/pg') const newDB = require('../db') const oldDB = require('./mongo')('mongodb://localhost/sos') const createItems = require('./tasks/createItems') const createUsers = require('./tasks/createUsers') const uploadImages = require('./tasks/uploadImages') const saveExcelDataFile = require('./tasks/writeExcelSheet') async function doMigration() { const items = await oldDB.sock.find().lean().exec() const users = await oldDB.user.find().populate('purchases').lean().exec() const carts = users.map(u => u.purchases).flat() const coupons = await oldDB.coupon.find().lean().exec() console.log(`Loaded ${carts.length} purchases from ${users.length} users`) console.log(`Inserting ${users.length} users into the database`) const importAdmin = await createUsers(newDB, users); console.log(` Found user account ${importAdmin.uuid} (${importAdmin.email}) to attribute file uploads to`) console.log(`\nInserting ${items.length} items into database`) const itemImages = await createItems(newDB, items); const numImages = itemImages.map(({images}) => images.length).reduce((a,b) => b+a, 0) console.log(`\nImporting ${numImages} images into database`) await uploadImages(newDB, itemImages, importAdmin.uuid) console.log('\nWriting Excel data file') await saveExcelDataFile(users, coupons, items, path.join(__dirname, './datafile.xlsx')) } doMigration() .catch(console.error) .finally(() => { pg.end() oldDB._connection.close() })