npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ai-sql-optimizer

v1.0.1

Published

A CLI tool to optimize SQL queries using AI

Readme

AI SQL Optimizer 🚀

An intelligent Node.js library and CLI tool that leverages Gemini to analyze, refactor, and index your SQL queries.

ai-sql-optimizer acts as your personal AI Database Administrator. By integrating seamlessly into your CLI or directly into your Node.js application, it detects anti-patterns, suggests schema indexes, and rewrites your raw SQL queries for maximum performance.

🌟 Features

  • CLI Support: Seamlessly optimize query strings instantly via your terminal.
  • Node.js Integration: Built as a standard CommonJS module so you can script query performance testing programmatically.
  • Gemini Powered: Uses the official Google Generative AI JavaScript SDK and modern models to safely analyze execution paths.
  • Strict Formatting: Automatically breaks down outputs into the optimized query, index suggestions, and exact technical explanations.

📦 Installation

To use the package globally as a CLI tool:

npm install -g ai-sql-optimizer

Or install it locally as a dependency in your own Node.js project:

npm install ai-sql-optimizer

⚙️ Configuration

To run this tool, you will need to provide your Gemini API key using environment variables. This project uses the dotenv package to securely load variables from a .env file into Node.js.

  1. In the root of your project, clone the example env:
    cp .env.example .env
  2. Open the newly created .env file and configure your API key:
    GEMINI_API_KEY=sk-your-real-api-key-here

Using dotenv in Node.js

We make API authentication a breeze. The dotenv package is automatically initialized at the top of our module via require('dotenv').config().

This reads your .env file and securely binds your authentication key to Node's process.env.GEMINI_API_KEY. Behind the scenes, the Gemini Client natively uses this variable, eliminating any need to hardcode tokens.

💻 Usage

1. Command Line Interface (CLI)

Once installed globally, you can pass any raw SQL query directly to the ai-sql-opt command:

ai-sql-opt "SELECT * FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE o.status = 'PENDING';"

2. Programmatic Usage (Node.js)

You can pull the library directly into your own codebase using standard CommonJS modules:

require('dotenv').config(); // Load your project's local env
const { optimize } = require('ai-sql-optimizer');

async function testPerformance() {
    const rawSql = "SELECT name, email FROM accounts WHERE id IN (SELECT account_id FROM subscriptions);";
    
    // Call the asynchronous optimize method
    const output = await optimize(rawSql);
    console.log(output);
}

testPerformance();

📊 Example Input/Output

Input

ai-sql-opt "SELECT * FROM Customers WHERE Country = 'USA' OR Country = 'Canada';"

Output

--- Original Query ---
SELECT * FROM Customers WHERE Country = 'USA' OR Country = 'Canada';

Optimizing...

--- AI Response ---
Optimized Query:
SELECT customer_id, name, email FROM Customers WHERE Country IN ('USA', 'Canada');

Suggestions:
- Consider adding a standard B-tree index on the `Country` column if this filter is executed frequently.
- Avoid Using `SELECT *`. Extract exactly what you need.

Explanation:
Refactoring the `OR` statements into an `IN` clause provides the database planner with a cleaner list of values. Avoiding the wildcard `SELECT` reduces network IO and memory footprint.

🔮 Future Improvements

  • [ ] Provide configurable Gemini models and temperature settings via CLI flags.
  • [ ] Add support for alternative AI providers like Anthropic Claude.
  • [ ] Introduce schema integrations so the AI can analyze database layouts and exact types, not just the raw SQL.
  • [ ] Integrate with popular ORM logging tools natively.

📄 License

This project is licensed under the MIT License.