Skip to main content

Plugin System

The plugins package in @repo/crud provides a powerful extensibility mechanism for CRUD operations. Plugins can intercept and modify the behavior of CRUD operations before and after they are executed.

Features

  • Hooks System: Intercept CRUD operations before and after execution
  • Initialization: Initialize plugins with access to the CRUD engine
  • Modular Design: Easily combine multiple plugins
  • Built-in Plugins: Common plugins for logging, validation, and more
  • Custom Plugins: Create your own plugins for specific needs
  • TypeScript Support: Full TypeScript support with generics

Basic Usage

Creating a Plugin

Using a Plugin

Built-in Plugins

Logging Plugin

The logging plugin logs all CRUD operations:

Validation Plugin

The validation plugin validates data before creating or updating resources:

Caching Plugin

The caching plugin caches the results of CRUD operations:

Transformation Plugin

The transformation plugin transforms data before and after CRUD operations:

Soft Delete Plugin

The soft delete plugin implements soft deletion for resources:

Internationalization Plugin

The internationalization plugin adds support for translating resources:

Creating Custom Plugins

Plugin Interface

Creating a Factory Function

You can create a factory function for your custom plugin:

Combining Plugins

Plugin Order

Plugins are executed in the order they are provided in the plugins array:
For a getList operation, the execution order would be:
  1. loggingPlugin.hooks.beforeGetList
  2. validationPlugin.hooks.beforeGetList
  3. cachingPlugin.hooks.beforeGetList
  4. Actual getList operation
  5. cachingPlugin.hooks.afterGetList
  6. validationPlugin.hooks.afterGetList
  7. loggingPlugin.hooks.afterGetList
Note that the “after” hooks are executed in reverse order, allowing each plugin to process the result in the reverse order of the “before” hooks.

Creating a Plugin Composition

You can create a function to compose multiple plugins:

Advanced Usage

Resource-Specific Plugins

You can create plugins that only apply to specific resources:

Dynamic Plugins

You can create plugins that can be enabled or disabled dynamically:

Plugin with State

You can create plugins that maintain state:

API Reference

For a complete API reference, please refer to the TypeScript definitions in the package.