games-express-api/seeder.js

54 lines
1.0 KiB
JavaScript
Raw Normal View History

2024-09-12 15:48:27 -04:00
import fs from 'fs'
import mongoose from 'mongoose'
import 'colors'
import dotenv from 'dotenv'
dotenv.config()
import Game from './models/Game.js'
import User from './models/User.js'
mongoose.connect(Bun.env.MONGO_URI)
const games = JSON.parse(
fs.readFileSync(`${__dirname}/_data/games.json`, 'utf-8')
)
const users = JSON.parse(
fs.readFileSync(`${__dirname}/_data/users.json`, 'utf-8')
)
const importData = async () => {
try {
await Game.create(games)
await User.create(users)
console.log('Data Imported...'.green.inverse)
process.exit()
} catch (err) {
console.error(err)
}
}
const deleteData = async () => {
try {
await Game.deleteMany()
await User.deleteMany()
console.log('Data Destroyed...'.red.inverse)
process.exit()
} catch (err) {
console.error(err)
}
}
if (process.argv[2] === '-i') {
importData()
} else if (process.argv[2] === '-d') {
deleteData()
} else {
console.log(
'Please use the "-i" flag for importing or the "-d" flag for deleting'.red
.bold
)
process.exit()
}