@andrew_l/mongo-transaction 0.3.22 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,18 +1,17 @@
1
- import { Awaitable, Fn, AnyFunction, RetryOnErrorConfig } from '@andrew_l/toolkit';
2
- import { ClientSession, ClientSessionOptions } from 'mongodb';
3
-
1
+ import { AnyFunction, Awaitable, Fn, RetryOnErrorConfig } from "@andrew_l/toolkit";
2
+ import { ClientSession, ClientSessionOptions } from "mongodb";
4
3
  type OnMongoSessionCommittedResult<T> = {
5
- /**
6
- * Executes the provided function upon transaction commit.
7
- *
8
- * Returns `T` if the transaction is committed and the function completes successfully.
9
- *
10
- * Returns `undefined` if the transaction is explicitly aborted or ends without committing.
11
- *
12
- * Rejects if the function throws an error.
13
- */
14
- promise: Promise<T | undefined>;
15
- cancel: () => void;
4
+ /**
5
+ * Executes the provided function upon transaction commit.
6
+ *
7
+ * Returns `T` if the transaction is committed and the function completes successfully.
8
+ *
9
+ * Returns `undefined` if the transaction is explicitly aborted or ends without committing.
10
+ *
11
+ * Rejects if the function throws an error.
12
+ */
13
+ promise: Promise<T | undefined>;
14
+ cancel: () => void;
16
15
  };
17
16
  /**
18
17
  * Executes the provided function upon transaction commit.
@@ -39,7 +38,6 @@ type OnMongoSessionCommittedResult<T> = {
39
38
  */
40
39
  declare function onMongoSessionCommitted<T>(fn: () => Awaitable<T>): OnMongoSessionCommittedResult<T>;
41
40
  declare function onMongoSessionCommitted<T>(session: ClientSession, fn: () => Awaitable<T>): OnMongoSessionCommittedResult<T>;
42
-
43
41
  /**
44
42
  * Returns the current transaction session if executed within `withMongoTransaction()` otherwise returns `null`
45
43
  *
@@ -56,37 +54,35 @@ declare function onMongoSessionCommitted<T>(session: ClientSession, fn: () => Aw
56
54
  * @group Hooks
57
55
  */
58
56
  declare function useMongoSession(): ClientSession | null;
59
-
60
57
  interface TransactionEffect {
61
- /**
62
- * Specifies when the transaction effect should run:
63
- *
64
- * `pre` - execute immediately
65
- *
66
- * `post` - execute before transaction commit
67
- *
68
- * @default: "pre"
69
- */
70
- flush: 'pre' | 'post';
71
- /**
72
- * Setup effect function. You can return a cleanup callback to be used as a rollback.
73
- */
74
- setup: EffectCallback;
75
- /**
76
- * Cleanup function.
77
- */
78
- cleanup?: EffectCleanup;
79
- /**
80
- * Useful for debugging execution logs.
81
- */
82
- name?: string;
83
- dependencies?: readonly any[];
58
+ /**
59
+ * Specifies when the transaction effect should run:
60
+ *
61
+ * `pre` - execute immediately
62
+ *
63
+ * `post` - execute before transaction commit
64
+ *
65
+ * @default: "pre"
66
+ */
67
+ flush: 'pre' | 'post';
68
+ /**
69
+ * Setup effect function. You can return a cleanup callback to be used as a rollback.
70
+ */
71
+ setup: EffectCallback;
72
+ /**
73
+ * Cleanup function.
74
+ */
75
+ cleanup?: EffectCleanup;
76
+ /**
77
+ * Useful for debugging execution logs.
78
+ */
79
+ name?: string;
80
+ dependencies?: readonly any[];
84
81
  }
85
82
  type EffectCallback = () => Awaitable<EffectCleanup | void>;
86
83
  type EffectCleanup = () => Awaitable<void>;
87
84
  type OnCommittedCallback = () => Awaitable<void>;
88
85
  type OnRollbackCallback = () => Awaitable<void>;
89
-
90
86
  type UseTransactionEffectOptions = Partial<Pick<TransactionEffect, 'name' | 'flush' | 'dependencies'>>;
91
87
  /**
92
88
  * Executes a transactional effect with cleanup on error or rollback.
@@ -124,7 +120,6 @@ type UseTransactionEffectOptions = Partial<Pick<TransactionEffect, 'name' | 'flu
124
120
  * @group Hooks
125
121
  */
126
122
  declare function useTransactionEffect(setup: TransactionEffect['setup'], options?: UseTransactionEffectOptions): Promise<void>;
127
-
128
123
  /**
129
124
  * Registers a callback to be executed upon transaction commitment, with support
130
125
  * for dependency-based updates.
@@ -167,7 +162,6 @@ declare function useTransactionEffect(setup: TransactionEffect['setup'], options
167
162
  * @group Hooks
168
163
  */
169
164
  declare function onCommitted(callback: OnCommittedCallback, dependencies?: readonly any[]): Fn;
