foldkit-cms-drizzle 0.6.14 → 0.6.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/tables.ts","../src/index.ts"],"mappings":";;;;;;;;;;;;;;qBAea;EACX,2CAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAUP,0CAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAYN,6CAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAUE;EACX,uCAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAUP,sCAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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0BACO,KAAK,cAAc,cAAc,eAAe;;;;;;WAOvD,SACP,WAAW,OAAO,OAAO,OACzB,YAAY,YACZ,OAAO;;WAGA,YAAY;;;;;;WAMZ;;WAEA,UAAU,WAAW,OAAO,OAAO;;;iBAI7B;WACN;WACA;WACA;WACA;WACA;WACA;;;;;;;;;;iBAWM,WAAW;WACjB;WACA;WACA,IAAI;WACJ,KAAK;WACL;;qBA6BE;EACJ,OAAA,MAAI,QAAU,gBAAgB;;;;;;;IAwhCC,SAAA,cAAc,YAAW,OAAA,OAAA,OAAI;;IAxLhD,MAAA,IAAA,MAAI;eAAsB,KAAK,+BAA0B,OAAA,OAAA;UAAQ,OAAA;eAmBtC;eAAwB;;IAkC3C,SAAA,MAAA,WAAU,OAAA,OAAA,WAAG,OAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -529,7 +529,8 @@ const CmsServer = { make: (config) => {
|
|
|
529
529
|
const ran = yield* handler.run({
|
|
530
530
|
input: decoded,
|
|
531
531
|
principal,
|
|
532
|
-
requestId: `publish:${entry.id}:${held ?? "new"}
|
|
532
|
+
requestId: `publish:${entry.id}:${held ?? "new"}`,
|
|
533
|
+
operation: Option.some(`publish:${entry.id}:${held ?? "new"}`)
|
|
533
534
|
});
|
|
534
535
|
const targetId = creating ? String(ran.output.id) : entry.targetId;
|
|
535
536
|
const shown = served.type.roles.published;
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["text","integer","pgText","pgInteger"],"sources":["../src/tables.ts","../src/index.ts"],"sourcesContent":["/**\n * The three tables the CMS keeps beside the application's own. Drizzle tables\n * are made per dialect, so there is one definition for each; they have the same\n * columns. An application adds them to its schema and its migrations as it does\n * any table, and may define its own instead, so long as the columns are these.\n */\nimport { integer as pgInteger, jsonb, pgTable, text as pgText } from 'drizzle-orm/pg-core'\nimport { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'\n\n/**\n * - `cms_entries`: a piece of content from first keystroke to archive. It exists\n * before its content row does.\n * - `cms_drafts`: an entry's one working copy; its id is the entry's.\n * - `cms_revisions`: published values, append-only.\n */\nexport const sqliteTables = () => ({\n entries: sqliteTable('cms_entries', {\n id: text('id').primaryKey(),\n type: text('type').notNull(),\n targetId: text('target_id'),\n label: text('label').notNull(),\n createdBy: text('created_by'),\n createdAt: text('created_at').notNull(),\n archivedAt: text('archived_at'),\n revision: integer('revision'),\n }),\n drafts: sqliteTable('cms_drafts', {\n id: text('id').primaryKey(),\n values: text('values', { mode: 'json' }),\n model: text('model', { mode: 'json' }),\n form: text('form').notNull(),\n updatedAt: text('updated_at').notNull(),\n updatedBy: text('updated_by'),\n baseRevision: integer('base_revision'),\n scheduledFor: text('scheduled_for'),\n scheduledBy: text('scheduled_by'),\n scheduleError: text('schedule_error'),\n }),\n revisions: sqliteTable('cms_revisions', {\n id: text('id').primaryKey(),\n entryId: text('entry_id').notNull(),\n n: integer('n').notNull(),\n values: text('values', { mode: 'json' }),\n publishedAt: text('published_at').notNull(),\n publishedBy: text('published_by'),\n }),\n})\n\nexport const pgTables = () => ({\n entries: pgTable('cms_entries', {\n id: pgText('id').primaryKey(),\n type: pgText('type').notNull(),\n targetId: pgText('target_id'),\n label: pgText('label').notNull(),\n createdBy: pgText('created_by'),\n createdAt: pgText('created_at').notNull(),\n archivedAt: pgText('archived_at'),\n revision: pgInteger('revision'),\n }),\n drafts: pgTable('cms_drafts', {\n id: pgText('id').primaryKey(),\n values: jsonb('values'),\n model: jsonb('model'),\n form: pgText('form').notNull(),\n updatedAt: pgText('updated_at').notNull(),\n updatedBy: pgText('updated_by'),\n baseRevision: pgInteger('base_revision'),\n scheduledFor: pgText('scheduled_for'),\n scheduledBy: pgText('scheduled_by'),\n scheduleError: pgText('schedule_error'),\n }),\n revisions: pgTable('cms_revisions', {\n id: pgText('id').primaryKey(),\n entryId: pgText('entry_id').notNull(),\n n: pgInteger('n').notNull(),\n values: jsonb('values'),\n publishedAt: pgText('published_at').notNull(),\n publishedBy: pgText('published_by'),\n }),\n})\n\n/** The `create table` statements for SQLite, for a test or a first migration. */\nexport const sqliteSchema = `\ncreate table if not exists cms_entries (\n id text primary key, type text not null, target_id text, label text not null,\n created_by text, created_at text not null, archived_at text, revision integer\n);\ncreate table if not exists cms_drafts (\n id text primary key, \"values\" text, model text, form text not null,\n updated_at text not null, updated_by text, base_revision integer,\n scheduled_for text, scheduled_by text, schedule_error text\n);\ncreate table if not exists cms_revisions (\n id text primary key, entry_id text not null, n integer not null, \"values\" text,\n published_at text not null, published_by text\n);\n`\n\nexport type CmsTables = ReturnType<typeof sqliteTables> | ReturnType<typeof pgTables>\n","/**\n * `foldkit-cms-drizzle` — the server half of `foldkit-cms`.\n *\n * It keeps an author's unpublished work in three tables beside the\n * application's own, and it is where the audience boundary is enforced, by a\n * `foldkit-guard` guard set: a principal its `authors` guards refuse is refused\n * entries, drafts and revisions outright, and sees of the content only the\n * rows the content's own Read rules show. Both are a binding's `visible`, so\n * they hold on every path a table is read by.\n *\n * Publishing runs the application's own mutation with the draft's value, inside\n * a transaction that also appends the revision and removes the draft, so a\n * publish happened entirely or did not happen.\n */\nimport {\n and,\n eq,\n inArray,\n isNotNull,\n isNull,\n lte,\n ne,\n sql,\n type AnyColumn,\n type SQL,\n} from 'drizzle-orm'\nimport { Effect, Exit, Option, Schema, Semaphore } from 'effect'\nimport { Cms, type Content, type Facts, type Transition } from 'foldkit-cms'\nimport { Entity, type AnyEntity } from 'foldkit-entity'\nimport { Guards, type Policy, type PrincipalGuard } from 'foldkit-guard'\nimport { GuardServer } from 'foldkit-guard/server'\nimport type { MutationDescriptor, RemoteForbidden } from 'foldkit-remote'\nimport {\n DrizzleDatabase,\n drizzleWrites,\n bind,\n query,\n returning,\n runStatement,\n source,\n type AnyEntityBinding,\n type DrizzleDatabaseService,\n type Visible,\n} from 'foldkit-remote-drizzle'\nimport {\n RemoteServer,\n MutationRefused,\n RemoteServerError,\n type EntitySource,\n type MutationOutcome,\n type MutationSource,\n type QuerySource,\n} from 'foldkit-remote-server'\nimport type { CmsTables } from './tables.js'\n\nexport { pgTables, sqliteSchema, sqliteTables, type CmsTables } from './tables.js'\n\n/**\n * A content type as the server holds it: its declaration, its Entity with the\n * guards attached that its binding was bound from, the binding of its table,\n * and the application's own handlers of its two publish mutations. What\n * publishing a post does stays the application's code.\n */\nexport interface ServedContent<P = any> {\n readonly type: Content<string, any, any, any>\n /** `type.entity` with the guard set's rules attached: `Post.pipe(Guards.attach(G, { … }))`. */\n readonly entity: AnyEntity\n readonly binding: AnyEntityBinding\n readonly create: MutationSource<P, DrizzleDatabase>\n readonly update: MutationSource<P, DrizzleDatabase>\n}\n\n/**\n * Runs some work so that it happened entirely or did not. Drizzle's own\n * transactions differ by driver, so the application says which this is. It\n * fails as the work does, or, when the database refuses the transaction\n * itself, as a `RemoteServerError` that says nothing of why.\n */\nexport type Transaction = <A, E>(\n work: Effect.Effect<A, E, DrizzleDatabase>,\n) => Effect.Effect<A, E | RemoteServerError, DrizzleDatabase>\n\nconst statement = (database: DrizzleDatabaseService, text: string) =>\n Effect.promise(() => {\n const runs = database as unknown as {\n run?: (query: SQL) => unknown\n execute?: (query: SQL) => unknown\n }\n return Promise.resolve(\n runs.run !== undefined ? runs.run(sql.raw(text)) : runs.execute!(sql.raw(text)),\n )\n })\n\n// One connection holds one transaction. Without this, a write another request\n// makes between this one's `begin` and its `rollback` is rolled back with it,\n// after it was reported done.\nconst turns = new WeakMap<object, Semaphore.Semaphore>()\nconst turnOf = (database: object): Semaphore.Semaphore => {\n const held = turns.get(database)\n if (held !== undefined) return held\n const made = Semaphore.makeUnsafe(1)\n turns.set(database, made)\n return made\n}\n\nexport const Transaction = {\n /**\n * `begin`, the work, then `commit` or `rollback`, as statements. For a database\n * that is one connection, such as a SQLite file, which it takes turns at: the\n * operations of this package wait for one another. A write of your own on the\n * same connection does not, so make it inside `Transaction.statements` too.\n * Over a pool each statement may take a different connection: use `drizzle` there.\n */\n statements: (<A, E>(work: Effect.Effect<A, E, DrizzleDatabase>) =>\n Effect.gen(function* () {\n const database = yield* DrizzleDatabase\n return yield* turnOf(database).withPermit(\n statement(database, 'begin').pipe(\n Effect.andThen(work),\n Effect.onExit(exit => statement(database, Exit.isSuccess(exit) ? 'commit' : 'rollback')),\n ),\n )\n })) as Transaction,\n /**\n * Drizzle's `database.transaction`, with the work given the transaction as its\n * database. For a driver whose transactions are asynchronous: Postgres, libSQL.\n */\n drizzle: (<A, E>(work: Effect.Effect<A, E, DrizzleDatabase>) =>\n Effect.gen(function* () {\n const database = (yield* DrizzleDatabase) as unknown as {\n transaction: <T>(run: (inner: DrizzleDatabaseService) => Promise<T>) => Promise<T>\n }\n const failed = Symbol('failed')\n let exit: Exit.Exit<A, E> | undefined\n // A failure must leave the callback as a rejection, or Drizzle commits.\n yield* runStatement(\n () =>\n database\n .transaction(async inner => {\n exit = await Effect.runPromiseExit(\n work.pipe(Effect.provideService(DrizzleDatabase, inner)),\n )\n if (!Exit.isSuccess(exit)) throw failed\n })\n .catch(error => {\n if (error !== failed) throw error\n }),\n 'Database write failed',\n )\n return yield* exit!\n })) as Transaction,\n}\n\nexport interface CmsServerConfig<User> {\n readonly tables: CmsTables\n readonly content: ReadonlyArray<ServedContent<Option.Option<User>>>\n /** How a publish is made whole: `Transaction.statements` or `Transaction.drizzle`. */\n readonly transaction: Transaction\n /** The guard set's bodies, which decide `authors` and `transitions`. */\n readonly policy: Policy<User>\n /**\n * Who reads and writes unpublished work: a principal any of these lets.\n * Everyone else is a visitor.\n */\n readonly authors: ReadonlyArray<PrincipalGuard<User>>\n /**\n * Who of the authors may make a transition: any of its guards lets. One not\n * named is any author's.\n */\n readonly transitions?:\n | { readonly [T in Transition]?: ReadonlyArray<PrincipalGuard<User>> | undefined }\n | undefined\n /**\n * Whether this author may make this transition of this entry, for a rule\n * about the entry itself: asked after the entry is found, the transition is\n * one its state offers, and `transitions` let. Default: they may.\n */\n readonly allow?: (\n principal: Option.Option<User>,\n transition: Transition,\n entry: EntryRow,\n ) => boolean\n /** The server's clock, passed in so a test can hold it. */\n readonly now?: () => Date\n /**\n * The most a draft may hold, as the characters of its JSON: its values and its\n * form's Model together. An author is trusted with their own content, not with\n * the database's disk. Default: one million.\n */\n readonly maxDraftSize?: number\n /** Who a principal is, for `createdBy` and `updatedBy`. */\n readonly nameOf?: (principal: Option.Option<User>) => string | null\n}\n\n/** A row of `cms_entries`, as the server's rules see it. */\nexport interface EntryRow {\n readonly id: string\n readonly type: string\n readonly targetId: string | null\n readonly label: string\n readonly archivedAt: string | null\n readonly revision: number | null\n}\n\n/**\n * One import: content published already, such as a site's first pages or what\n * another CMS held. `as` is whom it is imported as (an editor, so the create\n * handler writes the row); a runner that imports many fixes `as` once and\n * varies the rest. `at` is when it was published (default now); `entry`\n * names the entry (default a new id) — name it where another import points\n * at it, and import what is pointed at first.\n */\nexport interface ImportItem<P> {\n readonly type: string\n readonly values: unknown\n readonly as: P\n readonly at?: Date | undefined\n readonly entry?: string | undefined\n}\n\nconst refuse = (message: string) => new RemoteServerError({ message })\n\n/**\n * Whether a unique index on this column refused a write. Drivers say so in their\n * own words, and Drizzle wraps what they say, so every message down the chain of\n * causes is read. SQLite names the column; Postgres names the constraint, which\n * by default is made of the column's name.\n */\nconst refusedAsDuplicate = (defect: unknown, column: string): boolean => {\n const said: Array<string> = []\n for (let at = defect, depth = 0; at != null && depth < 5; depth++) {\n const { message, constraint, cause } = at as {\n readonly message?: unknown\n readonly constraint?: unknown\n readonly cause?: unknown\n }\n // Drizzle's own message quotes the statement, which names every column.\n if (!String(message).startsWith('Failed query'))\n said.push(String(message ?? at), String(constraint ?? ''))\n at = cause\n }\n const text = said.join(' ')\n const named = new RegExp(`(^|[^a-zA-Z0-9])${column.replace(/[^\\w]/g, '.')}([^a-zA-Z0-9]|$)`)\n return /unique|duplicate/i.test(text) && named.test(text)\n}\n\nexport const CmsServer = {\n make: <User>(config: CmsServerConfig<User>) => {\n type P = Option.Option<User>\n const { tables, policy } = config\n const now = config.now ?? (() => new Date())\n const nameOf = config.nameOf ?? (() => null)\n const maxDraftSize = config.maxDraftSize ?? 1_000_000\n\n const byType = new Map(config.content.map(served => [served.type.name, served]))\n for (const { type, entity, create, update } of config.content) {\n if (!Entity.same(entity, type.entity))\n throw new Error(\n `foldkit-cms-drizzle: content \"${type.name}\" is of ${type.entity.name}, but was given ${entity.name}'s guards`,\n )\n for (const [handler, declared] of [\n [create, type.publish.create],\n [update, type.publish.update],\n ] as const)\n if (handler.mutation !== declared.name)\n throw new Error(\n `foldkit-cms-drizzle: content \"${type.name}\" publishes through \"${declared.name}\", but was given the handler of \"${handler.mutation}\"`,\n )\n const shown = type.roles.published\n if (shown === undefined) continue\n // Whether the rule is right is the application's to say. That it reads the\n // published field at all is checked here: one that does not shows a visitor\n // every row or none.\n if (!Guards.guardFields(entity, 'Read').includes(shown.key))\n throw new Error(\n `foldkit-cms-drizzle: content \"${type.name}\" can be unpublished, but its Read rule does not read \"${shown.key}\"; attach one that does, such as \\`Read: [G.isPublished, G.isAuthor]\\``,\n )\n }\n\n /** Whether any of `guards` lets `principal`, each body told `interaction`. */\n const anyOf = (\n guards: ReadonlyArray<PrincipalGuard<User>>,\n principal: P,\n interaction: string,\n ): Effect.Effect<boolean> =>\n Effect.map(\n Effect.forEach(guards, guard => policy.decide(guard, { principal, interaction })),\n decisions => decisions.some(decision => decision === true),\n )\n /**\n * Whether `principal` is an author, asked as the interaction it is for:\n * reading unpublished work, or changing it, so a body can tell the two.\n */\n const isAuthor = (principal: P, interaction: 'Read' | 'Update') =>\n anyOf(config.authors, principal, interaction)\n /** Whether `transitions` let this author make `transition`; an author's when it names none. */\n const transitionLets = (principal: P, transition: Transition) =>\n Option.match(Option.fromUndefinedOr(config.transitions?.[transition]), {\n onNone: () => Effect.succeed(true),\n onSome: guards => anyOf(guards, principal, transition),\n })\n\n // Unpublished work is an author's. To anyone else these tables are empty, on\n // every path they are read by.\n // The principal reaches a binding untyped: one that is not the guard set's is a\n // defect, never read as a visitor.\n const authorsOnly: Visible = principal =>\n policy.isPrincipal(principal)\n ? Effect.map(isAuthor(principal, 'Read'), author => (author ? undefined : sql`0 = 1`))\n : Effect.die(\n new Error('[foldkit-cms-drizzle] the principal is not an Option of the guard set’s'),\n )\n const Db = bind(Cms.Entities, {\n Entry: {\n table: tables.entries,\n visible: authorsOnly,\n relations: {\n draft: { foreignKey: tables.drafts.id },\n revisions: {\n foreignKey: tables.revisions.entryId,\n orderBy: [{ column: tables.revisions.n, direction: 'desc' }],\n },\n },\n // Not a column: this server derives it, below, with its own clock.\n derived: { state: { supplied: true }, may: { supplied: true } },\n },\n Draft: { table: tables.drafts, visible: authorsOnly },\n Revision: { table: tables.revisions, visible: authorsOnly },\n })\n\n /** What is known of some entries, for their state: their drafts, and whether a visitor sees their rows. */\n const factsOf = (entries: ReadonlyArray<EntryRow>) =>\n Effect.gen(function* () {\n const database = yield* DrizzleDatabase\n const ids = entries.map(entry => entry.id)\n const drafts =\n ids.length === 0\n ? []\n : yield* runStatement(\n () =>\n database\n .select({\n id: tables.drafts.id,\n scheduledFor: tables.drafts.scheduledFor,\n scheduleError: tables.drafts.scheduleError,\n })\n .from(tables.drafts)\n .where(inArray(tables.drafts.id, ids)),\n 'Database query failed',\n )\n const draftOf = new Map(drafts.map(draft => [String(draft.id), draft]))\n\n // Per content type, which target rows exist and which a visitor would see.\n const rows = new Map<string, 'visible' | 'hidden'>()\n for (const [name, { type, binding }] of byType) {\n const targets = entries.flatMap(entry =>\n entry.type === name && entry.targetId !== null ? [entry.targetId] : [],\n )\n if (targets.length === 0) continue\n const id = binding.columns.id!\n const publishedColumn =\n type.roles.published === undefined\n ? undefined\n : binding.columns[type.roles.published.key]\n const found = yield* runStatement(\n () =>\n database\n .select({\n id,\n ...(publishedColumn === undefined ? {} : { shown: publishedColumn }),\n })\n .from(binding.table)\n .where(inArray(id, targets)),\n 'Database query failed',\n )\n for (const row of found)\n rows.set(\n `${name}:${String(row.id)}`,\n publishedColumn === undefined || row.shown !== null ? 'visible' : 'hidden',\n )\n }\n\n return new Map(\n entries.map((entry): readonly [string, Facts] => {\n const draft = draftOf.get(entry.id)\n return [\n entry.id,\n {\n archivedAt: entry.archivedAt,\n row:\n entry.targetId === null\n ? 'none'\n : (rows.get(`${entry.type}:${entry.targetId}`) ?? 'none'),\n draft:\n draft === undefined\n ? null\n : {\n scheduledFor: (draft.scheduledFor as string | null) ?? null,\n scheduleError: (draft.scheduleError as string | null) ?? null,\n },\n },\n ]\n }),\n )\n })\n\n const entryColumns = {\n id: tables.entries.id,\n type: tables.entries.type,\n targetId: tables.entries.targetId,\n label: tables.entries.label,\n archivedAt: tables.entries.archivedAt,\n revision: tables.entries.revision,\n }\n const findEntry = (id: string) =>\n Effect.gen(function* () {\n const database = yield* DrizzleDatabase\n const [row] = yield* runStatement(\n () =>\n database\n .select(entryColumns)\n .from(tables.entries)\n .where(eq(tables.entries.id, id))\n .limit(1),\n 'Database query failed',\n )\n return row as EntryRow | undefined\n })\n\n // An entry's `state` and `may` are not columns. The generated source reads the\n // rest; the state is derived from what is known of each entry, with this server's\n // clock, and `may` is what `allow` lets the reader ask of it.\n const generated = source<P>(Db.Entry)\n const entries: EntitySource<P, DrizzleDatabase> = {\n ...generated,\n read: context =>\n Effect.gen(function* () {\n const wantsState = context.fields.includes('state')\n const wantsMay = context.fields.includes('may')\n const fields = context.fields.filter(field => field !== 'state' && field !== 'may')\n const records = yield* generated.read({\n ...context,\n // Both need these of each entry, asked for or not; `allow` is given the row.\n fields:\n wantsState || wantsMay\n ? [\n ...new Set([\n ...fields,\n 'type',\n 'targetId',\n 'archivedAt',\n ...(wantsMay ? ['label', 'revision'] : []),\n ]),\n ]\n : fields,\n })\n if (!wantsState && !wantsMay) return records\n const rows = records.map((record): EntryRow => ({\n id: record.id,\n type: String(record.values.type),\n targetId: (record.values.targetId as string | null) ?? null,\n label: String(record.values.label ?? ''),\n archivedAt: (record.values.archivedAt as string | null) ?? null,\n revision: (record.values.revision as number | null) ?? null,\n }))\n const facts = yield* factsOf(rows)\n const at = now()\n // Each transition's guards are decided once for this reader, not per entry.\n const lets = new Map(\n wantsMay\n ? yield* Effect.forEach(Cms.transitions, transition =>\n Effect.map(\n transitionLets(context.principal, transition),\n allowed => [transition, allowed] as const,\n ),\n )\n : [],\n )\n return records.map((record, index) => {\n const row = rows[index]!\n const known = facts.get(record.id)!\n return {\n id: record.id,\n values: {\n ...Object.fromEntries(\n Object.entries(record.values).filter(\n ([field]) => field === 'id' || context.fields.includes(field),\n ),\n ),\n ...(wantsState ? { state: Cms.state(known, at) } : {}),\n // What `transitions` and `allow` let this reader ask of this entry,\n // whatever its state offers now: the state says that, and a draft\n // saved a moment later changes it without asking the server again.\n ...(wantsMay\n ? {\n may: Cms.transitions.filter(\n transition =>\n lets.get(transition) === true &&\n config.allow?.(context.principal, transition, row) !== false,\n ),\n }\n : {}),\n },\n }\n })\n }),\n }\n\n // The descriptor carries all three questions and the order; this says only\n // which table answers them. The audience boundary is unchanged: `visible`\n // on the binding is conjoined with the body, as for every source here.\n const worklist: QuerySource<P, DrizzleDatabase> = query(Cms.Entries, { entity: Db.Entry })\n\n /** A mutation of this server: its principal and its database are fixed, its input is the descriptor's. */\n const operation = <Name extends string, Input, Output>(\n descriptor: MutationDescriptor<Name, Input, Output>,\n run: (context: {\n readonly input: Input\n readonly principal: P\n }) => Effect.Effect<\n MutationOutcome<Output>,\n RemoteServerError | MutationRefused | RemoteForbidden,\n DrizzleDatabase\n >,\n ): MutationSource<P, DrizzleDatabase> =>\n // Asked before anything is looked up: a visitor is not told which entries there are.\n // Then the whole of it is one transaction: a save that made an entry and\n // could not make its draft leaves neither.\n RemoteServer.mutation<P, DrizzleDatabase, Name, Input, Output>(descriptor, context =>\n Effect.flatMap(isAuthor(context.principal, 'Update'), author =>\n author\n ? config.transaction(run(context))\n : Effect.fail(RemoteServer.forbid('Only an author may change unpublished work')),\n ),\n )\n\n /**\n * A scheduled draft is published, when its time comes, as whoever scheduled it.\n * So changing it, replacing it or removing it is for someone who could have\n * scheduled it: otherwise an author who may not publish would publish through\n * someone who may.\n */\n const promised = (principal: P, entry: EntryRow) =>\n Effect.gen(function* () {\n const [draft] = yield* readRows(\n Db.Draft,\n { scheduledFor: tables.drafts.scheduledFor },\n entry.id,\n )\n if (draft?.scheduledFor == null) return\n if (\n !(yield* transitionLets(principal, 'schedule')) ||\n config.allow?.(principal, 'schedule', entry) === false\n )\n return yield* RemoteServer.forbid(\n 'This draft is scheduled, and only someone who may schedule it may change it',\n )\n })\n\n /**\n * Whether this author may make this transition: its `transitions`, and `allow`\n * of the entry once there is one.\n */\n const asking = (principal: P, transition: Transition, entry: EntryRow | undefined) =>\n Effect.flatMap(transitionLets(principal, transition), lets =>\n !lets || (entry !== undefined && config.allow?.(principal, transition, entry) === false)\n ? Effect.fail(RemoteServer.forbid(`This author may not ${transition} this entry`))\n : Effect.void,\n )\n\n const draftFields = ['values', 'model', 'form', 'updatedAt', 'updatedBy', 'baseRevision']\n const draftPatch = returning(Db.Draft, draftFields)\n const entryPatch = returning(Db.Entry, [\n 'type',\n 'targetId',\n 'label',\n 'createdAt',\n 'archivedAt',\n 'revision',\n ])\n /**\n * The entry as the client should now hold it, its state with it: an operation\n * changes the state, and a patch without it would leave the old one on screen.\n */\n const entryPatches = (id: string) =>\n Effect.gen(function* () {\n const patches = entryPatch.patches(yield* readRows(Db.Entry, entryPatch.columns, id))\n const entry = yield* findEntry(id)\n if (entry === undefined) return patches\n const facts = (yield* factsOf([entry])).get(id)!\n return patches.map(patch => ({\n ...patch,\n values: { ...patch.values, state: Cms.state(facts, now()) },\n }))\n })\n const readRows = (binding: AnyEntityBinding, columns: Record<string, AnyColumn>, id: string) =>\n Effect.gen(function* () {\n const database = yield* DrizzleDatabase\n return yield* runStatement(\n () => database.select(columns).from(binding.table).where(eq(binding.columns.id!, id)),\n 'Database query failed',\n )\n })\n\n const SaveDraft = operation(Cms.Operations.SaveDraft, ({ input, principal }) =>\n Effect.gen(function* () {\n if (!byType.has(input.type))\n return yield* refuse(`\"${input.type}\" is not a type of content this server knows`)\n const size = JSON.stringify([input.values, input.model]).length\n if (size > maxDraftSize)\n return yield* refuse(\n `This draft is too large to save: ${size} characters, and ${maxDraftSize} is the most`,\n )\n // An id nobody has is something new: the client named it, and this makes it.\n const existing = yield* findEntry(input.entry)\n if (existing !== undefined && existing.type !== input.type)\n return yield* refuse(`This entry is of \"${existing.type}\", not \"${input.type}\"`)\n if (existing?.archivedAt != null) return yield* refuse('An archived entry takes no draft')\n yield* asking(principal, 'save', existing)\n if (existing !== undefined) yield* promised(principal, existing)\n\n const database = yield* drizzleWrites\n const who = nameOf(principal)\n const id = existing?.id ?? input.entry\n const [held] = yield* readRows(Db.Draft, { updatedAt: tables.drafts.updatedAt }, id)\n const previous = held === undefined ? null : String(held.updatedAt)\n // Two saves in one millisecond must still be told apart by the next one.\n const at = now().toISOString()\n const updatedAt =\n previous !== null && at <= previous\n ? new Date(new Date(previous).getTime() + 1).toISOString()\n : at\n const written = {\n values: input.values,\n model: input.model,\n form: input.form,\n updatedAt,\n updatedBy: who,\n // A draft that changed is worth trying again when its time comes.\n scheduleError: null,\n }\n\n // The first draft of an entry has nothing to have been based on. Asked before\n // anything is written, so a refusal leaves no entry behind.\n if (previous === null && input.basedOn !== null)\n return yield* RemoteServer.refuse(\n Cms.Operations.SaveDraft,\n Cms.refusal.conflict,\n 'This draft was discarded',\n )\n if (existing === undefined) {\n yield* runStatement(\n () =>\n database.insert(tables.entries).values({\n id,\n type: input.type,\n targetId: null,\n label: input.label,\n createdBy: who,\n createdAt: at,\n archivedAt: null,\n revision: null,\n }),\n 'Database write failed',\n )\n }\n if (previous === null) {\n yield* runStatement(\n () => database.insert(tables.drafts).values({ id, ...written, baseRevision: null }),\n 'Database write failed',\n )\n } else {\n // Compare and set: the save names what it was made from, and a newer one\n // on the server means someone else saved in between.\n const changed = yield* runStatement(\n () =>\n database\n .update(tables.drafts)\n .set(written)\n .where(\n and(eq(tables.drafts.id, id), eq(tables.drafts.updatedAt, input.basedOn ?? '')),\n )\n .returning({ id: tables.drafts.id }),\n 'Database write failed',\n )\n if (changed.length === 0)\n return yield* RemoteServer.refuse(\n Cms.Operations.SaveDraft,\n Cms.refusal.conflict,\n 'This draft was saved by someone else since',\n )\n }\n if (existing !== undefined && existing.label !== input.label) {\n yield* runStatement(\n () =>\n database\n .update(tables.entries)\n .set({ label: input.label })\n .where(eq(tables.entries.id, id))\n .returning({ id: tables.entries.id }),\n 'Database write failed',\n )\n }\n\n const entities = [\n ...(yield* entryPatches(id)),\n ...draftPatch.patches(yield* readRows(Db.Draft, draftPatch.columns, id)),\n ]\n return { output: { entry: id as never, updatedAt }, entities }\n }),\n )\n\n const DiscardDraft = operation(Cms.Operations.DiscardDraft, ({ input, principal }) =>\n Effect.gen(function* () {\n const entry = yield* findEntry(input.entry)\n if (entry === undefined) return yield* refuse('There is no such entry')\n yield* asking(principal, 'discard', entry)\n yield* promised(principal, entry)\n const database = yield* drizzleWrites\n yield* runStatement(\n () => database.delete(tables.drafts).where(eq(tables.drafts.id, entry.id)),\n 'Database write failed',\n )\n const gone = [{ entity: 'CmsDraft', id: entry.id }]\n // Never published, and now with nothing entered: there is nothing left of it.\n if (entry.targetId !== null)\n return { output: {}, deleted: gone, entities: yield* entryPatches(entry.id) }\n yield* runStatement(\n () => database.delete(tables.entries).where(eq(tables.entries.id, entry.id)),\n 'Database write failed',\n )\n return { output: {}, deleted: [...gone, { entity: 'CmsEntry', id: entry.id }] }\n }),\n )\n\n const done: Readonly<Record<Exclude<Transition, 'save' | 'discard'>, string>> = {\n publish: 'published',\n unpublish: 'unpublished',\n schedule: 'scheduled',\n unschedule: 'unscheduled',\n archive: 'archived',\n unarchive: 'unarchived',\n restore: 'restored',\n }\n /** The entry, if its state offers this transition now. */\n const offering = (id: string, transition: Exclude<Transition, 'save' | 'discard'>) =>\n Effect.gen(function* () {\n const entry = yield* findEntry(id)\n if (entry === undefined) return yield* refuse('There is no such entry')\n const served = byType.get(entry.type)\n if (served === undefined)\n return yield* refuse(`\"${entry.type}\" is not a type of content this server knows`)\n const facts = (yield* factsOf([entry])).get(entry.id)!\n if (!Cms.offers(facts, now(), served.type).includes(transition))\n return yield* refuse(`This entry cannot be ${done[transition]} as it stands`)\n return { entry, served, facts }\n })\n\n /** What the `published` role's column holds for \"now\". */\n const stamp = (column: AnyColumn, at: Date) => {\n const kind = column.dataType.split(' ')[0]\n return kind === 'string' ? at.toISOString() : kind === 'number' ? at.getTime() : at\n }\n\n const scheduledPatch = returning(Db.Draft, ['scheduledFor', 'scheduleError'])\n const revisionPatch = returning(Db.Revision, ['n', 'values', 'publishedAt', 'publishedBy'])\n\n type Found = Effect.Success<ReturnType<typeof offering>>\n\n /** The draft as the publish mutation would take it, or why it would not. */\n const readied = ({ entry, served, facts }: Found) =>\n Effect.gen(function* () {\n const [draft] = yield* readRows(Db.Draft, { values: tables.drafts.values }, entry.id)\n // A row that was deleted under its entry is made again.\n const creating = facts.row === 'none'\n const handler = creating ? served.create : served.update\n const value =\n creating || typeof draft?.values !== 'object' || draft.values === null\n ? draft?.values\n : { ...draft.values, id: entry.targetId }\n const decoded = yield* Schema.decodeUnknownEffect(handler.Input)(value).pipe(\n Effect.mapError(error => refuse(`This draft is not ready to publish: ${String(error)}`)),\n )\n return { draft, creating, handler, decoded }\n })\n\n /** A publish, for the operation that asks for one, the schedule that comes due, and an import. */\n const publishing = (found: Found, principal: P, basedOn: number | null, at: Date = now()) =>\n Effect.gen(function* () {\n const { entry, served } = found\n const database = yield* DrizzleDatabase\n const held = entry.revision\n const conflict = RemoteServer.refuse(\n Cms.Operations.Publish,\n Cms.refusal.conflict,\n 'This entry was published by someone else since',\n )\n if (held !== basedOn) return yield* conflict\n const { draft, creating, handler, decoded } = yield* readied(found)\n\n // A taken slug is refused by name, on its key. This is advice: two publishes\n // can both pass it, and then the unique index is the rule, below.\n const address = served.type.roles.slug\n const slug =\n address === undefined\n ? undefined\n : (decoded as Readonly<Record<string, unknown>> | null)?.[address.key]\n const taken = (key: string) =>\n RemoteServer.refuse(\n Cms.Operations.Publish,\n Cms.refusal.field(key, `That address is taken: \"${String(slug)}\" is already used`),\n )\n if (address !== undefined && typeof slug === 'string') {\n const column = served.binding.columns[address.key]!\n const id = served.binding.columns.id!\n const others = yield* runStatement(\n () =>\n database\n .select({ id })\n .from(served.binding.table)\n .where(and(eq(column, slug), creating ? undefined : ne(id, entry.targetId!)))\n .limit(1),\n 'Database query failed',\n )\n if (others.length > 0) return yield* taken(address.key)\n }\n\n return yield* Effect.gen(function* () {\n const writes = yield* drizzleWrites\n const n = (held ?? 0) + 1\n // Compare and set, first: of two publishes made from one revision, one\n // finds the number already moved, and nothing of it is written.\n const moved = yield* runStatement(\n () =>\n writes\n .update(tables.entries)\n .set({ revision: n })\n .where(\n and(\n eq(tables.entries.id, entry.id),\n held === null\n ? isNull(tables.entries.revision)\n : eq(tables.entries.revision, held),\n ),\n )\n .returning({ id: tables.entries.id }),\n 'Database write failed',\n )\n if (moved.length === 0) return yield* conflict\n // Scheduled publishing has no client request, so the id names this\n // publish of this entry: the same on a retry, another on the next.\n const ran = yield* handler.run({\n input: decoded,\n principal,\n requestId: `publish:${entry.id}:${held ?? 'new'}`,\n })\n const targetId = creating\n ? String((ran.output as { readonly id: unknown }).id)\n : entry.targetId!\n const shown = served.type.roles.published\n if (shown !== undefined) {\n // Publishing shows the row. One already shown keeps its first date.\n const column = served.binding.columns[shown.key]!\n yield* runStatement(\n () =>\n writes\n .update(served.binding.table)\n .set({ [shown.key]: stamp(column, at) })\n .where(and(eq(served.binding.columns.id!, targetId), isNull(column)))\n .returning({ id: served.binding.columns.id! }),\n 'Database write failed',\n )\n }\n const revisionId = `${entry.id}:${n}`\n yield* runStatement(\n () =>\n writes.insert(tables.revisions).values({\n id: revisionId,\n entryId: entry.id,\n n,\n values: draft?.values,\n publishedAt: at.toISOString(),\n publishedBy: nameOf(principal),\n }),\n 'Database write failed',\n )\n yield* runStatement(\n () => writes.delete(tables.drafts).where(eq(tables.drafts.id, entry.id)),\n 'Database write failed',\n )\n yield* runStatement(\n () =>\n writes\n .update(tables.entries)\n .set({ targetId })\n .where(eq(tables.entries.id, entry.id))\n .returning({ id: tables.entries.id }),\n 'Database write failed',\n )\n return {\n output: { entry: entry.id as never, targetId, revision: n },\n entities: [\n ...ran.entities,\n // The row as the handler left it, every column: a handler need not patch what it wrote.\n ...(yield* returning.row(served.binding, targetId)),\n ...(yield* entryPatches(entry.id)),\n ...revisionPatch.patches(\n yield* readRows(Db.Revision, revisionPatch.columns, revisionId),\n ),\n ],\n connections: ran.connections,\n deleted: [...ran.deleted, { entity: 'CmsDraft', id: entry.id }],\n }\n }).pipe(\n // The publish that lost the race: the index refused it, and it is the same\n // news as the check's. Any other defect is left as it was.\n Effect.catchDefect(defect =>\n address !== undefined &&\n refusedAsDuplicate(defect, served.binding.columns[address.key]!.name)\n ? Effect.fail(taken(address.key))\n : Effect.die(defect),\n ),\n )\n })\n\n const Publish = operation(Cms.Operations.Publish, ({ input, principal }) =>\n Effect.gen(function* () {\n const found = yield* offering(input.entry, 'publish')\n yield* asking(principal, 'publish', found.entry)\n return yield* publishing(found, principal, input.basedOn)\n }),\n )\n\n /** Writes to an entry's draft, and answers with both as the client should now hold them. */\n const drafted = (id: string, values: object) =>\n Effect.gen(function* () {\n const writes = yield* drizzleWrites\n yield* runStatement(\n () =>\n writes\n .update(tables.drafts)\n .set(values)\n .where(eq(tables.drafts.id, id))\n .returning({ id: tables.drafts.id }),\n 'Database write failed',\n )\n return {\n output: {},\n entities: [\n ...(yield* entryPatches(id)),\n ...scheduledPatch.patches(yield* readRows(Db.Draft, scheduledPatch.columns, id)),\n ],\n }\n })\n\n const Schedule = operation(Cms.Operations.Schedule, ({ input, principal }) =>\n Effect.gen(function* () {\n const found = yield* offering(input.entry, 'schedule')\n yield* asking(principal, 'schedule', found.entry)\n if (Number.isNaN(new Date(input.at).getTime()))\n return yield* refuse(`\"${input.at}\" is not a time`)\n // What cannot be published now is not promised for later.\n yield* readied(found)\n return yield* drafted(found.entry.id, {\n scheduledFor: new Date(input.at).toISOString(),\n scheduledBy: nameOf(principal),\n scheduleError: null,\n })\n }),\n )\n\n const Unschedule = operation(Cms.Operations.Unschedule, ({ input, principal }) =>\n Effect.gen(function* () {\n const found = yield* offering(input.entry, 'unschedule')\n yield* asking(principal, 'unschedule', found.entry)\n return yield* drafted(found.entry.id, {\n scheduledFor: null,\n scheduledBy: null,\n scheduleError: null,\n })\n }),\n )\n\n /** Archiving and its undoing: a fact about the entry, and for what can be hidden, the row. */\n const archiving = (transition: 'archive' | 'unarchive') =>\n operation(\n transition === 'archive' ? Cms.Operations.Archive : Cms.Operations.Unarchive,\n ({ input, principal }) =>\n Effect.gen(function* () {\n const { entry, served } = yield* offering(input.entry, transition)\n yield* asking(principal, transition, entry)\n const shown = served.type.roles.published\n // Archiving what is on show takes it off: that is unpublishing, and asks as it does.\n if (transition === 'archive' && shown !== undefined && entry.targetId !== null) {\n const published = returning(served.binding, [shown.key])\n const [row] = published.patches(\n yield* readRows(served.binding, published.columns, entry.targetId),\n )\n const onShow =\n row !== undefined && Option.isSome(Option.fromNullishOr(row.values[shown.key]))\n if (onShow) yield* asking(principal, 'unpublish', entry)\n }\n const writes = yield* drizzleWrites\n yield* runStatement(\n () =>\n writes\n .update(tables.entries)\n .set({ archivedAt: transition === 'archive' ? now().toISOString() : null })\n .where(eq(tables.entries.id, entry.id))\n .returning({ id: tables.entries.id }),\n 'Database write failed',\n )\n if (transition === 'unarchive' || shown === undefined || entry.targetId === null)\n return { output: {}, entities: yield* entryPatches(entry.id) }\n // What is put away is not left on show. It comes back as unpublished work.\n yield* runStatement(\n () =>\n writes\n .update(served.binding.table)\n .set({ [shown.key]: null })\n .where(eq(served.binding.columns.id!, entry.targetId!))\n .returning({ id: served.binding.columns.id! }),\n 'Database write failed',\n )\n const content = returning(served.binding, [shown.key])\n return {\n output: {},\n entities: [\n ...content.patches(\n yield* readRows(served.binding, content.columns, entry.targetId),\n ),\n ...(yield* entryPatches(entry.id)),\n ],\n }\n }),\n )\n\n /**\n * A revision's value becomes the working copy, and nothing is published. It\n * replaces what the draft held, and takes back a promise made of that: what\n * was scheduled is not what is there now. The saved Model goes too, so an\n * editor fills its form from the values, key by key.\n */\n const Restore = operation(Cms.Operations.Restore, ({ input, principal }) =>\n Effect.gen(function* () {\n const { entry } = yield* offering(input.entry, 'restore')\n yield* asking(principal, 'restore', entry)\n yield* promised(principal, entry)\n const database = yield* DrizzleDatabase\n const [revision] = yield* runStatement(\n () =>\n database\n .select({ values: tables.revisions.values })\n .from(tables.revisions)\n .where(\n and(eq(tables.revisions.entryId, entry.id), eq(tables.revisions.n, input.revision)),\n )\n .limit(1),\n 'Database query failed',\n )\n if (revision === undefined)\n return yield* refuse(`This entry has no revision ${input.revision}`)\n\n const writes = yield* drizzleWrites\n const [held] = yield* readRows(Db.Draft, { updatedAt: tables.drafts.updatedAt }, entry.id)\n const at = now().toISOString()\n const updatedAt =\n held !== undefined && at <= String(held.updatedAt)\n ? new Date(new Date(String(held.updatedAt)).getTime() + 1).toISOString()\n : at\n const restored = {\n values: revision.values,\n model: null,\n form: `restored@${input.revision}`,\n updatedAt,\n updatedBy: nameOf(principal),\n baseRevision: entry.revision,\n scheduledFor: null,\n scheduledBy: null,\n scheduleError: null,\n }\n yield* runStatement(\n () =>\n held === undefined\n ? writes.insert(tables.drafts).values({ id: entry.id, ...restored })\n : writes\n .update(tables.drafts)\n .set(restored)\n .where(eq(tables.drafts.id, entry.id))\n .returning({ id: tables.drafts.id }),\n 'Database write failed',\n )\n return {\n output: { updatedAt },\n entities: [\n ...(yield* entryPatches(entry.id)),\n ...draftPatch.patches(yield* readRows(Db.Draft, draftPatch.columns, entry.id)),\n ...scheduledPatch.patches(yield* readRows(Db.Draft, scheduledPatch.columns, entry.id)),\n ],\n }\n }),\n )\n\n /**\n * Publishes every draft whose time has come, each in its own transaction, as\n * whoever scheduled it: `as` says who a stored name is. One that fails stays\n * scheduled with the reason, and is left alone until its draft changes, so a\n * fault the author must fix is not tried again every minute. The package owns\n * no timer: a cron trigger, an interval or a queue calls this.\n */\n const due = (at: Date, options: { readonly as: (scheduledBy: string | null) => P }) =>\n Effect.gen(function* () {\n const database = yield* DrizzleDatabase\n const waiting = yield* runStatement(\n () =>\n database\n .select({ id: tables.drafts.id, scheduledBy: tables.drafts.scheduledBy })\n .from(tables.drafts)\n .where(\n and(\n isNotNull(tables.drafts.scheduledFor),\n lte(tables.drafts.scheduledFor, at.toISOString()),\n isNull(tables.drafts.scheduleError),\n // What is put away keeps its promise, and keeps it waiting.\n sql`${tables.drafts.id} in (select ${tables.entries.id} from ${tables.entries} where ${tables.entries.archivedAt} is null)`,\n ),\n ),\n 'Database query failed',\n )\n const outcomes: Array<{ readonly entry: string; readonly error: string | null }> = []\n for (const draft of waiting) {\n const id = String(draft.id)\n const principal = options.as((draft.scheduledBy as string | null) ?? null)\n const error = yield* config\n .transaction(\n Effect.gen(function* () {\n if (!(yield* isAuthor(principal, 'Update')))\n return yield* RemoteServer.forbid('Whoever scheduled this is no longer an author')\n const found = yield* offering(id, 'publish')\n yield* asking(principal, 'publish', found.entry)\n yield* publishing(found, principal, found.entry.revision)\n return null\n }),\n )\n .pipe(\n Effect.catch(failure => Effect.succeed(failure.message)),\n Effect.catchDefect(defect => Effect.succeed(String(defect))),\n )\n // Recorded after the failed publish was undone, or it would be undone with it.\n if (error !== null) yield* config.transaction(drafted(id, { scheduleError: error }))\n outcomes.push({ entry: id, error })\n }\n return outcomes\n })\n\n /**\n * Content published already, such as a site's first pages or what another CMS\n * held, written by the path a publish takes: an entry and a draft of\n * `values`, then the publish, which runs the type's own `create` handler as\n * `as` and records revision 1. It is the server's act, not an author's, so\n * `allow` is not asked. `at` is when it was published (default now); `entry`\n * names the entry (default a new id). One transaction: it all lands or none.\n */\n const importing = (item: ImportItem<P>) =>\n config.transaction(\n Effect.gen(function* () {\n const served = byType.get(item.type)\n if (served === undefined)\n return yield* refuse(`\"${item.type}\" is not a type of content this server knows`)\n const writes = yield* drizzleWrites\n const id = item.entry ?? Cms.newEntryId()\n const at = item.at ?? now()\n const labelKey = served.type.roles.label?.key\n const named =\n labelKey === undefined\n ? undefined\n : (item.values as Readonly<Record<string, unknown>> | null)?.[labelKey]\n const entry: EntryRow = {\n id,\n type: item.type,\n targetId: null,\n label: typeof named === 'string' ? named : '',\n archivedAt: null,\n revision: null,\n }\n yield* runStatement(\n () =>\n writes.insert(tables.entries).values({\n ...entry,\n createdBy: nameOf(item.as),\n createdAt: at.toISOString(),\n }),\n 'Database write failed',\n )\n yield* runStatement(\n () =>\n writes.insert(tables.drafts).values({\n id,\n values: item.values,\n model: null,\n form: '',\n updatedAt: at.toISOString(),\n updatedBy: nameOf(item.as),\n baseRevision: null,\n }),\n 'Database write failed',\n )\n const facts: Facts = {\n archivedAt: null,\n row: 'none',\n draft: { scheduledFor: null, scheduleError: null },\n }\n const published = yield* publishing({ entry, served, facts }, item.as, null, at)\n return { entry: id, targetId: published.output.targetId }\n }),\n )\n\n const Unpublish = operation(Cms.Operations.Unpublish, ({ input, principal }) =>\n Effect.gen(function* () {\n const { entry, served } = yield* offering(input.entry, 'unpublish')\n yield* asking(principal, 'unpublish', entry)\n const shown = served.type.roles.published!\n const writes = yield* drizzleWrites\n yield* runStatement(\n () =>\n writes\n .update(served.binding.table)\n .set({ [shown.key]: null })\n .where(eq(served.binding.columns.id!, entry.targetId!))\n .returning({ id: served.binding.columns.id! }),\n 'Database write failed',\n )\n const content = returning(served.binding, [shown.key])\n return {\n output: {},\n entities: [\n ...content.patches(yield* readRows(served.binding, content.columns, entry.targetId!)),\n ...(yield* entryPatches(entry.id)),\n ],\n }\n }),\n )\n\n const sources: ReadonlyArray<EntitySource<P, DrizzleDatabase>> = [\n entries,\n source<P>(Db.Draft),\n source<P>(Db.Revision),\n // A content type's own members' Read rules, as well as its rows'.\n ...config.content.map(served =>\n source<P>(served.binding, { filter: GuardServer.filter(served.entity, policy) }),\n ),\n ]\n const mutations: ReadonlyArray<MutationSource<P, DrizzleDatabase>> = [\n SaveDraft,\n DiscardDraft,\n Publish,\n Unpublish,\n Schedule,\n Unschedule,\n archiving('archive'),\n archiving('unarchive'),\n Restore,\n ]\n\n // A content type with an address is found by it, behind the same boundary as\n // every other read of its table.\n // The descriptor carries what the query means, so this registers the source\n // and says no more: the address column and the order come from the body,\n // through the binding that knows which column holds which field.\n const slugQueries = config.content.flatMap(({ type, binding }) =>\n type.roles.slug === undefined ? [] : [{ descriptor: Cms.bySlug(type), binding }],\n )\n const bySlug = slugQueries.map(({ descriptor, binding }) =>\n query(descriptor, { entity: binding }),\n )\n // The content's bindings must apply the guard set's own rules, not one that only\n // looks like them: what `GuardServer.validate` checks of any server.\n GuardServer.validate(policy, {\n entities: config.content.map(served => served.entity),\n domain: {\n registry: {\n queries: new Map(slugQueries.map(({ descriptor }) => [descriptor.name, descriptor])),\n },\n },\n server: RemoteServer.make({ entities: sources, queries: bySlug }),\n })\n\n return {\n /**\n * The sources of the content types and of the CMS's own Entities, each behind\n * the audience boundary. Register these and not `source(binding)` of a content\n * table: that is how the boundary cannot be forgotten.\n */\n sources,\n queries: [worklist, ...bySlug] as ReadonlyArray<QuerySource<P, DrizzleDatabase>>,\n mutations,\n due,\n import: importing,\n /** The bindings of `Entry`, `Draft` and `Revision`, for a handler that returns patches of them. */\n bindings: Db,\n }\n },\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAeA,MAAa,sBAAsB;CACjC,SAAS,YAAY,eAAe;EAClC,IAAIA,OAAK,IAAI,CAAC,CAAC,WAAW;EAC1B,MAAMA,OAAK,MAAM,CAAC,CAAC,QAAQ;EAC3B,UAAUA,OAAK,WAAW;EAC1B,OAAOA,OAAK,OAAO,CAAC,CAAC,QAAQ;EAC7B,WAAWA,OAAK,YAAY;EAC5B,WAAWA,OAAK,YAAY,CAAC,CAAC,QAAQ;EACtC,YAAYA,OAAK,aAAa;EAC9B,UAAUC,UAAQ,UAAU;CAC9B,CAAC;C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K;EAClB,OAAU,GAAG,QAAQ;EAErB,GAAG,OAAO,QAAQ,KAAI,WACpB,OAAU,OAAO,SAAS,EAAE,QAAQ,YAAY,OAAO,OAAO,QAAQ,MAAM,EAAE,CAAC,CACjF;CACF;CACA,MAAM,YAA+D;EACnE;EACA;EACA;EACA;EACA;EACA;EACA,UAAU,SAAS;EACnB,UAAU,WAAW;EACrB;CACF;CAOA,MAAM,cAAc,OAAO,QAAQ,SAAS,EAAE,MAAM,cAClD,KAAK,MAAM,SAAS,KAAA,IAAY,CAAC,IAAI,CAAC;EAAE,YAAY,IAAI,OAAO,IAAI;EAAG;CAAQ,CAAC,CACjF;CACA,MAAM,SAAS,YAAY,KAAK,EAAE,YAAY,cAC5C,MAAM,YAAY,EAAE,QAAQ,QAAQ,CAAC,CACvC;CAGA,YAAY,SAAS,QAAQ;EAC3B,UAAU,OAAO,QAAQ,KAAI,WAAU,OAAO,MAAM;EACpD,QAAQ,EACN,UAAU,EACR,SAAS,IAAI,IAAI,YAAY,KAAK,EAAE,iBAAiB,CAAC,WAAW,MAAM,UAAU,CAAC,CAAC,EACrF,EACF;EACA,QAAQ,aAAa,KAAK;GAAE,UAAU;GAAS,SAAS;EAAO,CAAC;CAClE,CAAC;CAED,OAAO;;;;;;EAML;EACA,SAAS,CAAC,UAAU,GAAG,MAAM;EAC7B;EACA;EACA,QAAQ;;EAER,UAAU;CACZ;AACF,EACF"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["text","integer","pgText","pgInteger"],"sources":["../src/tables.ts","../src/index.ts"],"sourcesContent":["/**\n * The three tables the CMS keeps beside the application's own. Drizzle tables\n * are made per dialect, so there is one definition for each; they have the same\n * columns. An application adds them to its schema and its migrations as it does\n * any table, and may define its own instead, so long as the columns are these.\n */\nimport { integer as pgInteger, jsonb, pgTable, text as pgText } from 'drizzle-orm/pg-core'\nimport { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'\n\n/**\n * - `cms_entries`: a piece of content from first keystroke to archive. It exists\n * before its content row does.\n * - `cms_drafts`: an entry's one working copy; its id is the entry's.\n * - `cms_revisions`: published values, append-only.\n */\nexport const sqliteTables = () => ({\n entries: sqliteTable('cms_entries', {\n id: text('id').primaryKey(),\n type: text('type').notNull(),\n targetId: text('target_id'),\n label: text('label').notNull(),\n createdBy: text('created_by'),\n createdAt: text('created_at').notNull(),\n archivedAt: text('archived_at'),\n revision: integer('revision'),\n }),\n drafts: sqliteTable('cms_drafts', {\n id: text('id').primaryKey(),\n values: text('values', { mode: 'json' }),\n model: text('model', { mode: 'json' }),\n form: text('form').notNull(),\n updatedAt: text('updated_at').notNull(),\n updatedBy: text('updated_by'),\n baseRevision: integer('base_revision'),\n scheduledFor: text('scheduled_for'),\n scheduledBy: text('scheduled_by'),\n scheduleError: text('schedule_error'),\n }),\n revisions: sqliteTable('cms_revisions', {\n id: text('id').primaryKey(),\n entryId: text('entry_id').notNull(),\n n: integer('n').notNull(),\n values: text('values', { mode: 'json' }),\n publishedAt: text('published_at').notNull(),\n publishedBy: text('published_by'),\n }),\n})\n\nexport const pgTables = () => ({\n entries: pgTable('cms_entries', {\n id: pgText('id').primaryKey(),\n type: pgText('type').notNull(),\n targetId: pgText('target_id'),\n label: pgText('label').notNull(),\n createdBy: pgText('created_by'),\n createdAt: pgText('created_at').notNull(),\n archivedAt: pgText('archived_at'),\n revision: pgInteger('revision'),\n }),\n drafts: pgTable('cms_drafts', {\n id: pgText('id').primaryKey(),\n values: jsonb('values'),\n model: jsonb('model'),\n form: pgText('form').notNull(),\n updatedAt: pgText('updated_at').notNull(),\n updatedBy: pgText('updated_by'),\n baseRevision: pgInteger('base_revision'),\n scheduledFor: pgText('scheduled_for'),\n scheduledBy: pgText('scheduled_by'),\n scheduleError: pgText('schedule_error'),\n }),\n revisions: pgTable('cms_revisions', {\n id: pgText('id').primaryKey(),\n entryId: pgText('entry_id').notNull(),\n n: pgInteger('n').notNull(),\n values: jsonb('values'),\n publishedAt: pgText('published_at').notNull(),\n publishedBy: pgText('published_by'),\n }),\n})\n\n/** The `create table` statements for SQLite, for a test or a first migration. */\nexport const sqliteSchema = `\ncreate table if not exists cms_entries (\n id text primary key, type text not null, target_id text, label text not null,\n created_by text, created_at text not null, archived_at text, revision integer\n);\ncreate table if not exists cms_drafts (\n id text primary key, \"values\" text, model text, form text not null,\n updated_at text not null, updated_by text, base_revision integer,\n scheduled_for text, scheduled_by text, schedule_error text\n);\ncreate table if not exists cms_revisions (\n id text primary key, entry_id text not null, n integer not null, \"values\" text,\n published_at text not null, published_by text\n);\n`\n\nexport type CmsTables = ReturnType<typeof sqliteTables> | ReturnType<typeof pgTables>\n","/**\n * `foldkit-cms-drizzle` — the server half of `foldkit-cms`.\n *\n * It keeps an author's unpublished work in three tables beside the\n * application's own, and it is where the audience boundary is enforced, by a\n * `foldkit-guard` guard set: a principal its `authors` guards refuse is refused\n * entries, drafts and revisions outright, and sees of the content only the\n * rows the content's own Read rules show. Both are a binding's `visible`, so\n * they hold on every path a table is read by.\n *\n * Publishing runs the application's own mutation with the draft's value, inside\n * a transaction that also appends the revision and removes the draft, so a\n * publish happened entirely or did not happen.\n */\nimport {\n and,\n eq,\n inArray,\n isNotNull,\n isNull,\n lte,\n ne,\n sql,\n type AnyColumn,\n type SQL,\n} from 'drizzle-orm'\nimport { Effect, Exit, Option, Schema, Semaphore } from 'effect'\nimport { Cms, type Content, type Facts, type Transition } from 'foldkit-cms'\nimport { Entity, type AnyEntity } from 'foldkit-entity'\nimport { Guards, type Policy, type PrincipalGuard } from 'foldkit-guard'\nimport { GuardServer } from 'foldkit-guard/server'\nimport type { MutationDescriptor, RemoteForbidden } from 'foldkit-remote'\nimport {\n DrizzleDatabase,\n drizzleWrites,\n bind,\n query,\n returning,\n runStatement,\n source,\n type AnyEntityBinding,\n type DrizzleDatabaseService,\n type Visible,\n} from 'foldkit-remote-drizzle'\nimport {\n RemoteServer,\n MutationRefused,\n RemoteServerError,\n type EntitySource,\n type MutationOutcome,\n type MutationSource,\n type QuerySource,\n} from 'foldkit-remote-server'\nimport type { CmsTables } from './tables.js'\n\nexport { pgTables, sqliteSchema, sqliteTables, type CmsTables } from './tables.js'\n\n/**\n * A content type as the server holds it: its declaration, its Entity with the\n * guards attached that its binding was bound from, the binding of its table,\n * and the application's own handlers of its two publish mutations. What\n * publishing a post does stays the application's code.\n */\nexport interface ServedContent<P = any> {\n readonly type: Content<string, any, any, any>\n /** `type.entity` with the guard set's rules attached: `Post.pipe(Guards.attach(G, { … }))`. */\n readonly entity: AnyEntity\n readonly binding: AnyEntityBinding\n readonly create: MutationSource<P, DrizzleDatabase>\n readonly update: MutationSource<P, DrizzleDatabase>\n}\n\n/**\n * Runs some work so that it happened entirely or did not. Drizzle's own\n * transactions differ by driver, so the application says which this is. It\n * fails as the work does, or, when the database refuses the transaction\n * itself, as a `RemoteServerError` that says nothing of why.\n */\nexport type Transaction = <A, E>(\n work: Effect.Effect<A, E, DrizzleDatabase>,\n) => Effect.Effect<A, E | RemoteServerError, DrizzleDatabase>\n\nconst statement = (database: DrizzleDatabaseService, text: string) =>\n Effect.promise(() => {\n const runs = database as unknown as {\n run?: (query: SQL) => unknown\n execute?: (query: SQL) => unknown\n }\n return Promise.resolve(\n runs.run !== undefined ? runs.run(sql.raw(text)) : runs.execute!(sql.raw(text)),\n )\n })\n\n// One connection holds one transaction. Without this, a write another request\n// makes between this one's `begin` and its `rollback` is rolled back with it,\n// after it was reported done.\nconst turns = new WeakMap<object, Semaphore.Semaphore>()\nconst turnOf = (database: object): Semaphore.Semaphore => {\n const held = turns.get(database)\n if (held !== undefined) return held\n const made = Semaphore.makeUnsafe(1)\n turns.set(database, made)\n return made\n}\n\nexport const Transaction = {\n /**\n * `begin`, the work, then `commit` or `rollback`, as statements. For a database\n * that is one connection, such as a SQLite file, which it takes turns at: the\n * operations of this package wait for one another. A write of your own on the\n * same connection does not, so make it inside `Transaction.statements` too.\n * Over a pool each statement may take a different connection: use `drizzle` there.\n */\n statements: (<A, E>(work: Effect.Effect<A, E, DrizzleDatabase>) =>\n Effect.gen(function* () {\n const database = yield* DrizzleDatabase\n return yield* turnOf(database).withPermit(\n statement(database, 'begin').pipe(\n Effect.andThen(work),\n Effect.onExit(exit => statement(database, Exit.isSuccess(exit) ? 'commit' : 'rollback')),\n ),\n )\n })) as Transaction,\n /**\n * Drizzle's `database.transaction`, with the work given the transaction as its\n * database. For a driver whose transactions are asynchronous: Postgres, libSQL.\n */\n drizzle: (<A, E>(work: Effect.Effect<A, E, DrizzleDatabase>) =>\n Effect.gen(function* () {\n const database = (yield* DrizzleDatabase) as unknown as {\n transaction: <T>(run: (inner: DrizzleDatabaseService) => Promise<T>) => Promise<T>\n }\n const failed = Symbol('failed')\n let exit: Exit.Exit<A, E> | undefined\n // A failure must leave the callback as a rejection, or Drizzle commits.\n yield* runStatement(\n () =>\n database\n .transaction(async inner => {\n exit = await Effect.runPromiseExit(\n work.pipe(Effect.provideService(DrizzleDatabase, inner)),\n )\n if (!Exit.isSuccess(exit)) throw failed\n })\n .catch(error => {\n if (error !== failed) throw error\n }),\n 'Database write failed',\n )\n return yield* exit!\n })) as Transaction,\n}\n\nexport interface CmsServerConfig<User> {\n readonly tables: CmsTables\n readonly content: ReadonlyArray<ServedContent<Option.Option<User>>>\n /** How a publish is made whole: `Transaction.statements` or `Transaction.drizzle`. */\n readonly transaction: Transaction\n /** The guard set's bodies, which decide `authors` and `transitions`. */\n readonly policy: Policy<User>\n /**\n * Who reads and writes unpublished work: a principal any of these lets.\n * Everyone else is a visitor.\n */\n readonly authors: ReadonlyArray<PrincipalGuard<User>>\n /**\n * Who of the authors may make a transition: any of its guards lets. One not\n * named is any author's.\n */\n readonly transitions?:\n | { readonly [T in Transition]?: ReadonlyArray<PrincipalGuard<User>> | undefined }\n | undefined\n /**\n * Whether this author may make this transition of this entry, for a rule\n * about the entry itself: asked after the entry is found, the transition is\n * one its state offers, and `transitions` let. Default: they may.\n */\n readonly allow?: (\n principal: Option.Option<User>,\n transition: Transition,\n entry: EntryRow,\n ) => boolean\n /** The server's clock, passed in so a test can hold it. */\n readonly now?: () => Date\n /**\n * The most a draft may hold, as the characters of its JSON: its values and its\n * form's Model together. An author is trusted with their own content, not with\n * the database's disk. Default: one million.\n */\n readonly maxDraftSize?: number\n /** Who a principal is, for `createdBy` and `updatedBy`. */\n readonly nameOf?: (principal: Option.Option<User>) => string | null\n}\n\n/** A row of `cms_entries`, as the server's rules see it. */\nexport interface EntryRow {\n readonly id: string\n readonly type: string\n readonly targetId: string | null\n readonly label: string\n readonly archivedAt: string | null\n readonly revision: number | null\n}\n\n/**\n * One import: content published already, such as a site's first pages or what\n * another CMS held. `as` is whom it is imported as (an editor, so the create\n * handler writes the row); a runner that imports many fixes `as` once and\n * varies the rest. `at` is when it was published (default now); `entry`\n * names the entry (default a new id) — name it where another import points\n * at it, and import what is pointed at first.\n */\nexport interface ImportItem<P> {\n readonly type: string\n readonly values: unknown\n readonly as: P\n readonly at?: Date | undefined\n readonly entry?: string | undefined\n}\n\nconst refuse = (message: string) => new RemoteServerError({ message })\n\n/**\n * Whether a unique index on this column refused a write. Drivers say so in their\n * own words, and Drizzle wraps what they say, so every message down the chain of\n * causes is read. SQLite names the column; Postgres names the constraint, which\n * by default is made of the column's name.\n */\nconst refusedAsDuplicate = (defect: unknown, column: string): boolean => {\n const said: Array<string> = []\n for (let at = defect, depth = 0; at != null && depth < 5; depth++) {\n const { message, constraint, cause } = at as {\n readonly message?: unknown\n readonly constraint?: unknown\n readonly cause?: unknown\n }\n // Drizzle's own message quotes the statement, which names every column.\n if (!String(message).startsWith('Failed query'))\n said.push(String(message ?? at), String(constraint ?? ''))\n at = cause\n }\n const text = said.join(' ')\n const named = new RegExp(`(^|[^a-zA-Z0-9])${column.replace(/[^\\w]/g, '.')}([^a-zA-Z0-9]|$)`)\n return /unique|duplicate/i.test(text) && named.test(text)\n}\n\nexport const CmsServer = {\n make: <User>(config: CmsServerConfig<User>) => {\n type P = Option.Option<User>\n const { tables, policy } = config\n const now = config.now ?? (() => new Date())\n const nameOf = config.nameOf ?? (() => null)\n const maxDraftSize = config.maxDraftSize ?? 1_000_000\n\n const byType = new Map(config.content.map(served => [served.type.name, served]))\n for (const { type, entity, create, update } of config.content) {\n if (!Entity.same(entity, type.entity))\n throw new Error(\n `foldkit-cms-drizzle: content \"${type.name}\" is of ${type.entity.name}, but was given ${entity.name}'s guards`,\n )\n for (const [handler, declared] of [\n [create, type.publish.create],\n [update, type.publish.update],\n ] as const)\n if (handler.mutation !== declared.name)\n throw new Error(\n `foldkit-cms-drizzle: content \"${type.name}\" publishes through \"${declared.name}\", but was given the handler of \"${handler.mutation}\"`,\n )\n const shown = type.roles.published\n if (shown === undefined) continue\n // Whether the rule is right is the application's to say. That it reads the\n // published field at all is checked here: one that does not shows a visitor\n // every row or none.\n if (!Guards.guardFields(entity, 'Read').includes(shown.key))\n throw new Error(\n `foldkit-cms-drizzle: content \"${type.name}\" can be unpublished, but its Read rule does not read \"${shown.key}\"; attach one that does, such as \\`Read: [G.isPublished, G.isAuthor]\\``,\n )\n }\n\n /** Whether any of `guards` lets `principal`, each body told `interaction`. */\n const anyOf = (\n guards: ReadonlyArray<PrincipalGuard<User>>,\n principal: P,\n interaction: string,\n ): Effect.Effect<boolean> =>\n Effect.map(\n Effect.forEach(guards, guard => policy.decide(guard, { principal, interaction })),\n decisions => decisions.some(decision => decision === true),\n )\n /**\n * Whether `principal` is an author, asked as the interaction it is for:\n * reading unpublished work, or changing it, so a body can tell the two.\n */\n const isAuthor = (principal: P, interaction: 'Read' | 'Update') =>\n anyOf(config.authors, principal, interaction)\n /** Whether `transitions` let this author make `transition`; an author's when it names none. */\n const transitionLets = (principal: P, transition: Transition) =>\n Option.match(Option.fromUndefinedOr(config.transitions?.[transition]), {\n onNone: () => Effect.succeed(true),\n onSome: guards => anyOf(guards, principal, transition),\n })\n\n // Unpublished work is an author's. To anyone else these tables are empty, on\n // every path they are read by.\n // The principal reaches a binding untyped: one that is not the guard set's is a\n // defect, never read as a visitor.\n const authorsOnly: Visible = principal =>\n policy.isPrincipal(principal)\n ? Effect.map(isAuthor(principal, 'Read'), author => (author ? undefined : sql`0 = 1`))\n : Effect.die(\n new Error('[foldkit-cms-drizzle] the principal is not an Option of the guard set’s'),\n )\n const Db = bind(Cms.Entities, {\n Entry: {\n table: tables.entries,\n visible: authorsOnly,\n relations: {\n draft: { foreignKey: tables.drafts.id },\n revisions: {\n foreignKey: tables.revisions.entryId,\n orderBy: [{ column: tables.revisions.n, direction: 'desc' }],\n },\n },\n // Not a column: this server derives it, below, with its own clock.\n derived: { state: { supplied: true }, may: { supplied: true } },\n },\n Draft: { table: tables.drafts, visible: authorsOnly },\n Revision: { table: tables.revisions, visible: authorsOnly },\n })\n\n /** What is known of some entries, for their state: their drafts, and whether a visitor sees their rows. */\n const factsOf = (entries: ReadonlyArray<EntryRow>) =>\n Effect.gen(function* () {\n const database = yield* DrizzleDatabase\n const ids = entries.map(entry => entry.id)\n const drafts =\n ids.length === 0\n ? []\n : yield* runStatement(\n () =>\n database\n .select({\n id: tables.drafts.id,\n scheduledFor: tables.drafts.scheduledFor,\n scheduleError: tables.drafts.scheduleError,\n })\n .from(tables.drafts)\n .where(inArray(tables.drafts.id, ids)),\n 'Database query failed',\n )\n const draftOf = new Map(drafts.map(draft => [String(draft.id), draft]))\n\n // Per content type, which target rows exist and which a visitor would see.\n const rows = new Map<string, 'visible' | 'hidden'>()\n for (const [name, { type, binding }] of byType) {\n const targets = entries.flatMap(entry =>\n entry.type === name && entry.targetId !== null ? [entry.targetId] : [],\n )\n if (targets.length === 0) continue\n const id = binding.columns.id!\n const publishedColumn =\n type.roles.published === undefined\n ? undefined\n : binding.columns[type.roles.published.key]\n const found = yield* runStatement(\n () =>\n database\n .select({\n id,\n ...(publishedColumn === undefined ? {} : { shown: publishedColumn }),\n })\n .from(binding.table)\n .where(inArray(id, targets)),\n 'Database query failed',\n )\n for (const row of found)\n rows.set(\n `${name}:${String(row.id)}`,\n publishedColumn === undefined || row.shown !== null ? 'visible' : 'hidden',\n )\n }\n\n return new Map(\n entries.map((entry): readonly [string, Facts] => {\n const draft = draftOf.get(entry.id)\n return [\n entry.id,\n {\n archivedAt: entry.archivedAt,\n row:\n entry.targetId === null\n ? 'none'\n : (rows.get(`${entry.type}:${entry.targetId}`) ?? 'none'),\n draft:\n draft === undefined\n ? null\n : {\n scheduledFor: (draft.scheduledFor as string | null) ?? null,\n scheduleError: (draft.scheduleError as string | null) ?? null,\n },\n },\n ]\n }),\n )\n })\n\n const entryColumns = {\n id: tables.entries.id,\n type: tables.entries.type,\n targetId: tables.entries.targetId,\n label: tables.entries.label,\n archivedAt: tables.entries.archivedAt,\n revision: tables.entries.revision,\n }\n const findEntry = (id: string) =>\n Effect.gen(function* () {\n const database = yield* DrizzleDatabase\n const [row] = yield* runStatement(\n () =>\n database\n .select(entryColumns)\n .from(tables.entries)\n .where(eq(tables.entries.id, id))\n .limit(1),\n 'Database query failed',\n )\n return row as EntryRow | undefined\n })\n\n // An entry's `state` and `may` are not columns. The generated source reads the\n // rest; the state is derived from what is known of each entry, with this server's\n // clock, and `may` is what `allow` lets the reader ask of it.\n const generated = source<P>(Db.Entry)\n const entries: EntitySource<P, DrizzleDatabase> = {\n ...generated,\n read: context =>\n Effect.gen(function* () {\n const wantsState = context.fields.includes('state')\n const wantsMay = context.fields.includes('may')\n const fields = context.fields.filter(field => field !== 'state' && field !== 'may')\n const records = yield* generated.read({\n ...context,\n // Both need these of each entry, asked for or not; `allow` is given the row.\n fields:\n wantsState || wantsMay\n ? [\n ...new Set([\n ...fields,\n 'type',\n 'targetId',\n 'archivedAt',\n ...(wantsMay ? ['label', 'revision'] : []),\n ]),\n ]\n : fields,\n })\n if (!wantsState && !wantsMay) return records\n const rows = records.map((record): EntryRow => ({\n id: record.id,\n type: String(record.values.type),\n targetId: (record.values.targetId as string | null) ?? null,\n label: String(record.values.label ?? ''),\n archivedAt: (record.values.archivedAt as string | null) ?? null,\n revision: (record.values.revision as number | null) ?? null,\n }))\n const facts = yield* factsOf(rows)\n const at = now()\n // Each transition's guards are decided once for this reader, not per entry.\n const lets = new Map(\n wantsMay\n ? yield* Effect.forEach(Cms.transitions, transition =>\n Effect.map(\n transitionLets(context.principal, transition),\n allowed => [transition, allowed] as const,\n ),\n )\n : [],\n )\n return records.map((record, index) => {\n const row = rows[index]!\n const known = facts.get(record.id)!\n return {\n id: record.id,\n values: {\n ...Object.fromEntries(\n Object.entries(record.values).filter(\n ([field]) => field === 'id' || context.fields.includes(field),\n ),\n ),\n ...(wantsState ? { state: Cms.state(known, at) } : {}),\n // What `transitions` and `allow` let this reader ask of this entry,\n // whatever its state offers now: the state says that, and a draft\n // saved a moment later changes it without asking the server again.\n ...(wantsMay\n ? {\n may: Cms.transitions.filter(\n transition =>\n lets.get(transition) === true &&\n config.allow?.(context.principal, transition, row) !== false,\n ),\n }\n : {}),\n },\n }\n })\n }),\n }\n\n // The descriptor carries all three questions and the order; this says only\n // which table answers them. The audience boundary is unchanged: `visible`\n // on the binding is conjoined with the body, as for every source here.\n const worklist: QuerySource<P, DrizzleDatabase> = query(Cms.Entries, { entity: Db.Entry })\n\n /** A mutation of this server: its principal and its database are fixed, its input is the descriptor's. */\n const operation = <Name extends string, Input, Output>(\n descriptor: MutationDescriptor<Name, Input, Output>,\n run: (context: {\n readonly input: Input\n readonly principal: P\n }) => Effect.Effect<\n MutationOutcome<Output>,\n RemoteServerError | MutationRefused | RemoteForbidden,\n DrizzleDatabase\n >,\n ): MutationSource<P, DrizzleDatabase> =>\n // Asked before anything is looked up: a visitor is not told which entries there are.\n // Then the whole of it is one transaction: a save that made an entry and\n // could not make its draft leaves neither.\n RemoteServer.mutation<P, DrizzleDatabase, Name, Input, Output>(descriptor, context =>\n Effect.flatMap(isAuthor(context.principal, 'Update'), author =>\n author\n ? config.transaction(run(context))\n : Effect.fail(RemoteServer.forbid('Only an author may change unpublished work')),\n ),\n )\n\n /**\n * A scheduled draft is published, when its time comes, as whoever scheduled it.\n * So changing it, replacing it or removing it is for someone who could have\n * scheduled it: otherwise an author who may not publish would publish through\n * someone who may.\n */\n const promised = (principal: P, entry: EntryRow) =>\n Effect.gen(function* () {\n const [draft] = yield* readRows(\n Db.Draft,\n { scheduledFor: tables.drafts.scheduledFor },\n entry.id,\n )\n if (draft?.scheduledFor == null) return\n if (\n !(yield* transitionLets(principal, 'schedule')) ||\n config.allow?.(principal, 'schedule', entry) === false\n )\n return yield* RemoteServer.forbid(\n 'This draft is scheduled, and only someone who may schedule it may change it',\n )\n })\n\n /**\n * Whether this author may make this transition: its `transitions`, and `allow`\n * of the entry once there is one.\n */\n const asking = (principal: P, transition: Transition, entry: EntryRow | undefined) =>\n Effect.flatMap(transitionLets(principal, transition), lets =>\n !lets || (entry !== undefined && config.allow?.(principal, transition, entry) === false)\n ? Effect.fail(RemoteServer.forbid(`This author may not ${transition} this entry`))\n : Effect.void,\n )\n\n const draftFields = ['values', 'model', 'form', 'updatedAt', 'updatedBy', 'baseRevision']\n const draftPatch = returning(Db.Draft, draftFields)\n const entryPatch = returning(Db.Entry, [\n 'type',\n 'targetId',\n 'label',\n 'createdAt',\n 'archivedAt',\n 'revision',\n ])\n /**\n * The entry as the client should now hold it, its state with it: an operation\n * changes the state, and a patch without it would leave the old one on screen.\n */\n const entryPatches = (id: string) =>\n Effect.gen(function* () {\n const patches = entryPatch.patches(yield* readRows(Db.Entry, entryPatch.columns, id))\n const entry = yield* findEntry(id)\n if (entry === undefined) return patches\n const facts = (yield* factsOf([entry])).get(id)!\n return patches.map(patch => ({\n ...patch,\n values: { ...patch.values, state: Cms.state(facts, now()) },\n }))\n })\n const readRows = (binding: AnyEntityBinding, columns: Record<string, AnyColumn>, id: string) =>\n Effect.gen(function* () {\n const database = yield* DrizzleDatabase\n return yield* runStatement(\n () => database.select(columns).from(binding.table).where(eq(binding.columns.id!, id)),\n 'Database query failed',\n )\n })\n\n const SaveDraft = operation(Cms.Operations.SaveDraft, ({ input, principal }) =>\n Effect.gen(function* () {\n if (!byType.has(input.type))\n return yield* refuse(`\"${input.type}\" is not a type of content this server knows`)\n const size = JSON.stringify([input.values, input.model]).length\n if (size > maxDraftSize)\n return yield* refuse(\n `This draft is too large to save: ${size} characters, and ${maxDraftSize} is the most`,\n )\n // An id nobody has is something new: the client named it, and this makes it.\n const existing = yield* findEntry(input.entry)\n if (existing !== undefined && existing.type !== input.type)\n return yield* refuse(`This entry is of \"${existing.type}\", not \"${input.type}\"`)\n if (existing?.archivedAt != null) return yield* refuse('An archived entry takes no draft')\n yield* asking(principal, 'save', existing)\n if (existing !== undefined) yield* promised(principal, existing)\n\n const database = yield* drizzleWrites\n const who = nameOf(principal)\n const id = existing?.id ?? input.entry\n const [held] = yield* readRows(Db.Draft, { updatedAt: tables.drafts.updatedAt }, id)\n const previous = held === undefined ? null : String(held.updatedAt)\n // Two saves in one millisecond must still be told apart by the next one.\n const at = now().toISOString()\n const updatedAt =\n previous !== null && at <= previous\n ? new Date(new Date(previous).getTime() + 1).toISOString()\n : at\n const written = {\n values: input.values,\n model: input.model,\n form: input.form,\n updatedAt,\n updatedBy: who,\n // A draft that changed is worth trying again when its time comes.\n scheduleError: null,\n }\n\n // The first draft of an entry has nothing to have been based on. Asked before\n // anything is written, so a refusal leaves no entry behind.\n if (previous === null && input.basedOn !== null)\n return yield* RemoteServer.refuse(\n Cms.Operations.SaveDraft,\n Cms.refusal.conflict,\n 'This draft was discarded',\n )\n if (existing === undefined) {\n yield* runStatement(\n () =>\n database.insert(tables.entries).values({\n id,\n type: input.type,\n targetId: null,\n label: input.label,\n createdBy: who,\n createdAt: at,\n archivedAt: null,\n revision: null,\n }),\n 'Database write failed',\n )\n }\n if (previous === null) {\n yield* runStatement(\n () => database.insert(tables.drafts).values({ id, ...written, baseRevision: null }),\n 'Database write failed',\n )\n } else {\n // Compare and set: the save names what it was made from, and a newer one\n // on the server means someone else saved in between.\n const changed = yield* runStatement(\n () =>\n database\n .update(tables.drafts)\n .set(written)\n .where(\n and(eq(tables.drafts.id, id), eq(tables.drafts.updatedAt, input.basedOn ?? '')),\n )\n .returning({ id: tables.drafts.id }),\n 'Database write failed',\n )\n if (changed.length === 0)\n return yield* RemoteServer.refuse(\n Cms.Operations.SaveDraft,\n Cms.refusal.conflict,\n 'This draft was saved by someone else since',\n )\n }\n if (existing !== undefined && existing.label !== input.label) {\n yield* runStatement(\n () =>\n database\n .update(tables.entries)\n .set({ label: input.label })\n .where(eq(tables.entries.id, id))\n .returning({ id: tables.entries.id }),\n 'Database write failed',\n )\n }\n\n const entities = [\n ...(yield* entryPatches(id)),\n ...draftPatch.patches(yield* readRows(Db.Draft, draftPatch.columns, id)),\n ]\n return { output: { entry: id as never, updatedAt }, entities }\n }),\n )\n\n const DiscardDraft = operation(Cms.Operations.DiscardDraft, ({ input, principal }) =>\n Effect.gen(function* () {\n const entry = yield* findEntry(input.entry)\n if (entry === undefined) return yield* refuse('There is no such entry')\n yield* asking(principal, 'discard', entry)\n yield* promised(principal, entry)\n const database = yield* drizzleWrites\n yield* runStatement(\n () => database.delete(tables.drafts).where(eq(tables.drafts.id, entry.id)),\n 'Database write failed',\n )\n const gone = [{ entity: 'CmsDraft', id: entry.id }]\n // Never published, and now with nothing entered: there is nothing left of it.\n if (entry.targetId !== null)\n return { output: {}, deleted: gone, entities: yield* entryPatches(entry.id) }\n yield* runStatement(\n () => database.delete(tables.entries).where(eq(tables.entries.id, entry.id)),\n 'Database write failed',\n )\n return { output: {}, deleted: [...gone, { entity: 'CmsEntry', id: entry.id }] }\n }),\n )\n\n const done: Readonly<Record<Exclude<Transition, 'save' | 'discard'>, string>> = {\n publish: 'published',\n unpublish: 'unpublished',\n schedule: 'scheduled',\n unschedule: 'unscheduled',\n archive: 'archived',\n unarchive: 'unarchived',\n restore: 'restored',\n }\n /** The entry, if its state offers this transition now. */\n const offering = (id: string, transition: Exclude<Transition, 'save' | 'discard'>) =>\n Effect.gen(function* () {\n const entry = yield* findEntry(id)\n if (entry === undefined) return yield* refuse('There is no such entry')\n const served = byType.get(entry.type)\n if (served === undefined)\n return yield* refuse(`\"${entry.type}\" is not a type of content this server knows`)\n const facts = (yield* factsOf([entry])).get(entry.id)!\n if (!Cms.offers(facts, now(), served.type).includes(transition))\n return yield* refuse(`This entry cannot be ${done[transition]} as it stands`)\n return { entry, served, facts }\n })\n\n /** What the `published` role's column holds for \"now\". */\n const stamp = (column: AnyColumn, at: Date) => {\n const kind = column.dataType.split(' ')[0]\n return kind === 'string' ? at.toISOString() : kind === 'number' ? at.getTime() : at\n }\n\n const scheduledPatch = returning(Db.Draft, ['scheduledFor', 'scheduleError'])\n const revisionPatch = returning(Db.Revision, ['n', 'values', 'publishedAt', 'publishedBy'])\n\n type Found = Effect.Success<ReturnType<typeof offering>>\n\n /** The draft as the publish mutation would take it, or why it would not. */\n const readied = ({ entry, served, facts }: Found) =>\n Effect.gen(function* () {\n const [draft] = yield* readRows(Db.Draft, { values: tables.drafts.values }, entry.id)\n // A row that was deleted under its entry is made again.\n const creating = facts.row === 'none'\n const handler = creating ? served.create : served.update\n const value =\n creating || typeof draft?.values !== 'object' || draft.values === null\n ? draft?.values\n : { ...draft.values, id: entry.targetId }\n const decoded = yield* Schema.decodeUnknownEffect(handler.Input)(value).pipe(\n Effect.mapError(error => refuse(`This draft is not ready to publish: ${String(error)}`)),\n )\n return { draft, creating, handler, decoded }\n })\n\n /** A publish, for the operation that asks for one, the schedule that comes due, and an import. */\n const publishing = (found: Found, principal: P, basedOn: number | null, at: Date = now()) =>\n Effect.gen(function* () {\n const { entry, served } = found\n const database = yield* DrizzleDatabase\n const held = entry.revision\n const conflict = RemoteServer.refuse(\n Cms.Operations.Publish,\n Cms.refusal.conflict,\n 'This entry was published by someone else since',\n )\n if (held !== basedOn) return yield* conflict\n const { draft, creating, handler, decoded } = yield* readied(found)\n\n // A taken slug is refused by name, on its key. This is advice: two publishes\n // can both pass it, and then the unique index is the rule, below.\n const address = served.type.roles.slug\n const slug =\n address === undefined\n ? undefined\n : (decoded as Readonly<Record<string, unknown>> | null)?.[address.key]\n const taken = (key: string) =>\n RemoteServer.refuse(\n Cms.Operations.Publish,\n Cms.refusal.field(key, `That address is taken: \"${String(slug)}\" is already used`),\n )\n if (address !== undefined && typeof slug === 'string') {\n const column = served.binding.columns[address.key]!\n const id = served.binding.columns.id!\n const others = yield* runStatement(\n () =>\n database\n .select({ id })\n .from(served.binding.table)\n .where(and(eq(column, slug), creating ? undefined : ne(id, entry.targetId!)))\n .limit(1),\n 'Database query failed',\n )\n if (others.length > 0) return yield* taken(address.key)\n }\n\n return yield* Effect.gen(function* () {\n const writes = yield* drizzleWrites\n const n = (held ?? 0) + 1\n // Compare and set, first: of two publishes made from one revision, one\n // finds the number already moved, and nothing of it is written.\n const moved = yield* runStatement(\n () =>\n writes\n .update(tables.entries)\n .set({ revision: n })\n .where(\n and(\n eq(tables.entries.id, entry.id),\n held === null\n ? isNull(tables.entries.revision)\n : eq(tables.entries.revision, held),\n ),\n )\n .returning({ id: tables.entries.id }),\n 'Database write failed',\n )\n if (moved.length === 0) return yield* conflict\n // Scheduled publishing has no client request, so the id names this\n // publish of this entry: the same on a retry, another on the next.\n const ran = yield* handler.run({\n input: decoded,\n principal,\n requestId: `publish:${entry.id}:${held ?? 'new'}`,\n operation: Option.some(`publish:${entry.id}:${held ?? 'new'}`),\n })\n const targetId = creating\n ? String((ran.output as { readonly id: unknown }).id)\n : entry.targetId!\n const shown = served.type.roles.published\n if (shown !== undefined) {\n // Publishing shows the row. One already shown keeps its first date.\n const column = served.binding.columns[shown.key]!\n yield* runStatement(\n () =>\n writes\n .update(served.binding.table)\n .set({ [shown.key]: stamp(column, at) })\n .where(and(eq(served.binding.columns.id!, targetId), isNull(column)))\n .returning({ id: served.binding.columns.id! }),\n 'Database write failed',\n )\n }\n const revisionId = `${entry.id}:${n}`\n yield* runStatement(\n () =>\n writes.insert(tables.revisions).values({\n id: revisionId,\n entryId: entry.id,\n n,\n values: draft?.values,\n publishedAt: at.toISOString(),\n publishedBy: nameOf(principal),\n }),\n 'Database write failed',\n )\n yield* runStatement(\n () => writes.delete(tables.drafts).where(eq(tables.drafts.id, entry.id)),\n 'Database write failed',\n )\n yield* runStatement(\n () =>\n writes\n .update(tables.entries)\n .set({ targetId })\n .where(eq(tables.entries.id, entry.id))\n .returning({ id: tables.entries.id }),\n 'Database write failed',\n )\n return {\n output: { entry: entry.id as never, targetId, revision: n },\n entities: [\n ...ran.entities,\n // The row as the handler left it, every column: a handler need not patch what it wrote.\n ...(yield* returning.row(served.binding, targetId)),\n ...(yield* entryPatches(entry.id)),\n ...revisionPatch.patches(\n yield* readRows(Db.Revision, revisionPatch.columns, revisionId),\n ),\n ],\n connections: ran.connections,\n deleted: [...ran.deleted, { entity: 'CmsDraft', id: entry.id }],\n }\n }).pipe(\n // The publish that lost the race: the index refused it, and it is the same\n // news as the check's. Any other defect is left as it was.\n Effect.catchDefect(defect =>\n address !== undefined &&\n refusedAsDuplicate(defect, served.binding.columns[address.key]!.name)\n ? Effect.fail(taken(address.key))\n : Effect.die(defect),\n ),\n )\n })\n\n const Publish = operation(Cms.Operations.Publish, ({ input, principal }) =>\n Effect.gen(function* () {\n const found = yield* offering(input.entry, 'publish')\n yield* asking(principal, 'publish', found.entry)\n return yield* publishing(found, principal, input.basedOn)\n }),\n )\n\n /** Writes to an entry's draft, and answers with both as the client should now hold them. */\n const drafted = (id: string, values: object) =>\n Effect.gen(function* () {\n const writes = yield* drizzleWrites\n yield* runStatement(\n () =>\n writes\n .update(tables.drafts)\n .set(values)\n .where(eq(tables.drafts.id, id))\n .returning({ id: tables.drafts.id }),\n 'Database write failed',\n )\n return {\n output: {},\n entities: [\n ...(yield* entryPatches(id)),\n ...scheduledPatch.patches(yield* readRows(Db.Draft, scheduledPatch.columns, id)),\n ],\n }\n })\n\n const Schedule = operation(Cms.Operations.Schedule, ({ input, principal }) =>\n Effect.gen(function* () {\n const found = yield* offering(input.entry, 'schedule')\n yield* asking(principal, 'schedule', found.entry)\n if (Number.isNaN(new Date(input.at).getTime()))\n return yield* refuse(`\"${input.at}\" is not a time`)\n // What cannot be published now is not promised for later.\n yield* readied(found)\n return yield* drafted(found.entry.id, {\n scheduledFor: new Date(input.at).toISOString(),\n scheduledBy: nameOf(principal),\n scheduleError: null,\n })\n }),\n )\n\n const Unschedule = operation(Cms.Operations.Unschedule, ({ input, principal }) =>\n Effect.gen(function* () {\n const found = yield* offering(input.entry, 'unschedule')\n yield* asking(principal, 'unschedule', found.entry)\n return yield* drafted(found.entry.id, {\n scheduledFor: null,\n scheduledBy: null,\n scheduleError: null,\n })\n }),\n )\n\n /** Archiving and its undoing: a fact about the entry, and for what can be hidden, the row. */\n const archiving = (transition: 'archive' | 'unarchive') =>\n operation(\n transition === 'archive' ? Cms.Operations.Archive : Cms.Operations.Unarchive,\n ({ input, principal }) =>\n Effect.gen(function* () {\n const { entry, served } = yield* offering(input.entry, transition)\n yield* asking(principal, transition, entry)\n const shown = served.type.roles.published\n // Archiving what is on show takes it off: that is unpublishing, and asks as it does.\n if (transition === 'archive' && shown !== undefined && entry.targetId !== null) {\n const published = returning(served.binding, [shown.key])\n const [row] = published.patches(\n yield* readRows(served.binding, published.columns, entry.targetId),\n )\n const onShow =\n row !== undefined && Option.isSome(Option.fromNullishOr(row.values[shown.key]))\n if (onShow) yield* asking(principal, 'unpublish', entry)\n }\n const writes = yield* drizzleWrites\n yield* runStatement(\n () =>\n writes\n .update(tables.entries)\n .set({ archivedAt: transition === 'archive' ? now().toISOString() : null })\n .where(eq(tables.entries.id, entry.id))\n .returning({ id: tables.entries.id }),\n 'Database write failed',\n )\n if (transition === 'unarchive' || shown === undefined || entry.targetId === null)\n return { output: {}, entities: yield* entryPatches(entry.id) }\n // What is put away is not left on show. It comes back as unpublished work.\n yield* runStatement(\n () =>\n writes\n .update(served.binding.table)\n .set({ [shown.key]: null })\n .where(eq(served.binding.columns.id!, entry.targetId!))\n .returning({ id: served.binding.columns.id! }),\n 'Database write failed',\n )\n const content = returning(served.binding, [shown.key])\n return {\n output: {},\n entities: [\n ...content.patches(\n yield* readRows(served.binding, content.columns, entry.targetId),\n ),\n ...(yield* entryPatches(entry.id)),\n ],\n }\n }),\n )\n\n /**\n * A revision's value becomes the working copy, and nothing is published. It\n * replaces what the draft held, and takes back a promise made of that: what\n * was scheduled is not what is there now. The saved Model goes too, so an\n * editor fills its form from the values, key by key.\n */\n const Restore = operation(Cms.Operations.Restore, ({ input, principal }) =>\n Effect.gen(function* () {\n const { entry } = yield* offering(input.entry, 'restore')\n yield* asking(principal, 'restore', entry)\n yield* promised(principal, entry)\n const database = yield* DrizzleDatabase\n const [revision] = yield* runStatement(\n () =>\n database\n .select({ values: tables.revisions.values })\n .from(tables.revisions)\n .where(\n and(eq(tables.revisions.entryId, entry.id), eq(tables.revisions.n, input.revision)),\n )\n .limit(1),\n 'Database query failed',\n )\n if (revision === undefined)\n return yield* refuse(`This entry has no revision ${input.revision}`)\n\n const writes = yield* drizzleWrites\n const [held] = yield* readRows(Db.Draft, { updatedAt: tables.drafts.updatedAt }, entry.id)\n const at = now().toISOString()\n const updatedAt =\n held !== undefined && at <= String(held.updatedAt)\n ? new Date(new Date(String(held.updatedAt)).getTime() + 1).toISOString()\n : at\n const restored = {\n values: revision.values,\n model: null,\n form: `restored@${input.revision}`,\n updatedAt,\n updatedBy: nameOf(principal),\n baseRevision: entry.revision,\n scheduledFor: null,\n scheduledBy: null,\n scheduleError: null,\n }\n yield* runStatement(\n () =>\n held === undefined\n ? writes.insert(tables.drafts).values({ id: entry.id, ...restored })\n : writes\n .update(tables.drafts)\n .set(restored)\n .where(eq(tables.drafts.id, entry.id))\n .returning({ id: tables.drafts.id }),\n 'Database write failed',\n )\n return {\n output: { updatedAt },\n entities: [\n ...(yield* entryPatches(entry.id)),\n ...draftPatch.patches(yield* readRows(Db.Draft, draftPatch.columns, entry.id)),\n ...scheduledPatch.patches(yield* readRows(Db.Draft, scheduledPatch.columns, entry.id)),\n ],\n }\n }),\n )\n\n /**\n * Publishes every draft whose time has come, each in its own transaction, as\n * whoever scheduled it: `as` says who a stored name is. One that fails stays\n * scheduled with the reason, and is left alone until its draft changes, so a\n * fault the author must fix is not tried again every minute. The package owns\n * no timer: a cron trigger, an interval or a queue calls this.\n */\n const due = (at: Date, options: { readonly as: (scheduledBy: string | null) => P }) =>\n Effect.gen(function* () {\n const database = yield* DrizzleDatabase\n const waiting = yield* runStatement(\n () =>\n database\n .select({ id: tables.drafts.id, scheduledBy: tables.drafts.scheduledBy })\n .from(tables.drafts)\n .where(\n and(\n isNotNull(tables.drafts.scheduledFor),\n lte(tables.drafts.scheduledFor, at.toISOString()),\n isNull(tables.drafts.scheduleError),\n // What is put away keeps its promise, and keeps it waiting.\n sql`${tables.drafts.id} in (select ${tables.entries.id} from ${tables.entries} where ${tables.entries.archivedAt} is null)`,\n ),\n ),\n 'Database query failed',\n )\n const outcomes: Array<{ readonly entry: string; readonly error: string | null }> = []\n for (const draft of waiting) {\n const id = String(draft.id)\n const principal = options.as((draft.scheduledBy as string | null) ?? null)\n const error = yield* config\n .transaction(\n Effect.gen(function* () {\n if (!(yield* isAuthor(principal, 'Update')))\n return yield* RemoteServer.forbid('Whoever scheduled this is no longer an author')\n const found = yield* offering(id, 'publish')\n yield* asking(principal, 'publish', found.entry)\n yield* publishing(found, principal, found.entry.revision)\n return null\n }),\n )\n .pipe(\n Effect.catch(failure => Effect.succeed(failure.message)),\n Effect.catchDefect(defect => Effect.succeed(String(defect))),\n )\n // Recorded after the failed publish was undone, or it would be undone with it.\n if (error !== null) yield* config.transaction(drafted(id, { scheduleError: error }))\n outcomes.push({ entry: id, error })\n }\n return outcomes\n })\n\n /**\n * Content published already, such as a site's first pages or what another CMS\n * held, written by the path a publish takes: an entry and a draft of\n * `values`, then the publish, which runs the type's own `create` handler as\n * `as` and records revision 1. It is the server's act, not an author's, so\n * `allow` is not asked. `at` is when it was published (default now); `entry`\n * names the entry (default a new id). One transaction: it all lands or none.\n */\n const importing = (item: ImportItem<P>) =>\n config.transaction(\n Effect.gen(function* () {\n const served = byType.get(item.type)\n if (served === undefined)\n return yield* refuse(`\"${item.type}\" is not a type of content this server knows`)\n const writes = yield* drizzleWrites\n const id = item.entry ?? Cms.newEntryId()\n const at = item.at ?? now()\n const labelKey = served.type.roles.label?.key\n const named =\n labelKey === undefined\n ? undefined\n : (item.values as Readonly<Record<string, unknown>> | null)?.[labelKey]\n const entry: EntryRow = {\n id,\n type: item.type,\n targetId: null,\n label: typeof named === 'string' ? named : '',\n archivedAt: null,\n revision: null,\n }\n yield* runStatement(\n () =>\n writes.insert(tables.entries).values({\n ...entry,\n createdBy: nameOf(item.as),\n createdAt: at.toISOString(),\n }),\n 'Database write failed',\n )\n yield* runStatement(\n () =>\n writes.insert(tables.drafts).values({\n id,\n values: item.values,\n model: null,\n form: '',\n updatedAt: at.toISOString(),\n updatedBy: nameOf(item.as),\n baseRevision: null,\n }),\n 'Database write failed',\n )\n const facts: Facts = {\n archivedAt: null,\n row: 'none',\n draft: { scheduledFor: null, scheduleError: null },\n }\n const published = yield* publishing({ entry, served, facts }, item.as, null, at)\n return { entry: id, targetId: published.output.targetId }\n }),\n )\n\n const Unpublish = operation(Cms.Operations.Unpublish, ({ input, principal }) =>\n Effect.gen(function* () {\n const { entry, served } = yield* offering(input.entry, 'unpublish')\n yield* asking(principal, 'unpublish', entry)\n const shown = served.type.roles.published!\n const writes = yield* drizzleWrites\n yield* runStatement(\n () =>\n writes\n .update(served.binding.table)\n .set({ [shown.key]: null })\n .where(eq(served.binding.columns.id!, entry.targetId!))\n .returning({ id: served.binding.columns.id! }),\n 'Database write failed',\n )\n const content = returning(served.binding, [shown.key])\n return {\n output: {},\n entities: [\n ...content.patches(yield* readRows(served.binding, content.columns, entry.targetId!)),\n ...(yield* entryPatches(entry.id)),\n ],\n }\n }),\n )\n\n const sources: ReadonlyArray<EntitySource<P, DrizzleDatabase>> = [\n entries,\n source<P>(Db.Draft),\n source<P>(Db.Revision),\n // A content type's own members' Read rules, as well as its rows'.\n ...config.content.map(served =>\n source<P>(served.binding, { filter: GuardServer.filter(served.entity, policy) }),\n ),\n ]\n const mutations: ReadonlyArray<MutationSource<P, DrizzleDatabase>> = [\n SaveDraft,\n DiscardDraft,\n Publish,\n Unpublish,\n Schedule,\n Unschedule,\n archiving('archive'),\n archiving('unarchive'),\n Restore,\n ]\n\n // A content type with an address is found by it, behind the same boundary as\n // every other read of its table.\n // The descriptor carries what the query means, so this registers the source\n // and says no more: the address column and the order come from the body,\n // through the binding that knows which column holds which field.\n const slugQueries = config.content.flatMap(({ type, binding }) =>\n type.roles.slug === undefined ? [] : [{ descriptor: Cms.bySlug(type), binding }],\n )\n const bySlug = slugQueries.map(({ descriptor, binding }) =>\n query(descriptor, { entity: binding }),\n )\n // The content's bindings must apply the guard set's own rules, not one that only\n // looks like them: what `GuardServer.validate` checks of any server.\n GuardServer.validate(policy, {\n entities: config.content.map(served => served.entity),\n domain: {\n registry: {\n queries: new Map(slugQueries.map(({ descriptor }) => [descriptor.name, descriptor])),\n },\n },\n server: RemoteServer.make({ entities: sources, queries: bySlug }),\n })\n\n return {\n /**\n * The sources of the content types and of the CMS's own Entities, each behind\n * the audience boundary. Register these and not `source(binding)` of a content\n * table: that is how the boundary cannot be forgotten.\n */\n sources,\n queries: [worklist, ...bySlug] as ReadonlyArray<QuerySource<P, DrizzleDatabase>>,\n mutations,\n due,\n import: importing,\n /** The bindings of `Entry`, `Draft` and `Revision`, for a handler that returns patches of them. */\n bindings: Db,\n }\n },\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAeA,MAAa,sBAAsB;CACjC,SAAS,YAAY,eAAe;EAClC,IAAIA,OAAK,IAAI,CAAC,CAAC,WAAW;EAC1B,MAAMA,OAAK,MAAM,CAAC,CAAC,QAAQ;EAC3B,UAAUA,OAAK,WAAW;EAC1B,OAAOA,OAAK,OAAO,CAAC,CAAC,QAAQ;EAC7B,WAAWA,OAAK,YAAY;EAC5B,WAAWA,OAAK,YAAY,CAAC,CAAC,QAAQ;EACtC,YAAYA,OAAK,aAAa;EAC9B,UAAUC,U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Aa,MAAM,EAAE,CAClC;EACF;CACF,CAAC,CACH;CAEA,MAAM,UAA2D;EAC/D;EACA,OAAU,GAAG,KAAK;EAClB,OAAU,GAAG,QAAQ;EAErB,GAAG,OAAO,QAAQ,KAAI,WACpB,OAAU,OAAO,SAAS,EAAE,QAAQ,YAAY,OAAO,OAAO,QAAQ,MAAM,EAAE,CAAC,CACjF;CACF;CACA,MAAM,YAA+D;EACnE;EACA;EACA;EACA;EACA;EACA;EACA,UAAU,SAAS;EACnB,UAAU,WAAW;EACrB;CACF;CAOA,MAAM,cAAc,OAAO,QAAQ,SAAS,EAAE,MAAM,cAClD,KAAK,MAAM,SAAS,KAAA,IAAY,CAAC,IAAI,CAAC;EAAE,YAAY,IAAI,OAAO,IAAI;EAAG;CAAQ,CAAC,CACjF;CACA,MAAM,SAAS,YAAY,KAAK,EAAE,YAAY,cAC5C,MAAM,YAAY,EAAE,QAAQ,QAAQ,CAAC,CACvC;CAGA,YAAY,SAAS,QAAQ;EAC3B,UAAU,OAAO,QAAQ,KAAI,WAAU,OAAO,MAAM;EACpD,QAAQ,EACN,UAAU,EACR,SAAS,IAAI,IAAI,YAAY,KAAK,EAAE,iBAAiB,CAAC,WAAW,MAAM,UAAU,CAAC,CAAC,EACrF,EACF;EACA,QAAQ,aAAa,KAAK;GAAE,UAAU;GAAS,SAAS;EAAO,CAAC;CAClE,CAAC;CAED,OAAO;;;;;;EAML;EACA,SAAS,CAAC,UAAU,GAAG,MAAM;EAC7B;EACA;EACA,QAAQ;;EAER,UAAU;CACZ;AACF,EACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "foldkit-cms-drizzle",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.15",
|
|
4
4
|
"description": "The server half of foldkit-cms over Drizzle: the entries, drafts and revisions tables, the draft operations with their conflict rule, the worklist query, and the audience boundary between a visitor and an author.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"foldkit",
|
|
@@ -39,12 +39,12 @@
|
|
|
39
39
|
"LICENSE"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"foldkit-cms": "0.7.
|
|
43
|
-
"foldkit-
|
|
44
|
-
"foldkit-
|
|
45
|
-
"foldkit-remote": "0.
|
|
46
|
-
"foldkit-remote-
|
|
47
|
-
"foldkit-remote-
|
|
42
|
+
"foldkit-cms": "0.7.12",
|
|
43
|
+
"foldkit-guard": "0.4.0",
|
|
44
|
+
"foldkit-entity": "0.11.0",
|
|
45
|
+
"foldkit-remote": "0.19.0",
|
|
46
|
+
"foldkit-remote-drizzle": "0.13.0",
|
|
47
|
+
"foldkit-remote-server": "0.16.0"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"drizzle-orm": "1.0.0-rc.5",
|
|
@@ -57,9 +57,9 @@
|
|
|
57
57
|
"foldkit": "0.169.0",
|
|
58
58
|
"tsdown": "^0.23.1",
|
|
59
59
|
"typescript": "7.0.2",
|
|
60
|
-
"foldkit-
|
|
61
|
-
"foldkit-
|
|
62
|
-
"foldkit-
|
|
60
|
+
"foldkit-form": "0.7.0",
|
|
61
|
+
"foldkit-surface": "0.10.2",
|
|
62
|
+
"foldkit-bundle": "0.8.1"
|
|
63
63
|
},
|
|
64
64
|
"scripts": {
|
|
65
65
|
"build": "tsdown",
|