-
Notifications
You must be signed in to change notification settings - Fork 31
feat: return numAffectedRows in Kysely driver #95
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?
Conversation
|
Thanks for the PR! This is looking good.
Instead, I would add this type right below export type ResultsArray<Result extends Record<string, any>> = Result[] & {
affectedRows?: bigint;
};Then, modify the sql = async <Result extends Record<string, any>>(
queryTemplate: TemplateStringsArray | string,
...params: unknown[]
): Promise<ResultsArray<Result>> => {
const statement = normalizeSql(queryTemplate, params);
const { rows, columns, affectedRows } = await this.exec(
statement.sql,
statement.params,
'all'
);
const results = convertRowsToObjects(rows, columns) as ResultsArray<Result>;
results.affectedRows = affectedRows;
return results;
}; |
src/drivers/sqlite-memory-driver.ts
Outdated
| break; | ||
| } | ||
|
|
||
| statementData.affectedRows = db.changes(); |
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.
The docs hint that db.changes(false, true) can return a bigint. Did you test that?
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.
Good catch, I changed this to (false, true). Sadly the type on the changes method is wrong so I had to add an unsafe cast. I could also change the unsafe cast to re-construct the BigInt instead
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.
When sqlite-wasm adds new functions/signatures, they do not update the types unless someone submits a pull request. (The reason given is that the SQLite team does not know TypeScript and is not interested in maintaining types.) You should be able to get such a PR accepted easily enough though. I've done it before.
Until then, use a @ts-expect-error comment instead of a cast.
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.
I went ahead and opened that pull request to sqlite-wasm: sqlite/sqlite-wasm/pull/122
|
Thanks for your review. I've addressed your concerns in a separate commit (I can squash later), see the other comments as well. I removed the export type ResultsArray<Result extends Record<string, any>> = Result[] & {
affectedRows?: bigint;
};Personally, I don't like this very much. Having extra properties on an array seems a bit magical to me. Also, a pretty big number of tests had to be changed as the following comparison would fail: I think the even bigger concern is that this might break downstream tests (users of SQLocal). Lmk what you think |
|
My goal with that suggestion, besides facilitating the removal of I think the better approach is to just un-private |
|
Okay, I used the |
DallasHoff
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.
Nothing major left. Just a couple little things. Thanks for working with me on this. I want to have this go in with version 0.17.
|
|
||
| if (this.transaction === null) { | ||
| rows = await this.client.sql(query.sql, ...query.parameters); | ||
| const statement = normalizeSql(query.sql, [...query.parameters]); |
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.
Is it necessary to make a copy of the array here?
src/client.ts
Outdated
| data.rows = message.data[0]?.rows ?? []; | ||
| data.columns = message.data[0]?.columns ?? []; | ||
| data.affectedRows = message.data[0]?.affectedRows; |
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.
We should declare const results = message.data[0] instead of duplicating it 3 times.
src/types.ts
Outdated
| export type RawResultData = { | ||
| rows: unknown[] | unknown[][]; | ||
| columns: string[]; | ||
| affectedRows?: bigint; |
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.
Let's follow Kysely's lead and name the key numAffectedRows here too.
src/types.ts
Outdated
| commit: () => Promise<void>; | ||
| rollback: () => Promise<void>; | ||
| lastAffectedRows?: bigint; | ||
| key: QueryKey; |
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.
Let's name this transactionKey.
src/drivers/sqlite-memory-driver.ts
Outdated
| // @ts-expect-error When the second parameter of `changes` is true, a | ||
| // bigint is returned, but the type is wrong. | ||
| // FIXME after https://github.com/sqlite/sqlite-wasm/pull/122 lands. |
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.
@ts-expect-error https://github.com/sqlite/sqlite-wasm/pull/122 will suffice for the comment.
|
Okay, that should be all. The array copy is necessary because the parameters array is |
Hi, thanks for the great project!
I moved a part of my project from better-sqlite3/Kysely to SQLocal/Kysely since I find it more convenient to work with a WASM blob rather than having to build a binary. In the process I noticed that I was no longer getting the
numAffectedRowsfrom Kysely'sexecuteQuerycall. This PR implement that.To do this, I had to expose
execSqlmethod on the SQLocal/client class. Please let me know if this is not the way. It also adds alastAffectedRowsfield to the transaction field.Tests were added as well.
Cheers