Examples

Learn how to build your own registry. Start with createRegistry; reach for the mutable BaseRegistry only when registrations arrive at runtime.

import type { StandardSchemaV1 } from '@standard-schema/spec';
import * as z from 'zod/mini';

Creating a Read-Only Registry

Build the registry once from a list. A later registration with the same $type wins, so appending a custom entry overrides a default.

import * as z from 'zod/mini';
import { createRegistry } from '@schema-engine/registry';
import type { BaseRegistration } from '@schema-engine/registry';

interface PluginRegistration extends BaseRegistration {
	execute: () => Promise<void>;
}

const defaults: PluginRegistration[] = [
	{ $type: 'cache', schema: z.object({}), metadata: { name: 'Cache' }, execute: async () => {} },
];
const custom: PluginRegistration = {
	$type: 'cache',
	schema: z.object({ ttl: z.number() }),
	metadata: { name: 'Tuned Cache' },
	execute: async () => {},
};

const plugins = createRegistry('Plugin', [...defaults, custom]);

plugins.get('cache')?.metadata.name; // 'Tuned Cache'

Creating a Plugin Registry (mutable)

Here's how to create a plugin registry that accepts registrations at runtime:

import { BaseRegistry } from '@schema-engine/registry';

interface PluginRegistration extends BaseRegistration {
	$type: string;
	schema: StandardSchemaV1;
	metadata: ComponentMetadata;
	execute: () => Promise<void>;
}

class PluginRegistry extends BaseRegistry<string, PluginRegistration> {
	get registryName(): string {
		return 'Plugin';
	}
}

const registry = new PluginRegistry();

registry.register({
	$type: 'my-plugin',
	schema: z.object({ enabled: z.boolean() }),
	metadata: {
		name: 'My Plugin',
		description: 'Does something useful',
	},
	execute: async () => {
		console.log('Plugin executed!');
	},
});

const plugin = registry.get('my-plugin');
if (plugin) {
	await plugin.execute();
}

This pattern works well for non-visual plugins where registrations expose behavior but no render function.

Creating a Component Registry

For visual components that need a render method:

interface WidgetRegistration extends ComponentRegistration<string, unknown, { id: string }> {
	$type: string;
	schema: StandardSchemaV1;
	metadata: ComponentMetadata;
	render: (props: { id: string }) => JSX.Element;
}

class WidgetRegistry extends BaseRegistry<string, WidgetRegistration> {
	get registryName(): string {
		return 'Widget';
	}
}

const widgets = new WidgetRegistry();

widgets.register({
	$type: 'card',
	schema: z.object({ title: z.string() }),
	metadata: {
		name: 'Card Widget',
		icon: 'card',
	},
	render: ({ id }) => <div id={id}>Card</div>,
});

Validating Registrations

Validate configs through each registration's Standard Schema (schema['~standard'].validate) or the SchemaMetadataCatalog in @schema-engine/forms for JSON Schema metadata.