@rsdk/db 2.5.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.
Files changed (149) hide show
  1. package/CHANGELOG.md +118 -0
  2. package/README.md +3 -0
  3. package/db/dist/context.base.d.ts +30 -0
  4. package/db/dist/context.base.js +52 -0
  5. package/db/dist/context.base.js.map +1 -0
  6. package/db/dist/context.storage.d.ts +22 -0
  7. package/db/dist/context.storage.js +32 -0
  8. package/db/dist/context.storage.js.map +1 -0
  9. package/db/dist/exceptions.d.ts +10 -0
  10. package/db/dist/exceptions.js +25 -0
  11. package/db/dist/exceptions.js.map +1 -0
  12. package/db/dist/index.d.ts +6 -0
  13. package/db/dist/index.js +16 -0
  14. package/db/dist/index.js.map +1 -0
  15. package/db/dist/propagation.enum.d.ts +30 -0
  16. package/db/dist/propagation.enum.js +35 -0
  17. package/db/dist/propagation.enum.js.map +1 -0
  18. package/db/dist/transactional.runner.d.ts +29 -0
  19. package/db/dist/transactional.runner.js +119 -0
  20. package/db/dist/transactional.runner.js.map +1 -0
  21. package/db/dist/types.d.ts +10 -0
  22. package/db/dist/types.js +3 -0
  23. package/db/dist/types.js.map +1 -0
  24. package/db.knex/dist/index.d.ts +9 -0
  25. package/db.knex/dist/index.js +15 -0
  26. package/db.knex/dist/index.js.map +1 -0
  27. package/db.knex/dist/inject-knex.decorator.d.ts +2 -0
  28. package/db.knex/dist/inject-knex.decorator.js +8 -0
  29. package/db.knex/dist/inject-knex.decorator.js.map +1 -0
  30. package/db.knex/dist/internal/context.d.ts +78 -0
  31. package/db.knex/dist/internal/context.js +233 -0
  32. package/db.knex/dist/internal/context.js.map +1 -0
  33. package/db.knex/dist/internal/exceptions.d.ts +65 -0
  34. package/db.knex/dist/internal/exceptions.js +94 -0
  35. package/db.knex/dist/internal/exceptions.js.map +1 -0
  36. package/db.knex/dist/internal/initializer.d.ts +9 -0
  37. package/db.knex/dist/internal/initializer.js +48 -0
  38. package/db.knex/dist/internal/initializer.js.map +1 -0
  39. package/db.knex/dist/internal/strategy.d.ts +18 -0
  40. package/db.knex/dist/internal/strategy.js +75 -0
  41. package/db.knex/dist/internal/strategy.js.map +1 -0
  42. package/db.knex/dist/internal/test.e2e/strategy.test.d.ts +1 -0
  43. package/db.knex/dist/internal/test.e2e/strategy.test.js +762 -0
  44. package/db.knex/dist/internal/test.e2e/strategy.test.js.map +1 -0
  45. package/db.knex/dist/internal/test.e2e/utils.d.ts +17 -0
  46. package/db.knex/dist/internal/test.e2e/utils.js +21 -0
  47. package/db.knex/dist/internal/test.e2e/utils.js.map +1 -0
  48. package/db.knex/dist/knex.module.d.ts +5 -0
  49. package/db.knex/dist/knex.module.js +34 -0
  50. package/db.knex/dist/knex.module.js.map +1 -0
  51. package/db.knex/dist/knex.plugin.d.ts +10 -0
  52. package/db.knex/dist/knex.plugin.js +20 -0
  53. package/db.knex/dist/knex.plugin.js.map +1 -0
  54. package/db.knex/dist/providers/index.d.ts +4 -0
  55. package/db.knex/dist/providers/index.js +21 -0
  56. package/db.knex/dist/providers/index.js.map +1 -0
  57. package/db.knex/dist/providers/knex-logger.adapter.d.ts +10 -0
  58. package/db.knex/dist/providers/knex-logger.adapter.js +45 -0
  59. package/db.knex/dist/providers/knex-logger.adapter.js.map +1 -0
  60. package/db.knex/dist/providers/knex.config.d.ts +10 -0
  61. package/db.knex/dist/providers/knex.config.js +76 -0
  62. package/db.knex/dist/providers/knex.config.js.map +1 -0
  63. package/db.knex/dist/providers/knex.factory.d.ts +13 -0
  64. package/db.knex/dist/providers/knex.factory.js +96 -0
  65. package/db.knex/dist/providers/knex.factory.js.map +1 -0
  66. package/db.knex/dist/providers/knex.indicator.d.ts +8 -0
  67. package/db.knex/dist/providers/knex.indicator.js +41 -0
  68. package/db.knex/dist/providers/knex.indicator.js.map +1 -0
  69. package/db.knex/dist/types.d.ts +12 -0
  70. package/db.knex/dist/types.js +20 -0
  71. package/db.knex/dist/types.js.map +1 -0
  72. package/db.typeorm/dist/data-source-extractor.d.ts +20 -0
  73. package/db.typeorm/dist/data-source-extractor.js +43 -0
  74. package/db.typeorm/dist/data-source-extractor.js.map +1 -0
  75. package/db.typeorm/dist/index.d.ts +8 -0
  76. package/db.typeorm/dist/index.js +12 -0
  77. package/db.typeorm/dist/index.js.map +1 -0
  78. package/db.typeorm/dist/internal/context.d.ts +46 -0
  79. package/db.typeorm/dist/internal/context.js +123 -0
  80. package/db.typeorm/dist/internal/context.js.map +1 -0
  81. package/db.typeorm/dist/internal/exceptions.d.ts +67 -0
  82. package/db.typeorm/dist/internal/exceptions.js +99 -0
  83. package/db.typeorm/dist/internal/exceptions.js.map +1 -0
  84. package/db.typeorm/dist/internal/initializer.d.ts +15 -0
  85. package/db.typeorm/dist/internal/initializer.js +158 -0
  86. package/db.typeorm/dist/internal/initializer.js.map +1 -0
  87. package/db.typeorm/dist/internal/strategy.d.ts +54 -0
  88. package/db.typeorm/dist/internal/strategy.js +108 -0
  89. package/db.typeorm/dist/internal/strategy.js.map +1 -0
  90. package/db.typeorm/dist/internal/test/strategy.test.e2e.d.ts +5 -0
  91. package/db.typeorm/dist/internal/test/strategy.test.e2e.js +779 -0
  92. package/db.typeorm/dist/internal/test/strategy.test.e2e.js.map +1 -0
  93. package/db.typeorm/dist/internal/test/util.d.ts +22 -0
  94. package/db.typeorm/dist/internal/test/util.js +72 -0
  95. package/db.typeorm/dist/internal/test/util.js.map +1 -0
  96. package/db.typeorm/dist/modules/migration-checker.module.d.ts +9 -0
  97. package/db.typeorm/dist/modules/migration-checker.module.js +42 -0
  98. package/db.typeorm/dist/modules/migration-checker.module.js.map +1 -0
  99. package/db.typeorm/dist/providers/index.d.ts +3 -0
  100. package/db.typeorm/dist/providers/index.js +20 -0
  101. package/db.typeorm/dist/providers/index.js.map +1 -0
  102. package/db.typeorm/dist/providers/typeorm-logger.adapter.d.ts +13 -0
  103. package/db.typeorm/dist/providers/typeorm-logger.adapter.js +61 -0
  104. package/db.typeorm/dist/providers/typeorm-logger.adapter.js.map +1 -0
  105. package/db.typeorm/dist/providers/typeorm.config.d.ts +10 -0
  106. package/db.typeorm/dist/providers/typeorm.config.js +79 -0
  107. package/db.typeorm/dist/providers/typeorm.config.js.map +1 -0
  108. package/db.typeorm/dist/providers/typeorm.healthcheck.d.ts +8 -0
  109. package/db.typeorm/dist/providers/typeorm.healthcheck.js +36 -0
  110. package/db.typeorm/dist/providers/typeorm.healthcheck.js.map +1 -0
  111. package/db.typeorm/dist/typeorm.errors-transformer.d.ts +5 -0
  112. package/db.typeorm/dist/typeorm.errors-transformer.js +32 -0
  113. package/db.typeorm/dist/typeorm.errors-transformer.js.map +1 -0
  114. package/db.typeorm/dist/typeorm.plugin.d.ts +17 -0
  115. package/db.typeorm/dist/typeorm.plugin.js +81 -0
  116. package/db.typeorm/dist/typeorm.plugin.js.map +1 -0
  117. package/db.typeorm/dist/types.d.ts +14 -0
  118. package/db.typeorm/dist/types.js +18 -0
  119. package/db.typeorm/dist/types.js.map +1 -0
  120. package/dist/context.base.d.ts +30 -0
  121. package/dist/context.base.js +52 -0
  122. package/dist/context.base.js.map +1 -0
  123. package/dist/context.storage.d.ts +22 -0
  124. package/dist/context.storage.js +32 -0
  125. package/dist/context.storage.js.map +1 -0
  126. package/dist/exceptions.d.ts +10 -0
  127. package/dist/exceptions.js +25 -0
  128. package/dist/exceptions.js.map +1 -0
  129. package/dist/index.d.ts +6 -0
  130. package/dist/index.js +16 -0
  131. package/dist/index.js.map +1 -0
  132. package/dist/propagation.enum.d.ts +30 -0
  133. package/dist/propagation.enum.js +35 -0
  134. package/dist/propagation.enum.js.map +1 -0
  135. package/dist/transactional.runner.d.ts +29 -0
  136. package/dist/transactional.runner.js +119 -0
  137. package/dist/transactional.runner.js.map +1 -0
  138. package/dist/types.d.ts +10 -0
  139. package/dist/types.js +3 -0
  140. package/dist/types.js.map +1 -0
  141. package/package.json +21 -0
  142. package/src/context.base.ts +61 -0
  143. package/src/context.storage.ts +34 -0
  144. package/src/exceptions.ts +21 -0
  145. package/src/index.ts +10 -0
  146. package/src/propagation.enum.ts +36 -0
  147. package/src/transactional.runner.ts +222 -0
  148. package/src/types.ts +37 -0
  149. package/tsconfig.json +11 -0
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ export { ContextStorage } from './context.storage';
2
+ export { BaseContext } from './context.base';
3
+ export { TransactionalStrategy, CallBack } from './types';
4
+ export {
5
+ NeverRunningInTransaction,
6
+ CallOutOfContextWithMandatory,
7
+ IncompatibleIsolationLevels,
8
+ } from './exceptions';
9
+ export { TransactionRunner } from './transactional.runner';
10
+ export { Propagation } from './propagation.enum';
@@ -0,0 +1,36 @@
1
+ export enum Propagation {
2
+ /**
3
+ * Support a current transaction, throw an exception if none exists.
4
+ */
5
+ MANDATORY,
6
+
7
+ /**
8
+ * Execute within a nested transaction if a current transaction exists, behave like REQUIRED else.
9
+ */
10
+ NESTED,
11
+
12
+ /**
13
+ * Execute non-transactionally, throw an exception if a transaction exists.
14
+ */
15
+ NEVER,
16
+
17
+ /**
18
+ * Execute non-transactionally, suspend the current transaction if one exists.
19
+ */
20
+ NOT_SUPPORTED,
21
+
22
+ /**
23
+ * (default behaviour) - Support a current transaction, create a new one if none exists.
24
+ */
25
+ REQUIRED,
26
+
27
+ /**
28
+ * Create a new transaction, and suspend the current transaction if one exists.
29
+ */
30
+ REQUIRES_NEW,
31
+
32
+ /**
33
+ * Support a current transaction, execute non-transactionally if none exists.
34
+ */
35
+ SUPPORTS,
36
+ }
@@ -0,0 +1,222 @@
1
+ import { unreachable } from '@rsdk/common';
2
+ import { redecorate } from '@rsdk/decorators';
3
+
4
+ import type { BaseContext } from './context.base';
5
+ import {
6
+ CallOutOfContextWithMandatory,
7
+ NeverRunningInTransaction,
8
+ } from './exceptions';
9
+ import { Propagation } from './propagation.enum';
10
+ import type { TransactionalStrategy } from './types';
11
+
12
+ export class TransactionRunner<
13
+ TIsolation extends string,
14
+ TContext extends BaseContext<TIsolation>,
15
+ > {
16
+ private static defaultPropagation = Propagation.REQUIRED;
17
+
18
+ constructor(
19
+ private readonly strategy: TransactionalStrategy<TIsolation, TContext>,
20
+ ) {}
21
+
22
+ /**
23
+ * Вызовы метода будут обернуты в transactional context
24
+ */
25
+ // eslint-disable-next-line @typescript-eslint/naming-convention
26
+ Transactional(payload?: {
27
+ isolation?: TIsolation;
28
+ propagation?: Propagation;
29
+ }): MethodDecorator {
30
+ const { isolation, propagation = TransactionRunner.defaultPropagation } =
31
+ payload ?? {};
32
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
33
+ const runner = this;
34
+
35
+ return function (_, __, descriptor) {
36
+ // eslint-disable-next-line @typescript-eslint/ban-types
37
+ const original = descriptor.value as Function;
38
+
39
+ descriptor.value = function (this: any, ...args: any[]): any {
40
+ const bindedOriginal = original.bind(this, ...args);
41
+
42
+ return runner.bindTransactional(bindedOriginal, {
43
+ isolation: isolation,
44
+ propagation,
45
+ })();
46
+ } as any;
47
+
48
+ Object.defineProperty(descriptor.value, 'name', {
49
+ value: original.name,
50
+ writable: false,
51
+ });
52
+
53
+ redecorate(original, descriptor.value as object);
54
+ };
55
+ }
56
+
57
+ transactional<TResult>(...args: [fn: () => TResult]): Promise<TResult>;
58
+ transactional<TResult>(
59
+ ...args: [isolation: TIsolation, fn: () => TResult]
60
+ ): Promise<TResult>;
61
+ transactional<TResult>(
62
+ ...args:
63
+ | [fn: () => TResult]
64
+ | [
65
+ runOptions: {
66
+ isolation: TIsolation;
67
+ propagation: Propagation;
68
+ },
69
+ fn: () => TResult,
70
+ ]
71
+ ): Promise<TResult>;
72
+
73
+ transactional<TResult>(
74
+ ...args:
75
+ | [fn: () => TResult]
76
+ | [options: TIsolation, fn: () => TResult]
77
+ | [
78
+ runOptions: {
79
+ isolation: TIsolation;
80
+ propagation: Propagation;
81
+ },
82
+ fn: () => TResult,
83
+ ]
84
+ ): Promise<TResult> {
85
+ const { fn, isolation, propagation } = this.parseRunArgs(args);
86
+
87
+ return this.bindTransactional(fn, {
88
+ propagation,
89
+ isolation,
90
+ })();
91
+ }
92
+
93
+ /**
94
+ * Вернет функцию, которая вызовет переданный коллбек в transactional context
95
+ */
96
+ private bindTransactional<TResult, TArgs extends any[]>(
97
+ fn: (...args: TArgs) => TResult,
98
+ payload?: {
99
+ isolation?: TIsolation | undefined;
100
+ propagation?: Propagation | undefined;
101
+ },
102
+ ): (...args: TArgs) => Promise<TResult> {
103
+ const { propagation = TransactionRunner.defaultPropagation, isolation } =
104
+ payload ?? {};
105
+
106
+ return async (...args: TArgs): Promise<TResult> => {
107
+ const bindedFn = (): TResult => fn(...args);
108
+
109
+ const currentContext = this.strategy.getContext();
110
+
111
+ switch (propagation) {
112
+ case Propagation.REQUIRES_NEW:
113
+ return this.strategy.runFromNewContext(isolation, fn, ...args);
114
+
115
+ case Propagation.NEVER:
116
+ if (currentContext && !currentContext.suspended) {
117
+ throw new NeverRunningInTransaction();
118
+ }
119
+
120
+ if (currentContext) {
121
+ return bindedFn();
122
+ }
123
+
124
+ return this.strategy.runOutTransactionalContext(fn, ...args);
125
+
126
+ case Propagation.MANDATORY:
127
+ if (!currentContext || currentContext.suspended) {
128
+ throw new CallOutOfContextWithMandatory();
129
+ }
130
+
131
+ currentContext.assertIsolationCompatibility(isolation);
132
+
133
+ return bindedFn();
134
+
135
+ case Propagation.NESTED:
136
+ if (!currentContext || currentContext.suspended) {
137
+ return await this.strategy.runFromNewContext(
138
+ isolation,
139
+ fn,
140
+ ...args,
141
+ );
142
+ }
143
+
144
+ return await this.strategy.nestedExecute(
145
+ currentContext,
146
+ isolation,
147
+ fn,
148
+ ...args,
149
+ );
150
+
151
+ case Propagation.NOT_SUPPORTED:
152
+ if (currentContext?.suspended) {
153
+ return bindedFn();
154
+ }
155
+
156
+ return this.strategy.runOutTransactionalContext(fn, ...args);
157
+
158
+ case Propagation.REQUIRED:
159
+ if (!currentContext || currentContext.suspended) {
160
+ return await this.strategy.runFromNewContext(
161
+ isolation,
162
+ fn,
163
+ ...args,
164
+ );
165
+ }
166
+
167
+ currentContext.assertIsolationCompatibility(isolation);
168
+
169
+ return bindedFn();
170
+
171
+ case Propagation.SUPPORTS:
172
+ return bindedFn();
173
+
174
+ default:
175
+ return unreachable(propagation);
176
+ }
177
+ };
178
+ }
179
+
180
+ private parseRunArgs<TResult>(
181
+ args:
182
+ | [fn: () => TResult]
183
+ | [isolation: TIsolation, fn: () => TResult]
184
+ | [
185
+ runOptions: {
186
+ isolation: TIsolation;
187
+ propagation: Propagation;
188
+ },
189
+ fn: () => TResult,
190
+ ],
191
+ ): {
192
+ fn: () => TResult;
193
+ isolation?: TIsolation;
194
+ propagation: Propagation;
195
+ } {
196
+ if (args.length === 1) {
197
+ return {
198
+ fn: args[0],
199
+ propagation: TransactionRunner.defaultPropagation,
200
+ };
201
+ }
202
+
203
+ if (
204
+ typeof args[0] === 'object' &&
205
+ args[0] !== null &&
206
+ 'isolation' in args[0] &&
207
+ 'propagation' in args[0]
208
+ ) {
209
+ return {
210
+ isolation: args[0].isolation,
211
+ fn: args[1],
212
+ propagation: args[0].propagation,
213
+ };
214
+ }
215
+
216
+ return {
217
+ isolation: args[0],
218
+ fn: args[1],
219
+ propagation: TransactionRunner.defaultPropagation,
220
+ };
221
+ }
222
+ }
package/src/types.ts ADDED
@@ -0,0 +1,37 @@
1
+ import type { BaseContext } from './context.base';
2
+
3
+ export type CallBack<TArgs extends any[], TResult> = (
4
+ ...args: TArgs
5
+ ) => TResult;
6
+
7
+ export interface TransactionalStrategy<
8
+ TIsolation extends string,
9
+ TContext extends BaseContext<TIsolation>,
10
+ > {
11
+ bindContext<TResult, TArgs extends any[]>(
12
+ fn: CallBack<TArgs, TResult>,
13
+ options?: TIsolation,
14
+ ): CallBack<TArgs, Promise<TResult>>;
15
+
16
+ isRunning(): boolean;
17
+
18
+ getContext(): TContext | undefined;
19
+
20
+ runFromNewContext<TArgs extends any[], TResult>(
21
+ options: TIsolation | undefined,
22
+ fn: (...args: TArgs) => TResult,
23
+ ...args: any[]
24
+ ): Promise<TResult>;
25
+
26
+ runOutTransactionalContext<TArgs extends any[], TResult>(
27
+ fn: (...args: TArgs) => TResult,
28
+ ...args: any[]
29
+ ): Promise<TResult>;
30
+
31
+ nestedExecute<TArgs extends any[], TResult>(
32
+ currentContext: TContext,
33
+ options: TIsolation | undefined,
34
+ fn: (...args: TArgs) => TResult,
35
+ ...param4: any[]
36
+ ): Promise<TResult>;
37
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+
3
+ "extends": "@rsdk/tsconfig/base.json",
4
+ "compilerOptions": {
5
+ "declaration": true,
6
+ "outDir": "dist",
7
+ "incremental": false
8
+ },
9
+ "include": ["src/**/*"],
10
+ "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
11
+ }