Skip to content
For developers

The plugin API reference

On this page

Every option of defineCmsPlugin and every hook, with its type. Look things up here.

Plugins is the guide: how to write a plugin, with examples.

All types on this page are exported from tinyzoo. They are part of the public API, so they follow semver.

Functions

ExportTypeWhat it does
defineCmsPlugin(def)(def: CmsPluginDef) => CmsPluginChecks a plugin and returns it. A mistake throws a message that starts with Plugin "<name>":
pluginFile(base, relative)(base: string | URL, relative: string) => stringThe absolute path of a file next to the plugin's module. Pass import.meta.url as base

A site registers plugins in cms.config.ts with export const plugins = [eventsPlugin()]. A plugin exported on its own is found too. The list is read when the site is built.

Options

CmsPluginDef, the object defineCmsPlugin takes. Only name and version are required.

OptionTypeWhat it does
namestringLowercase letters, digits and dashes (events, my-shop). Everything the plugin adds lives under this name
versionstringThe plugin's own version, as 1.2.3. Its upgrade steps are recorded with it
entitiesunknown[]Collections made with defineEntity (any Entity fits). Each is named <name> or <name>_…
singletonsunknown[]Globals made with defineSingleton, named the same way
tablesPgTable[]Drizzle tables that are not entries, see Tables
tablesSourcestringThe same tables as source, for the site's schema.ts, see Tables
serverHandlersCmsPluginHandler[]Server routes, see Server routes
adminPagesCmsPluginAdminPage[]Pages in the admin, see Admin pages
navItemsCmsPluginNavItem[]Entries in the admin's sidebar, see Sidebar entries
dashboardWidgetsCmsDashboardWidget[]Cards on the admin's home page, see Home widgets
entityPanelsCmsEntityPanel[]Panels in the entry editor, see Entry panels
fieldsRecord<string, CmsFieldRenderer>Components for a field type, see Field editors
componentsstringAn absolute folder. Every .vue file in it becomes a global component, named after its file. fields, dashboardWidgets and entityPanels need it
composablesstringAn absolute folder. Every export of its files is auto-imported in the website and the admin, like the site's own composables/
i18nRecord<string, Record<string, unknown>>The plugin's admin texts per language, see Texts
hooksCmsPluginHooksCode tinyzoo calls at set moments, see Hooks
graph(entity: CmsEntity) => CmsGraphExtension | voidChanges to a collection's list in the admin, see List notes
serverPluginsstring[]Absolute paths of Nitro plugin files (export default defineNitroPlugin(…)). They start with the server
appPluginsCmsPluginAppPlugin[]Nuxt app plugins for the website and the admin, see App plugins
upgradesCmsPluginUpgradeStep[]Data work a migration cannot do, see Upgrade steps

Names a plugin cannot take: cms, core, tinyzoo, admin, api, content, media, plugins, p, auth and system.

In entity names, dashes become underscores. The plugin my-shop names its entities my_shop or my_shop_….

Tables

tables and tablesSource go together: give both or neither.

OptionTypeWhat it is
tablesPgTable[]Tables made with pgTable from drizzle-orm/pg-core. Each is named <name> or <name>_… (dashes as underscores), in the default schema
tablesSourcestringThe same tables as source. Only export const <name in camelCase> = pgTable('<name>', …) statements, in plain JavaScript. drizzle-orm/pg-core's builders and sql are imported for it

tinyzoo generate pastes tablesSource into the site's server/db/schema.ts, so drizzle-kit makes the tables' migrations. Before that it runs the source and compares every table with the one in tables: columns, primary keys, foreign keys, unique constraints, indexes and checks. A difference stops it.

On the server the tables are in the registry: useCms().reg.pluginTables is a Map from the SQL name to the table. A table's name cannot be one an entity's table already has.

Server routes

CmsPluginHandler, one item of serverHandlers. Give it a route, a contentPath, or both.

FieldTypeDefaultWhat it does
handlerstringrequiredThe absolute path of a Nitro handler file (export default defineEventHandler(…))
methodCmsHttpMethod'get''get', 'post', 'put', 'patch' or 'delete'
routestringThe address under /api/cms/_p/<name>. It starts with / and does not end with one. :param matches one segment
accessCmsRouteAccess'user'Who may call route: 'user' (anyone signed in), 'admin' or 'public'. Checked before the handler runs
contentPathstringAlso serves the handler at /api/content/<contentPath>, open to anyone. The first segment is exactly _<plugin name>, dashes kept, as in _events/next or _my-shop/feed. The forms plugin's _form is the one exception. A plugin named global, search, sitemap, tree or form serves no content routes

An address under /api/cms/_p/<name>/ that no route declares gets the strictest access the plugin uses.

Handlers can use these server helpers without an import:

HelperFrozenWhat it does
useCms()yesReturns at least { db, reg }: the site's drizzle database and the CmsRegistry
requireCmsUser(event)yesReturns the signed-in person (CmsUser: id, role, email?, name?, locale?). Throws 401 when nobody is signed in, 403 role-removed when their role is gone from the config
audit, parseOr422, rateLimit, clientIp, sendCmsMailnoThe server's own. They can change in a minor release, so test a plugin that uses them with each new minor version of tinyzoo

db and the columns of an entity's table are drizzle's. Their types follow drizzle-orm (the public API). The driver under db is not promised: use drizzle's API, not db.$client.

Admin pages

CmsPluginAdminPage, one item of adminPages.

