@ooples/token-optimizer-mcp
Version:
Intelligent context window optimization for Claude Code - store content externally via caching and compression, freeing up your context window for what matters
174 lines • 5.11 kB
TypeScript
/**
* Smart ORM - 83% token reduction through intelligent ORM query optimization
*
* Features:
* - ORM query analysis (Prisma, Sequelize, TypeORM, Mongoose)
* - N+1 query problem detection
* - Eager loading suggestions
* - Query count optimization
* - Relationship analysis
* - Index recommendations for ORM queries
* - Generated SQL inspection
*/
import { CacheEngine } from '../../core/cache-engine.js';
import type { TokenCounter } from '../../core/token-counter.js';
import type { MetricsCollector } from '../../core/metrics.js';
export type ORMType = 'prisma' | 'sequelize' | 'typeorm' | 'mongoose' | 'generic';
export interface SmartORMOptions {
ormCode: string;
ormType: ORMType;
detectN1?: boolean;
suggestEagerLoading?: boolean;
analyzeRelationships?: boolean;
estimateQueries?: boolean;
modelDefinitions?: string;
force?: boolean;
ttl?: number;
}
export interface Relationship {
type: 'include' | 'join' | 'populate' | 'select' | 'with';
name: string;
model?: string;
nested?: boolean;
}
export interface N1Instance {
type: 'loop_query' | 'map_query' | 'sequential_query';
location: number;
severity: 'low' | 'medium' | 'high';
description: string;
affectedModel?: string;
estimatedQueries?: number;
}
export interface EagerLoadingSuggestion {
type: 'include_relation' | 'join_table' | 'populate_field' | 'select_related';
description: string;
estimatedReduction: number;
example: string;
model?: string;
relationship?: string;
}
export interface QueryReduction {
type: 'batch_query' | 'dataloader' | 'aggregate' | 'subquery';
description: string;
currentQueries: number;
optimizedQueries: number;
savings: number;
}
export interface IndexSuggestion {
table: string;
columns: string[];
type: 'btree' | 'hash' | 'composite' | 'unique';
reason: string;
estimatedImprovement: string;
}
export interface SmartORMResult {
query: {
orm: string;
models: string[];
relationships: Relationship[];
estimatedQueries: number;
};
n1Problems?: {
hasN1: boolean;
instances: N1Instance[];
severity: 'low' | 'medium' | 'high';
totalEstimatedQueries?: number;
};
optimizations?: {
eagerLoading: EagerLoadingSuggestion[];
queryReductions: QueryReduction[];
indexSuggestions: IndexSuggestion[];
estimatedImprovement: number;
};
sql?: {
queries: string[];
totalQueries: number;
optimizedQueries?: string[];
};
cached: boolean;
metrics: {
originalTokens: number;
compactedTokens: number;
reductionPercentage: number;
};
}
export declare class SmartORM {
private cache;
private tokenCounter;
private metrics;
constructor(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector);
run(options: SmartORMOptions): Promise<SmartORMResult>;
private analyzeORM;
private parseORMQuery;
private extractModels;
private extractRelationships;
private estimateQueryCount;
private hasQueriesInLoops;
private detectN1Problems;
private hasQueryPattern;
private suggestEagerLoading;
private generateQueryReductions;
private generateIndexSuggestions;
private estimateGeneratedSQL;
private calculateEstimatedImprovement;
private transformOutput;
private generateCacheKey;
private getCachedResult;
private cacheResult;
}
/**
* Factory Function - Use Constructor Injection
*/
export declare function getSmartOrm(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector): SmartORM;
/**
* CLI Function - Create Resources and Use Factory
*/
export declare function runSmartORM(options: SmartORMOptions): Promise<string>;
export declare const SMART_ORM_TOOL_DEFINITION: {
name: string;
description: string;
inputSchema: {
type: string;
properties: {
ormCode: {
type: string;
description: string;
};
ormType: {
type: string;
enum: string[];
description: string;
};
detectN1: {
type: string;
description: string;
};
suggestEagerLoading: {
type: string;
description: string;
};
analyzeRelationships: {
type: string;
description: string;
};
estimateQueries: {
type: string;
description: string;
};
modelDefinitions: {
type: string;
description: string;
};
force: {
type: string;
description: string;
};
ttl: {
type: string;
description: string;
};
};
required: string[];
};
};
//# sourceMappingURL=smart-orm.d.ts.map