Skip to main content

CRUD Engine

The CRUD Engine is the core component of the @repo/crud package, providing enhanced CRUD operations with support for plugins, permissions, and auditing. It wraps a data provider from the @repo/data package and adds additional functionality.

Features

  • Enhanced CRUD Operations: Extends the basic CRUD operations with additional features
  • Plugin System: Support for plugins that can modify behavior before and after operations
  • Permissions Integration: Built-in support for access control
  • Audit Logging: Automatic tracking of data changes
  • Internationalization: Support for multiple languages
  • Error Handling: Consistent error handling across operations

Basic Usage

Creating a CRUD Engine

Using the CRUD Engine

Configuration Options

The CRUD Engine accepts the following configuration options:

Plugin System

The CRUD Engine includes a powerful plugin system that allows you to extend its functionality. Plugins can modify the behavior of CRUD operations by intercepting them before and after they are executed.

Plugin Interface

Creating a Plugin

Plugin Execution Order

Plugins are executed in the order they are provided in the plugins array. For example, if you have three plugins:
The execution order for a getList operation would be:
  1. pluginA.hooks.beforeGetList
  2. pluginB.hooks.beforeGetList
  3. pluginC.hooks.beforeGetList
  4. Actual getList operation
  5. pluginC.hooks.afterGetList
  6. pluginB.hooks.afterGetList
  7. pluginA.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.

Permissions Integration

The CRUD Engine includes built-in support for permissions checking. When enabled, it will check if the current user has permission to perform the requested operation.

Enabling Permissions

Permissions Context

When using permissions, you need to set the current user in the permissions context:

Audit Logging

The CRUD Engine includes built-in support for audit logging. When enabled, it will log all changes to data.

Enabling Audit Logging

Audit Context

When using audit logging, you need to set the current user in the audit context:

Internationalization

The CRUD Engine includes built-in support for internationalization. You can specify the default locale and supported locales when creating the engine.

Configuring Internationalization

Using Translations

The CRUD Engine uses the next-intl package for translations. You can use the useTranslation hook to get translations:

Error Handling

The CRUD Engine includes built-in error handling. You can provide a custom error handler when creating the engine.

Custom Error Handler

Advanced Usage

Creating a CRUD Engine with a Provider Type

You can create a CRUD Engine with a specific provider type:

Extending the CRUD Engine

You can extend the CRUD Engine with custom methods:

API Reference

CrudEngine Class

Factory Functions

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