-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate.js
More file actions
26 lines (24 loc) · 747 Bytes
/
populate.js
File metadata and controls
26 lines (24 loc) · 747 Bytes
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
import { readFile } from "fs/promises";
import mongoose from "mongoose";
import dotenv from "dotenv";
dotenv.config();
import Job from "./models/JobModel.js";
import User from "./models/UserModel.js";
try {
await mongoose.connect(process.env.MONGO_URI);
// const user = await User.findOne({ email: "test@test.com" });
const user = await User.findOne({ email: "john@gmail.com" });
const jsonJobs = JSON.parse(
await readFile(new URL("./utils/mockData.json", import.meta.url))
);
const jobs = jsonJobs.map((job) => {
return { ...job, createdBy: user._id };
});
await Job.deleteMany({ createdBy: user._id });
await Job.create(jobs);
console.log("Success");
process.exit(0);
} catch (error) {
console.log(error);
process.exit(1);
}