adaptic-backend 1.0.340 → 1.0.341

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 (110) hide show
  1. package/Account.cjs +2543 -2265
  2. package/Account.d.ts +19 -2
  3. package/Action.cjs +1116 -838
  4. package/Action.d.ts +19 -2
  5. package/Alert.cjs +2626 -2348
  6. package/Alert.d.ts +19 -2
  7. package/Allocation.cjs +2578 -2300
  8. package/Allocation.d.ts +19 -2
  9. package/AlpacaAccount.cjs +2383 -2105
  10. package/AlpacaAccount.d.ts +19 -2
  11. package/Asset.cjs +1813 -1535
  12. package/Asset.d.ts +19 -2
  13. package/Authenticator.cjs +2568 -2290
  14. package/Authenticator.d.ts +19 -2
  15. package/Customer.cjs +2495 -2217
  16. package/Customer.d.ts +19 -2
  17. package/EconomicEvent.cjs +645 -367
  18. package/EconomicEvent.d.ts +19 -2
  19. package/MarketSentiment.cjs +618 -340
  20. package/MarketSentiment.d.ts +19 -2
  21. package/NewsArticle.cjs +1933 -1655
  22. package/NewsArticle.d.ts +19 -2
  23. package/NewsArticleAssetSentiment.cjs +1842 -1564
  24. package/NewsArticleAssetSentiment.d.ts +19 -2
  25. package/ScheduledOptionOrder.cjs +594 -316
  26. package/ScheduledOptionOrder.d.ts +19 -2
  27. package/Session.cjs +2570 -2292
  28. package/Session.d.ts +19 -2
  29. package/Trade.cjs +1073 -795
  30. package/Trade.d.ts +19 -2
  31. package/User.cjs +2288 -2010
  32. package/User.d.ts +19 -2
  33. package/VerificationToken.cjs +606 -328
  34. package/VerificationToken.d.ts +19 -2
  35. package/esm/Account.d.ts +19 -2
  36. package/esm/Account.d.ts.map +1 -1
  37. package/esm/Account.js.map +1 -1
  38. package/esm/Account.mjs +2534 -2263
  39. package/esm/Action.d.ts +19 -2
  40. package/esm/Action.d.ts.map +1 -1
  41. package/esm/Action.js.map +1 -1
  42. package/esm/Action.mjs +1107 -836
  43. package/esm/Alert.d.ts +19 -2
  44. package/esm/Alert.d.ts.map +1 -1
  45. package/esm/Alert.js.map +1 -1
  46. package/esm/Alert.mjs +2617 -2346
  47. package/esm/Allocation.d.ts +19 -2
  48. package/esm/Allocation.d.ts.map +1 -1
  49. package/esm/Allocation.js.map +1 -1
  50. package/esm/Allocation.mjs +2569 -2298
  51. package/esm/AlpacaAccount.d.ts +19 -2
  52. package/esm/AlpacaAccount.d.ts.map +1 -1
  53. package/esm/AlpacaAccount.js.map +1 -1
  54. package/esm/AlpacaAccount.mjs +2374 -2103
  55. package/esm/Asset.d.ts +19 -2
  56. package/esm/Asset.d.ts.map +1 -1
  57. package/esm/Asset.js.map +1 -1
  58. package/esm/Asset.mjs +1804 -1533
  59. package/esm/Authenticator.d.ts +19 -2
  60. package/esm/Authenticator.d.ts.map +1 -1
  61. package/esm/Authenticator.js.map +1 -1
  62. package/esm/Authenticator.mjs +2559 -2288
  63. package/esm/Customer.d.ts +19 -2
  64. package/esm/Customer.d.ts.map +1 -1
  65. package/esm/Customer.js.map +1 -1
  66. package/esm/Customer.mjs +2486 -2215
  67. package/esm/EconomicEvent.d.ts +19 -2
  68. package/esm/EconomicEvent.d.ts.map +1 -1
  69. package/esm/EconomicEvent.js.map +1 -1
  70. package/esm/EconomicEvent.mjs +636 -365
  71. package/esm/MarketSentiment.d.ts +19 -2
  72. package/esm/MarketSentiment.d.ts.map +1 -1
  73. package/esm/MarketSentiment.js.map +1 -1
  74. package/esm/MarketSentiment.mjs +609 -338
  75. package/esm/NewsArticle.d.ts +19 -2
  76. package/esm/NewsArticle.d.ts.map +1 -1
  77. package/esm/NewsArticle.js.map +1 -1
  78. package/esm/NewsArticle.mjs +1924 -1653
  79. package/esm/NewsArticleAssetSentiment.d.ts +19 -2
  80. package/esm/NewsArticleAssetSentiment.d.ts.map +1 -1
  81. package/esm/NewsArticleAssetSentiment.js.map +1 -1
  82. package/esm/NewsArticleAssetSentiment.mjs +1833 -1562
  83. package/esm/ScheduledOptionOrder.d.ts +19 -2
  84. package/esm/ScheduledOptionOrder.d.ts.map +1 -1
  85. package/esm/ScheduledOptionOrder.js.map +1 -1
  86. package/esm/ScheduledOptionOrder.mjs +585 -314
  87. package/esm/Session.d.ts +19 -2
  88. package/esm/Session.d.ts.map +1 -1
  89. package/esm/Session.js.map +1 -1
  90. package/esm/Session.mjs +2561 -2290
  91. package/esm/Trade.d.ts +19 -2
  92. package/esm/Trade.d.ts.map +1 -1
  93. package/esm/Trade.js.map +1 -1
  94. package/esm/Trade.mjs +1064 -793
  95. package/esm/User.d.ts +19 -2
  96. package/esm/User.d.ts.map +1 -1
  97. package/esm/User.js.map +1 -1
  98. package/esm/User.mjs +2279 -2008
  99. package/esm/VerificationToken.d.ts +19 -2
  100. package/esm/VerificationToken.d.ts.map +1 -1
  101. package/esm/VerificationToken.js.map +1 -1
  102. package/esm/VerificationToken.mjs +597 -326
  103. package/esm/prismaClient.d.ts +4 -5
  104. package/esm/prismaClient.d.ts.map +1 -1
  105. package/esm/prismaClient.js.map +1 -1
  106. package/esm/prismaClient.mjs +13 -11
  107. package/package.json +1 -1
  108. package/prismaClient.cjs +13 -11
  109. package/prismaClient.d.ts +4 -5
  110. package/server.cjs +37 -5
