v3: comments say what the code can't — the documentation pass wanted JSDoc on every export and every field of an exported interface, which filled the code with restatements ("The dump.", "Whether it is loading.", "The props of X.", "Renders the X.") and module docs that told a reader nothing ("The hot and new feeds, and the props every main feed of the index takes."); some were wrong as well as vague. Every comment in api/, shared/ and src/ is rewritten under one rule, now stated in the README's "Comments" section: every module and export still has a JSDoc, and it gives context (who uses it and where), usage (what a caller must know: a precondition, what it leaves to the caller, a side effect) or its place in the chain (where its input comes from, where its output goes); a field, parameter, private function or local gets one only when it adds to its parent's, and context about a whole type goes on the type; a type's doc is phrased from the value's side ("Returned to Y by X when Z", "Passed to Y by X"); full sentences, as short as they can be, no label-colon-list; // goes wherever the code isn't obvious and says why, and the section banners and narration go. Every claim was checked against the code, which turned up some forty comments that were wrong: DumpCard's claimed it was only used by the hot and new feeds, the WebSocket welcome claimed to be a socket's first message (presence comes first), UpdateUserRequest said null removes a description (it keeps it), deletedDumpIds leaves out that it also holds dumps made private, THEME_COLOR claimed to mirror index.html's, and the like. The routers' JSDoc now says where they are mounted and what becomes of a thrown APIException. doc-lint.ts lets a field without a doc through; a module or export without one still fails. No code changes; 204 backend and 628 frontend tests pass, and doc:lint, deno check, deno lint and tsc are clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* The `dumps` table: its row type, its guard, the column lists services select
|
||||
* it with, and its mappers to and from the API's {@link Dump}.
|
||||
* Maps the `dumps` table to the API's {@link Dump}. Every service that reads
|
||||
* dumps selects them with the column lists here, checks each row with
|
||||
* {@link isDumpRow} and converts it with {@link dumpRowToApi}.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
@@ -8,67 +9,69 @@
|
||||
import type { SQLOutputValue } from "node:sqlite";
|
||||
import type { Dump, RichContent } from "../../shared/dump.ts";
|
||||
|
||||
/** A row of the `dumps` table, with its live comment count. */
|
||||
/**
|
||||
* Read from the `dumps` table with {@link DUMP_SELECT_COLUMNS}, or written back
|
||||
* from a {@link Dump} by {@link dumpApiToRow}. It lacks `url_canonical`, which
|
||||
* only the duplicate lookup reads.
|
||||
*/
|
||||
export interface DumpRow {
|
||||
/** The dump's id. */
|
||||
id: string;
|
||||
/** `url` for a link, `file` for an upload. */
|
||||
kind: string;
|
||||
/** The dump's title. */
|
||||
title: string;
|
||||
/** The poster's description, in Markdown. */
|
||||
comment: string | null;
|
||||
/** The poster's id. */
|
||||
user_id: string;
|
||||
/** When the dump was posted. */
|
||||
created_at: string;
|
||||
/** When the dump was last edited, if ever. */
|
||||
updated_at: string | null;
|
||||
/** The dump's URL slug. */
|
||||
slug: string | null;
|
||||
/** The link exactly as submitted, for a URL dump. */
|
||||
/** The link as the poster submitted it. */
|
||||
url: string | null;
|
||||
/** The link's preview, as {@link RichContent} JSON. */
|
||||
/** {@link RichContent} as JSON. */
|
||||
rich_content: string | null;
|
||||
/** The uploaded file's name, for a file dump. */
|
||||
file_name: string | null;
|
||||
/** The uploaded file's type, for a file dump. */
|
||||
file_mime: string | null;
|
||||
/** The uploaded file's size in bytes, for a file dump. */
|
||||
file_size: number | null;
|
||||
/** How many users upvoted the dump. */
|
||||
vote_count: number;
|
||||
/** How many comments it has, not counting deleted ones (computed). */
|
||||
/** Computed by a subquery, which counts the comments that aren't deleted. */
|
||||
comment_count: number;
|
||||
/** 1 when only the poster sees the dump. */
|
||||
is_private: number;
|
||||
/** The uploaded thumbnail's type, null without one. */
|
||||
custom_thumbnail_mime: string | null;
|
||||
/** Any other column the query selects. */
|
||||
/** Lets a query select more columns, such as a join's. */
|
||||
[key: string]: SQLOutputValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* The stored columns of {@link DumpRow} — keep in sync with it and
|
||||
* {@link isDumpRow}. Services select with these instead of repeating the
|
||||
* column list themselves.
|
||||
* Lists the stored columns of {@link DumpRow}. A column added to the table must
|
||||
* be added here, to {@link DUMP_COLUMNS_ALIASED}, to {@link DumpRow} and to
|
||||
* {@link isDumpRow}.
|
||||
*/
|
||||
export const DUMP_COLUMNS =
|
||||
"id, kind, title, slug, comment, user_id, created_at, updated_at, url, rich_content, file_name, file_mime, file_size, vote_count, is_private, custom_thumbnail_mime";
|
||||
|
||||
/** {@link DUMP_COLUMNS}, for a query aliasing `dumps` as `d`. */
|
||||
/**
|
||||
* Lists {@link DUMP_COLUMNS} for a query that aliases `dumps` as `d`. The
|
||||
* playlist service selects with it and counts the comments itself.
|
||||
*/
|
||||
export const DUMP_COLUMNS_ALIASED =
|
||||
"d.id, d.kind, d.title, d.slug, d.comment, d.user_id, d.created_at, d.updated_at, d.url, d.rich_content, d.file_name, d.file_mime, d.file_size, d.vote_count, d.is_private, d.custom_thumbnail_mime";
|
||||
|
||||
/** {@link DUMP_COLUMNS} and the computed `comment_count`: a full {@link DumpRow}. */
|
||||
/**
|
||||
* Selects a whole {@link DumpRow}, the stored columns and the computed
|
||||
* `comment_count`.
|
||||
*/
|
||||
export const DUMP_SELECT_COLUMNS: string = `${DUMP_COLUMNS},
|
||||
(SELECT COUNT(*) FROM comments WHERE dump_id = dumps.id AND deleted = 0) as comment_count`;
|
||||
|
||||
/** {@link DUMP_SELECT_COLUMNS}, for a query aliasing `dumps` as `d`. */
|
||||
/**
|
||||
* Selects a whole {@link DumpRow} in a query that aliases `dumps` as `d` to
|
||||
* join it with other tables.
|
||||
*/
|
||||
export const DUMP_SELECT_COLUMNS_ALIASED: string = `${DUMP_COLUMNS_ALIASED},
|
||||
(SELECT COUNT(*) FROM comments WHERE dump_id = d.id AND deleted = 0) as comment_count`;
|
||||
|
||||
/** Checks whether `obj` is a {@link DumpRow}. */
|
||||
/**
|
||||
* Checks whether `obj`, a row from a query, is a {@link DumpRow}. It checks
|
||||
* each column's type, not that `kind` is `url` or `file`.
|
||||
*/
|
||||
export function isDumpRow(obj: unknown): obj is DumpRow {
|
||||
return !!obj &&
|
||||
typeof obj === "object" &&
|
||||
@@ -99,7 +102,10 @@ export function isDumpRow(obj: unknown): obj is DumpRow {
|
||||
obj.custom_thumbnail_mime === null);
|
||||
}
|
||||
|
||||
/** Converts a row to the API's {@link Dump}. */
|
||||
/**
|
||||
* Converts a row to the API's {@link Dump}, parsing its preview's JSON. It
|
||||
* leaves `categoryIds` out: the services add them with `attachCategoryIds`.
|
||||
*/
|
||||
export function dumpRowToApi(row: DumpRow): Dump {
|
||||
return {
|
||||
id: row.id,
|
||||
@@ -124,7 +130,10 @@ export function dumpRowToApi(row: DumpRow): Dump {
|
||||
};
|
||||
}
|
||||
|
||||
/** Converts a {@link Dump} back to a row. */
|
||||
/**
|
||||
* Converts a {@link Dump} back to a row, for a service that writes an edited
|
||||
* dump's columns.
|
||||
*/
|
||||
export function dumpApiToRow(dump: Dump): DumpRow {
|
||||
return {
|
||||
id: dump.id,
|
||||
|
||||
Reference in New Issue
Block a user