-
-
Notifications
You must be signed in to change notification settings - Fork 38
Sqlite nested transactions #114
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
Merged
oskardudycz
merged 11 commits into
event-driven-io:main
from
davecosec:sqlite-nested-transactions
Jul 29, 2025
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5626a0d
First pass
davecosec 64454d9
Adjusted the methods inside of begin and commit, and let the transact…
davecosec 06792dc
Added ability to enable transaction nesting
davecosec 6bf5faa
Removed redundant parameter
davecosec cf57ad9
Removed console.logs
davecosec 9b11789
fixing types
davecosec 08ab7f4
Removed a lot of redundant transaction counter instantiations
davecosec 8ccc302
Uncommented file based tests
davecosec e2c501b
Added option parameter to sqliteAlwaysNewClientPool and allowNestedTr…
davecosec 6c3b181
Added test to fail if nested transaction is attempted but is not enabled
davecosec da677ee
Ensured that there's a single active transaction within the connection
oskardudycz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,8 @@ export type SQLitePoolConnectionOptions< | |
ConnectorType extends SQLiteConnectorType = SQLiteConnectorType, | ||
> = { | ||
connector: ConnectorType; | ||
transactionCounter: TransactionNestingCounter; | ||
allowNestedTransactions: boolean; | ||
type: 'PoolClient'; | ||
connect: Promise<SQLitePoolClient>; | ||
close: (client: SQLitePoolClient) => Promise<void>; | ||
|
@@ -55,6 +57,8 @@ export type SQLiteClientConnectionOptions< | |
ConnectorType extends SQLiteConnectorType = SQLiteConnectorType, | ||
> = { | ||
connector: ConnectorType; | ||
transactionCounter: TransactionNestingCounter; | ||
allowNestedTransactions: boolean; | ||
type: 'Client'; | ||
connect: Promise<SQLiteClient>; | ||
close: (client: SQLiteClient) => Promise<void>; | ||
|
@@ -79,31 +83,74 @@ export const sqliteClientConnection = < | |
>( | ||
options: SQLiteClientConnectionOptions<ConnectorType>, | ||
): SQLiteClientConnection<ConnectorType> => { | ||
const { connect, close } = options; | ||
const { connect, close, allowNestedTransactions, transactionCounter } = | ||
options; | ||
|
||
return createConnection({ | ||
connector: options.connector, | ||
connect, | ||
close, | ||
initTransaction: (connection) => | ||
sqliteTransaction(options.connector, connection), | ||
initTransaction: (connection) => { | ||
return sqliteTransaction( | ||
|
||
options.connector, | ||
connection, | ||
transactionCounter, | ||
allowNestedTransactions, | ||
); | ||
}, | ||
executor: () => sqliteSQLExecutor(options.connector), | ||
}); | ||
}; | ||
|
||
export type TransactionNestingCounter = { | ||
increment: () => void; | ||
decrement: () => void; | ||
reset: () => void; | ||
level: number; | ||
}; | ||
|
||
export const transactionNestingCounter = (): TransactionNestingCounter => { | ||
let transactionLevel = 0; | ||
|
||
return { | ||
reset: () => { | ||
transactionLevel = 0; | ||
}, | ||
increment: () => { | ||
transactionLevel++; | ||
}, | ||
decrement: () => { | ||
transactionLevel--; | ||
|
||
if (transactionLevel < 0) { | ||
throw new Error('Transaction level is out of bounds'); | ||
} | ||
}, | ||
get level() { | ||
return transactionLevel; | ||
}, | ||
}; | ||
}; | ||
|
||
export const sqlitePoolClientConnection = < | ||
ConnectorType extends SQLiteConnectorType = SQLiteConnectorType, | ||
>( | ||
options: SQLitePoolConnectionOptions<ConnectorType>, | ||
): SQLitePoolClientConnection<ConnectorType> => { | ||
const { connect, close } = options; | ||
const { connect, close, transactionCounter, allowNestedTransactions } = | ||
options; | ||
|
||
return createConnection({ | ||
connector: options.connector, | ||
connect, | ||
close, | ||
initTransaction: (connection) => | ||
sqliteTransaction(options.connector, connection), | ||
sqliteTransaction( | ||
options.connector, | ||
connection, | ||
transactionCounter, | ||
allowNestedTransactions ?? false, | ||
), | ||
executor: () => sqliteSQLExecutor(options.connector), | ||
}); | ||
}; | ||
|
@@ -113,11 +160,13 @@ export function sqliteConnection< | |
>( | ||
options: SQLitePoolConnectionOptions<ConnectorType>, | ||
): SQLitePoolClientConnection; | ||
|
||
export function sqliteConnection< | ||
ConnectorType extends SQLiteConnectorType = SQLiteConnectorType, | ||
>( | ||
options: SQLiteClientConnectionOptions<ConnectorType>, | ||
): SQLiteClientConnection; | ||
|
||
export function sqliteConnection< | ||
ConnectorType extends SQLiteConnectorType = SQLiteConnectorType, | ||
>( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need to have transactionCounter as part of options? Shouldn’t be that created internally in the connection and kept there?🤔