import { type Access, type AuthUser, anyone, type ID, isStaff, isSystemAdmin } from './access.js' import type { CollectionConfig, Config } from './config.js' import type { Field } from './fields.js' export const USERS = 'users' export const MEDIA = 'media' export const MEDIA_FOLDERS = 'media-folders' export const SESSIONS = 'sessions' export const LOGIN_ATTEMPTS = 'login-attempts' export const VERSIONS = 'document-versions' export const SCHEDULED_JOBS = 'scheduled-jobs' export const WEBHOOK_DELIVERIES = 'webhook-deliveries' export const EMAIL_DELIVERIES = 'email-deliveries' export const DATABASE_BACKUPS = 'database-backups' export const JOB_RUNS = 'job-runs' export const ROLES = 'user-roles' export const USER_IDENTITIES = 'user-identities' export const AUDIT_LOGS = 'audit-logs' export const OUTBOX = 'outbox' export const REALTIME_EVENTS = 'realtime-events' export const RATE_LIMITS = 'rate-limits' export const BULK_OPERATIONS = 'bulk-operations' export const SAVED_VIEWS = 'saved-views' export const CONNECTIONS = 'connections' export const AI_USAGE = 'ai-usage' export const EMAIL_LOG = 'email-log' export const EMAIL_SUPPRESSIONS = 'email-suppressions' export const ADMIN_SETTINGS = 'admin-settings' /** Collections Easy CMS uses internally. Not exposed over REST or in the admin UI. */ export const INTERNAL_COLLECTIONS: ReadonlySet = new Set([ SESSIONS, LOGIN_ATTEMPTS, VERSIONS, SCHEDULED_JOBS, WEBHOOK_DELIVERIES, EMAIL_DELIVERIES, DATABASE_BACKUPS, JOB_RUNS, ROLES, USER_IDENTITIES, AUDIT_LOGS, OUTBOX, REALTIME_EVENTS, RATE_LIMITS, BULK_OPERATIONS, SAVED_VIEWS, CONNECTIONS, AI_USAGE, EMAIL_LOG, EMAIL_SUPPRESSIONS, ADMIN_SETTINGS, ]) export const DEFAULT_ROLES = ['admin', 'editor'] as const /** * Who created a document, set by Easy CMS (`auth.rbac`): for roles given "own documents only". * Added to every collection but users; trusted Local API calls may set it, e.g. in imports. */ export const CREATED_BY_FIELD: Field = { name: 'createdBy', type: 'relationship', to: USERS, admin: { position: 'sidebar' }, access: { update: () => false }, label: { en: 'Created by', th: 'สร้างโดย' }, } /** Adds `createdBy` to every collection but users (a collection with its own is reported). */ export function withCreatedBy(config: Config): Config { if (config.auth?.rbac !== true) return config return { ...config, collections: (config.collections ?? []).map((c) => c.slug === USERS || c.fields?.some((f) => f.name === CREATED_BY_FIELD.name) ? c : { ...c, fields: [...c.fields, CREATED_BY_FIELD] }, ), } } /** Admins can update anyone; other users only themselves. */ const adminOrSelf: Access = ({ user }) => { if (!user) return false if (isSystemAdmin(user)) return true return { id: { equals: user.id } } } const adminOnly = { update: ({ user }: { user: AuthUser | null }) => isSystemAdmin(user), } /** Staff read users; members (`auth.members`) only themselves. */ const staffOrSelf: Access = ({ user }) => { if (!user) return false if (user.member !== true) return true return { id: { equals: user.id } } } function userFields(roles: readonly string[], rbac: boolean, members: boolean): Field[] { const defaultRole = roles.includes('editor') ? 'editor' : (roles[roles.length - 1] as string) return [ { name: 'email', type: 'email', required: true, unique: true, label: { en: 'Email', th: 'อีเมล' }, }, { name: 'name', type: 'text', label: { en: 'Name', th: 'ชื่อ' } }, // With roles from the admin (`auth.rbac`), any role in Settings → Roles: checked on save. rbac ? { name: 'role', type: 'text', required: true, defaultValue: defaultRole, access: adminOnly, label: { en: 'Role', th: 'บทบาท' }, } : { name: 'role', type: 'select', options: roles, required: true, defaultValue: defaultRole, access: adminOnly, label: { en: 'Role', th: 'บทบาท' }, }, { name: 'active', type: 'boolean', defaultValue: true, access: adminOnly, label: { en: 'Active', th: 'ใช้งาน' }, }, // Accounts made by signing up (`auth.members.signUp`) are `false` until the email is confirmed. ...(members ? [ { name: 'emailVerified', type: 'boolean', access: adminOnly, admin: { position: 'sidebar' }, label: { en: 'Email confirmed', th: 'ยืนยันอีเมลแล้ว' }, } satisfies Field, ] : []), { name: 'passwordHash', type: 'text', hidden: true }, ] } /** * Adds the built-in users collection, merging a user-supplied `users` * collection into it (extra fields appended, access and hooks overridden). */ export function withUsers(config: Config): Config { const roles = config.auth?.roles ?? DEFAULT_ROLES const custom = config.collections?.find((c) => c.slug === USERS) const users: CollectionConfig = { slug: USERS, labels: { singular: { en: 'User', th: 'ผู้ใช้' }, plural: { en: 'Users', th: 'ผู้ใช้' } }, useAsTitle: 'email', ...custom, admin: { icon: 'users', ...custom?.admin }, fields: [ ...userFields(roles, config.auth?.rbac === true, config.auth?.members?.signUp !== undefined), ...(custom?.fields ?? []), ], access: { read: staffOrSelf, create: ({ user }) => isSystemAdmin(user), update: adminOrSelf, delete: ({ user }) => isSystemAdmin(user), ...custom?.access, }, } const others = (config.collections ?? []).filter((c) => c.slug !== USERS) return { ...config, collections: [users, ...others] } } /** File metadata is set by uploads, never edited directly. */ const systemField = { update: () => false } function mediaFields(): Field[] { return [ { name: 'filename', type: 'text', required: true, unique: true, access: systemField, label: { en: 'File name', th: 'ชื่อไฟล์' }, }, { name: 'originalName', type: 'text', access: systemField, label: { en: 'Original name', th: 'ชื่อเดิม' }, }, { name: 'mimeType', type: 'text', required: true, access: systemField, label: { en: 'Type', th: 'ชนิด' }, }, { name: 'filesize', type: 'number', required: true, access: systemField, label: { en: 'Size (bytes)', th: 'ขนาด (ไบต์)' }, }, { name: 'width', type: 'number', access: systemField, label: { en: 'Width', th: 'กว้าง' } }, { name: 'height', type: 'number', access: systemField, label: { en: 'Height', th: 'สูง' } }, { name: 'sizes', type: 'json', access: systemField, label: { en: 'Resized copies', th: 'ขนาดย่อ' }, }, { name: 'alt', type: 'text', label: { en: 'Alternative text', th: 'ข้อความแทนรูป' } }, ] } /** Set by Easy CMS from the file's folder (`upload.folders`): only served to who may see it. */ const privateField: Field = { name: 'private', type: 'boolean', index: true, access: systemField, admin: { position: 'sidebar' }, label: { en: 'Private', th: 'ส่วนตัว' }, } interface StoredSize { filename: string width?: number height?: number } /** Only admins (signed in, not API keys) see and set a folder's permissions. */ const adminField = { read: ({ user }: { user: { role: string; apiKey?: unknown } | null }) => user?.role === 'admin' && !user.apiKey, update: ({ user }: { user: { role: string; apiKey?: unknown } | null }) => user?.role === 'admin' && !user.apiKey, } /** * Folders of the media library (`upload.folders`). Roles' grants on `media` apply to them too; * with `auth.rbac`, a folder's `permissions` narrow them for its files and subfolders. */ export const mediaFoldersCollection: CollectionConfig = { slug: MEDIA_FOLDERS, labels: { singular: { en: 'Folder', th: 'โฟลเดอร์' }, plural: { en: 'Folders', th: 'โฟลเดอร์' }, }, useAsTitle: 'name', admin: { icon: 'folder', list: { tree: 'parent', sort: 'name' } }, access: { read: isStaff, create: isStaff, update: isStaff, delete: isStaff }, hooks: { // What the reader may do here (`view`, `edit`, `manage`), for the admin's buttons. afterRead: [ async ({ doc, user, cms }) => ({ ...doc, level: await cms.folders.level(user, doc.id as ID), }), ], }, fields: [ { name: 'name', type: 'text', required: true, maxLength: 120, label: { en: 'Name', th: 'ชื่อ' }, }, { name: 'parent', type: 'relationship', to: MEDIA_FOLDERS, index: true, label: { en: 'In folder', th: 'อยู่ในโฟลเดอร์' }, }, // For code to find the folder (`folder: 'banners'` on upload fields); set when it is made. { name: 'key', type: 'text', unique: true, maxLength: 60, access: { update: ({ id }) => id === undefined }, label: { en: 'Key', th: 'คีย์' }, }, // Its files and subfolders are private: only for users who may see them, and signed links. { name: 'private', type: 'boolean', access: { update: adminField.update }, label: { en: 'Private', th: 'ส่วนตัว' }, }, // `null`: as the parent folder. Otherwise each role's level (`view`, `edit`, `manage`); // roles not listed get nothing. { name: 'permissions', type: 'json', access: adminField, label: { en: 'Permissions', th: 'สิทธิ์เข้าถึง' }, }, ], } const folderField: Field = { name: 'folder', type: 'relationship', to: MEDIA_FOLDERS, index: true, admin: { position: 'sidebar' }, label: { en: 'Folder', th: 'โฟลเดอร์' }, } /** * Adds the built-in media collection, merging a user-supplied `media` * collection into it like `withUsers`. Files are public; metadata is readable by anyone * so frontends can populate uploads, and writable by logged-in users. With `upload.folders`, * also the folders and each file's `folder`. */ export function withMedia(config: Config): Config { const custom = config.collections?.find((c) => c.slug === MEDIA) const folders = config.upload?.folders === true const media: CollectionConfig = { slug: MEDIA, labels: { singular: { en: 'Media', th: 'สื่อ' }, plural: { en: 'Media', th: 'คลังสื่อ' } }, useAsTitle: 'filename', ...custom, admin: { icon: 'image', ...custom?.admin }, fields: [ ...mediaFields(), ...(folders ? [folderField, privateField] : []), ...(custom?.fields ?? []), ], access: { read: anyone, create: isStaff, update: isStaff, delete: isStaff, ...custom?.access, }, hooks: { ...custom?.hooks, afterRead: [ ({ doc, cms }) => { const sizes = (doc.sizes ?? {}) as Record return { ...doc, url: typeof doc.filename === 'string' ? cms.mediaURL(doc.filename) : null, sizes: Object.fromEntries( Object.entries(sizes).map(([name, size]) => [ name, { ...size, url: cms.mediaURL(size.filename) }, ]), ), } }, ...(custom?.hooks?.afterRead ?? []), ], afterDelete: [ async ({ doc, cms }) => { const sizes = Object.values((doc.sizes ?? {}) as Record) const storage = cms.storageFor(doc.private === true) for (const key of [doc.filename, ...sizes.map((s) => s.filename)]) { if (typeof key === 'string') await storage.delete(key) } }, ...(custom?.hooks?.afterDelete ?? []), ], }, } // A `media-folders` collection in the config (e.g. from a plugin) adds fields, access and hooks. const customFolders = config.collections?.find((c) => c.slug === MEDIA_FOLDERS) const mediaFolders: CollectionConfig = customFolders ? { ...mediaFoldersCollection, // A field named like a built-in one changes it (e.g. `key` unique per tenant). fields: [ ...mediaFoldersCollection.fields.map((field) => { const own = customFolders.fields.find((f) => f.name === field.name) return own ? ({ ...field, ...own } as Field) : field }), ...customFolders.fields.filter( (f) => !mediaFoldersCollection.fields.some((b) => b.name === f.name), ), ], access: { ...mediaFoldersCollection.access, ...customFolders.access }, hooks: { ...customFolders.hooks, afterRead: [ ...(mediaFoldersCollection.hooks?.afterRead ?? []), ...(customFolders.hooks?.afterRead ?? []), ], }, } : mediaFoldersCollection const others = (config.collections ?? []).filter( (c) => c.slug !== MEDIA && c.slug !== MEDIA_FOLDERS, ) return { ...config, collections: [ ...others.slice(0, 1), media, ...(folders ? [mediaFolders] : []), ...others.slice(1), ], } } const nobody: Access = () => false /** Collections that back sessions and login rate limiting. */ export const internalCollections: readonly CollectionConfig[] = [ { slug: SESSIONS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ { name: 'tokenHash', type: 'text', required: true, unique: true }, { name: 'user', type: 'relationship', to: USERS, required: true }, { name: 'expiresAt', type: 'date', required: true, index: true }, // How it started: `password`, a sign-in provider's id, or empty (before 1.43, other ways). { name: 'method', type: 'text', index: true }, ], }, { slug: LOGIN_ATTEMPTS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [{ name: 'key', type: 'text', required: true, index: true }], }, ] /** * Snapshots for collections and globals with `versions`. Only added to the schema when one of * them uses versions, so projects without versions keep their tables unchanged. */ export const versionsCollection: CollectionConfig = { slug: VERSIONS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ // Collection slug, or `global:`. { name: 'parent', type: 'text', required: true, index: true }, // Document id; 0 for globals. { name: 'doc', type: 'number', required: true, index: true }, { name: 'status', type: 'text' }, // The newest version of the document: its current editable state. { name: 'latest', type: 'boolean', index: true }, { name: 'author', type: 'number' }, { name: 'snapshot', type: 'json', required: true }, ], } /** Publish/unpublish jobs for collections and globals with `schedule`; added only when used. */ export const scheduledJobsCollection: CollectionConfig = { slug: SCHEDULED_JOBS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ // Collection slug, or `global:`. { name: 'parent', type: 'text', required: true, index: true }, // Document id; 0 for globals. { name: 'doc', type: 'number', required: true, index: true }, { name: 'action', type: 'text', required: true }, { name: 'runAt', type: 'text', required: true, index: true }, // pending | done | failed { name: 'state', type: 'text', required: true, index: true }, { name: 'error', type: 'text' }, { name: 'author', type: 'number' }, ], } /** * Bulk actions on every document matching a list's filters (#280), run in steps of 100 by * whichever process gets there first: the one that started it, the admin asking for progress, or * the jobs. */ export const bulkOperationsCollection: CollectionConfig = { slug: BULK_OPERATIONS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ { name: 'collection', type: 'text', required: true }, // publish | unpublish | delete | move { name: 'action', type: 'text', required: true }, // The list's `where`, and for `move` the folder (`null`: the top). { name: 'query', type: 'json' }, { name: 'folder', type: 'text' }, { name: 'transferTo', type: 'text' }, { name: 'user', type: 'text', required: true, index: true }, // pending | running | cancelling | done | cancelled | failed { name: 'state', type: 'text', required: true, index: true }, { name: 'total', type: 'number', required: true }, { name: 'done', type: 'number', required: true }, { name: 'skipped', type: 'number', required: true }, { name: 'failed', type: 'number', required: true }, // The last id handled: the next step continues after it, in id order. { name: 'after', type: 'json' }, // The first documents skipped or failed, with why: `{ id, reason, message? }`. { name: 'problems', type: 'json' }, { name: 'error', type: 'text' }, // A step holds the operation until then; a process that stopped lets it go. { name: 'leaseUntil', type: 'text' }, { name: 'finishedAt', type: 'text' }, ], } /** * Lists saved by admin users (#87): a list's filters, search, sort, columns, page size and row * height, for its user or shared with everyone who may read the collection. A row of kind * `default` holds the view a user opens the collection with. */ export const savedViewsCollection: CollectionConfig = { slug: SAVED_VIEWS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ { name: 'collection', type: 'text', required: true, index: true }, { name: 'user', type: 'text', required: true, index: true }, // view | default { name: 'kind', type: 'text', required: true }, { name: 'name', type: 'text' }, { name: 'shared', type: 'boolean' }, // `{ query, columns?, pageSize?, density? }`: the list's URL query, and what the browser kept. { name: 'state', type: 'json' }, // For `default`: a view's id, or `config:` for one of `admin.views`. { name: 'target', type: 'text' }, ], } /** * Accounts connected to plugins' integrations (#311): one row per connection (its plain values, * its secrets encrypted), one per integration turned off, and OAuth choices waiting (`pending`). */ export const connectionsCollection: CollectionConfig = { slug: CONNECTIONS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ // connection | integration | pending { name: 'kind', type: 'text', required: true }, { name: 'integration', type: 'text', required: true, index: true }, { name: 'name', type: 'text' }, { name: 'isDefault', type: 'boolean' }, { name: 'values', type: 'json' }, // AES-256-GCM, with a key derived from the secret (see integrations.ts). { name: 'secret', type: 'text' }, { name: 'account', type: 'text' }, { name: 'lastUsedAt', type: 'text' }, { name: 'lastError', type: 'text' }, { name: 'enabled', type: 'boolean' }, ], } /** * One row per AI request (#357): who, which feature and connection, the tokens. Never the prompt or * the answer. Added with `ai`; kept 90 days. */ export const aiUsageCollection: CollectionConfig = { slug: AI_USAGE, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ { name: 'user', type: 'text', index: true }, { name: 'feature', type: 'text', required: true }, { name: 'integration', type: 'text', required: true }, { name: 'connection', type: 'text' }, { name: 'input', type: 'number' }, { name: 'output', type: 'number' }, { name: 'ok', type: 'boolean' }, { name: 'error', type: 'text' }, // The document it was for, e.g. the image of alt text. { name: 'about', type: 'text' }, // Calls to the provider: 2 when a 429 or 5xx was tried again (providers may bill both). { name: 'attempts', type: 'number' }, // Bytes of the images sent, after picking the smallest size at least 1024 pixels wide. { name: 'imageBytes', type: 'number' }, // A hash of the feature, the document and the prompt, never the prompt: the same request // again shows a field generated over and over. { name: 'promptHash', type: 'text' }, ], } /** Webhook deliveries until they succeed; added only when `webhooks` is set. */ export const webhookDeliveriesCollection: CollectionConfig = { slug: WEBHOOK_DELIVERIES, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ { name: 'url', type: 'text', required: true }, { name: 'event', type: 'text', required: true }, // The JSON body as first sent: retries are byte-for-byte the same, signature included. { name: 'body', type: 'textarea', required: true }, { name: 'delivery', type: 'text', required: true }, { name: 'attempts', type: 'number', required: true }, { name: 'nextAttemptAt', type: 'text', required: true, index: true }, // pending | failed { name: 'state', type: 'text', required: true, index: true }, { name: 'error', type: 'text' }, ], } /** Backups of the database (Settings → Backups): the record of each, the files are in storage. */ export const databaseBackupsCollection: CollectionConfig = { slug: DATABASE_BACKUPS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ // pending | running | done | failed { name: 'state', type: 'text', required: true, index: true }, // manual | scheduled { name: 'trigger', type: 'text', required: true }, { name: 'filename', type: 'text' }, { name: 'size', type: 'number' }, { name: 'startedAt', type: 'text', index: true }, { name: 'finishedAt', type: 'text' }, { name: 'error', type: 'text' }, // Who started it (email), for manual ones. { name: 'author', type: 'text' }, { name: 'downloadedBy', type: 'text' }, { name: 'downloadedAt', type: 'text' }, // Where it is (#395): `{ server | :: { state, … } }`. { name: 'copies', type: 'json' }, ], } /** * Settings admins change in the admin rather than the code, one row per key (e.g. `backups`), * the value as JSON. What the config sets wins over them. */ export const adminSettingsCollection: CollectionConfig = { slug: ADMIN_SETTINGS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ { name: 'key', type: 'text', required: true, unique: true }, { name: 'value', type: 'json' }, ], } /** * Every email sent (Settings → Email): who, what kind and how it went, never its content (links * in it sign people in). Kept `emailLog.keepDays` (30). */ export const emailLogCollection: CollectionConfig = { slug: EMAIL_LOG, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ { name: 'to', type: 'text', required: true }, { name: 'cc', type: 'text' }, { name: 'bcc', type: 'number' }, { name: 'subject', type: 'text' }, { name: 'tag', type: 'text', index: true }, // sent | pending | failed { name: 'state', type: 'text', required: true, index: true }, { name: 'attempts', type: 'number' }, { name: 'error', type: 'text' }, { name: 'adapter', type: 'text' }, { name: 'sentAt', type: 'text' }, // The provider's id for it: its webhook's delivery events name the email by it. { name: 'providerId', type: 'text', index: true }, // delivered | bounced | complained { name: 'delivery', type: 'text', index: true }, { name: 'deliveryAt', type: 'text' }, ], } /** * Addresses that get no email (Settings → Email): they bounced for good or reported spam, as the * provider's webhook said, or an admin added them. Kept until an admin removes them. */ export const emailSuppressionsCollection: CollectionConfig = { slug: EMAIL_SUPPRESSIONS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ // Lowercased. { name: 'address', type: 'text', required: true, unique: true }, // bounced | complained | manual { name: 'reason', type: 'text', required: true }, { name: 'detail', type: 'text' }, // The integration that reported it, or the admin who added it. { name: 'source', type: 'text' }, ], } /** Outside accounts users sign in with: added with `auth.providers` or sign-in integrations. */ export const userIdentitiesCollection: CollectionConfig = { slug: USER_IDENTITIES, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ { name: 'user', type: 'relationship', to: USERS, required: true, index: true }, // The provider's id (`google`) and its id for the account (`sub`). { name: 'provider', type: 'text', required: true, index: true }, { name: 'subject', type: 'text', required: true, index: true }, { name: 'email', type: 'text' }, { name: 'lastUsedAt', type: 'text' }, ], } /** The audit log (`audit`): append-only, each row signed. Added only with `audit`. */ export const auditLogsCollection: CollectionConfig = { slug: AUDIT_LOGS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ { name: 'action', type: 'text', required: true, index: true }, // A collection slug, `global:`, or an area (auth, roles, backups…). { name: 'target', type: 'text', index: true }, { name: 'doc', type: 'text', index: true }, { name: 'title', type: 'text' }, { name: 'actorId', type: 'text' }, { name: 'actorEmail', type: 'text', index: true }, // user | api-key | system | scheduler { name: 'via', type: 'text' }, { name: 'ip', type: 'text' }, { name: 'userAgent', type: 'text' }, { name: 'changes', type: 'json' }, { name: 'detail', type: 'json' }, { name: 'signature', type: 'text' }, // The part of the site, e.g. the tenant (`audit.scope`). { name: 'scope', type: 'text', index: true }, ], } /** Emails until they are sent; added only when `email` is set. */ /** * When each of the config's `jobs` last ran, one row per job; added only when `jobs` is set. An * instance claims a run by moving `lastRunAt` from the value it read (compare and set). */ export const jobRunsCollection: CollectionConfig = { slug: JOB_RUNS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ { name: 'name', type: 'text', required: true, unique: true }, { name: 'lastRunAt', type: 'text', required: true }, // running | ok | failed; empty until it first runs here. { name: 'lastStatus', type: 'text' }, { name: 'lastError', type: 'text' }, { name: 'lastDurationMs', type: 'number' }, // Set from Settings → Jobs by an admin: paused, or a schedule in place of the code's (only // for jobs with `adminSchedule`). Empty: as the code says. { name: 'pausedAt', type: 'text' }, { name: 'pausedBy', type: 'text' }, { name: 'everyOverride', type: 'number' }, { name: 'atOverride', type: 'text' }, { name: 'changedBy', type: 'text' }, { name: 'changedAt', type: 'text' }, ], } /** * After hooks, webhooks and purges of saves, until they ran; added only with `outbox: true`. Each * row is written in the transaction of its save. */ export const outboxCollection: CollectionConfig = { slug: OUTBOX, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ // create | update | draft | delete, as the webhook event. { name: 'event', type: 'text', required: true }, // The collection's or the global's slug. { name: 'target', type: 'text', required: true }, { name: 'global', type: 'boolean' }, { name: 'docId', type: 'text' }, // The stored status before the save, for publish and unpublish events. { name: 'before', type: 'text' }, // JSON: the document as saved (as deleted), the one before, and the user who saved. { name: 'doc', type: 'textarea', required: true }, { name: 'previousDoc', type: 'textarea' }, { name: 'user', type: 'textarea' }, // Steps already done (hooks, webhooks, purge), so a retry runs only the rest. { name: 'done', type: 'text' }, { name: 'attempts', type: 'number', required: true }, { name: 'nextAttemptAt', type: 'text', required: true, index: true }, // pending | running | failed { name: 'state', type: 'text', required: true, index: true }, { name: 'error', type: 'text' }, ], } /** * What changed in collections with `realtime: true`, for subscribers (ADR-0070); added only when * one has it. The id orders the events and is the stream's `Last-Event-ID`. Each row is written * in the transaction of its save, and kept `realtime.retentionHours` for clients that reconnect. */ export const realtimeEventsCollection: CollectionConfig = { slug: REALTIME_EVENTS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ { name: 'collection', type: 'text', required: true }, { name: 'docId', type: 'text', required: true }, // create | update | delete | publish | unpublish { name: 'event', type: 'text', required: true }, // Only the pending draft changed (drafts kept as versions): for those who read drafts. { name: 'draft', type: 'boolean' }, // Published before or after the change (always, without drafts): who reads only published // documents may have seen it, or may see it now. { name: 'published', type: 'boolean' }, // JSON: the stored document before an update or delete, never sent: who could see it then // gets `leave` or `delete`, nobody else. { name: 'before', type: 'textarea' }, ], } /** * Requests counted per client and window, across instances; added only with `rateLimit`. Each * instance adds what it counted every few seconds (see RateLimiter). */ export const rateLimitsCollection: CollectionConfig = { slug: RATE_LIMITS, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ // `::` { name: 'key', type: 'text', required: true, unique: true }, { name: 'count', type: 'number', required: true }, // When the window is over, for clearing old rows. { name: 'expiresAt', type: 'text', required: true, index: true }, ], } export const emailDeliveriesCollection: CollectionConfig = { slug: EMAIL_DELIVERIES, access: { read: nobody, create: nobody, update: nobody, delete: nobody }, fields: [ // The message as JSON. { name: 'message', type: 'textarea', required: true }, { name: 'attempts', type: 'number', required: true }, { name: 'nextAttemptAt', type: 'text', required: true, index: true }, // pending | failed { name: 'state', type: 'text', required: true, index: true }, { name: 'error', type: 'text' }, ], }