-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Connection): add an observer api on transaction (#160)
* feat(Connection): add an observer api on transaction * `'commit'` event if transaction committed and is not aborted * `'abort'` event if error happen during transaction * `'abort'` event if `session.abortTransaction()` * refactor: code flow in transaction * Do not need anymore hacks to get rif of TS and eslint warnings. * Better error management ``` > void Promise.resolve('resolve').then((v) => {console.log(v); throw new Error('throw from onfulfilled')}, e => console.error('catch in onrejected', e)).catch(e => console.error('catch in .catch', e)) undefined > resolve catch in .catch Error: throw from onfulfilled at REPL22:1:63 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) > void Promise.reject(new Error('promise error')).then(() => {/* noop */}, e => {console.error('catch in onrejected and rethrow', e); throw e}).catch(e => console.error('catch in .catch', e)) undefined > catch in onrejected and rethrow Error: promise error at REPL23:1:16 catch in .catch Error: promise error at REPL23:1:16 ``` Refs: zakodium/profid#1699
- Loading branch information
Showing
7 changed files
with
249 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
declare module '@ioc:Zakodium/Mongodb/Database/Transaction' { | ||
import { EventEmitter } from 'node:events'; | ||
|
||
import { ClientSession, Db } from 'mongodb'; | ||
|
||
export interface TransactionEvents { | ||
/** | ||
* The transaction commits successfully. | ||
* | ||
* @example | ||
* Consider you have a collection of items storing metadata of file is filesystem. | ||
* Consider when you delete an item from this collection, you must delete associated file. | ||
* | ||
* ```ts | ||
* const item = await connection.transaction((session, db, tx) => { | ||
* const item = await db.collection('test').findOneAndDelete({ _id }, { session }); | ||
* | ||
* tx.on('commit', () => { | ||
* Drive.delete(deletedItem.file.path); | ||
* }); | ||
* | ||
* // some other logic that could fail | ||
* // or await session.abortTransaction() | ||
* // commit will not emit in this case | ||
* | ||
* return item; | ||
* }) | ||
* ``` | ||
*/ | ||
commit: [session: ClientSession, db: Db]; | ||
|
||
/** | ||
* The transaction aborted (optional error). | ||
* Two cases of abortion are possible: | ||
* - if from `session.abortTransaction()`, no error | ||
* - if from a throw, error is set | ||
*/ | ||
abort: [session: ClientSession, db: Db, error?: Error]; | ||
} | ||
|
||
export class TransactionEventEmitter extends EventEmitter<TransactionEvents> {} | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { EventEmitter } from 'node:events'; | ||
|
||
import { ClientSession, Db } from 'mongodb'; | ||
|
||
export interface TransactionEvents { | ||
commit: [session: ClientSession, db: Db]; | ||
abort: [session: ClientSession, db: Db, error?: Error]; | ||
} | ||
|
||
export class TransactionEventEmitter extends EventEmitter<TransactionEvents> {} |
This file contains 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