docs/docs/en/plugin-development/server/context.md
In NocoBase, each request generates a ctx object, which is an instance of Context. Context encapsulates request and response information while providing NocoBase-specific functionality -- such as database access, cache operations, permission management, internationalization, and logging.
NocoBase's Application is based on Koa, so ctx is essentially a Koa Context, but NocoBase extends it with additional APIs, allowing you to conveniently handle business logic in Middleware and Actions. Each request has an independent ctx, ensuring data isolation between requests.
ctx.action provides information about the Action being executed for the current request, including:
resourceManager.use(async (ctx) => {
console.log(ctx.action.actionName); // Current Action name
ctx.body = `Action: ${ctx.action.actionName}`;
});
Internationalization (i18n) support.
ctx.i18n provides locale informationctx.t() is used to translate strings based on languageresourceManager.use(async (ctx) => {
const msg = ctx.t('Hello World'); // Returns translation based on current language
ctx.body = msg;
});
ctx.db provides an interface for database access, allowing you to directly operate on models and execute queries.
resourceManager.use(async (ctx) => {
const users = await ctx.db.getRepository('users').find();
ctx.body = users;
});
ctx.cache provides cache operations, supporting reading from and writing to the cache, commonly used to accelerate data access or save temporary state.
resourceManager.use(async (ctx) => {
await ctx.cache.set('key', 'value', { ttl: 60 }); // Cache for 60 seconds
const val = await ctx.cache.get('key');
ctx.body = val;
});
ctx.app is the NocoBase application instance, allowing access to global configuration, plugins, and services.
resourceManager.use(async (ctx) => {
console.log(ctx.app);
ctx.body = 'Check console for app';
});
ctx.auth.user retrieves the currently authenticated user's information, suitable for use in permission checks or business logic.
resourceManager.use(async (ctx) => {
if (!ctx.auth.user) {
ctx.throw(401, 'Unauthorized');
}
ctx.body = `Hello ${ctx.auth.user.username}`;
});
ctx.state is used to share data in the middleware chain.
resourceManager.use(async (ctx) => {
ctx.body = `Current User: ${ctx.state.currentRoles.join(',')}`;
});
ctx.logger provides logging capabilities, supporting multi-level log output.
resourceManager.use(async (ctx) => {
ctx.logger.info('Processing request for:', ctx.path);
ctx.body = 'Logged successfully';
});
ctx.permission is used for permission management, ctx.can() is used to check whether the current user has permission to execute a certain operation.
resourceManager.use(async (ctx) => {
const canEdit = await ctx.can('edit', 'posts');
if (!canEdit) {
ctx.throw(403, 'Forbidden');
}
ctx.body = 'You have permission to edit posts';
});
ctx objectctx is an extension of Koa Context, integrating NocoBase functionalityctx.db, ctx.cache, ctx.auth, ctx.state, ctx.logger, ctx.can(), ctx.t(), etc.ctx in Middleware and Actions allows for convenient handling of requests, responses, permissions, logs, and the databasectx to handle requests in middlewarectx.action in resource Actionsctx.permission and ctx.can()ctx.cache for cache operationsctx.loggerctx.t() and ctx.i18n