getRoutineMetadata
getRoutineMetadata?: (
details: RoutineDetails,
config: InstantiatedConfig
) => RoutineMetadata;
This function allows you to customize how Kanel generates TypeScript types for database routines (functions and procedures). Similar to getMetadata
, you can use this to override the default behavior and customize the generated types according to your needs.
details
The details
parameter describes the routine being processed. It can be either a FunctionDetails
or ProcedureDetails
from the extract-pg-schema
package. The key properties include:
name
: The name of the routineschemaName
: The schema the routine is defined incomment
: The comment/documentation for the routineparameters
: Array of parameter definitionsreturnType
: For functions, this describes the return type which can be either:- A simple string type name
- A table type with columns
- A composite type
Each parameter in the parameters
array has the following structure:
{
name: string;
type: string;
// ... other properties
}
config
The config
parameter provides access to the instantiated configuration, including the typeMap
which maps PostgreSQL types to TypeScript types.
Output
The RoutineMetadata
type that your function should return is defined as:
type RoutineMetadata = {
parametersName: string; // Name for the parameters interface
parameters: Parameter[]; // Array of parameter definitions
returnTypeName: string; // Name for the return type
returnTypeComment?: string[]; // Optional comments for the return type
returnTypeOverride?: string; // Optional override for the return type
path: string; // Path where the types should be generated
};
type Parameter = {
name: string; // Parameter name
typeOverride?: string; // Optional override for the parameter type
nullableOverride?: boolean; // Optional override for nullability
optionalOverride?: boolean; // Optional override for optionality
comment?: string[]; // Optional comments for the parameter
};
Usage Example
Here's an example of how you might use getRoutineMetadata
to customize routine type generation:
getRoutineMetadata: (details, config) => {
// Convert snake_case to PascalCase for type names
const typeName = details.name
.split("_")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join("");
return {
parametersName: `${typeName}Params`,
parameters: details.parameters.map((param) => ({
name: param.name,
// Make all parameters optional by default
optionalOverride: true,
// Add parameter documentation
comment: param.comment ? [param.comment] : undefined,
})),
returnTypeName: `${typeName}Result`,
path: `types/${details.schemaName}/${typeName}`,
};
};