Public
Like
sqliteWriter
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
The QueryWriter class is a utility for generating and executing SQL queries using natural language and OpenAI. It provides a simplified interface for interacting with your Val Town SQLite database and generating SQL queries based on user inputs.
This val is inspired by prisma-gpt. PRs welcome! See Todos below for some ideas I have.
- Import the QueryWriter class into your script:
 
- Create an instance of QueryWriter, providing the desired table and an optional model:
 
- Call the 
writeQuery()method to generate an SQL query based on a user input string: 
const userInput = "Show me all the customers with more than $1000 in purchases.";
const query = await writer.writeQuery(userInput);
- Alternatively, use the 
gptQuery()method to both generate and execute the SQL query: 
const userInput = "Show me all the customers with more than $1000 in purchases.";
const result = await writer.gptQuery(userInput);
- Handle the generated query or query result according to your application's needs.
 
Creates a new instance of the QueryWriter class.
table: The name of the database table to operate on.model(optional): The model to use for generating SQL queries. Defaults to "gpt-3.5-turbo".apiKey(optional): An OpenAI API key. Defaults toDeno.env.get("OPENAI_API_KEY").
Generates an SQL query based on the provided user input string.
str: The user input string describing the desired query.
Returns a Promise that resolves to the generated SQL query.
Generates and executes an SQL query based on the provided user input string.
str: The user input string describing the desired query.
Returns a Promise that resolves to the result of executing the generated SQL query.
- Handle multiple tables for more complex use cases
 -  Edit prompt to allow for more than just 
SELECTqueries - Allow a user to add to the system prompt maybe?
 - Expand usage beyond just Turso SQLite to integrate with other databases
 
Migrated from folder: projects/gptTools/sqliteWriter