FieldTypeWhat it does
pathstringThe address under /admin/p/<name>/. '' is the plugin's first page. :id matches one segment
filestringThe absolute path of the page's Vue file. Start it with definePageMeta({ layout: 'admin' })

CmsPluginNavItem, one item of navItems. Give it a page or a to.

FieldTypeDefaultWhat it does
labelLocalizedText (string | Record<string, string>)requiredThe text, or one text per language ({ en: 'Events', cs: 'Akce' })
iconstringAn Iconify name, such as i-lucide-calendar
pagestringOne of the plugin's own adminPages, by its path
tostringAny address in the admin, starting with /admin
group'content' | 'settings''content'Where in the sidebar the entry goes
adminOnlybooleanfalseShows the entry to administrators only

Home widgets

CmsDashboardWidget, one item of dashboardWidgets.

FieldTypeDefaultWhat it does
componentstringrequiredA component of the plugin's components folder, by its file name (StatsVisitors)
ordernumber0Sorts the widgets of all plugins, smallest first. Equal orders keep the order the plugins are registered in
adminOnlybooleanfalseShows the widget to administrators only

Widgets sit in the home's main column, before "Recent". The component draws its own card and heading. When it renders nothing, the home leaves no gap. Two plugins cannot use one component name for a widget or a panel.

Entry panels

CmsEntityPanel, one item of entityPanels.

FieldTypeDefaultWhat it does
componentstringrequiredA component of the plugin's components folder
labelLocalizedTextrequiredThe heading above the panel
entitiesstring[]every collectionThe collections it is shown for. A name that is not a collection of the site stops the build

The panel sits in the side column of the entry editor, under the details. Its component gets the props entity (string), id (string) and locale (string | null, the language being edited). It is outside the form, and it is not shown for an entry that has not been saved yet. A panel that renders nothing is left out, heading and all.

App plugins

CmsPluginAppPlugin, one item of appPlugins.

FieldTypeDefaultWhat it does
srcstringrequiredThe absolute path of a Nuxt plugin file (export default defineNuxtPlugin(…))
mode'client' | 'server' | 'all''all'Where it runs: in the browser, while rendering, or both

They are added after the site's and tinyzoo's own plugins. The website and the admin are one app, so they run on both.

Field editors

CmsFieldRenderer, the value for one field type in fields. Give it at least one of the two.

FieldTypeWhat it does
editstringThe name of a global component that edits the field in the form
cellstringThe name of a global component that shows the value in lists

The site's own cms.fields in app.config.ts wins over a plugin. Two plugins cannot both register one field type.

Texts

i18n holds the plugin's admin texts, one object per language: { en: { title: 'Events' }, cs: { title: 'Akce' } }.

  • en is required. Every other language falls back to it.
  • Another language may leave keys out. It cannot have a key that en does not have.
  • A value is a string, an object of strings, or a plural ({ one, few, other }).
  • The admin reads them as t('plugins.<name>.<key>').

Hooks

CmsPluginHooks, the object in hooks. There is one hook.

HookTypeWhen it runs
retention(ctx: CmsRetentionContext) => Record<string, number> | void (or a promise of it)Once a day, in the cms:retention task. Delete what the plugin must not keep, and return the counts ({ drafts: 12 }). The counts go into the audit log, never the data

CmsRetentionContext, what the hook receives:

FieldTypeWhat it is
txany (a drizzle transaction)The task's transaction. It holds the task's lock, so one instance runs at a time
regCmsRegistryThe site's entities: entities, byName, locales, plugins, pluginOf and pluginTables
batchnumberHow many rows to delete in one statement

List notes

graph(entity) is called once for every entity of the site. It returns nothing, or a CmsGraphExtension:

FieldTypeWhat it does
createbooleanfalse hides the "New …" button of that collection
notices{ icon?: string; key: `plugins.${string}`; params?: Record<string, string | number> }[]Short lines under the collection's list. key is one of the plugin's texts, written in full: plugins.<name>.<key>

entity is a CmsEntity: the entity with its field types erased. It has its name, fields, content, publish and so on.

Upgrade steps

CmsPluginUpgradeStep, one item of upgrades. It is the same as one of tinyzoo's own steps (upgrading), with since optional.

FieldTypeDefaultWhat it does
idstringrequiredKebab-case. Stored as <name>:<id> in cms_upgrades. Never change it once released
descriptionstringrequiredOne plain line: what the step does and why
phase'before' | 'after'requiredRuns before or after the site's pending migrations
run(ctx: UpgradeContext) => void | Promise<void>requiredThe work. It must be idempotent: running it twice gives the same result as once
when(ctx: UpgradeContext) => boolean | Promise<boolean>Whether the step matters in this run. A step with when runs every time it is true
freshInstall'skip' | 'run''skip'On an empty database a step is recorded without running, unless this is 'run'
sincestringthe plugin's versionThe version that brought the step

UpgradeContext, what run and when receive:

FieldTypeWhat it is
runSqlRunnerRuns SQL in the migration's transaction, under its lock
pendingStatementsstring[]Every statement of the site's migrations this run applies
freshInstallbooleantrue when no migration had been applied before: an empty database

Where things live

WhatWhere
Collections and tables<name> or <name>_…
Server routes/api/cms/_p/<name>/…
Public routes/api/content/_<segment>/…
Admin pages/admin/p/<name>/…
Textsplugins.<name>.…
Upgrade steps<name>:<id>

Didn’t find what you need?

Write to us. We’re happy to help, and we’ll add it to the guide.

Get in touch

This page’s source on GitHub