import { executeQuery } from "~/lib/db";
import helper from "~/functions/helper";

import cache from "~/functions/cache";

async function getList() {
	const cachekey = "sourcesList";

	let list = null;

	if (cache.has(cachekey)) {
		list = cache.get(cachekey);
	} else {
		const rows = await executeQuery({
			query: `
			SELECT
				sourceID,
				IFNULL(sourceName, '') AS sourceName
			FROM
				sources
			ORDER BY
				sourceName
		`,
		});

		list = helper.emptyOrRows(rows);

		cache.set(cachekey, list);
	}

	return list;
}

export default {
	getList,
};
