-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bun.sql
tracking issue (Postgres client)
#15088
Comments
Can this be an import from import { sql } from 'bun:postgres';
const [{ x }] = await sql`select 1 as x`;
console.log({ x }); |
Support this method in Postgres client, please. |
What's the reason for a |
I'd like to see some features such as flexible prepared statements and Pool as both exist on libs such as Postgres.js and Node-PG. This is personal but the prepared statements on sql(
'INSERT INTO photos(user_id, photo_url) VALUES ($1, $2)',
['1', 'example.com']
)
/**
* Alternatively you could also accept an object as a param
* which could have more metadata to improve tracing later on:
*/
sql({
name: 'add-photo',
text: 'INSERT INTO photos(user_id, photo_url) VALUES ($1, $2)',
values: ['1', 'example.com'],
}) It can be a future enhancement tho. |
https://github.com/oven-sh/bun/tree/jarred/fix-postgres-duplicate-columns don't forget this. |
@rafaell-lycan This is a must feature in my opinion if we were to migrate from existing pg libraries. |
@mithleshjs it defaults to using prepared statements for all queries (similar to postgres.js) |
import { bindParams, findMany, type FindManyParams } from "@knaadh/pg-raw";
import { Client } from "pg";
const pgClient = new Client({
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
host: process.env.DB_HOST,
port: process.env.DB_PORT ? Number.parseInt(process.env.DB_PORT, 10) : 5432,
database: process.env.DB_DATABASE,
});
const params: FindManyParams = {
table: "users",
query: {
select: {
id: true,
name: true
},
where: {
name: "@name",
},
limit: 10,
},
};
const rawQuery = findMany(params);
// Output: SELECT "id", "name" FROM "users" WHERE "name" = '@name' LIMIT 10
const { text, values } = bindParams(rawQuery, { name: "Alan Wake" });
/*
{
text: "SELECT \"id\", \"name\" FROM \"users\" WHERE \"name\" = $1 LIMIT 10",
values: [ "Alan Wake" ],
}
*/
const result = await pgClient.query(text, values);
console.log(result.rows[0]); @Jarred-Sumner @rafaell-lycan We built our raw query builder and the generated query string works flawlessly with node-postgres as show in the code above and every other Postgres client and tools. But it doesn't work with Postgres.js despite the fact the generated query is just a raw query string. The reason being the |
Sorry, I was misinformed. Ignore this comment. |
@nicksrandall did you try it |
@Jarred-Sumner Yes, it looks like I was a bit premature in commenting on this issue. Weirdly, I had When I change the connection string to I apologize for creating unnecessary noise. |
Edit: I figured this out, see the edits below. Hi, I have two questions about using Postgres in Bun, they are specifically in context of connecting to a Supabase Postgres database.
Thanks Edit: looking at the I tried this: import { SQL } from "bun";
// Note that this URL looks like this: `postgresql://postgres:${password}@db.${projectId}.supabase.co:5432/postgres`
import supabaseUrl from "../../secrets/supabaseUrl";
const sql = new SQL(supabaseUrl);
const users = await sql`select * from users`;
console.log(users); But this still gives me this error:
Edit: How to get Supabase connection to work with Bun's Postgres support:
import { sql } from "bun";
const version = await sql`SELECT version()`;
console.log(version); Call with:
Gary Austin over at the Supabase Discord helped me figure this out. |
@Jarred-Sumner I don't understand why the postgres driver isn't split up into it's own import like you guys did with bun:sqlite. If the goal is introduce more database drivers, won't you eventually be unable to support database specific features? How is it possible to abstract all database drivers into a single utility without losing a 100 percent feature parity with libraries that are designed to connect to only a single database? What am I missing? |
See: #17029 |
Bun.sql
is Bun's builtin postgres client, currently only available on canary builds of Bun.sqlite and other database protocols will be added sometime after it ships.
Minimum necessary to unflag for Bun v1.2:
sql(array)
inINSERT INTO
queriessslmode=require
Feature complete:
COPY
protocolPoint
& other geo-related typesNice to have:
--sql-preconnect
CLI flag to start connecting to DB before code loadssql.begin(callback)
API for transactionssql.array(array)
for various operations involving arraysThe text was updated successfully, but these errors were encountered: