Clear Router Plugin
Arkormˣ provides first class support for Clear Router through the Clear Router plugin which connects Arkormˣ models to Clear Router route model binding, allowing controller method parameters to be resolved automatically from route parameters.
Installation
pnpm add arkormx @arkormx/plugin-clear-routernpm install arkormx @arkormx/plugin-clear-routeryarn add arkormx @arkormx/plugin-clear-routerEnable Legacy Metadata
Clear Router's container binding feature depends heavily on legacy metadata for full support, update your project's tsconfig.json file and set experimentalDecorators and emitDecoratorMetadata to true to enable legacy metadata support.
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}While this gives you complete access to how Clear Router's container binding feature is intended to be used, it is not required as TypeScript 5.2+ Decorators are also supported.
Usage
Register the plugin with Clear Router:
import { ClearRouter } from 'clear-router';
import { clearRouterPlugin } from '@arkormx/plugin-clear-router';
ClearRouter.use(clearRouterPlugin);Route Model Binding
Once the plugin is registered, Clear Router can resolve Arkormˣ models directly inside controller methods.
import Profile from './models/Profile';
import { Bind } from 'clear-router/decorators';
import { Controller } from 'clear-router';
class ProfileController extends Controller {
@Bind()
show(profile: Profile) {
return {
data: {
id: profile.getAttribute('id'),
name: profile.name,
},
};
}
}Define the route using the route parameter:
ClearRouter.get('/profiles/:profile', [ProfileController, 'show']);When a request matches:
GET /profiles/1Clear Router will resolve the :profile route parameter into a Profile model instance before calling the controller method.
Custom Model Path
Use modelsPath when your models live outside Arkormˣ’s configured model directory:
ClearRouter.use(clearRouterPlugin, {
modelsPath: path.join(process.cwd(), 'src/models'),
});