Compare commits

..

No commits in common. "8c0970a19a6b3df27d1750d27e5bafaa9ae9ac70" and "136b58c44f7f1f9e32fb40eab91aa8fd378569ba" have entirely different histories.

2 changed files with 9 additions and 51 deletions

View File

@ -1,6 +1,5 @@
import { type DB, db } from "$lib/server/db/db"; import { type DB, db } from "$lib/server/db/db";
import logger from "../logger"; import logger from "../logger";
class TasksService { class TasksService {
private db: DB; private db: DB;
private caller: "internal" | "api"; private caller: "internal" | "api";
@ -13,57 +12,16 @@ class TasksService {
public async getAll() { public async getAll() {
logger.info("Fetching all task records..."); logger.info("Fetching all task records...");
try { try {
const tasks = await this.db.query.tasks.findMany({ const result = await this.db.query.tasks.findMany({
with: { with: {
type: true, type: true,
}, },
}); });
logger.debug(`Found ${tasks.length} records.`); logger.debug(`Found ${result.length} records.`);
return { tasks, status: "ok" }; return result;
} catch (error) { } catch (e) {
logger.error({ msg: "Error querying the database.", error }); logger.error({ msg: "Error querying the database.", error: e });
return { status: "failed", error }; return false as const;
}
}
public async getByTaskId(taskIds: Array<string>) {
const mappedTasks = taskIds.map(x => {
const prefix = x.slice(0, 2);
const task_id = x.slice(2);
return { prefix, task_id };
});
logger.info(
`Fetching ${
taskIds.length === 0
? "0 records"
: taskIds.length < 10
? taskIds.join(", ")
: `${taskIds.length} records`
}.`,
);
try {
const tasks = await db.query.tasks.findMany({
with: { type: true },
where: (tasks, { inArray }) => inArray(tasks.taskId, mappedTasks.map(x => x.task_id)),
});
return { tasks, status: "ok" };
} catch (error) {
logger.error({ msg: "Error querying the database.", error });
return { status: "failed", error };
}
}
public async getByDbId(ids: Array<number>) {
logger.info(`Fetching ${ids.length} records.`);
try {
const tasks = await db.query.tasks.findMany({
with: { type: true },
where: (tasks, { inArray }) => inArray(tasks.id, ids),
});
return { tasks, status: "ok" };
} catch (error) {
logger.error({ msg: "Error querying the database.", error });
return { status: "failed", error };
} }
} }
} }

View File

@ -4,8 +4,8 @@
let { data }: PageProps = $props(); let { data }: PageProps = $props();
</script> </script>
{#if data.tasks.status === "ok" && data.tasks.tasks !== undefined} {#if data.tasks}
<p>{data.tasks.tasks.length} total records.</p> <p>{data.tasks.length} total records.</p>
<table> <table>
<thead> <thead>
<tr> <tr>
@ -15,7 +15,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{#each data.tasks.tasks as task (task.id)} {#each data.tasks as task (task.id)}
<tr> <tr>
<td <td
>{task.type?.prefix + >{task.type?.prefix +