|
|
|
#!/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 allUsers = await oldDB.user.find().populate('purchases').lean().exec()
|
|
|
|
const carts = allUsers.map(u => u.purchases).flat()
|
|
|
|
const coupons = await oldDB.coupon.find().lean().exec()
|
|
|
|
|
|
|
|
const registeredUsers = allUsers.filter(user => user.email)
|
|
|
|
|
|
|
|
console.log(`Loaded ${carts.length} purchases and ${registeredUsers.length} users`)
|
|
|
|
|
|
|
|
console.log(`Inserting ${registeredUsers.length} users into the database`)
|
|
|
|
const importAdmin = await createUsers(newDB, registeredUsers);
|
|
|
|
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(allUsers, coupons, items, path.join(__dirname, './datafile.xlsx'))
|
|
|
|
}
|
|
|
|
|
|
|
|
doMigration()
|
|
|
|
.catch(console.error)
|
|
|
|
.finally(() => {
|
|
|
|
pg.end()
|
|
|
|
oldDB._connection.close()
|
|
|
|
})
|
|
|
|
|