-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Bun.SQL: statement and columns field for SQLResultArray
#18892
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
base: main
Are you sure you want to change the base?
Bun.SQL: statement and columns field for SQLResultArray
#18892
Conversation
Jarred-Sumner
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this PR
Instead of creating the columns array dynamically before each time we run the callback, could we make it a lazy getter on the handle and avoid the work of creating the array when it usually never will be used?
To do that, you could add another item to this array in the class generator:
bun/src/bun.js/api/postgres.classes.ts
Line 80 in 727b085
| values: ["pendingValue", "target", "columns", "binding"], |
Then, a getter function declaration in the list where the other functions are in that file
Then, a getter implementation in postgres.zig, probably somewhat similar to getQueries
Lines 1389 to 1398 in 727b085
| pub fn getQueries(_: *PostgresSQLConnection, thisValue: JSC.JSValue, globalObject: *JSC.JSGlobalObject) JSC.JSValue { | |
| if (PostgresSQLConnection.queriesGetCached(thisValue)) |value| { | |
| return value; | |
| } | |
| const array = JSC.JSValue.createEmptyArray(globalObject, 0); | |
| PostgresSQLConnection.queriesSetCached(thisValue, globalObject, array); | |
| return array; | |
| } |
This should cover include all possible fields. Made all fields optional given result might be empty for INSERT/UPDATE.
|
Great suggestion! I refactored to the lazier approach. Given I also updated the types so they should be abit cleaner on output. It could potentially be less strict with the optional fields but I'm worried something weird might happen in the future due to Also I noticed that there is a formatting issue. I'm not sure why but locally my VSCode's Happy to make any other changes if you have any other suggestions! Here is my latest test/result to cover all the changes: const sql = new Bun.SQL(connectionString);
const res = await sql`select 1 as x, 2 as longer_name, 3 as sp3c141_n4m3`;
console.log("Query:", res.query);
console.log("Statement:", res.statement);
console.log("Columns:", res.columns);
console.log("Value[0]:", res[0]); |
Bun.SQL: columns field for SQLResultArrayBun.SQL: statement and columns field for SQLResultArray
These might not always exist in other SQL variants.
Defaults to previous behavior (any), but allows to change if devs decide to enforce a strict type.
src/sql/postgres.zig
Outdated
|
|
||
| const obj = JSValue.createEmptyObject(globalObject, 2); | ||
|
|
||
| obj.put(globalObject, JSC.ZigString.static("string"), bun.String.init(this.query).toJS(globalObject)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| obj.put(globalObject, JSC.ZigString.static("string"), bun.String.init(this.query).toJS(globalObject)); | |
| obj.put(globalObject, JSC.ZigString.static("string"), bun.String.createUTF8ForJS(globalObject, this.query)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, was not aware of this one!
Maybe we should add to the String.init's documentation: "If you are creating a string for JS, you probably want to use createUTF8ForJS"?
I saw this pattern elsewhere in code. I assume this was added on at a later point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually in this case, I misread and this.query is already a bun.String so I assume this.query.toJS(globalObject) would be more optimal.
What does this PR do?
Improves: #18866
Exports a
columnsfield onSQLResultArrayproviding additional context on columns returned by the query. This is mostly just binding PostgreSQL'sRowDescriptionthat was already previously implemented.This is meant to behave similarly to postgres.js
columns. The main difference is ourtypeuses the internal OIDs while postgresql.js uses JDBC's OIDs. This would make ours more accurate but could impact DX that are used to the "less accurate" values.Here is the result of a test query:
How did you verify your code works?
bun-debug test test/js/sql/sql.test.ts)