Plugin Best Practices
Guidelines for plugins that install safely, behave the same on every host and say what they need.
Most of these are checked by the plugin preset of brick lint: a scoped name, a brick range, declared options with descriptions, synchronous setup, no connections outside on("start"), explicit access and a description on every route.
1. Declare the manifest
Give the plugin its npm name (@acme/brick-comments), the Brick versions it supports (brick: "^0.11.0") and the plugins it needs on the same host (requires: ["@brickkit/crud"]). Brick checks all three at install, so a user gets a clear error instead of a broken route.
2. Type the options, describe them
Declare options as a TypeBox schema with defaults and a description on each one. Brick fills the defaults, checks the values at install and names the plugin and option in the error. Do not read options from process.env: use secrets with defineSecret, so the app checks them at boot and the dashboard can list them.
options: t.Object({
maxLength: t.Integer({ default: 2000, minimum: 1, description: "Longest comment." }),
}),3. Keep setup synchronous; connect in on("start")
The resource, service, app and tables parts run while the app is defined, before it serves. They must be synchronous and must not open connections or run queries.
// ❌ Opens a client while the plugin installs
app: () => {
const client = createClient(url);
},
// ✅ Opens it when the app starts, closes it when it stops
app: ({ on }) => {
let client: Client | undefined;
on("start", async ({ secrets }) => {
client = await createClient(secrets.url.require()).connect();
});
on("stop", () => client?.close());
},4. Own your tables with tables
Declare the tables in tables and use the builders it receives. Brick builds them for the host's engine, prefixes their names so two installs do not clash, and adds them to the host's database, where schema sync and migrations find them. Do not ask users to create or pass in your tables.
5. Use the host's transaction
In hooks, write through tx, so your rows commit or roll back with the user's write. In actions, use ctx.db, the host service's database, so tenant routing and context stay intact. Side effects outside the database (email, webhooks) belong in afterCommit or in a listener.
6. Subscribe with hooks; do not ask for wiring
An audit or cascade plugin subscribes to the host's CRUD writes with hooks.after* itself. Users install it with one use() call.
7. Explicit access and validation on routes
Every action a plugin registers with routes.action() needs TypeBox input and output, an explicit method and path, a description, and an access decision: authorize, or a documented access: "public".
8. Avoid property collisions on host resources
What a resource part returns is attached to the host resource. Do not return keys that the resource already has (name, serviceName, table, id, idField, config, use, action, command) or __proto__, prototype and constructor. Brick refuses a collision at install.
9. Structured errors
Reject operations with ActionExecutionError and a standard code and status, so typed clients get the JSON error envelope:
import { ActionExecutionError } from "@brickkit/core";
throw new ActionExecutionError(
"BAD_REQUEST",
"Comment body cannot be blank",
400,
);