Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sql-js": patch
---

Allow multiple identical connections to be closed.
2 changes: 1 addition & 1 deletion plugins/sql/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions plugins/sql/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@

import { invoke } from '@tauri-apps/api/core'

const DatabaseConnections = new Map<string, number>()

const incrementConnectionCount = (path: string) => {
DatabaseConnections.set(path, (DatabaseConnections.get(path) ?? 0) + 1)
}

const decrementConnectionCount = (path: string) => {
const count = DatabaseConnections.get(path) ?? 0

if (count > 0) {
DatabaseConnections.set(path, count - 1)
}
}

const shouldCloseActualConnection = (path: string) => {
return DatabaseConnections.get(path) === 0
}

export interface QueryResult {
/** The number of rows affected by the query. */
rowsAffected: number
Expand All @@ -28,6 +46,7 @@ export default class Database {
path: string
constructor(path: string) {
this.path = path
incrementConnectionCount(path)
}

/**
Expand Down Expand Up @@ -160,6 +179,12 @@ export default class Database {
* @param db - Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope.
*/
async close(db?: string): Promise<boolean> {
decrementConnectionCount(this.path)

if (!shouldCloseActualConnection(this.path)) {
return true
}

const success = await invoke<boolean>('plugin:sql|close', {
db
})
Expand Down
Loading