Details Zhin context services (config, database, cron, permission) and how to register or consume them in plugins.
Install
npx skillscat add zhinjs/ai-skills/zhin-context-services Install via the SkillsCat registry.
We need to produce a 2-3 sentence plain-text summary, objective, factual, no marketing, no superlatives, no calls to action, natural prose, no bullet points, no headings, no markdown. At most 60 words. Must be only the summary text, no quotes. Let's craft: "This skill explains how to access and use Zhin's built‑in context services—config, database, cron, and permission—within plugins, showing how to inject them, check readiness, read configuration, schedule tasks, extend permission checks, and define database models.
Zhin Context & Services Skill
Use this skill when developers need to interact with built-in contexts or provide custom services via defineContext.
Accessing Built-in Services
import { usePlugin } from '@zhin.js/core'
const plugin = usePlugin()
// Inject built-in contexts
const config = plugin.inject('config')
const permissions = plugin.inject('permission')
const cronService = plugin.inject('cron')Use plugin.contextIsReady('service') to check if a context is available before using it.
Config Service
Load or read config files via ConfigService:
const configService = plugin.inject('config')
const config = configService?.get('zhin.config.yaml', {
debug: false,
plugins: []
})Notes:
- Supported formats:
.json,.yaml,.yml - Config values can reference environment variables using
${ENV_NAME:default}
Cron Service
Register scheduled tasks using the Cron class and addCron:
import { Cron } from '@zhin.js/core'
plugin.addCron(new Cron('0 * * * *', async () => {
plugin.logger.info('Hourly task')
}))Cron entries are auto-started and stopped when the plugin is disposed.
Permission Service
The permission service provides checks in MessageCommand.permit(...). You can also extend it by pushing a new check function:
const permission = plugin.inject('permission')
permission?.add({
name: /^role\(.+\)$/,
check: async (name, message) => {
// custom role validation
return false
}
})Database Service
If the database context is enabled, use it via plugin.inject('database'):
const database = plugin.inject('database')
const users = await database?.select('User')To define new models, call plugin.defineModel(name, definition) if the database service is active.
Defining Custom Contexts
import { defineContext, usePlugin } from '@zhin.js/core'
const plugin = usePlugin()
plugin.provide(defineContext({
name: 'metrics',
description: 'Metrics service',
mounted: async () => ({
counter: 0
}),
dispose: (value) => {
// cleanup
},
extensions: {
increment() {
this.counter += 1
}
}
}))Custom contexts can also extend Plugin.Extensions so you can call new helper methods from plugins.