drizzle-llm
v0.0.3
Published
LLM build query using Drizzle ORM
Readme
Drizzle LLM
Build-time SQL generation using LLM with natural language queries.
Installation
npm install drizzle-llmQuick Start
1. Configure
// drizzle-llm.config.ts
export default {
provider: {
type: 'openai',
apiKey: process.env.OPENAI_API_KEY,
},
paths: {
schema: './src/schema',
queries: './src/**/*.ts',
}
};2. Write Queries
// src/user.ts
import { llm } from "drizzle-llm";
export async function getUser(db: any, id: string): Promise<User[]> {
return db.all<User>(llm`ユーザーを${id}で取得する`);
}
export async function findUserName(db: any, id: string): Promise<string | null> {
return db.get<string>(llm`Find user name by id ${id}`);
}💡 Tip: Be explicit with types and methods for better SQL generation:
- Use
db.get()for single results,db.all()for multiple - Add TypeScript return types:
Promise<User[]>,Promise<string | null> - Use generic types:
db.get<string>(),db.all<User>()
3. Build
npx drizzle-llm buildThis generates user.sql:
-- 4a5f9c682c34d6ec808677691ea62d47
-- ユーザーを${0}で取得する
SELECT * FROM user WHERE id = $1How It Works
- Scan: Finds
llm\...`` calls in your code - Generate: Uses LLM to create SQL from natural language
- Cache: Stores results to avoid redundant API calls
- Runtime: Loads pre-generated SQL at runtime
Environment Variables
OPENAI_API_KEY=sk-...
DRIZZLE_LLM_AUTO_APPROVE=true # Skip confirmation promptsCLI Commands
$ npx drizzle-llm build # Generate SQL queriesConfiguration
interface DrizzleLLMConfig {
provider: {
type: 'openai' | 'anthropic';
apiKey: string;
model?: string;
};
paths: {
schema: string;
queries: string | string[];
};
cache?: {
enabled: boolean;
directory: string;
};
}Author
License
MIT © iwamatsu0430
