46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
/**
|
|
* Types the rows the category service reads from `categories`, and turns them
|
|
* into the {@link Category} the category routes send.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type { SQLOutputValue } from "node:sqlite";
|
|
import type { Category } from "../../shared/category.ts";
|
|
|
|
/** Returned by the category service's `SELECT`s on `categories`. */
|
|
export interface CategoryRow {
|
|
id: string;
|
|
slug: string;
|
|
name: string;
|
|
/** Sorts the categories, lowest first. */
|
|
position: number;
|
|
created_at: string;
|
|
updated_at: string | null;
|
|
[key: string]: SQLOutputValue;
|
|
}
|
|
|
|
/** Checks whether a row `node:sqlite` returned is a {@link CategoryRow}. */
|
|
export function isCategoryRow(obj: unknown): obj is CategoryRow {
|
|
return !!obj && typeof obj === "object" &&
|
|
"id" in obj && typeof obj.id === "string" &&
|
|
"slug" in obj && typeof obj.slug === "string" &&
|
|
"name" in obj && typeof obj.name === "string" &&
|
|
"position" in obj && typeof obj.position === "number" &&
|
|
"created_at" in obj && typeof obj.created_at === "string" &&
|
|
"updated_at" in obj &&
|
|
(typeof obj.updated_at === "string" || obj.updated_at === null);
|
|
}
|
|
|
|
/** Converts `row` to the {@link Category} the category routes send. */
|
|
export function categoryRowToApi(row: CategoryRow): Category {
|
|
return {
|
|
id: row.id,
|
|
slug: row.slug,
|
|
name: row.name,
|
|
position: row.position,
|
|
createdAt: new Date(row.created_at),
|
|
updatedAt: row.updated_at ? new Date(row.updated_at) : undefined,
|
|
};
|
|
}
|