170
-
171
165
  /**
172
166
  * Registers a callback to be executed upon transaction rollback, with support
173
167
  * for dependency-based updates.
@@ -210,47 +204,46 @@ declare function onCommitted(callback: OnCommittedCallback, dependencies?: reado
210
204
  * @group Hooks
211
205
  */
212
206
  declare function onRollback(callback: OnRollbackCallback, dependencies?: readonly any[]): Fn;
213
-
214
207
  interface MongoClientLike {
215
- startSession(options: Record<string, any>): ClientSessionLike;
208
+ startSession(options: Record<string, any>): ClientSessionLike;
216
209
  }
217
210
  interface ClientSessionLike {
218
- withTransaction(fn: AnyFunction): Promise<any>;
219
- endSession(): Promise<void>;
211
+ withTransaction(fn: AnyFunction): Promise<any>;
212
+ endSession(): Promise<void>;
220
213
  }
221
214
  type ConnectionValue = MongoClientLike | (() => Awaitable<MongoClientLike>);
222
215
  type Callback<T, K = any, Args extends Array<any> = any[]> = (this: K, session: ClientSession, ...args: Args) => Awaitable<T>;
223
216
  interface WithMongoTransactionOptions<T, K = any, Args extends Array<any> = any[]> {
224
- /**
225
- * Mongodb connection getter
226
- */
227
- connection: ConnectionValue;
228
- /**
229
- * Transaction session options
230
- *
231
- * @default: {
232
- * defaultTransactionOptions: {
233
- * readPreference: 'primary',
234
- * readConcern: { level: 'local' },
235
- * writeConcern: { w: 'majority' },
236
- * }
237
- * }
238
- */
239
- sessionOptions?: ClientSessionOptions;
240
- /**
241
- * Configures a timeoutMS expiry for the entire withTransactionCallback.
242
- *
243
- * @remarks
244
- * - The remaining timeout will not be applied to callback operations that do not use the ClientSession.
245
- * - Overriding timeoutMS for operations executed using the explicit session inside the provided callback will result in a client-side error.
246
- */
247
- timeoutMS?: number;
248
- /**
249
- * Transaction function that will be executed
250
- *
251
- * ⚠️ Possible several times!
252
- */
253
- fn: Callback<T, K, Args>;
217
+ /**
218
+ * Mongodb connection getter
219
+ */
220
+ connection: ConnectionValue;
221
+ /**
222
+ * Transaction session options
223
+ *
224
+ * @default: {
225
+ * defaultTransactionOptions: {
226
+ * readPreference: 'primary',
227
+ * readConcern: { level: 'local' },
228
+ * writeConcern: { w: 'majority' },
229
+ * }
230
+ * }
231
+ */
232
+ sessionOptions?: ClientSessionOptions;
233
+ /**
234
+ * Configures a timeoutMS expiry for the entire withTransactionCallback.
235
+ *
236
+ * @remarks
237
+ * - The remaining timeout will not be applied to callback operations that do not use the ClientSession.
238
+ * - Overriding timeoutMS for operations executed using the explicit session inside the provided callback will result in a client-side error.
239
+ */
240
+ timeoutMS?: number;
241
+ /**
242
+ * Transaction function that will be executed
243
+ *
244
+ * ⚠️ Possible several times!
245
+ */
246
+ fn: Callback<T, K, Args>;
254
247
  }
255
248
  type WithMongoTransactionWrapped<T, K = any, Args extends Array<any> = any[]> = (this: K, ...args: Args) => Promise<T>;
256
249
  /**
@@ -294,9 +287,7 @@ declare function withMongoTransaction<T, K = any, Args extends Array<any> = any[
294
287
  * });
295
288
  */
296
289
  declare function withMongoTransaction<T, K = any, Args extends Array<any> = any[]>(connection: ConnectionValue, fn: Callback<T, K, Args>, options?: Omit<WithMongoTransactionOptions<any>, 'fn' | 'connection'>): WithMongoTransactionWrapped<T, K, Args>;
297
-
298
- interface WithTransactionOptions extends Partial<RetryOnErrorConfig> {
299
- }
290
+ interface WithTransactionOptions extends Partial<RetryOnErrorConfig> {}
300
291
  /**
301
292
  * Wraps a function with transaction context, enabling retry logic and transactional effects.
302
293
  *
@@ -343,15 +334,22 @@ interface WithTransactionOptions extends Partial<RetryOnErrorConfig> {
343
334
  *
344
335
  * @group Main
345
336
  */
