jobs-monorepo/services/scraper-glassdoor/queries/save-jobs.ts
Elshimy Ziad Magdy Taha a281363b61 hiring cafe scraper
2025-12-29 12:19:33 +05:00

49 lines
1.2 KiB
TypeScript

import sql from "../db/db";
import { Job } from "../types/job";
export const saveJobs = async (jobs: Job[]) => {
if (jobs.length === 0) {
console.log("No jobs to save.");
return;
}
try {
const jobsForDb = jobs.map((job) => ({
id: job.id,
title: job.title,
company: job.company,
location: job.location,
company_link: job.companyLink,
job_link: job.jobLink,
provider: job.provider,
status: job.status,
job_timestamp: job.jobPostTime,
}));
const result = await sql`
INSERT INTO public.jobs ${sql(
jobsForDb,
"id",
"title",
"company",
"location",
"company_link",
"job_link",
"provider",
"status",
"job_timestamp"
)}
ON CONFLICT (id) DO UPDATE SET
title = EXCLUDED.title,
company = EXCLUDED.company,
company_link = EXCLUDED.company_link,
location = EXCLUDED.location,
job_link = EXCLUDED.job_link,
job_timestamp = EXCLUDED.job_timestamp
`;
console.log(`Saved ${jobs.length} jobs to the database.`);
} catch (error) {
console.error("Error saving jobs to the database:", error);
}
};