Skip to content

Commit 61458ca

Browse files
committed
feat(dialect-tauri): improve execute logic
1 parent 471b2e4 commit 61458ca

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

packages/dialect-tauri/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ export interface TauriSqlDialectConfig<T extends 'sqlite' | 'mysql' | 'postgres'
3131
type: T
3232
onCreateConnection?: (connection: DatabaseConnection) => Promisable<void>
3333
}
34-
```
34+
```
35+
36+
### known issue
37+
38+
- no RETURNING support
39+
- only return rows when executing raw sql

packages/dialect-tauri/src/driver.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,23 @@ class TauriSqlConnection implements DatabaseConnection {
7878
this.db = db
7979
}
8080

81-
async exec(sql: string, param?: readonly unknown[]) {
82-
const { lastInsertId, rowsAffected } = await this.db.execute(sql, param as any)
83-
return {
84-
numAffectedRows: BigInt(rowsAffected),
85-
insertId: BigInt(lastInsertId),
86-
}
87-
}
88-
8981
streamQuery<R>(): AsyncIterableIterator<QueryResult<R>> {
9082
throw new Error('Tauri sql plugin doesn\'t support streaming')
9183
}
9284

93-
async executeQuery<R>(compiledQuery: CompiledQuery<unknown>): Promise<QueryResult<R>> {
94-
const { parameters, sql, query } = compiledQuery
95-
return Promise.resolve((query.kind === 'SelectQueryNode' || query.kind === 'RawNode')
96-
? {
97-
rows: await this.db.select(sql, parameters as any),
98-
}
99-
: {
85+
async executeQuery<R>({ parameters, query, sql }: CompiledQuery<unknown>): Promise<QueryResult<R>> {
86+
switch (query.kind) {
87+
case 'SelectQueryNode':
88+
case 'RawNode':
89+
return { rows: await this.db.select<any>(sql, parameters) }
90+
default: {
91+
const { lastInsertId, rowsAffected } = await this.db.execute(sql, parameters)
92+
return {
10093
rows: [],
101-
...await this.exec(sql, parameters),
102-
},
103-
)
94+
insertId: BigInt(lastInsertId),
95+
numAffectedRows: BigInt(rowsAffected),
96+
}
97+
}
98+
}
10499
}
105100
}

packages/dialect-tauri/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export class TauriSqlDialect<T extends 'sqlite' | 'mysql' | 'postgres'> {
4242
* using [official sql plugin](https://github.com/tauri-apps/plugins-workspace/tree/dev/plugins/sql),
4343
* support MySQL, PostgreSQL and SQLite
4444
*
45-
* currently no support for bigint
45+
* - no RETURNING support
46+
* - only return rows when executing raw sql
4647
*/
4748
constructor(config: TauriSqlDialectConfig<T>) {
4849
this.#config = {

packages/dialect-tauri/src/type.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* https://github.com/tauri-apps/plugins-workspace/blob/dev/plugins/sql/guest-js/index.ts
33
*/
44
export interface TauriSqlDB {
5-
execute(query: string, bindValues?: unknown[]): Promise<QueryResult>
6-
select<T>(query: string, bindValues?: unknown[]): Promise<T>
5+
execute(query: string, bindValues?: readonly unknown[]): Promise<QueryResult>
6+
select<T>(query: string, bindValues?: readonly unknown[]): Promise<T>
77
close: () => Promise<boolean>
88
}
99
export interface QueryResult {

0 commit comments

Comments
 (0)