346
- declare function withTransaction<T, K = any, Args extends Array<any> = any[]>(fn: (this: K, ...args: Args) => T, { beforeRetryCallback, shouldRetryBasedOnError, maxAttempts, maxRetriesNumber, delayFactor, delayMaxMs, delayMinMs, }?: WithTransactionOptions): (this: K, ...args: Args) => Promise<Awaited<T>>;
347
-
337
+ declare function withTransaction<T, K = any, Args extends Array<any> = any[]>(fn: (this: K, ...args: Args) => T, {
338
+ beforeRetryCallback,
339
+ shouldRetryBasedOnError,
340
+ maxAttempts,
341
+ maxRetriesNumber,
342
+ delayFactor,
343
+ delayMaxMs,
344
+ delayMinMs
345
+ }?: WithTransactionOptions): (this: K, ...args: Args) => Promise<Awaited<T>>;
348
346
  interface TransactionControlled<T, K = any, Args extends Array<any> = any[]> {
349
- run: (this: K, ...args: Args) => Promise<void>;
350
- commit: () => Promise<void>;
351
- rollback: () => Promise<void>;
352
- result: Readonly<T | undefined>;
353
- error: Readonly<Error | undefined>;
354
- active: boolean;
347
+ run: (this: K, ...args: Args) => Promise<void>;
348
+ commit: () => Promise<void>;
349
+ rollback: () => Promise<void>;
350
+ result: Readonly<T | undefined>;
351
+ error: Readonly<Error | undefined>;
352
+ active: boolean;
355
353
  }
356
354
  /**
357
355
  * Wraps a function and returns a `TransactionControlled` interface, allowing manual control
@@ -387,5 +385,5 @@ interface TransactionControlled<T, K = any, Args extends Array<any> = any[]> {
387
385
  * @group Main
388
386
  */
389
387
  declare function withTransactionControlled<T, K = any, Args extends Array<any> = any[]>(fn: (this: K, ...args: Args) => T): TransactionControlled<Awaited<T>, K, Args>;
390
-
391
388
  export { type OnMongoSessionCommittedResult, type TransactionControlled, type UseTransactionEffectOptions, type WithMongoTransactionOptions, type WithTransactionOptions, onCommitted, onMongoSessionCommitted, onRollback, useMongoSession, useTransactionEffect, withMongoTransaction, withTransaction, withTransactionControlled };
389
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":["T","promise","Promise","cancel","Awaitable","fn","OnMongoSessionCommittedResult","ClientSession","session","T","ClientSession","fallback","contextValue","flush","setup","EffectCallback","cleanup","EffectCleanup","name","dependencies","Awaitable","callback","OnCommittedCallback","OnRollbackCallback","effects","cursor","byCursor","TransactionEffect","committed","TransactionOnCommitted","rollbacks","TransactionOnRollback","T","Args","fn","log","Logger","_active","error","Error","result","run","this","args","Promise","hooks","TransactionScopeHooks","constructor","active","_run","self","commit","rollback","reset","clean","TransactionScope","Awaited","scope","reason","effect","Partial","Pick","TransactionEffect","setup","UseTransactionEffectOptions","options","Promise","OnCommittedCallback","callback","dependencies","Fn","OnRollbackCallback","callback","dependencies","Fn","startSession","Record","options","ClientSessionLike","withTransaction","AnyFunction","fn","Promise","endSession","MongoClientLike","Awaitable","T","K","Args","Array","this","ClientSession","session","args","connection","ConnectionValue","sessionOptions","ClientSessionOptions","timeoutMS","Callback","WithMongoTransactionOptions","WithMongoTransactionWrapped","Omit","Partial","RetryOnErrorConfig","T","K","Args","Array","this","args","fn","beforeRetryCallback","shouldRetryBasedOnError","maxAttempts","maxRetriesNumber","delayFactor","delayMaxMs","delayMinMs","WithTransactionOptions","Promise","Awaited","T","K","Args","Array","run","this","args","Promise","commit","rollback","result","Readonly","error","Error","active","fn","TransactionControlled","Awaited"],"sources":["../src/hooks/onMongoSessionCommitted.d.ts","../src/hooks/useMongoSession.d.ts","../src/scope.d.ts","../src/hooks/useTransactionEffect.d.ts","../src/hooks/onCommitted.d.ts","../src/hooks/onRollback.d.ts","../src/withMongoTransaction.d.ts","../src/withTransaction.d.ts","../src/withTransactionControlled.d.ts"],"mappings":";;KAEY,6BAAA;EAAA;;;;;;;;;EAURC,OAAAA,EAAS,OAAO,CAAC,CAAA;EACjBE,MAAAA;AAAAA;;;;;;;;;;;;;;;;AAyBuG;AAC3G;;;;;;;iBADwB,uBAAA,IAA2BE,EAAAA,QAAU,SAAA,CAAU,CAAA,IAAK,6BAAA,CAA8B,CAAA;AAAA,iBAClF,uBAAA,IAA2BG,OAAAA,EAAS,aAAA,EAAeH,EAAAA,QAAU,SAAA,CAAU,CAAA,IAAK,6BAAA,CAA8B,CAAA;;AArClI;;;;;;;;;;AAWU;AAyBV;;;iBCrBwB,eAAA,IAAmB,aAAa;AAAA,UChBvC,iBAAA;;AFCjB;;;;;;;;EESIQ,KAAAA;EFEM;AAAA;AAyBV;EEvBIC,KAAAA,EAAO,cAAA;EFuBoC;;;EEnB3CE,OAAAA,GAAU,aAAa;EFmBiD;;;EEfxEE,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