-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate.js
More file actions
33 lines (27 loc) · 824 Bytes
/
populate.js
File metadata and controls
33 lines (27 loc) · 824 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
27
28
29
30
31
32
33
import { readFile } from "fs/promises";
import mongoose from "mongoose";
import dotenv from "dotenv";
//model imports
import User from "./models/userModel.js";
import Job from "./models/jobModel.js";
dotenv.config();
try {
// @ts-ignore
await mongoose.connect(process.env.MONGO_URL);
const user = await User.findOne({ email: "niharika@gmail.com" });
// const user = await User.findOne({ email: "peter@gmail.com" });
const jsonJobs = JSON.parse(
// @ts-ignore
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);
}