/** * The `categories` table: its row type, its guard, and its mapper to the API's * {@link Category}. * * @module */ import type { SQLOutputValue } from "node:sqlite"; import type { Category } from "../../shared/category.ts"; /** A row of the `categories` table. */ export interface CategoryRow { /** The category's id. */ id: string; /** The category's URL slug, at the site root. */ slug: string; /** The category's display name. */ name: string; /** The category's place in the list, ascending. */ position: number; /** When the category was created. */ created_at: string; /** When the category was last changed, if ever. */ updated_at: string | null; /** Any other column the query selects. */ [key: string]: SQLOutputValue; } /** Checks whether `obj` 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 a row to the API's {@link Category}. */ 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, }; }