-
Notifications
You must be signed in to change notification settings - Fork 1
/
seeder.js
50 lines (42 loc) · 1.13 KB
/
seeder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import mongoose from "mongoose"
import dotenv from "dotenv"
import colors from "colors"
import users from "./data/users.js"
import gists from "./data/gists.js"
import User from "./models/UserModel.js"
import Gist from "./models/GistModel.js"
import connectDB from "./config/db.js"
dotenv.config()
connectDB()
const importData = async () => {
try {
await User.deleteMany({})
await Gist.deleteMany({})
const createdUsers = await User.insertMany(users)
// const mainOwner = "62086ca7fedfdb1ad22b220d"
const sampleGists = gists.map((gist) => {
return { ...gist, owner: createdUsers[2] }
})
// console.log(sampleGists)
await Gist.insertMany(sampleGists)
console.log("Data Imported Successfully".green.inverse)
process.exit()
} catch (err) {
console.log(`error: ${err}`.red)
}
}
const destroyData = async () => {
try {
await User.deleteMany({})
await Gist.deleteMany({})
console.log("Data Destroyed Successfully".red.inverse)
process.exit()
} catch (err) {
console.log(`error: ${err}`.red)
}
}
if (process.argv[2] === "-d") {
destroyData()
} else {
importData()
}