Files
m3e-canvas/db/schema.ts
T
pastilhas 80529dd9e7 six defects, and a typecheck the repo could not pass
Found by reviewing the plugin rather than running it. Nothing was live — the
table does not exist and the plugin is not installed — so all of these were
waiting for the first person to use it.

── The second unnamed design could not be saved ──

`uniqueIndex(userId, title)`, and every design starts life called "Untitled":
`titleOf` returns it for a document with no title and the API defaults to it. So
the first saved, the second collided, and the insert threw under a panel that
could only say "Changes are not being saved." Renaming one design to match
another did the same.

Two designs may share a name, as files and notes and dashboards do. Nothing looks
a design up by title, so uniqueness bought nothing. The index is now ordinary and
on the columns the list query actually uses.

The lesson from `dashboards.id` that the schema comment cites is that a key must
be scoped to its owner. `userId` on the row does that; making the pair unique as
well imported the collision it was citing, one account at a time.

── New did not start a new design ──

It cleared `?doc=`, and no parameter already means "open what I had last",
answered with `/documents/latest`. So New handed back the design on screen and
pointed the next save at its id. There is a `new` sentinel now, in its own module
because both panels read it and neither owns it.

── Two ways the load effect raced the debounced save ──

Changing `?doc=` does not unmount the panel, so the unmount flush never ran for
it: an edit made within the debounce and navigated away from was written after
`latest.current` had been replaced, which is to say never.

And the first save of a new design puts its id in the URL, which lands in the
same effect — re-reading the row overwrote in-memory state with the copy the
server had just been given, discarding whatever was drawn during the round trip.

The effect now flushes what is pending before it reads, and does not re-read a
document it already holds. The editor is keyed on a load generation rather than
on `row?.id`, which could not express "blank again" and left the previous design
on screen with `rowId` cleared underneath it.

── The typecheck ──

354 of the 355 errors were upstream's tree measured against this platform's
settings, which it was never written for. `bunx tsgo` is how this repo is kept
clean and it could not be clean while this plugin existed. `exclude` does not
help: adapter.ts imports the tree back in. So vendor.sh marks each vendored file
unchecked in the same deterministic pass that rewrites its imports. Types still
resolve; only the errors inside those files are silenced, which is what the
adapter already says the seam is for. Whole repo: 0.

── PROVENANCE.md understated what is done to the copy ──

It said "the one edit vendor.sh makes" and described the import rewrite. It also
runs apply-host.py, which places our own lib/host.ts and wires three export
paths. Four files are not upstream and the document whose job is making the copy
trustworthy did not say so. It lists all four now.

Also: web/host.ts imported a path only correct at its destination, so the file
could never typecheck where it is edited — apply-host.py rewrites that line when
placing it, and re-marks the placed copy, which it overwrites after vendor.sh has
marked it. PUT is no longer full-replace: omitting `title` renamed a design to
"Untitled" and omitting `ui` nulled it, a trap set for the second caller. And a
document is bounded at 4 MB, measured in bytes, where the title was capped at 200
and the document at nothing.
2026-09-05 19:52:27 +01:00

71 lines
3.3 KiB
TypeScript

import { pgTable, serial, text, integer, timestamp, jsonb, index } from 'drizzle-orm/pg-core';
/**
* A design, and the editor state that goes with it.
*
* ── Why the document is `jsonb` and not columns ──
*
* A `Doc` belongs to the vendored editor, not to Officer. Upstream adds fields to it constantly —
* `theme`, `platform` and `dynamicColor` all arrived after the first release — and every one of those
* would be a migration here for a shape we do not own and cannot influence.
*
* So it is stored whole. Officer never reads inside it except to show a title, which means an upstream
* field addition is a re-vendor rather than a schema change. The cost is honest: no querying by
* anything inside a design, which nothing wants to do.
*
* ── Per user, and enforced here ──
*
* `userId` is on the row and every query filters by it. That is the multi-user answer being a property
* of the schema rather than a filter somebody has to remember on each new endpoint — the same reasoning
* `service_connections` uses, and the mistake `dashboards.id` made by being a global primary key fed by
* `slugify(name)`, so that two members naming a thing the same way collide.
*/
export const m3eCanvasDocuments = pgTable(
'm3e_canvas_documents',
{
id: serial('id').primaryKey(),
userId: integer('user_id').notNull(),
/** Lifted out of the document so a list does not have to parse every design to draw itself. */
title: text('title').notNull().default('Untitled'),
/** The whole upstream `Doc`. Officer does not look inside it. */
doc: jsonb('doc').notNull(),
/**
* Panel widths, favourites, which language — upstream's `UI_KEY` state.
*
* Separate from the document because it is not the work: losing it costs a person their panel
* sizes, and losing a design costs them the design. Different things deserve different care.
*/
ui: jsonb('ui'),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
},
(table) => ({
/*
Ordinary, NOT unique — and it used to be `uniqueIndex(userId, title)`, which could not work.
Every design starts life called "Untitled": `titleOf` returns it for a document with no title of
its own, and the API defaults to it. So the first design saved, the second one collided, and the
insert threw under a panel that could only say "Changes are not being saved." Renaming one design
to match another did the same thing.
Two designs may share a name. That is true of files, of notes and of dashboards, and the
alternative — uniquifying the title on save — renames somebody's work behind their back to
satisfy an index. Nothing here looks a design up BY title, so uniqueness bought nothing and cost
the ordinary case.
The lesson from `dashboards.id` that the note above cites is that a key must be scoped to its
owner. `userId` on the row does that. Making the pair unique as well imported the collision it
was citing, one account at a time.
The columns are the list query: filter by user, newest first.
*/
byUser: index('m3e_canvas_documents_user_updated').on(table.userId, table.updatedAt),
}),
);
export type M3eCanvasDocument = typeof m3eCanvasDocuments.$inferSelect;