package/Action.cjs CHANGED
@@ -27,939 +27,1217 @@ exports.Action = {
27
27
  * @param client - Apollo Client instance.
28
28
  * @returns The created Action or null.
29
29
  */
30
+ /**
31
+ * Create a new Action record.
32
+ * Enhanced with connection resilience against Prisma connection errors.
33
+ * @param props - Properties for the new record.
34
+ * @param globalClient - Apollo Client instance.
35
+ * @returns The created Action or null.
36
+ */
30
37
  async create(props, globalClient) {
31
- const [modules, client] = await Promise.all([
32
- (0, client_1.getApolloModules)(),
33
- globalClient
34
- ? Promise.resolve(globalClient)
35
- : client_1.client
36
- ]);
37
- const { gql, ApolloError } = modules;
38
- const CREATE_ONE_ACTION = gql `
39
- mutation createOneAction($data: ActionCreateInput!) {
40
- createOneAction(data: $data) {
41
- ${selectionSet}
42
- }
43
- }
44
- `;
45
- const variables = {
46
- data: {
47
- sequence: props.sequence !== undefined ? props.sequence : undefined,
48
- type: props.type !== undefined ? props.type : undefined,
49
- primary: props.primary !== undefined ? props.primary : undefined,
50
- note: props.note !== undefined ? props.note : undefined,
51
- status: props.status !== undefined ? props.status : undefined,
52
- alpacaOrderId: props.alpacaOrderId !== undefined ? props.alpacaOrderId : undefined,
53
- trade: props.trade ?
54
- typeof props.trade === 'object' && Object.keys(props.trade).length === 1 && Object.keys(props.trade)[0] === 'id'
55
- ? { connect: {
56
- id: props.trade.id
57
- }
58
- }
59
- : { connectOrCreate: {
60
- where: {
61
- id: props.trade.id !== undefined ? props.trade.id : undefined,
62
- alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
63
- equals: props.trade.alpacaAccountId
64
- } : undefined,
65
- symbol: props.trade.symbol !== undefined ? {
66
- equals: props.trade.symbol
67
- } : undefined,
68
- },
69
- create: {
70
- alpacaAccountId: props.trade.alpacaAccountId !== undefined ? props.trade.alpacaAccountId : undefined,
71
- signal: props.trade.signal !== undefined ? props.trade.signal : undefined,
72
- strategy: props.trade.strategy !== undefined ? props.trade.strategy : undefined,
73
- analysis: props.trade.analysis !== undefined ? props.trade.analysis : undefined,
74
- summary: props.trade.summary !== undefined ? props.trade.summary : undefined,
75
- confidence: props.trade.confidence !== undefined ? props.trade.confidence : undefined,
76
- timestamp: props.trade.timestamp !== undefined ? props.trade.timestamp : undefined,
77
- status: props.trade.status !== undefined ? props.trade.status : undefined,
78
- symbol: props.trade.symbol !== undefined ? props.trade.symbol : undefined,
79
- entryPrice: props.trade.entryPrice !== undefined ? props.trade.entryPrice : undefined,
80
- exitPrice: props.trade.exitPrice !== undefined ? props.trade.exitPrice : undefined,
81
- entryQty: props.trade.entryQty !== undefined ? props.trade.entryQty : undefined,
82
- exitQty: props.trade.exitQty !== undefined ? props.trade.exitQty : undefined,
83
- entryValue: props.trade.entryValue !== undefined ? props.trade.entryValue : undefined,
84
- exitValue: props.trade.exitValue !== undefined ? props.trade.exitValue : undefined,
85
- entryTime: props.trade.entryTime !== undefined ? props.trade.entryTime : undefined,
86
- exitTime: props.trade.exitTime !== undefined ? props.trade.exitTime : undefined,
87
- pnlAmount: props.trade.pnlAmount !== undefined ? props.trade.pnlAmount : undefined,
88
- pnlPercent: props.trade.pnlPercent !== undefined ? props.trade.pnlPercent : undefined,
89
- durationMinutes: props.trade.durationMinutes !== undefined ? props.trade.durationMinutes : undefined,
90
- marketPhase: props.trade.marketPhase !== undefined ? props.trade.marketPhase : undefined,
91
- marketVolatility: props.trade.marketVolatility !== undefined ? props.trade.marketVolatility : undefined,
92
- thresholdsJson: props.trade.thresholdsJson !== undefined ? props.trade.thresholdsJson : undefined,
93
- },
94
- }
95
- } : undefined,
96
- },
97
- };
98
- const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
99
- try {
100
- const response = await client.mutate({ mutation: CREATE_ONE_ACTION, variables: filteredVariables });
101
- if (response.errors && response.errors.length > 0)
102
- throw new Error(response.errors[0].message);
103
- if (response && response.data && response.data.createOneAction) {
104
- return response.data.createOneAction;
38
+ var _a, _b, _c, _d, _e;
39
+ // Maximum number of retries for database connection issues
40
+ const MAX_RETRIES = 3;
41
+ let retryCount = 0;
42
+ let lastError = null;
43
+ // Retry loop to handle potential database connection issues
44
+ while (retryCount < MAX_RETRIES) {
45
+ try {
46
+ const [modules, client] = await Promise.all([
47
+ (0, client_1.getApolloModules)(),
48
+ globalClient
49
+ ? Promise.resolve(globalClient)
50
+ : client_1.client
51
+ ]);
52
+ const { gql, ApolloError } = modules;
53
+ const CREATE_ONE_ACTION = gql `
54
+ mutation createOneAction($data: ActionCreateInput!) {
55
+ createOneAction(data: $data) {
56
+ ${selectionSet}
57
+ }
58
+ }
59
+ `;
60
+ const variables = {
61
+ data: {
62
+ sequence: props.sequence !== undefined ? props.sequence : undefined,
63
+ type: props.type !== undefined ? props.type : undefined,
64
+ primary: props.primary !== undefined ? props.primary : undefined,
65
+ note: props.note !== undefined ? props.note : undefined,
66
+ status: props.status !== undefined ? props.status : undefined,
67
+ alpacaOrderId: props.alpacaOrderId !== undefined ? props.alpacaOrderId : undefined,
68
+ trade: props.trade ?
69
+ typeof props.trade === 'object' && Object.keys(props.trade).length === 1 && Object.keys(props.trade)[0] === 'id'
70
+ ? { connect: {
71
+ id: props.trade.id
72
+ }
73
+ }
74
+ : { connectOrCreate: {
75
+ where: {
76
+ id: props.trade.id !== undefined ? props.trade.id : undefined,
77
+ alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
78
+ equals: props.trade.alpacaAccountId
79
+ } : undefined,
80
+ symbol: props.trade.symbol !== undefined ? {
81
+ equals: props.trade.symbol
82
+ } : undefined,
83
+ },
84
+ create: {
85
+ alpacaAccountId: props.trade.alpacaAccountId !== undefined ? props.trade.alpacaAccountId : undefined,
86
+ signal: props.trade.signal !== undefined ? props.trade.signal : undefined,
87
+ strategy: props.trade.strategy !== undefined ? props.trade.strategy : undefined,
88
+ analysis: props.trade.analysis !== undefined ? props.trade.analysis : undefined,
89
+ summary: props.trade.summary !== undefined ? props.trade.summary : undefined,
90
+ confidence: props.trade.confidence !== undefined ? props.trade.confidence : undefined,
91
+ timestamp: props.trade.timestamp !== undefined ? props.trade.timestamp : undefined,
92
+ status: props.trade.status !== undefined ? props.trade.status : undefined,
93
+ symbol: props.trade.symbol !== undefined ? props.trade.symbol : undefined,
94
+ entryPrice: props.trade.entryPrice !== undefined ? props.trade.entryPrice : undefined,
95
+ exitPrice: props.trade.exitPrice !== undefined ? props.trade.exitPrice : undefined,
96
+ entryQty: props.trade.entryQty !== undefined ? props.trade.entryQty : undefined,
97
+ exitQty: props.trade.exitQty !== undefined ? props.trade.exitQty : undefined,
98
+ entryValue: props.trade.entryValue !== undefined ? props.trade.entryValue : undefined,
99
+ exitValue: props.trade.exitValue !== undefined ? props.trade.exitValue : undefined,
100
+ entryTime: props.trade.entryTime !== undefined ? props.trade.entryTime : undefined,
101
+ exitTime: props.trade.exitTime !== undefined ? props.trade.exitTime : undefined,
102
+ pnlAmount: props.trade.pnlAmount !== undefined ? props.trade.pnlAmount : undefined,
103
+ pnlPercent: props.trade.pnlPercent !== undefined ? props.trade.pnlPercent : undefined,
104
+ durationMinutes: props.trade.durationMinutes !== undefined ? props.trade.durationMinutes : undefined,
105
+ marketPhase: props.trade.marketPhase !== undefined ? props.trade.marketPhase : undefined,
106
+ marketVolatility: props.trade.marketVolatility !== undefined ? props.trade.marketVolatility : undefined,
107
+ thresholdsJson: props.trade.thresholdsJson !== undefined ? props.trade.thresholdsJson : undefined,
108
+ },
109
+ }
110
+ } : undefined,
111
+ },
112
+ };
113
+ const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
114
+ const response = await client.mutate({
115
+ mutation: CREATE_ONE_ACTION,
116
+ variables: filteredVariables,
117
+ // Don't cache mutations, but ensure we're using the freshest context
118
+ fetchPolicy: 'no-cache'
119
+ });
120
+ if (response.errors && response.errors.length > 0)
121
+ throw new Error(response.errors[0].message);
122
+ if (response && response.data && response.data.createOneAction) {
123
+ return response.data.createOneAction;
124
+ }
125
+ else {
126
+ return null;
127
+ }
105
128
  }
106
- else {
107
- return null;
129
+ catch (error) {
130
+ lastError = error;
131
+ // Check if this is a database connection error that we should retry
132
+ const isConnectionError = ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Server has closed the connection')) ||
133
+ ((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('Cannot reach database server')) ||
134
+ ((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection timed out')) ||
135
+ ((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('Accelerate')) || // Prisma Accelerate proxy errors
136
+ (error.networkError && ((_e = error.networkError.message) === null || _e === void 0 ? void 0 : _e.includes('Failed to fetch')));
137
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
138
+ retryCount++;
139
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
140
+ console.warn("Database connection error, retrying...");
141
+ await new Promise(resolve => setTimeout(resolve, delay));
142
+ continue;
143
+ }
144
+ // Log the error and rethrow
145
+ console.error("Database error occurred:", error);
146
+ throw error;
108
147
  }
109
148
  }
110
- catch (error) {
111
- console.error('Error in createOneAction:', error);
112
- throw error;
113
- }
149
+ // If we exhausted retries, throw the last error
150
+ throw lastError;
114
151
  },
115
152
  /**
116
153
  * Create multiple Action records.
154
+ * Enhanced with connection resilience against Prisma connection errors.
117
155
  * @param props - Array of Action objects for the new records.
118
156
  * @param globalClient - Apollo Client instance.
119
157
  * @returns The count of created records or null.
120
158
  */
121
159
  async createMany(props, globalClient) {
122
- const [modules, client] = await Promise.all([
123
- (0, client_1.getApolloModules)(),
124
- globalClient
125
- ? Promise.resolve(globalClient)
126
- : client_1.client
127
- ]);
128
- const { gql, ApolloError } = modules;
129
- const CREATE_MANY_ACTION = gql `
130
- mutation createManyAction($data: [ActionCreateManyInput!]!) {
131
- createManyAction(data: $data) {
132
- count
133
- }
134
- }`;
135
- const variables = {
136
- data: props.map(prop => ({
137
- sequence: prop.sequence !== undefined ? prop.sequence : undefined,
138
- tradeId: prop.tradeId !== undefined ? prop.tradeId : undefined,
139
- type: prop.type !== undefined ? prop.type : undefined,
140
- primary: prop.primary !== undefined ? prop.primary : undefined,
141
- note: prop.note !== undefined ? prop.note : undefined,
142
- status: prop.status !== undefined ? prop.status : undefined,
143
- alpacaOrderId: prop.alpacaOrderId !== undefined ? prop.alpacaOrderId : undefined,
144
- })),
145
- };
146
- const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
147
- try {
148
- const response = await client.mutate({ mutation: CREATE_MANY_ACTION, variables: filteredVariables });
149
- if (response.errors && response.errors.length > 0)
150
- throw new Error(response.errors[0].message);
151
- if (response && response.data && response.data.createManyAction) {
152
- return response.data.createManyAction;
160
+ var _a, _b, _c, _d, _e;
161
+ // Maximum number of retries for database connection issues
162
+ const MAX_RETRIES = 3;
163
+ let retryCount = 0;
164
+ let lastError = null;
165
+ // Retry loop to handle potential database connection issues
166
+ while (retryCount < MAX_RETRIES) {
167
+ try {
168
+ const [modules, client] = await Promise.all([
169
+ (0, client_1.getApolloModules)(),
170
+ globalClient
171
+ ? Promise.resolve(globalClient)
172
+ : client_1.client
173
+ ]);
174
+ const { gql, ApolloError } = modules;
175
+ const CREATE_MANY_ACTION = gql `
176
+ mutation createManyAction($data: [ActionCreateManyInput!]!) {
177
+ createManyAction(data: $data) {
178
+ count
153
179
  }
154
- else {
155
- return null;
180
+ }`;
181
+ const variables = {
182
+ data: props.map(prop => ({
183
+ sequence: prop.sequence !== undefined ? prop.sequence : undefined,
184
+ tradeId: prop.tradeId !== undefined ? prop.tradeId : undefined,
185
+ type: prop.type !== undefined ? prop.type : undefined,
186
+ primary: prop.primary !== undefined ? prop.primary : undefined,
187
+ note: prop.note !== undefined ? prop.note : undefined,
188
+ status: prop.status !== undefined ? prop.status : undefined,
189
+ alpacaOrderId: prop.alpacaOrderId !== undefined ? prop.alpacaOrderId : undefined,
190
+ })),
191
+ };
192
+ const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
193
+ const response = await client.mutate({
194
+ mutation: CREATE_MANY_ACTION,
195
+ variables: filteredVariables,
196
+ // Don't cache mutations, but ensure we're using the freshest context
197
+ fetchPolicy: 'no-cache'
198
+ });
199
+ if (response.errors && response.errors.length > 0)
200
+ throw new Error(response.errors[0].message);
201
+ if (response && response.data && response.data.createManyAction) {
202
+ return response.data.createManyAction;
203
+ }
204
+ else {
205
+ return null;
206
+ }
207
+ }
208
+ catch (error) {
209
+ lastError = error;
210
+ // Check if this is a database connection error that we should retry
211
+ const isConnectionError = ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Server has closed the connection')) ||
212
+ ((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('Cannot reach database server')) ||
213
+ ((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection timed out')) ||
214
+ ((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('Accelerate')) || // Prisma Accelerate proxy errors
215
+ (error.networkError && ((_e = error.networkError.message) === null || _e === void 0 ? void 0 : _e.includes('Failed to fetch')));
216
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
217
+ retryCount++;
218
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
219
+ console.warn("Database connection error, retrying...");
220
+ await new Promise(resolve => setTimeout(resolve, delay));
221
+ continue;
222
+ }
223
+ // Log the error and rethrow
224
+ console.error("Database error occurred:", error);
225
+ throw error;
156
226
  }
157
227
  }
158
- catch (error) {
159
- console.error('Error in createManyAction:', error);
160
- throw error;
161
- }
228
+ // If we exhausted retries, throw the last error
229
+ throw lastError;
162
230
  },
163
231
  /**
164
232
  * Update a single Action record.
233
+ * Enhanced with connection resilience against Prisma connection errors.
165
234
  * @param props - Properties to update.
166
235
  * @param globalClient - Apollo Client instance.
167
236
  * @returns The updated Action or null.
168
237
  */
169
238
  async update(props, globalClient) {
170
- const [modules, client] = await Promise.all([
171
- (0, client_1.getApolloModules)(),
172
- globalClient
173
- ? Promise.resolve(globalClient)
174
- : client_1.client
175
- ]);
176
- const { gql, ApolloError } = modules;
177
- const UPDATE_ONE_ACTION = gql `
178
- mutation updateOneAction($data: ActionUpdateInput!, $where: ActionWhereUniqueInput!) {
179
- updateOneAction(data: $data, where: $where) {
180
- ${selectionSet}
181
- }
182
- }`;
183
- const variables = {
184
- where: {
185
- id: props.id !== undefined ? props.id : undefined,
186
- alpacaOrderId: props.alpacaOrderId !== undefined ? props.alpacaOrderId : undefined,
187
- tradeId: props.tradeId !== undefined ? {
188
- equals: props.tradeId
189
- } : undefined,
190
- },
191
- data: {
192
- id: props.id !== undefined ? {
193
- set: props.id
194
- } : undefined,
195
- sequence: props.sequence !== undefined ? {
196
- set: props.sequence
197
- } : undefined,
198
- type: props.type !== undefined ? {
199
- set: props.type
200
- } : undefined,
201
- primary: props.primary !== undefined ? {
202
- set: props.primary
203
- } : undefined,
204
- note: props.note !== undefined ? {
205
- set: props.note
206
- } : undefined,
207
- status: props.status !== undefined ? {
208
- set: props.status
209
- } : undefined,
210
- createdAt: props.createdAt !== undefined ? {
211
- set: props.createdAt
212
- } : undefined,
213
- updatedAt: props.updatedAt !== undefined ? {
214
- set: props.updatedAt
215
- } : undefined,
216
- alpacaOrderId: props.alpacaOrderId !== undefined ? {
217
- set: props.alpacaOrderId
218
- } : undefined,
219
- trade: props.trade ?
220
- typeof props.trade === 'object' && Object.keys(props.trade).length === 1 && (Object.keys(props.trade)[0] === 'id' || Object.keys(props.trade)[0] === 'symbol')
221
- ? {
222
- connect: {
223
- id: props.trade.id
224
- }
225
- } : { upsert: {
226
- where: {
227
- id: props.trade.id !== undefined ? {
228
- equals: props.trade.id
229
- } : undefined,
230
- alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
231
- equals: props.trade.alpacaAccountId
232
- } : undefined,
233
- symbol: props.trade.symbol !== undefined ? {
234
- equals: props.trade.symbol
235
- } : undefined,
236
- },
237
- update: {
238
- id: props.trade.id !== undefined ? {
239
- set: props.trade.id
240
- } : undefined,
241
- alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
242
- set: props.trade.alpacaAccountId
243
- } : undefined,
244
- signal: props.trade.signal !== undefined ? {
245
- set: props.trade.signal
246
- } : undefined,
247
- strategy: props.trade.strategy !== undefined ? {
248
- set: props.trade.strategy
249
- } : undefined,
250
- analysis: props.trade.analysis !== undefined ? {
251
- set: props.trade.analysis
252
- } : undefined,
253
- summary: props.trade.summary !== undefined ? {
254
- set: props.trade.summary
255
- } : undefined,
256
- confidence: props.trade.confidence !== undefined ? {
257
- set: props.trade.confidence
258
- } : undefined,
259
- timestamp: props.trade.timestamp !== undefined ? {
260
- set: props.trade.timestamp
261
- } : undefined,
262
- status: props.trade.status !== undefined ? {
263
- set: props.trade.status
264
- } : undefined,
265
- symbol: props.trade.symbol !== undefined ? {
266
- set: props.trade.symbol
267
- } : undefined,
268
- entryPrice: props.trade.entryPrice !== undefined ? {
269
- set: props.trade.entryPrice
270
- } : undefined,
271
- exitPrice: props.trade.exitPrice !== undefined ? {
272
- set: props.trade.exitPrice
273
- } : undefined,
274
- entryQty: props.trade.entryQty !== undefined ? {
275
- set: props.trade.entryQty
276
- } : undefined,
277
- exitQty: props.trade.exitQty !== undefined ? {
278
- set: props.trade.exitQty
279
- } : undefined,
280
- entryValue: props.trade.entryValue !== undefined ? {
281
- set: props.trade.entryValue
282
- } : undefined,
283
- exitValue: props.trade.exitValue !== undefined ? {
284
- set: props.trade.exitValue
285
- } : undefined,
286
- entryTime: props.trade.entryTime !== undefined ? {
287
- set: props.trade.entryTime
288
- } : undefined,
289
- exitTime: props.trade.exitTime !== undefined ? {
290
- set: props.trade.exitTime
291
- } : undefined,
292
- pnlAmount: props.trade.pnlAmount !== undefined ? {
293
- set: props.trade.pnlAmount
294
- } : undefined,
295
- pnlPercent: props.trade.pnlPercent !== undefined ? {
296
- set: props.trade.pnlPercent
297
- } : undefined,
298
- durationMinutes: props.trade.durationMinutes !== undefined ? {
299
- set: props.trade.durationMinutes
300
- } : undefined,
301
- marketPhase: props.trade.marketPhase !== undefined ? {
302
- set: props.trade.marketPhase
303
- } : undefined,
304
- marketVolatility: props.trade.marketVolatility !== undefined ? {
305
- set: props.trade.marketVolatility
306
- } : undefined,
307
- thresholdsJson: props.trade.thresholdsJson !== undefined ? {
308
- set: props.trade.thresholdsJson
309
- } : undefined,
310
- },
311
- create: {
312
- alpacaAccountId: props.trade.alpacaAccountId !== undefined ? props.trade.alpacaAccountId : undefined,
313
- signal: props.trade.signal !== undefined ? props.trade.signal : undefined,
314
- strategy: props.trade.strategy !== undefined ? props.trade.strategy : undefined,
315
- analysis: props.trade.analysis !== undefined ? props.trade.analysis : undefined,
316
- summary: props.trade.summary !== undefined ? props.trade.summary : undefined,
317
- confidence: props.trade.confidence !== undefined ? props.trade.confidence : undefined,
318
- timestamp: props.trade.timestamp !== undefined ? props.trade.timestamp : undefined,
319
- status: props.trade.status !== undefined ? props.trade.status : undefined,
320
- symbol: props.trade.symbol !== undefined ? props.trade.symbol : undefined,
321
- entryPrice: props.trade.entryPrice !== undefined ? props.trade.entryPrice : undefined,
322
- exitPrice: props.trade.exitPrice !== undefined ? props.trade.exitPrice : undefined,
323
- entryQty: props.trade.entryQty !== undefined ? props.trade.entryQty : undefined,
324
- exitQty: props.trade.exitQty !== undefined ? props.trade.exitQty : undefined,
325
- entryValue: props.trade.entryValue !== undefined ? props.trade.entryValue : undefined,
326
- exitValue: props.trade.exitValue !== undefined ? props.trade.exitValue : undefined,
327
- entryTime: props.trade.entryTime !== undefined ? props.trade.entryTime : undefined,
328
- exitTime: props.trade.exitTime !== undefined ? props.trade.exitTime : undefined,
329
- pnlAmount: props.trade.pnlAmount !== undefined ? props.trade.pnlAmount : undefined,
330
- pnlPercent: props.trade.pnlPercent !== undefined ? props.trade.pnlPercent : undefined,
331
- durationMinutes: props.trade.durationMinutes !== undefined ? props.trade.durationMinutes : undefined,
332
- marketPhase: props.trade.marketPhase !== undefined ? props.trade.marketPhase : undefined,
333
- marketVolatility: props.trade.marketVolatility !== undefined ? props.trade.marketVolatility : undefined,
334
- thresholdsJson: props.trade.thresholdsJson !== undefined ? props.trade.thresholdsJson : undefined,
335
- },
336
- }
337
- } : undefined,
338
- },
339
- };
340
- const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
341
- try {
342
- const response = await client.mutate({ mutation: UPDATE_ONE_ACTION, variables: filteredVariables });
343
- if (response.errors && response.errors.length > 0)
344
- throw new Error(response.errors[0].message);
345
- if (response && response.data && response.data.updateOneAction) {
346
- return response.data.updateOneAction;
239
+ var _a, _b, _c, _d, _e;
240
+ // Maximum number of retries for database connection issues
241
+ const MAX_RETRIES = 3;
242
+ let retryCount = 0;
243
+ let lastError = null;
244
+ // Retry loop to handle potential database connection issues
245
+ while (retryCount < MAX_RETRIES) {
246
+ try {
247
+ const [modules, client] = await Promise.all([
248
+ (0, client_1.getApolloModules)(),
249
+ globalClient
250
+ ? Promise.resolve(globalClient)
251
+ : client_1.client
252
+ ]);
253
+ const { gql, ApolloError } = modules;
254
+ const UPDATE_ONE_ACTION = gql `
255
+ mutation updateOneAction($data: ActionUpdateInput!, $where: ActionWhereUniqueInput!) {
256
+ updateOneAction(data: $data, where: $where) {
257
+ ${selectionSet}
347
258
  }
348
- else {
349
- return null;
259
+ }`;
260
+ const variables = {
261
+ where: {
262
+ id: props.id !== undefined ? props.id : undefined,
263
+ alpacaOrderId: props.alpacaOrderId !== undefined ? props.alpacaOrderId : undefined,
264
+ tradeId: props.tradeId !== undefined ? {
265
+ equals: props.tradeId
266
+ } : undefined,
267
+ },
268
+ data: {
269
+ id: props.id !== undefined ? {
270
+ set: props.id
271
+ } : undefined,
272
+ sequence: props.sequence !== undefined ? {
273
+ set: props.sequence
274
+ } : undefined,
275
+ type: props.type !== undefined ? {
276
+ set: props.type
277
+ } : undefined,
278
+ primary: props.primary !== undefined ? {
279
+ set: props.primary
280
+ } : undefined,
281
+ note: props.note !== undefined ? {
282
+ set: props.note
283
+ } : undefined,
284
+ status: props.status !== undefined ? {
285
+ set: props.status
286
+ } : undefined,
287
+ createdAt: props.createdAt !== undefined ? {
288
+ set: props.createdAt
289
+ } : undefined,
290
+ updatedAt: props.updatedAt !== undefined ? {
291
+ set: props.updatedAt
292
+ } : undefined,
293
+ alpacaOrderId: props.alpacaOrderId !== undefined ? {
294
+ set: props.alpacaOrderId
295
+ } : undefined,
296
+ trade: props.trade ?
297
+ typeof props.trade === 'object' && Object.keys(props.trade).length === 1 && (Object.keys(props.trade)[0] === 'id' || Object.keys(props.trade)[0] === 'symbol')
298
+ ? {
299
+ connect: {
300
+ id: props.trade.id
301
+ }
302
+ } : { upsert: {
303
+ where: {
304
+ id: props.trade.id !== undefined ? {
305
+ equals: props.trade.id
306
+ } : undefined,
307
+ alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
308
+ equals: props.trade.alpacaAccountId
309
+ } : undefined,
310
+ symbol: props.trade.symbol !== undefined ? {
311
+ equals: props.trade.symbol
312
+ } : undefined,
313
+ },
314
+ update: {
315
+ id: props.trade.id !== undefined ? {
316
+ set: props.trade.id
317
+ } : undefined,
318
+ alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
319
+ set: props.trade.alpacaAccountId
320
+ } : undefined,
321
+ signal: props.trade.signal !== undefined ? {
322
+ set: props.trade.signal
323
+ } : undefined,
324
+ strategy: props.trade.strategy !== undefined ? {
325
+ set: props.trade.strategy
326
+ } : undefined,
327
+ analysis: props.trade.analysis !== undefined ? {
328
+ set: props.trade.analysis
329
+ } : undefined,
330
+ summary: props.trade.summary !== undefined ? {
331
+ set: props.trade.summary
332
+ } : undefined,
333
+ confidence: props.trade.confidence !== undefined ? {
334
+ set: props.trade.confidence
335
+ } : undefined,
336
+ timestamp: props.trade.timestamp !== undefined ? {
337
+ set: props.trade.timestamp
338
+ } : undefined,
339
+ status: props.trade.status !== undefined ? {
340
+ set: props.trade.status
341
+ } : undefined,
342
+ symbol: props.trade.symbol !== undefined ? {
343
+ set: props.trade.symbol
344
+ } : undefined,
345
+ entryPrice: props.trade.entryPrice !== undefined ? {
346
+ set: props.trade.entryPrice
347
+ } : undefined,
348
+ exitPrice: props.trade.exitPrice !== undefined ? {
349
+ set: props.trade.exitPrice
350
+ } : undefined,
351
+ entryQty: props.trade.entryQty !== undefined ? {
352
+ set: props.trade.entryQty
353
+ } : undefined,
354
+ exitQty: props.trade.exitQty !== undefined ? {
355
+ set: props.trade.exitQty
356
+ } : undefined,
357
+ entryValue: props.trade.entryValue !== undefined ? {
358
+ set: props.trade.entryValue
359
+ } : undefined,
360
+ exitValue: props.trade.exitValue !== undefined ? {
361
+ set: props.trade.exitValue
362
+ } : undefined,
363
+ entryTime: props.trade.entryTime !== undefined ? {
364
+ set: props.trade.entryTime
365
+ } : undefined,
366
+ exitTime: props.trade.exitTime !== undefined ? {
367
+ set: props.trade.exitTime
368
+ } : undefined,
369
+ pnlAmount: props.trade.pnlAmount !== undefined ? {
370
+ set: props.trade.pnlAmount
371
+ } : undefined,
372
+ pnlPercent: props.trade.pnlPercent !== undefined ? {
373
+ set: props.trade.pnlPercent
374
+ } : undefined,
375
+ durationMinutes: props.trade.durationMinutes !== undefined ? {
376
+ set: props.trade.durationMinutes
377
+ } : undefined,
378
+ marketPhase: props.trade.marketPhase !== undefined ? {
379
+ set: props.trade.marketPhase
380
+ } : undefined,
381
+ marketVolatility: props.trade.marketVolatility !== undefined ? {
382
+ set: props.trade.marketVolatility
383
+ } : undefined,
384
+ thresholdsJson: props.trade.thresholdsJson !== undefined ? {
385
+ set: props.trade.thresholdsJson
386
+ } : undefined,
387
+ },
388
+ create: {
389
+ alpacaAccountId: props.trade.alpacaAccountId !== undefined ? props.trade.alpacaAccountId : undefined,
390
+ signal: props.trade.signal !== undefined ? props.trade.signal : undefined,
391
+ strategy: props.trade.strategy !== undefined ? props.trade.strategy : undefined,
392
+ analysis: props.trade.analysis !== undefined ? props.trade.analysis : undefined,
393
+ summary: props.trade.summary !== undefined ? props.trade.summary : undefined,
394
+ confidence: props.trade.confidence !== undefined ? props.trade.confidence : undefined,
395
+ timestamp: props.trade.timestamp !== undefined ? props.trade.timestamp : undefined,
396
+ status: props.trade.status !== undefined ? props.trade.status : undefined,
397
+ symbol: props.trade.symbol !== undefined ? props.trade.symbol : undefined,
398
+ entryPrice: props.trade.entryPrice !== undefined ? props.trade.entryPrice : undefined,
399
+ exitPrice: props.trade.exitPrice !== undefined ? props.trade.exitPrice : undefined,
400
+ entryQty: props.trade.entryQty !== undefined ? props.trade.entryQty : undefined,
401
+ exitQty: props.trade.exitQty !== undefined ? props.trade.exitQty : undefined,
402
+ entryValue: props.trade.entryValue !== undefined ? props.trade.entryValue : undefined,
403
+ exitValue: props.trade.exitValue !== undefined ? props.trade.exitValue : undefined,
404
+ entryTime: props.trade.entryTime !== undefined ? props.trade.entryTime : undefined,
405
+ exitTime: props.trade.exitTime !== undefined ? props.trade.exitTime : undefined,
406
+ pnlAmount: props.trade.pnlAmount !== undefined ? props.trade.pnlAmount : undefined,
407
+ pnlPercent: props.trade.pnlPercent !== undefined ? props.trade.pnlPercent : undefined,
408
+ durationMinutes: props.trade.durationMinutes !== undefined ? props.trade.durationMinutes : undefined,
409
+ marketPhase: props.trade.marketPhase !== undefined ? props.trade.marketPhase : undefined,
410
+ marketVolatility: props.trade.marketVolatility !== undefined ? props.trade.marketVolatility : undefined,
411
+ thresholdsJson: props.trade.thresholdsJson !== undefined ? props.trade.thresholdsJson : undefined,
412
+ },
413
+ }
414
+ } : undefined,
415
+ },
416
+ };
417
+ const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
418
+ const response = await client.mutate({
419
+ mutation: UPDATE_ONE_ACTION,
420
+ variables: filteredVariables,
421
+ // Don't cache mutations, but ensure we're using the freshest context
422
+ fetchPolicy: 'no-cache'
423
+ });
424
+ if (response.errors && response.errors.length > 0)
425
+ throw new Error(response.errors[0].message);
426
+ if (response && response.data && response.data.updateOneAction) {
427
+ return response.data.updateOneAction;
428
+ }
429
+ else {
430
+ return null;
431
+ }
432
+ }
433
+ catch (error) {
434
+ lastError = error;
435
+ // Check if this is a database connection error that we should retry
436
+ const isConnectionError = ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Server has closed the connection')) ||
437
+ ((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('Cannot reach database server')) ||
438
+ ((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection timed out')) ||
439
+ ((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('Accelerate')) || // Prisma Accelerate proxy errors
440
+ (error.networkError && ((_e = error.networkError.message) === null || _e === void 0 ? void 0 : _e.includes('Failed to fetch')));
441
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
442
+ retryCount++;
443
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
444
+ console.warn("Database connection error, retrying...");
445
+ await new Promise(resolve => setTimeout(resolve, delay));
446
+ continue;
447
+ }
448
+ // Log the error and rethrow
449
+ console.error("Database error occurred:", error);
450
+ throw error;
350
451
  }
351
452
  }
352
- catch (error) {
353
- console.error('Error in updateOneAction:', error);
354
- throw error;
355
- }
453
+ // If we exhausted retries, throw the last error
454
+ throw lastError;
356
455
  },
357
456
  /**
358
457
  * Upsert a single Action record.
458
+ * Enhanced with connection resilience against Prisma connection errors.
359
459
  * @param props - Properties to update.
360
460
  * @param globalClient - Apollo Client instance.
361
461
  * @returns The updated Action or null.
362
462
  */
363
463
  async upsert(props, globalClient) {
364
- const [modules, client] = await Promise.all([
365
- (0, client_1.getApolloModules)(),
366
- globalClient
367
- ? Promise.resolve(globalClient)
368
- : client_1.client
369
- ]);
370
- const { gql, ApolloError } = modules;
371
- const UPSERT_ONE_ACTION = gql `
372
- mutation upsertOneAction($where: ActionWhereUniqueInput!, $create: ActionCreateInput!, $update: ActionUpdateInput!) {
373
- upsertOneAction(where: $where, create: $create, update: $update) {
374
- ${selectionSet}
375
- }
376
- }`;
377
- const variables = {
378
- where: {
379
- id: props.id !== undefined ? props.id : undefined,
380
- alpacaOrderId: props.alpacaOrderId !== undefined ? props.alpacaOrderId : undefined,
381
- tradeId: props.tradeId !== undefined ? {
382
- equals: props.tradeId
383
- } : undefined,
384
- },
385
- create: {
386
- sequence: props.sequence !== undefined ? props.sequence : undefined,
387
- type: props.type !== undefined ? props.type : undefined,
388
- primary: props.primary !== undefined ? props.primary : undefined,
389
- note: props.note !== undefined ? props.note : undefined,
390
- status: props.status !== undefined ? props.status : undefined,
391
- alpacaOrderId: props.alpacaOrderId !== undefined ? props.alpacaOrderId : undefined,
392
- trade: props.trade ?
393
- typeof props.trade === 'object' && Object.keys(props.trade).length === 1 && Object.keys(props.trade)[0] === 'id'
394
- ? { connect: {
395
- id: props.trade.id
396
- }
397
- }
398
- : { connectOrCreate: {
399
- where: {
400
- id: props.trade.id !== undefined ? props.trade.id : undefined,
401
- alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
402
- equals: props.trade.alpacaAccountId
403
- } : undefined,
404
- symbol: props.trade.symbol !== undefined ? {
405
- equals: props.trade.symbol
406
- } : undefined,
407
- },
408
- create: {
409
- alpacaAccountId: props.trade.alpacaAccountId !== undefined ? props.trade.alpacaAccountId : undefined,
410
- signal: props.trade.signal !== undefined ? props.trade.signal : undefined,
411
- strategy: props.trade.strategy !== undefined ? props.trade.strategy : undefined,
412
- analysis: props.trade.analysis !== undefined ? props.trade.analysis : undefined,
413
- summary: props.trade.summary !== undefined ? props.trade.summary : undefined,
414
- confidence: props.trade.confidence !== undefined ? props.trade.confidence : undefined,
415
- timestamp: props.trade.timestamp !== undefined ? props.trade.timestamp : undefined,
416
- status: props.trade.status !== undefined ? props.trade.status : undefined,
417
- symbol: props.trade.symbol !== undefined ? props.trade.symbol : undefined,
418
- entryPrice: props.trade.entryPrice !== undefined ? props.trade.entryPrice : undefined,
419
- exitPrice: props.trade.exitPrice !== undefined ? props.trade.exitPrice : undefined,
420
- entryQty: props.trade.entryQty !== undefined ? props.trade.entryQty : undefined,
421
- exitQty: props.trade.exitQty !== undefined ? props.trade.exitQty : undefined,
422
- entryValue: props.trade.entryValue !== undefined ? props.trade.entryValue : undefined,
423
- exitValue: props.trade.exitValue !== undefined ? props.trade.exitValue : undefined,
424
- entryTime: props.trade.entryTime !== undefined ? props.trade.entryTime : undefined,
425
- exitTime: props.trade.exitTime !== undefined ? props.trade.exitTime : undefined,
426
- pnlAmount: props.trade.pnlAmount !== undefined ? props.trade.pnlAmount : undefined,
427
- pnlPercent: props.trade.pnlPercent !== undefined ? props.trade.pnlPercent : undefined,
428
- durationMinutes: props.trade.durationMinutes !== undefined ? props.trade.durationMinutes : undefined,
429
- marketPhase: props.trade.marketPhase !== undefined ? props.trade.marketPhase : undefined,
430
- marketVolatility: props.trade.marketVolatility !== undefined ? props.trade.marketVolatility : undefined,
431
- thresholdsJson: props.trade.thresholdsJson !== undefined ? props.trade.thresholdsJson : undefined,
432
- },
433
- }
464
+ var _a, _b, _c, _d, _e;
465
+ // Maximum number of retries for database connection issues
466
+ const MAX_RETRIES = 3;
467
+ let retryCount = 0;
468
+ let lastError = null;
469
+ // Retry loop to handle potential database connection issues
470
+ while (retryCount < MAX_RETRIES) {
471
+ try {
472
+ const [modules, client] = await Promise.all([
473
+ (0, client_1.getApolloModules)(),
474
+ globalClient
475
+ ? Promise.resolve(globalClient)
476
+ : client_1.client
477
+ ]);
478
+ const { gql, ApolloError } = modules;
479
+ const UPSERT_ONE_ACTION = gql `
480
+ mutation upsertOneAction($where: ActionWhereUniqueInput!, $create: ActionCreateInput!, $update: ActionUpdateInput!) {
481
+ upsertOneAction(where: $where, create: $create, update: $update) {
482
+ ${selectionSet}
483
+ }
484
+ }`;
485
+ const variables = {
486
+ where: {
487
+ id: props.id !== undefined ? props.id : undefined,
488
+ alpacaOrderId: props.alpacaOrderId !== undefined ? props.alpacaOrderId : undefined,
489
+ tradeId: props.tradeId !== undefined ? {
490
+ equals: props.tradeId
434
491
  } : undefined,
435
- },
436
- update: {
437
- sequence: props.sequence !== undefined ? {
438
- set: props.sequence
439
- } : undefined,
440
- type: props.type !== undefined ? {
441
- set: props.type
442
- } : undefined,
443
- primary: props.primary !== undefined ? {
444
- set: props.primary
445
- } : undefined,
446
- note: props.note !== undefined ? {
447
- set: props.note
448
- } : undefined,
449
- status: props.status !== undefined ? {
450
- set: props.status
451
- } : undefined,
452
- alpacaOrderId: props.alpacaOrderId !== undefined ? {
453
- set: props.alpacaOrderId
454
- } : undefined,
455
- trade: props.trade ?
456
- typeof props.trade === 'object' && Object.keys(props.trade).length === 1 && (Object.keys(props.trade)[0] === 'id' || Object.keys(props.trade)[0] === 'symbol')
457
- ? {
458
- connect: {
459
- id: props.trade.id
460
- }
461
- } : { upsert: {
462
- where: {
463
- id: props.trade.id !== undefined ? {
464
- equals: props.trade.id
465
- } : undefined,
466
- alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
467
- equals: props.trade.alpacaAccountId
468
- } : undefined,
469
- symbol: props.trade.symbol !== undefined ? {
470
- equals: props.trade.symbol
471
- } : undefined,
472
- },
473
- update: {
474
- id: props.trade.id !== undefined ? {
475
- set: props.trade.id
476
- } : undefined,
477
- alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
478
- set: props.trade.alpacaAccountId
479
- } : undefined,
480
- signal: props.trade.signal !== undefined ? {
481
- set: props.trade.signal
482
- } : undefined,
483
- strategy: props.trade.strategy !== undefined ? {
484
- set: props.trade.strategy
485
- } : undefined,
486
- analysis: props.trade.analysis !== undefined ? {
487
- set: props.trade.analysis
488
- } : undefined,
489
- summary: props.trade.summary !== undefined ? {
490
- set: props.trade.summary
491
- } : undefined,
492
- confidence: props.trade.confidence !== undefined ? {
493
- set: props.trade.confidence
494
- } : undefined,
495
- timestamp: props.trade.timestamp !== undefined ? {
496
- set: props.trade.timestamp
497
- } : undefined,
498
- status: props.trade.status !== undefined ? {
499
- set: props.trade.status
500
- } : undefined,
501
- symbol: props.trade.symbol !== undefined ? {
502
- set: props.trade.symbol
503
- } : undefined,
504
- entryPrice: props.trade.entryPrice !== undefined ? {
505
- set: props.trade.entryPrice
506
- } : undefined,
507
- exitPrice: props.trade.exitPrice !== undefined ? {
508
- set: props.trade.exitPrice
509
- } : undefined,
510
- entryQty: props.trade.entryQty !== undefined ? {
511
- set: props.trade.entryQty
512
- } : undefined,
513
- exitQty: props.trade.exitQty !== undefined ? {
514
- set: props.trade.exitQty
515
- } : undefined,
516
- entryValue: props.trade.entryValue !== undefined ? {
517
- set: props.trade.entryValue
518
- } : undefined,
519
- exitValue: props.trade.exitValue !== undefined ? {
520
- set: props.trade.exitValue
521
- } : undefined,
522
- entryTime: props.trade.entryTime !== undefined ? {
523
- set: props.trade.entryTime
524
- } : undefined,
525
- exitTime: props.trade.exitTime !== undefined ? {
526
- set: props.trade.exitTime
527
- } : undefined,
528
- pnlAmount: props.trade.pnlAmount !== undefined ? {
529
- set: props.trade.pnlAmount
530
- } : undefined,
531
- pnlPercent: props.trade.pnlPercent !== undefined ? {
532
- set: props.trade.pnlPercent
533
- } : undefined,
534
- durationMinutes: props.trade.durationMinutes !== undefined ? {
535
- set: props.trade.durationMinutes
536
- } : undefined,
537
- marketPhase: props.trade.marketPhase !== undefined ? {
538
- set: props.trade.marketPhase
539
- } : undefined,
540
- marketVolatility: props.trade.marketVolatility !== undefined ? {
541
- set: props.trade.marketVolatility
542
- } : undefined,
543
- thresholdsJson: props.trade.thresholdsJson !== undefined ? {
544
- set: props.trade.thresholdsJson
545
- } : undefined,
546
- },
547
- create: {
548
- alpacaAccountId: props.trade.alpacaAccountId !== undefined ? props.trade.alpacaAccountId : undefined,
549
- signal: props.trade.signal !== undefined ? props.trade.signal : undefined,
550
- strategy: props.trade.strategy !== undefined ? props.trade.strategy : undefined,
551
- analysis: props.trade.analysis !== undefined ? props.trade.analysis : undefined,
552
- summary: props.trade.summary !== undefined ? props.trade.summary : undefined,
553
- confidence: props.trade.confidence !== undefined ? props.trade.confidence : undefined,
554
- timestamp: props.trade.timestamp !== undefined ? props.trade.timestamp : undefined,
555
- status: props.trade.status !== undefined ? props.trade.status : undefined,
556
- symbol: props.trade.symbol !== undefined ? props.trade.symbol : undefined,
557
- entryPrice: props.trade.entryPrice !== undefined ? props.trade.entryPrice : undefined,
558
- exitPrice: props.trade.exitPrice !== undefined ? props.trade.exitPrice : undefined,
559
- entryQty: props.trade.entryQty !== undefined ? props.trade.entryQty : undefined,
560
- exitQty: props.trade.exitQty !== undefined ? props.trade.exitQty : undefined,
561
- entryValue: props.trade.entryValue !== undefined ? props.trade.entryValue : undefined,
562
- exitValue: props.trade.exitValue !== undefined ? props.trade.exitValue : undefined,
563
- entryTime: props.trade.entryTime !== undefined ? props.trade.entryTime : undefined,
564
- exitTime: props.trade.exitTime !== undefined ? props.trade.exitTime : undefined,
565
- pnlAmount: props.trade.pnlAmount !== undefined ? props.trade.pnlAmount : undefined,
566
- pnlPercent: props.trade.pnlPercent !== undefined ? props.trade.pnlPercent : undefined,
567
- durationMinutes: props.trade.durationMinutes !== undefined ? props.trade.durationMinutes : undefined,
568
- marketPhase: props.trade.marketPhase !== undefined ? props.trade.marketPhase : undefined,
569
- marketVolatility: props.trade.marketVolatility !== undefined ? props.trade.marketVolatility : undefined,
570
- thresholdsJson: props.trade.thresholdsJson !== undefined ? props.trade.thresholdsJson : undefined,
571
- },
572
- }
573
- } : undefined,
574
- },
575
- };
576
- const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
577
- try {
578
- const response = await client.mutate({ mutation: UPSERT_ONE_ACTION, variables: filteredVariables });
579
- if (response.errors && response.errors.length > 0)
580
- throw new Error(response.errors[0].message);
581
- if (response && response.data && response.data.upsertOneAction) {
582
- return response.data.upsertOneAction;
492
+ },
493
+ create: {
494
+ sequence: props.sequence !== undefined ? props.sequence : undefined,
495
+ type: props.type !== undefined ? props.type : undefined,
496
+ primary: props.primary !== undefined ? props.primary : undefined,
497
+ note: props.note !== undefined ? props.note : undefined,
498
+ status: props.status !== undefined ? props.status : undefined,
499
+ alpacaOrderId: props.alpacaOrderId !== undefined ? props.alpacaOrderId : undefined,
500
+ trade: props.trade ?
501
+ typeof props.trade === 'object' && Object.keys(props.trade).length === 1 && Object.keys(props.trade)[0] === 'id'
502
+ ? { connect: {
503
+ id: props.trade.id
504
+ }
505
+ }
506
+ : { connectOrCreate: {
507
+ where: {
508
+ id: props.trade.id !== undefined ? props.trade.id : undefined,
509
+ alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
510
+ equals: props.trade.alpacaAccountId
511
+ } : undefined,
512
+ symbol: props.trade.symbol !== undefined ? {
513
+ equals: props.trade.symbol
514
+ } : undefined,
515
+ },
516
+ create: {
517
+ alpacaAccountId: props.trade.alpacaAccountId !== undefined ? props.trade.alpacaAccountId : undefined,
518
+ signal: props.trade.signal !== undefined ? props.trade.signal : undefined,
519
+ strategy: props.trade.strategy !== undefined ? props.trade.strategy : undefined,
520
+ analysis: props.trade.analysis !== undefined ? props.trade.analysis : undefined,
521
+ summary: props.trade.summary !== undefined ? props.trade.summary : undefined,
522
+ confidence: props.trade.confidence !== undefined ? props.trade.confidence : undefined,
523
+ timestamp: props.trade.timestamp !== undefined ? props.trade.timestamp : undefined,
524
+ status: props.trade.status !== undefined ? props.trade.status : undefined,
525
+ symbol: props.trade.symbol !== undefined ? props.trade.symbol : undefined,
526
+ entryPrice: props.trade.entryPrice !== undefined ? props.trade.entryPrice : undefined,
527
+ exitPrice: props.trade.exitPrice !== undefined ? props.trade.exitPrice : undefined,
528
+ entryQty: props.trade.entryQty !== undefined ? props.trade.entryQty : undefined,
529
+ exitQty: props.trade.exitQty !== undefined ? props.trade.exitQty : undefined,
530
+ entryValue: props.trade.entryValue !== undefined ? props.trade.entryValue : undefined,
531
+ exitValue: props.trade.exitValue !== undefined ? props.trade.exitValue : undefined,
532
+ entryTime: props.trade.entryTime !== undefined ? props.trade.entryTime : undefined,
533
+ exitTime: props.trade.exitTime !== undefined ? props.trade.exitTime : undefined,
534
+ pnlAmount: props.trade.pnlAmount !== undefined ? props.trade.pnlAmount : undefined,
535
+ pnlPercent: props.trade.pnlPercent !== undefined ? props.trade.pnlPercent : undefined,
536
+ durationMinutes: props.trade.durationMinutes !== undefined ? props.trade.durationMinutes : undefined,
537
+ marketPhase: props.trade.marketPhase !== undefined ? props.trade.marketPhase : undefined,
538
+ marketVolatility: props.trade.marketVolatility !== undefined ? props.trade.marketVolatility : undefined,
539
+ thresholdsJson: props.trade.thresholdsJson !== undefined ? props.trade.thresholdsJson : undefined,
540
+ },
541
+ }
542
+ } : undefined,
543
+ },
544
+ update: {
545
+ sequence: props.sequence !== undefined ? {
546
+ set: props.sequence
547
+ } : undefined,
548
+ type: props.type !== undefined ? {
549
+ set: props.type
550
+ } : undefined,
551
+ primary: props.primary !== undefined ? {
552
+ set: props.primary
553
+ } : undefined,
554
+ note: props.note !== undefined ? {
555
+ set: props.note
556
+ } : undefined,
557
+ status: props.status !== undefined ? {
558
+ set: props.status
559
+ } : undefined,
560
+ alpacaOrderId: props.alpacaOrderId !== undefined ? {
561
+ set: props.alpacaOrderId
562
+ } : undefined,
563
+ trade: props.trade ?
564
+ typeof props.trade === 'object' && Object.keys(props.trade).length === 1 && (Object.keys(props.trade)[0] === 'id' || Object.keys(props.trade)[0] === 'symbol')
565
+ ? {
566
+ connect: {
567
+ id: props.trade.id
568
+ }
569
+ } : { upsert: {
570
+ where: {
571
+ id: props.trade.id !== undefined ? {
572
+ equals: props.trade.id
573
+ } : undefined,
574
+ alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
575
+ equals: props.trade.alpacaAccountId
576
+ } : undefined,
577
+ symbol: props.trade.symbol !== undefined ? {
578
+ equals: props.trade.symbol
579
+ } : undefined,
580
+ },
581
+ update: {
582
+ id: props.trade.id !== undefined ? {
583
+ set: props.trade.id
584
+ } : undefined,
585
+ alpacaAccountId: props.trade.alpacaAccountId !== undefined ? {
586
+ set: props.trade.alpacaAccountId
587
+ } : undefined,
588
+ signal: props.trade.signal !== undefined ? {
589
+ set: props.trade.signal
590
+ } : undefined,
591
+ strategy: props.trade.strategy !== undefined ? {
592
+ set: props.trade.strategy
593
+ } : undefined,
594
+ analysis: props.trade.analysis !== undefined ? {
595
+ set: props.trade.analysis
596
+ } : undefined,
597
+ summary: props.trade.summary !== undefined ? {
598
+ set: props.trade.summary
599
+ } : undefined,
600
+ confidence: props.trade.confidence !== undefined ? {
601
+ set: props.trade.confidence
602
+ } : undefined,
603
+ timestamp: props.trade.timestamp !== undefined ? {
604
+ set: props.trade.timestamp
605
+ } : undefined,
606
+ status: props.trade.status !== undefined ? {
607
+ set: props.trade.status
608
+ } : undefined,
609
+ symbol: props.trade.symbol !== undefined ? {
610
+ set: props.trade.symbol
611
+ } : undefined,
612
+ entryPrice: props.trade.entryPrice !== undefined ? {
613
+ set: props.trade.entryPrice
614
+ } : undefined,
615
+ exitPrice: props.trade.exitPrice !== undefined ? {
616
+ set: props.trade.exitPrice
617
+ } : undefined,
618
+ entryQty: props.trade.entryQty !== undefined ? {
619
+ set: props.trade.entryQty
620
+ } : undefined,
621
+ exitQty: props.trade.exitQty !== undefined ? {
622
+ set: props.trade.exitQty
623
+ } : undefined,
624
+ entryValue: props.trade.entryValue !== undefined ? {
625
+ set: props.trade.entryValue
626
+ } : undefined,
627
+ exitValue: props.trade.exitValue !== undefined ? {
628
+ set: props.trade.exitValue
629
+ } : undefined,
630
+ entryTime: props.trade.entryTime !== undefined ? {
631
+ set: props.trade.entryTime
632
+ } : undefined,
633
+ exitTime: props.trade.exitTime !== undefined ? {
634
+ set: props.trade.exitTime
635
+ } : undefined,
636
+ pnlAmount: props.trade.pnlAmount !== undefined ? {
637
+ set: props.trade.pnlAmount
638
+ } : undefined,
639
+ pnlPercent: props.trade.pnlPercent !== undefined ? {
640
+ set: props.trade.pnlPercent
641
+ } : undefined,
642
+ durationMinutes: props.trade.durationMinutes !== undefined ? {
643
+ set: props.trade.durationMinutes
644
+ } : undefined,
645
+ marketPhase: props.trade.marketPhase !== undefined ? {
646
+ set: props.trade.marketPhase
647
+ } : undefined,
648
+ marketVolatility: props.trade.marketVolatility !== undefined ? {
649
+ set: props.trade.marketVolatility
650
+ } : undefined,
651
+ thresholdsJson: props.trade.thresholdsJson !== undefined ? {
652
+ set: props.trade.thresholdsJson
653
+ } : undefined,
654
+ },
655
+ create: {
656
+ alpacaAccountId: props.trade.alpacaAccountId !== undefined ? props.trade.alpacaAccountId : undefined,
657
+ signal: props.trade.signal !== undefined ? props.trade.signal : undefined,
658
+ strategy: props.trade.strategy !== undefined ? props.trade.strategy : undefined,
659
+ analysis: props.trade.analysis !== undefined ? props.trade.analysis : undefined,
660
+ summary: props.trade.summary !== undefined ? props.trade.summary : undefined,
661
+ confidence: props.trade.confidence !== undefined ? props.trade.confidence : undefined,
662
+ timestamp: props.trade.timestamp !== undefined ? props.trade.timestamp : undefined,
663
+ status: props.trade.status !== undefined ? props.trade.status : undefined,
664
+ symbol: props.trade.symbol !== undefined ? props.trade.symbol : undefined,
665
+ entryPrice: props.trade.entryPrice !== undefined ? props.trade.entryPrice : undefined,
666
+ exitPrice: props.trade.exitPrice !== undefined ? props.trade.exitPrice : undefined,
667
+ entryQty: props.trade.entryQty !== undefined ? props.trade.entryQty : undefined,
668
+ exitQty: props.trade.exitQty !== undefined ? props.trade.exitQty : undefined,
669
+ entryValue: props.trade.entryValue !== undefined ? props.trade.entryValue : undefined,
670
+ exitValue: props.trade.exitValue !== undefined ? props.trade.exitValue : undefined,
671
+ entryTime: props.trade.entryTime !== undefined ? props.trade.entryTime : undefined,
672
+ exitTime: props.trade.exitTime !== undefined ? props.trade.exitTime : undefined,
673
+ pnlAmount: props.trade.pnlAmount !== undefined ? props.trade.pnlAmount : undefined,
674
+ pnlPercent: props.trade.pnlPercent !== undefined ? props.trade.pnlPercent : undefined,
675
+ durationMinutes: props.trade.durationMinutes !== undefined ? props.trade.durationMinutes : undefined,
676
+ marketPhase: props.trade.marketPhase !== undefined ? props.trade.marketPhase : undefined,
677
+ marketVolatility: props.trade.marketVolatility !== undefined ? props.trade.marketVolatility : undefined,
678
+ thresholdsJson: props.trade.thresholdsJson !== undefined ? props.trade.thresholdsJson : undefined,
679
+ },
680
+ }
681
+ } : undefined,
682
+ },
683
+ };
684
+ const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
685
+ const response = await client.mutate({
686
+ mutation: UPSERT_ONE_ACTION,
687
+ variables: filteredVariables,
688
+ // Don't cache mutations, but ensure we're using the freshest context
689
+ fetchPolicy: 'no-cache'
690
+ });
691
+ if (response.errors && response.errors.length > 0)
692
+ throw new Error(response.errors[0].message);
693
+ if (response && response.data && response.data.upsertOneAction) {
694
+ return response.data.upsertOneAction;
695
+ }
696
+ else {
697
+ return null;
698
+ }
583
699
  }
584
- else {
585
- return null;
700
+ catch (error) {
701
+ lastError = error;
702
+ // Check if this is a database connection error that we should retry
703
+ const isConnectionError = ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Server has closed the connection')) ||
704
+ ((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('Cannot reach database server')) ||
705
+ ((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection timed out')) ||
706
+ ((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('Accelerate')) || // Prisma Accelerate proxy errors
707
+ (error.networkError && ((_e = error.networkError.message) === null || _e === void 0 ? void 0 : _e.includes('Failed to fetch')));
708
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
709
+ retryCount++;
710
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
711
+ console.warn("Database connection error, retrying...");
712
+ await new Promise(resolve => setTimeout(resolve, delay));
713
+ continue;
714
+ }
715
+ // Log the error and rethrow
716
+ console.error("Database error occurred:", error);
717
+ throw error;
586
718
  }
587
719
  }
588
- catch (error) {
589
- console.error('Error in upsertOneAction:', error);
590
- throw error;
591
- }
720
+ // If we exhausted retries, throw the last error
721
+ throw lastError;
592
722
  },
593
723
  /**
594
724
  * Update multiple Action records.
725
+ * Enhanced with connection resilience against Prisma connection errors.
595
726
  * @param props - Array of Action objects for the updated records.
596
727
  * @param globalClient - Apollo Client instance.
597
728
  * @returns The count of created records or null.
598
729
  */
599
730
  async updateMany(props, globalClient) {
600
- const [modules, client] = await Promise.all([
601
- (0, client_1.getApolloModules)(),
602
- globalClient
603
- ? Promise.resolve(globalClient)
604
- : client_1.client
605
- ]);
606
- const { gql, ApolloError } = modules;
607
- const UPDATE_MANY_ACTION = gql `
608
- mutation updateManyAction($data: [ActionCreateManyInput!]!) {
609
- updateManyAction(data: $data) {
610
- count
611
- }
612
- }`;
613
- const variables = props.map(prop => ({
614
- where: {
615
- id: prop.id !== undefined ? prop.id : undefined,
616
- alpacaOrderId: prop.alpacaOrderId !== undefined ? prop.alpacaOrderId : undefined,
617
- tradeId: prop.tradeId !== undefined ? {
618
- equals: prop.tradeId
619
- } : undefined,
620
- },
621
- data: {
622
- id: prop.id !== undefined ? {
623
- set: prop.id
624
- } : undefined,
625
- sequence: prop.sequence !== undefined ? {
626
- set: prop.sequence
627
- } : undefined,
628
- type: prop.type !== undefined ? {
629
- set: prop.type
630
- } : undefined,
631
- primary: prop.primary !== undefined ? {
632
- set: prop.primary
633
- } : undefined,
634
- note: prop.note !== undefined ? {
635
- set: prop.note
636
- } : undefined,
637
- status: prop.status !== undefined ? {
638
- set: prop.status
639
- } : undefined,
640
- createdAt: prop.createdAt !== undefined ? {
641
- set: prop.createdAt
642
- } : undefined,
643
- updatedAt: prop.updatedAt !== undefined ? {
644
- set: prop.updatedAt
645
- } : undefined,
646
- alpacaOrderId: prop.alpacaOrderId !== undefined ? {
647
- set: prop.alpacaOrderId
648
- } : undefined,
649
- trade: prop.trade ?
650
- typeof prop.trade === 'object' && Object.keys(prop.trade).length === 1 && (Object.keys(prop.trade)[0] === 'id' || Object.keys(prop.trade)[0] === 'symbol')
651
- ? {
652
- connect: {
653
- id: prop.trade.id
654
- }
655
- } : { upsert: {
656
- where: {
657
- id: prop.trade.id !== undefined ? {
658
- equals: prop.trade.id
659
- } : undefined,
660
- alpacaAccountId: prop.trade.alpacaAccountId !== undefined ? {
661
- equals: prop.trade.alpacaAccountId
662
- } : undefined,
663
- symbol: prop.trade.symbol !== undefined ? {
664
- equals: prop.trade.symbol
665
- } : undefined,
666
- },
667
- update: {
668
- id: prop.trade.id !== undefined ? {
669
- set: prop.trade.id
670
- } : undefined,
671
- alpacaAccountId: prop.trade.alpacaAccountId !== undefined ? {
672
- set: prop.trade.alpacaAccountId
673
- } : undefined,
674
- signal: prop.trade.signal !== undefined ? {
675
- set: prop.trade.signal
676
- } : undefined,
677
- strategy: prop.trade.strategy !== undefined ? {
678
- set: prop.trade.strategy
679
- } : undefined,
680
- analysis: prop.trade.analysis !== undefined ? {
681
- set: prop.trade.analysis
682
- } : undefined,
683
- summary: prop.trade.summary !== undefined ? {
684
- set: prop.trade.summary
685
- } : undefined,
686
- confidence: prop.trade.confidence !== undefined ? {
687
- set: prop.trade.confidence
688
- } : undefined,
689
- timestamp: prop.trade.timestamp !== undefined ? {
690
- set: prop.trade.timestamp
691
- } : undefined,
692
- status: prop.trade.status !== undefined ? {
693
- set: prop.trade.status
694
- } : undefined,
695
- symbol: prop.trade.symbol !== undefined ? {
696
- set: prop.trade.symbol
697
- } : undefined,
698
- entryPrice: prop.trade.entryPrice !== undefined ? {
699
- set: prop.trade.entryPrice
700
- } : undefined,
701
- exitPrice: prop.trade.exitPrice !== undefined ? {
702
- set: prop.trade.exitPrice
703
- } : undefined,
704
- entryQty: prop.trade.entryQty !== undefined ? {
705
- set: prop.trade.entryQty
706
- } : undefined,
707
- exitQty: prop.trade.exitQty !== undefined ? {
708
- set: prop.trade.exitQty
709
- } : undefined,
710
- entryValue: prop.trade.entryValue !== undefined ? {
711
- set: prop.trade.entryValue
712
- } : undefined,
713
- exitValue: prop.trade.exitValue !== undefined ? {
714
- set: prop.trade.exitValue
715
- } : undefined,
716
- entryTime: prop.trade.entryTime !== undefined ? {
717
- set: prop.trade.entryTime
718
- } : undefined,
719
- exitTime: prop.trade.exitTime !== undefined ? {
720
- set: prop.trade.exitTime
721
- } : undefined,
722
- pnlAmount: prop.trade.pnlAmount !== undefined ? {
723
- set: prop.trade.pnlAmount
724
- } : undefined,
725
- pnlPercent: prop.trade.pnlPercent !== undefined ? {
726
- set: prop.trade.pnlPercent
727
- } : undefined,
728
- durationMinutes: prop.trade.durationMinutes !== undefined ? {
729
- set: prop.trade.durationMinutes
730
- } : undefined,
731
- marketPhase: prop.trade.marketPhase !== undefined ? {
732
- set: prop.trade.marketPhase
733
- } : undefined,
734
- marketVolatility: prop.trade.marketVolatility !== undefined ? {
735
- set: prop.trade.marketVolatility
736
- } : undefined,
737
- thresholdsJson: prop.trade.thresholdsJson !== undefined ? {
738
- set: prop.trade.thresholdsJson
739
- } : undefined,
740
- },
741
- create: {
742
- alpacaAccountId: prop.trade.alpacaAccountId !== undefined ? prop.trade.alpacaAccountId : undefined,
743
- signal: prop.trade.signal !== undefined ? prop.trade.signal : undefined,
744
- strategy: prop.trade.strategy !== undefined ? prop.trade.strategy : undefined,
745
- analysis: prop.trade.analysis !== undefined ? prop.trade.analysis : undefined,
746
- summary: prop.trade.summary !== undefined ? prop.trade.summary : undefined,
747
- confidence: prop.trade.confidence !== undefined ? prop.trade.confidence : undefined,
748
- timestamp: prop.trade.timestamp !== undefined ? prop.trade.timestamp : undefined,
749
- status: prop.trade.status !== undefined ? prop.trade.status : undefined,
750
- symbol: prop.trade.symbol !== undefined ? prop.trade.symbol : undefined,
751
- entryPrice: prop.trade.entryPrice !== undefined ? prop.trade.entryPrice : undefined,
752
- exitPrice: prop.trade.exitPrice !== undefined ? prop.trade.exitPrice : undefined,
753
- entryQty: prop.trade.entryQty !== undefined ? prop.trade.entryQty : undefined,
754
- exitQty: prop.trade.exitQty !== undefined ? prop.trade.exitQty : undefined,
755
- entryValue: prop.trade.entryValue !== undefined ? prop.trade.entryValue : undefined,
756
- exitValue: prop.trade.exitValue !== undefined ? prop.trade.exitValue : undefined,
757
- entryTime: prop.trade.entryTime !== undefined ? prop.trade.entryTime : undefined,
758
- exitTime: prop.trade.exitTime !== undefined ? prop.trade.exitTime : undefined,
759
- pnlAmount: prop.trade.pnlAmount !== undefined ? prop.trade.pnlAmount : undefined,
760
- pnlPercent: prop.trade.pnlPercent !== undefined ? prop.trade.pnlPercent : undefined,
761
- durationMinutes: prop.trade.durationMinutes !== undefined ? prop.trade.durationMinutes : undefined,
762
- marketPhase: prop.trade.marketPhase !== undefined ? prop.trade.marketPhase : undefined,
763
- marketVolatility: prop.trade.marketVolatility !== undefined ? prop.trade.marketVolatility : undefined,
764
- thresholdsJson: prop.trade.thresholdsJson !== undefined ? prop.trade.thresholdsJson : undefined,
765
- },
766
- }
767
- } : undefined,
768
- },
769
- }));
770
- const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
771
- try {
772
- const response = await client.mutate({ mutation: UPDATE_MANY_ACTION, variables: filteredVariables });
773
- if (response.errors && response.errors.length > 0)
774
- throw new Error(response.errors[0].message);
775
- if (response && response.data && response.data.updateManyAction) {
776
- return response.data.updateManyAction;
731
+ var _a, _b, _c, _d, _e;
732
+ // Maximum number of retries for database connection issues
733
+ const MAX_RETRIES = 3;
734
+ let retryCount = 0;
735
+ let lastError = null;
736
+ // Retry loop to handle potential database connection issues
737
+ while (retryCount < MAX_RETRIES) {
738
+ try {
739
+ const [modules, client] = await Promise.all([
740
+ (0, client_1.getApolloModules)(),
741
+ globalClient
742
+ ? Promise.resolve(globalClient)
743
+ : client_1.client
744
+ ]);
745
+ const { gql, ApolloError } = modules;
746
+ const UPDATE_MANY_ACTION = gql `
747
+ mutation updateManyAction($data: [ActionCreateManyInput!]!) {
748
+ updateManyAction(data: $data) {
749
+ count
777
750
  }
778
- else {
779
- return null;
751
+ }`;
752
+ const variables = props.map(prop => ({
753
+ where: {
754
+ id: prop.id !== undefined ? prop.id : undefined,
755
+ alpacaOrderId: prop.alpacaOrderId !== undefined ? prop.alpacaOrderId : undefined,
756
+ tradeId: prop.tradeId !== undefined ? {
757
+ equals: prop.tradeId
758
+ } : undefined,
759
+ },
760
+ data: {
761
+ id: prop.id !== undefined ? {
762
+ set: prop.id
763
+ } : undefined,
764
+ sequence: prop.sequence !== undefined ? {
765
+ set: prop.sequence
766
+ } : undefined,
767
+ type: prop.type !== undefined ? {
768
+ set: prop.type
769
+ } : undefined,
770
+ primary: prop.primary !== undefined ? {
771
+ set: prop.primary
772
+ } : undefined,
773
+ note: prop.note !== undefined ? {
774
+ set: prop.note
775
+ } : undefined,
776
+ status: prop.status !== undefined ? {
777
+ set: prop.status
778
+ } : undefined,
779
+ createdAt: prop.createdAt !== undefined ? {
780
+ set: prop.createdAt
781
+ } : undefined,
782
+ updatedAt: prop.updatedAt !== undefined ? {
783
+ set: prop.updatedAt
784
+ } : undefined,
785
+ alpacaOrderId: prop.alpacaOrderId !== undefined ? {
786
+ set: prop.alpacaOrderId
787
+ } : undefined,
788
+ trade: prop.trade ?
789
+ typeof prop.trade === 'object' && Object.keys(prop.trade).length === 1 && (Object.keys(prop.trade)[0] === 'id' || Object.keys(prop.trade)[0] === 'symbol')
790
+ ? {
791
+ connect: {
792
+ id: prop.trade.id
793
+ }
794
+ } : { upsert: {
795
+ where: {
796
+ id: prop.trade.id !== undefined ? {
797
+ equals: prop.trade.id
798
+ } : undefined,
799
+ alpacaAccountId: prop.trade.alpacaAccountId !== undefined ? {
800
+ equals: prop.trade.alpacaAccountId
801
+ } : undefined,
802
+ symbol: prop.trade.symbol !== undefined ? {
803
+ equals: prop.trade.symbol
804
+ } : undefined,
805
+ },
806
+ update: {
807
+ id: prop.trade.id !== undefined ? {
808
+ set: prop.trade.id
809
+ } : undefined,
810
+ alpacaAccountId: prop.trade.alpacaAccountId !== undefined ? {
811
+ set: prop.trade.alpacaAccountId
812
+ } : undefined,
813
+ signal: prop.trade.signal !== undefined ? {
814
+ set: prop.trade.signal
815
+ } : undefined,
816
+ strategy: prop.trade.strategy !== undefined ? {
817
+ set: prop.trade.strategy
818
+ } : undefined,
819
+ analysis: prop.trade.analysis !== undefined ? {
820
+ set: prop.trade.analysis
821
+ } : undefined,
822
+ summary: prop.trade.summary !== undefined ? {
823
+ set: prop.trade.summary
824
+ } : undefined,
825
+ confidence: prop.trade.confidence !== undefined ? {
826
+ set: prop.trade.confidence
827
+ } : undefined,
828
+ timestamp: prop.trade.timestamp !== undefined ? {
829
+ set: prop.trade.timestamp
830
+ } : undefined,
831
+ status: prop.trade.status !== undefined ? {
832
+ set: prop.trade.status
833
+ } : undefined,
834
+ symbol: prop.trade.symbol !== undefined ? {
835
+ set: prop.trade.symbol
836
+ } : undefined,
837
+ entryPrice: prop.trade.entryPrice !== undefined ? {
838
+ set: prop.trade.entryPrice
839
+ } : undefined,
840
+ exitPrice: prop.trade.exitPrice !== undefined ? {
841
+ set: prop.trade.exitPrice
842
+ } : undefined,
843
+ entryQty: prop.trade.entryQty !== undefined ? {
844
+ set: prop.trade.entryQty
845
+ } : undefined,
846
+ exitQty: prop.trade.exitQty !== undefined ? {
847
+ set: prop.trade.exitQty
848
+ } : undefined,
849
+ entryValue: prop.trade.entryValue !== undefined ? {
850
+ set: prop.trade.entryValue
851
+ } : undefined,
852
+ exitValue: prop.trade.exitValue !== undefined ? {
853
+ set: prop.trade.exitValue
854
+ } : undefined,
855
+ entryTime: prop.trade.entryTime !== undefined ? {
856
+ set: prop.trade.entryTime
857
+ } : undefined,
858
+ exitTime: prop.trade.exitTime !== undefined ? {
859
+ set: prop.trade.exitTime
860
+ } : undefined,
861
+ pnlAmount: prop.trade.pnlAmount !== undefined ? {
862
+ set: prop.trade.pnlAmount
863
+ } : undefined,
864
+ pnlPercent: prop.trade.pnlPercent !== undefined ? {
865
+ set: prop.trade.pnlPercent
866
+ } : undefined,
867
+ durationMinutes: prop.trade.durationMinutes !== undefined ? {
868
+ set: prop.trade.durationMinutes
869
+ } : undefined,
870
+ marketPhase: prop.trade.marketPhase !== undefined ? {
871
+ set: prop.trade.marketPhase
872
+ } : undefined,
873
+ marketVolatility: prop.trade.marketVolatility !== undefined ? {
874
+ set: prop.trade.marketVolatility
875
+ } : undefined,
876
+ thresholdsJson: prop.trade.thresholdsJson !== undefined ? {
877
+ set: prop.trade.thresholdsJson
878
+ } : undefined,
879
+ },
880
+ create: {
881
+ alpacaAccountId: prop.trade.alpacaAccountId !== undefined ? prop.trade.alpacaAccountId : undefined,
882
+ signal: prop.trade.signal !== undefined ? prop.trade.signal : undefined,
883
+ strategy: prop.trade.strategy !== undefined ? prop.trade.strategy : undefined,
884
+ analysis: prop.trade.analysis !== undefined ? prop.trade.analysis : undefined,
885
+ summary: prop.trade.summary !== undefined ? prop.trade.summary : undefined,
886
+ confidence: prop.trade.confidence !== undefined ? prop.trade.confidence : undefined,
887
+ timestamp: prop.trade.timestamp !== undefined ? prop.trade.timestamp : undefined,
888
+ status: prop.trade.status !== undefined ? prop.trade.status : undefined,
889
+ symbol: prop.trade.symbol !== undefined ? prop.trade.symbol : undefined,
890
+ entryPrice: prop.trade.entryPrice !== undefined ? prop.trade.entryPrice : undefined,
891
+ exitPrice: prop.trade.exitPrice !== undefined ? prop.trade.exitPrice : undefined,
892
+ entryQty: prop.trade.entryQty !== undefined ? prop.trade.entryQty : undefined,
893
+ exitQty: prop.trade.exitQty !== undefined ? prop.trade.exitQty : undefined,
894
+ entryValue: prop.trade.entryValue !== undefined ? prop.trade.entryValue : undefined,
895
+ exitValue: prop.trade.exitValue !== undefined ? prop.trade.exitValue : undefined,
896
+ entryTime: prop.trade.entryTime !== undefined ? prop.trade.entryTime : undefined,
897
+ exitTime: prop.trade.exitTime !== undefined ? prop.trade.exitTime : undefined,
898
+ pnlAmount: prop.trade.pnlAmount !== undefined ? prop.trade.pnlAmount : undefined,
899
+ pnlPercent: prop.trade.pnlPercent !== undefined ? prop.trade.pnlPercent : undefined,
900
+ durationMinutes: prop.trade.durationMinutes !== undefined ? prop.trade.durationMinutes : undefined,
901
+ marketPhase: prop.trade.marketPhase !== undefined ? prop.trade.marketPhase : undefined,
902
+ marketVolatility: prop.trade.marketVolatility !== undefined ? prop.trade.marketVolatility : undefined,
903
+ thresholdsJson: prop.trade.thresholdsJson !== undefined ? prop.trade.thresholdsJson : undefined,
904
+ },
905
+ }
906
+ } : undefined,
907
+ },
908
+ }));
909
+ const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
910
+ const response = await client.mutate({
911
+ mutation: UPDATE_MANY_ACTION,
912
+ variables: filteredVariables,
913
+ // Don't cache mutations, but ensure we're using the freshest context
914
+ fetchPolicy: 'no-cache'
915
+ });
916
+ if (response.errors && response.errors.length > 0)
917
+ throw new Error(response.errors[0].message);
918
+ if (response && response.data && response.data.updateManyAction) {
919
+ return response.data.updateManyAction;
920
+ }
921
+ else {
922
+ return null;
923
+ }
924
+ }
925
+ catch (error) {
926
+ lastError = error;
927
+ // Check if this is a database connection error that we should retry
928
+ const isConnectionError = ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Server has closed the connection')) ||
929
+ ((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('Cannot reach database server')) ||
930
+ ((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection timed out')) ||
931
+ ((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('Accelerate')) || // Prisma Accelerate proxy errors
932
+ (error.networkError && ((_e = error.networkError.message) === null || _e === void 0 ? void 0 : _e.includes('Failed to fetch')));
933
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
934
+ retryCount++;
935
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
936
+ console.warn("Database connection error, retrying...");
937
+ await new Promise(resolve => setTimeout(resolve, delay));
938
+ continue;
939
+ }
940
+ // Log the error and rethrow
941
+ console.error("Database error occurred:", error);
942
+ throw error;
780
943
  }
781
944
  }
782
- catch (error) {
783
- console.error('Error in updateManyAction:', error);
784
- throw error;
785
- }
945
+ // If we exhausted retries, throw the last error
946
+ throw lastError;
786
947
  },
787
948
  /**
788
949
  * Delete a single Action record.
789
- * @param props - Properties to update.
950
+ * Enhanced with connection resilience against Prisma connection errors.
951
+ * @param props - Properties to identify the record to delete.
790
952
  * @param globalClient - Apollo Client instance.
791
953
  * @returns The deleted Action or null.
792
954
  */
793
955
  async delete(props, globalClient) {
794
- const [modules, client] = await Promise.all([
795
- (0, client_1.getApolloModules)(),
796
- globalClient
797
- ? Promise.resolve(globalClient)
798
- : client_1.client
799
- ]);
800
- const { gql, ApolloError } = modules;
801
- const DELETE_ONE_ACTION = gql `
802
- mutation deleteOneAction($where: ActionWhereUniqueInput!) {
803
- deleteOneAction(where: $where) {
804
- id
805
- }
806
- }`;
807
- const variables = {
808
- where: {
809
- id: props.id ? props.id : undefined,
956
+ var _a, _b, _c, _d, _e;
957
+ // Maximum number of retries for database connection issues
958
+ const MAX_RETRIES = 3;
959
+ let retryCount = 0;
960
+ let lastError = null;
961
+ // Retry loop to handle potential database connection issues
962
+ while (retryCount < MAX_RETRIES) {
963
+ try {
964
+ const [modules, client] = await Promise.all([
965
+ (0, client_1.getApolloModules)(),
966
+ globalClient
967
+ ? Promise.resolve(globalClient)
968
+ : client_1.client
969
+ ]);
970
+ const { gql, ApolloError } = modules;
971
+ const DELETE_ONE_ACTION = gql `
972
+ mutation deleteOneAction($where: ActionWhereUniqueInput!) {
973
+ deleteOneAction(where: $where) {
974
+ id
810
975
  }
811
- };
812
- const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
813
- try {
814
- const response = await client.mutate({ mutation: DELETE_ONE_ACTION, variables: filteredVariables });
815
- if (response.errors && response.errors.length > 0)
816
- throw new Error(response.errors[0].message);
817
- if (response && response.data && response.data.deleteOneAction) {
818
- return response.data.deleteOneAction;
976
+ }`;
977
+ const variables = {
978
+ where: {
979
+ id: props.id ? props.id : undefined,
980
+ }
981
+ };
982
+ const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
983
+ const response = await client.mutate({
984
+ mutation: DELETE_ONE_ACTION,
985
+ variables: filteredVariables,
986
+ // Don't cache mutations, but ensure we're using the freshest context
987
+ fetchPolicy: 'no-cache'
988
+ });
989
+ if (response.errors && response.errors.length > 0)
990
+ throw new Error(response.errors[0].message);
991
+ if (response && response.data && response.data.deleteOneAction) {
992
+ return response.data.deleteOneAction;
993
+ }
994
+ else {
995
+ return null;
996
+ }
819
997
  }
820
- else {
821
- return null;
998
+ catch (error) {
999
+ lastError = error;
1000
+ // Check if this is a database connection error that we should retry
1001
+ const isConnectionError = ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Server has closed the connection')) ||
1002
+ ((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('Cannot reach database server')) ||
1003
+ ((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection timed out')) ||
1004
+ ((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('Accelerate')) || // Prisma Accelerate proxy errors
1005
+ (error.networkError && ((_e = error.networkError.message) === null || _e === void 0 ? void 0 : _e.includes('Failed to fetch')));
1006
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
1007
+ retryCount++;
1008
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
1009
+ console.warn("Database connection error, retrying...");
1010
+ await new Promise(resolve => setTimeout(resolve, delay));
1011
+ continue;
1012
+ }
1013
+ // Log the error and rethrow
1014
+ console.error("Database error occurred:", error);
1015
+ throw error;
822
1016
  }
823
1017
  }
824
- catch (error) {
825
- console.error('Error in deleteOneAction:', error);
826
- throw error;
827
- }
1018
+ // If we exhausted retries, throw the last error
1019
+ throw lastError;
828
1020
  },
829
1021
  /**
830
1022
  * Retrieve a single Action record by ID.
831
- * @param props - Properties to update.
1023
+ * Enhanced with connection resilience against Prisma connection errors.
1024
+ * @param props - Properties to identify the record.
832
1025
  * @param globalClient - Apollo Client instance.
1026
+ * @param whereInput - Optional custom where input.
833
1027
  * @returns The retrieved Action or null.
834
1028
  */
835
1029
  async get(props, globalClient, whereInput) {
836
- var _a, _b;
837
- const [modules, client] = await Promise.all([
838
- (0, client_1.getApolloModules)(),
839
- globalClient
840
- ? Promise.resolve(globalClient)
841
- : client_1.client
842
- ]);
843
- const { gql, ApolloError } = modules;
844
- const GET_ACTION = gql `
845
- query getAction($where: ActionWhereUniqueInput!) {
846
- getAction(where: $where) {
847
- ${selectionSet}
848
- }
849
- }`;
850
- const variables = {
851
- where: whereInput ? whereInput : {
852
- id: props.id !== undefined ? props.id : undefined,
853
- alpacaOrderId: props.alpacaOrderId !== undefined ? props.alpacaOrderId : undefined,
854
- tradeId: props.tradeId !== undefined ? {
855
- equals: props.tradeId
856
- } : undefined,
857
- },
858
- };
859
- const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
860
- try {
861
- const response = await client.query({ query: GET_ACTION, variables: filteredVariables });
862
- if (response.errors && response.errors.length > 0)
863
- throw new Error(response.errors[0].message);
864
- return (_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.getAction) !== null && _b !== void 0 ? _b : null;
865
- }
866
- catch (error) {
867
- if (error instanceof ApolloError && error.message === 'No Action found') {
868
- return null;
1030
+ var _a, _b, _c, _d, _e, _f, _g;
1031
+ // Maximum number of retries for database connection issues
1032
+ const MAX_RETRIES = 3;
1033
+ let retryCount = 0;
1034
+ let lastError = null;
1035
+ // Retry loop to handle potential database connection issues
1036
+ while (retryCount < MAX_RETRIES) {
1037
+ try {
1038
+ const [modules, client] = await Promise.all([
1039
+ (0, client_1.getApolloModules)(),
1040
+ globalClient
1041
+ ? Promise.resolve(globalClient)
1042
+ : client_1.client
1043
+ ]);
1044
+ const { gql, ApolloError } = modules;
1045
+ const GET_ACTION = gql `
1046
+ query getAction($where: ActionWhereUniqueInput!) {
1047
+ getAction(where: $where) {
1048
+ ${selectionSet}
1049
+ }
1050
+ }`;
1051
+ const variables = {
1052
+ where: whereInput ? whereInput : {
1053
+ id: props.id !== undefined ? props.id : undefined,
1054
+ alpacaOrderId: props.alpacaOrderId !== undefined ? props.alpacaOrderId : undefined,
1055
+ tradeId: props.tradeId !== undefined ? {
1056
+ equals: props.tradeId
1057
+ } : undefined,
1058
+ },
1059
+ };
1060
+ const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
1061
+ const response = await client.query({
1062
+ query: GET_ACTION,
1063
+ variables: filteredVariables,
1064
+ fetchPolicy: 'network-only', // Force network request to avoid stale cache
1065
+ });
1066
+ if (response.errors && response.errors.length > 0)
1067
+ throw new Error(response.errors[0].message);
1068
+ return (_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.getAction) !== null && _b !== void 0 ? _b : null;
869
1069
  }
870
- else {
871
- console.error('Error in getAction:', error);
1070
+ catch (error) {
1071
+ lastError = error;
1072
+ // Check if this is a "No record found" error - this is an expected condition, not a failure
1073
+ if (error.message === 'No Action found') {
1074
+ return null;
1075
+ }
1076
+ // Check if this is a database connection error that we should retry
1077
+ const isConnectionError = ((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('Server has closed the connection')) ||
1078
+ ((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('Cannot reach database server')) ||
1079
+ ((_e = error.message) === null || _e === void 0 ? void 0 : _e.includes('Connection timed out')) ||
1080
+ ((_f = error.message) === null || _f === void 0 ? void 0 : _f.includes('Accelerate')) || // Prisma Accelerate proxy errors
1081
+ (error.networkError && ((_g = error.networkError.message) === null || _g === void 0 ? void 0 : _g.includes('Failed to fetch')));
1082
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
1083
+ retryCount++;
1084
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
1085
+ console.warn("Database connection error, retrying...");
1086
+ await new Promise(resolve => setTimeout(resolve, delay));
1087
+ continue;
1088
+ }
1089
+ // Log the error and rethrow
1090
+ console.error("Database error occurred:", error);
872
1091
  throw error;
873
1092
  }
874
1093
  }
1094
+ // If we exhausted retries, throw the last error
1095
+ throw lastError;
875
1096
  },
876
1097
  /**
877
1098
  * Retrieve all Actions records.
1099
+ * Enhanced with connection resilience against Prisma connection errors.
878
1100
  * @param globalClient - Apollo Client instance.
879
1101
  * @returns An array of Action records or null.
880
1102
  */
881
1103
  async getAll(globalClient) {
882
- var _a, _b;
883
- const [modules, client] = await Promise.all([
884
- (0, client_1.getApolloModules)(),
885
- globalClient
886
- ? Promise.resolve(globalClient)
887
- : client_1.client
888
- ]);
889
- const { gql, ApolloError } = modules;
890
- const GET_ALL_ACTION = gql `
891
- query getAllAction {
892
- actions {
893
- ${selectionSet}
894
- }
895
- }`;
896
- try {
897
- const response = await client.query({ query: GET_ALL_ACTION });
898
- if (response.errors && response.errors.length > 0)
899
- throw new Error(response.errors[0].message);
900
- return (_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.actions) !== null && _b !== void 0 ? _b : null;
901
- }
902
- catch (error) {
903
- if (error instanceof ApolloError && error.message === 'No Action found') {
904
- return null;
1104
+ var _a, _b, _c, _d, _e, _f, _g;
1105
+ // Maximum number of retries for database connection issues
1106
+ const MAX_RETRIES = 3;
1107
+ let retryCount = 0;
1108
+ let lastError = null;
1109
+ // Retry loop to handle potential database connection issues
1110
+ while (retryCount < MAX_RETRIES) {
1111
+ try {
1112
+ const [modules, client] = await Promise.all([
1113
+ (0, client_1.getApolloModules)(),
1114
+ globalClient
1115
+ ? Promise.resolve(globalClient)
1116
+ : client_1.client
1117
+ ]);
1118
+ const { gql, ApolloError } = modules;
1119
+ const GET_ALL_ACTION = gql `
1120
+ query getAllAction {
1121
+ actions {
1122
+ ${selectionSet}
905
1123
  }
906
- else {
907
- console.error('Error in getAction:', error);
1124
+ }`;
1125
+ const response = await client.query({
1126
+ query: GET_ALL_ACTION,
1127
+ fetchPolicy: 'network-only', // Force network request to avoid stale cache
1128
+ });
1129
+ if (response.errors && response.errors.length > 0)
1130
+ throw new Error(response.errors[0].message);
1131
+ return (_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.actions) !== null && _b !== void 0 ? _b : null;
1132
+ }
1133
+ catch (error) {
1134
+ lastError = error;
1135
+ // Check if this is a "No record found" error - this is an expected condition, not a failure
1136
+ if (error.message === 'No Action found') {
1137
+ return null;
1138
+ }
1139
+ // Check if this is a database connection error that we should retry
1140
+ const isConnectionError = ((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('Server has closed the connection')) ||
1141
+ ((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('Cannot reach database server')) ||
1142
+ ((_e = error.message) === null || _e === void 0 ? void 0 : _e.includes('Connection timed out')) ||
1143
+ ((_f = error.message) === null || _f === void 0 ? void 0 : _f.includes('Accelerate')) || // Prisma Accelerate proxy errors
1144
+ (error.networkError && ((_g = error.networkError.message) === null || _g === void 0 ? void 0 : _g.includes('Failed to fetch')));
1145
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
1146
+ retryCount++;
1147
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
1148
+ console.warn("Database connection error, retrying...");
1149
+ await new Promise(resolve => setTimeout(resolve, delay));
1150
+ continue;
1151
+ }
1152
+ // Log the error and rethrow
1153
+ console.error("Database error occurred:", error);
908
1154
  throw error;
909
1155
  }
910
1156
  }
1157
+ // If we exhausted retries, throw the last error
1158
+ throw lastError;
911
1159
  },
912
1160
  /**
913
1161
  * Find multiple Action records based on conditions.
1162
+ * Enhanced with connection resilience against Prisma connection errors.
914
1163
  * @param props - Conditions to find records.
915
1164
  * @param globalClient - Apollo Client instance.
1165
+ * @param whereInput - Optional custom where input.
916
1166
  * @returns An array of found Action records or null.
917
1167
  */
918
1168
  async findMany(props, globalClient, whereInput) {
919
- const [modules, client] = await Promise.all([
920
- (0, client_1.getApolloModules)(),
921
- globalClient
922
- ? Promise.resolve(globalClient)
923
- : client_1.client
924
- ]);
925
- const { gql, ApolloError } = modules;
926
- const FIND_MANY_ACTION = gql `
927
- query findManyAction($where: ActionWhereInput!) {
928
- actions(where: $where) {
929
- ${selectionSet}
930
- }
931
- }`;
932
- const variables = {
933
- where: whereInput ? whereInput : {
934
- id: props.id !== undefined ? {
935
- equals: props.id
936
- } : undefined,
937
- tradeId: props.tradeId !== undefined ? {
938
- equals: props.tradeId
939
- } : undefined,
940
- },
941
- };
942
- const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
943
- try {
944
- const response = await client.query({ query: FIND_MANY_ACTION, variables: filteredVariables });
945
- if (response.errors && response.errors.length > 0)
946
- throw new Error(response.errors[0].message);
947
- if (response && response.data && response.data.actions) {
948
- return response.data.actions;
1169
+ var _a, _b, _c, _d, _e;
1170
+ // Maximum number of retries for database connection issues
1171
+ const MAX_RETRIES = 3;
1172
+ let retryCount = 0;
1173
+ let lastError = null;
1174
+ // Retry loop to handle potential database connection issues
1175
+ while (retryCount < MAX_RETRIES) {
1176
+ try {
1177
+ const [modules, client] = await Promise.all([
1178
+ (0, client_1.getApolloModules)(),
1179
+ globalClient
1180
+ ? Promise.resolve(globalClient)
1181
+ : client_1.client
1182
+ ]);
1183
+ const { gql, ApolloError } = modules;
1184
+ const FIND_MANY_ACTION = gql `
1185
+ query findManyAction($where: ActionWhereInput!) {
1186
+ actions(where: $where) {
1187
+ ${selectionSet}
949
1188
  }
950
- else {
951
- return [];
952
- }
953
- }
954
- catch (error) {
955
- if (error instanceof ApolloError && error.message === 'No Action found') {
956
- return null;
1189
+ }`;
1190
+ const variables = {
1191
+ where: whereInput ? whereInput : {
1192
+ id: props.id !== undefined ? {
1193
+ equals: props.id
1194
+ } : undefined,
1195
+ tradeId: props.tradeId !== undefined ? {
1196
+ equals: props.tradeId
1197
+ } : undefined,
1198
+ },
1199
+ };
1200
+ const filteredVariables = (0, utils_1.removeUndefinedProps)(variables);
1201
+ const response = await client.query({
1202
+ query: FIND_MANY_ACTION,
1203
+ variables: filteredVariables,
1204
+ fetchPolicy: 'network-only', // Force network request to avoid stale cache
1205
+ });
1206
+ if (response.errors && response.errors.length > 0)
1207
+ throw new Error(response.errors[0].message);
1208
+ if (response && response.data && response.data.actions) {
1209
+ return response.data.actions;
1210
+ }
1211
+ else {
1212
+ return [];
1213
+ }
957
1214
  }
958
- else {
959
- console.error('Error in getAction:', error);
1215
+ catch (error) {
1216
+ lastError = error;
1217
+ // Check if this is a "No record found" error - this is an expected condition, not a failure
1218
+ if (error.message === 'No Action found') {
1219
+ return null;
1220
+ }
1221
+ // Check if this is a database connection error that we should retry
1222
+ const isConnectionError = ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Server has closed the connection')) ||
1223
+ ((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('Cannot reach database server')) ||
1224
+ ((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection timed out')) ||
1225
+ ((_d = error.message) === null || _d === void 0 ? void 0 : _d.includes('Accelerate')) || // Prisma Accelerate proxy errors
1226
+ (error.networkError && ((_e = error.networkError.message) === null || _e === void 0 ? void 0 : _e.includes('Failed to fetch')));
1227
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
1228
+ retryCount++;
1229
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
1230
+ console.warn("Database connection error, retrying...");
1231
+ await new Promise(resolve => setTimeout(resolve, delay));
1232
+ continue;
1233
+ }
1234
+ // Log the error and rethrow
1235
+ console.error("Database error occurred:", error);
960
1236
  throw error;
961
1237
  }
962
1238
  }
1239
+ // If we exhausted retries, throw the last error
1240
+ throw lastError;
963
1241
  }
964
1242
  };
965
1243
  //# sourceMappingURL=Action.js.map