This chrome extension allows developers to quickly generate and test SQL queries on a Postgres database using ChatGPT, a powerful language model developed by OpenAI.
Extract SQL queries generated by ChatGPT and send them to a Node.js API Connect the API to a global database cache service (PolyScale) to reduce the risk of query abuse and improve performance Display query results in a table on the ChatGPT page
- A Neon account
- A PolyScale account (optional)
- Vercel CLI
- Clone the repository to your local machine
- Run npm install to install the necessary dependencies
- Run npm run build to build the extension
- Go to chrome://extensions/ in your chrome browser
- Turn on developer mode
- Click on "Load unpacked"
- Select the "dist" folder in your local repository
- Open chat.openai.com
- Generate SQL queries using ChatGPT
- Click on the "Run SQL" button below the query to send the query to the API
- Query results will be displayed in a table on the ChatGPT page
The API is a simple Node.js function deployed to Vercel. The following packages are required:
pg
dotenv
Create a new folder and Node.js app by running the following commands on your terminal:
npm init
npm install pg dotenv
Create a api/handler.js and paste the following code:
const { Pool } = require('pg');
require('dotenv').config();
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
exports.handler = async (event) => {
const { sql, chatId } = JSON.parse(event.body);
const schema = `chat_${chatId}`;
const client = await pool.connect();
try {
await client.query(`SET search_path TO ${schema}`);
const { rows } = await client.query(sql);
return {
statusCode: 200,
body: JSON.stringify(rows),
};
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify({ error: err.message }),
};
} finally {
client.release();
}
};
Note that the DATABASE_URL is the one provided by PolyScale when you connect it to your Neon database.