useKanelContext
useKanelContext() is the V4 way to access Kanel's runtime context (configuration, schemas, etc.) from within hooks and custom functions.
Overview
In V4, hooks no longer receive configuration as a parameter. Instead, you use useKanelContext() to access the context when needed:
V3 (deprecated):
module.exports = {
preRenderHooks: [
(outputAcc, instantiatedConfig) => {
console.log(instantiatedConfig.outputPath);
return outputAcc;
},
],
};V4:
import { useKanelContext } from "kanel";
module.exports = {
preRenderHooks: [
(outputAcc) => {
const context = useKanelContext();
console.log(context.config.outputPath);
return outputAcc;
},
],
};The KanelContext Type
useKanelContext() returns a KanelContext object with the following structure:
type KanelContext = {
/** General TypeScript configuration affecting all TypeScript generators */
typescriptConfig: TypescriptConfig;
/** Original config as passed to Kanel */
config: Config;
/** Extracted database schemas */
schemas: Record<string, Schema>;
};typescriptConfig
Contains TypeScript-specific settings that apply to all TypeScript generators:
const context = useKanelContext();
console.log(context.typescriptConfig.enumStyle); // 'literal-union' or 'enum'
console.log(context.typescriptConfig.tsModuleFormat); // 'esm', 'commonjs', etc.config
The original configuration object as passed to Kanel. This is either a V3 or V4 config:
const context = useKanelContext();
console.log(context.config.connection); // Database connection config
console.log(context.config.outputPath); // Output directory
console.log(context.config.preDeleteOutputFolder); // Boolean
console.log(context.config.generators); // Array of generator functions (V4)schemas
The extracted database schemas as a record keyed by schema name:
const context = useKanelContext();
const publicSchema = context.schemas.public;
// Iterate over all tables in the public schema
for (const table of publicSchema.tables) {
console.log(table.name);
console.log(table.columns);
}
// Access views, enums, etc.
console.log(publicSchema.views);
console.log(publicSchema.types);The Schema type comes from extract-pg-schema and contains:
tables- Table definitionsviews- View definitionsmaterializedViews- Materialized view definitionstypes- Composite types, enums, domains, ranges
How It Works
useKanelContext() uses Node.js's AsyncLocalStorage to provide context that's automatically available throughout the async execution chain.
See Also
- preRenderHooks - Using context in pre-render hooks
- postRenderHooks - Using context in post-render hooks
- Migration Guide - Complete V3 to V4 migration guide