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

42 lines
1 KiB
TypeScript

import sql from "../db/db";
import { Job } from "../types/job";
export const saveJob = async (job: Job): Promise<number | null> => {
try {
const result = await sql`
INSERT INTO public.jobs (
title,
company,
location,
company_link,
job_link,
provider,
status,
job_timestamp
) VALUES (
${job.title},
${job.company},
${job.location},
${job.companyLink},
${job.jobLink},
${job.provider},
${job.status},
${job.jobPostTime}
)
ON CONFLICT (job_link) DO UPDATE SET
title = EXCLUDED.title,
company = EXCLUDED.company,
company_link = EXCLUDED.company_link,
location = EXCLUDED.location,
job_timestamp = EXCLUDED.job_timestamp
RETURNING id
`;
const id = result[0]?.id ?? null;
console.log(`Saved job with id ${id} to the database.`);
return id;
} catch (error) {
console.error("Error saving job to the database:", error);
return null;
}
};