@dbos-inc/typeorm-datasource 3.0.7-preview → 3.0.8-preview

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.
@@ -1,249 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TypeOrmDataSource = exports.IsolationLevel = void 0;
4
- const dbos_sdk_1 = require("@dbos-inc/dbos-sdk");
5
- const datasource_1 = require("@dbos-inc/dbos-sdk/datasource");
6
- Object.defineProperty(exports, "IsolationLevel", { enumerable: true, get: function () { return datasource_1.PGIsolationLevel; } });
7
- const typeorm_1 = require("typeorm");
8
- const async_hooks_1 = require("async_hooks");
9
- const superjson_1 = require("superjson");
10
- const asyncLocalCtx = new async_hooks_1.AsyncLocalStorage();
11
- function getCurrentDSContextStore() {
12
- return asyncLocalCtx.getStore();
13
- }
14
- function assertCurrentDSContextStore() {
15
- const ctx = getCurrentDSContextStore();
16
- if (!ctx)
17
- throw new dbos_sdk_1.Error.DBOSInvalidWorkflowTransitionError('Invalid use of TypeOrmDs outside of a `transaction`');
18
- return ctx;
19
- }
20
- class TypeOrmDSTH {
21
- name;
22
- config;
23
- entities;
24
- dsType = 'TypeOrm';
25
- dataSource;
26
- constructor(name, config,
27
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
28
- entities) {
29
- this.name = name;
30
- this.config = config;
31
- this.entities = entities;
32
- }
33
- async createInstance() {
34
- const ds = new typeorm_1.DataSource({
35
- type: 'postgres',
36
- url: this.config.connectionString,
37
- connectTimeoutMS: this.config.connectionTimeoutMillis,
38
- entities: this.entities,
39
- poolSize: this.config.max,
40
- });
41
- await ds.initialize();
42
- return ds;
43
- }
44
- async initialize() {
45
- this.dataSource = await this.createInstance();
46
- return Promise.resolve();
47
- }
48
- async destroy() {
49
- await this.dataSource?.destroy();
50
- }
51
- async #checkExecution(client, workflowID, funcNum) {
52
- const { rows } = await client.query(`SELECT output
53
- FROM dbos.transaction_completion
54
- WHERE workflow_id=$1 AND function_num=$2;`, [workflowID, funcNum]);
55
- if (rows.length !== 1) {
56
- return undefined;
57
- }
58
- if (rows[0].output === null) {
59
- return undefined;
60
- }
61
- return { res: superjson_1.SuperJSON.parse(rows[0].output) };
62
- }
63
- async #recordOutput(client, workflowID, funcNum, output) {
64
- const serialOutput = superjson_1.SuperJSON.stringify(output);
65
- await client.query(`INSERT INTO dbos.transaction_completion (
66
- workflow_id,
67
- function_num,
68
- output,
69
- created_at
70
- ) VALUES ($1, $2, $3, $4)`, [workflowID, funcNum, serialOutput, Date.now()]);
71
- }
72
- async #recordError(client, workflowID, funcNum, error) {
73
- const serialError = superjson_1.SuperJSON.stringify(error);
74
- await client.query(`INSERT INTO dbos.transaction_completion (
75
- workflow_id,
76
- function_num,
77
- error,
78
- created_at
79
- ) VALUES ($1, $2, $3, $4)`, [workflowID, funcNum, serialError, Date.now()]);
80
- }
81
- /* Required by base class */
82
- async invokeTransactionFunction(config, target, func, ...args) {
83
- const isolationLevel = config?.isolationLevel ?? 'SERIALIZABLE';
84
- const readOnly = config?.readOnly ? true : false;
85
- const wfid = dbos_sdk_1.DBOS.workflowID;
86
- const funcnum = dbos_sdk_1.DBOS.stepID;
87
- const funcname = func.name;
88
- // Retry loop if appropriate
89
- let retryWaitMillis = 1;
90
- const backoffFactor = 1.5;
91
- const maxRetryWaitMs = 2000; // Maximum wait 2 seconds.
92
- const shouldCheckOutput = false;
93
- if (this.dataSource === undefined) {
94
- throw new dbos_sdk_1.Error.DBOSInvalidWorkflowTransitionError('Invalid use of Datasource');
95
- }
96
- while (true) {
97
- let failedForRetriableReasons = false;
98
- try {
99
- const result = this.dataSource.transaction(isolationLevel, async (transactionEntityManager) => {
100
- if (this.dataSource === undefined) {
101
- throw new dbos_sdk_1.Error.DBOSInvalidWorkflowTransitionError('Invalid use of Datasource');
102
- }
103
- if (shouldCheckOutput && !readOnly && wfid) {
104
- const executionResult = await this.#checkExecution(this.dataSource, wfid, funcnum);
105
- if (executionResult) {
106
- dbos_sdk_1.DBOS.span?.setAttribute('cached', true);
107
- return executionResult.res;
108
- }
109
- }
110
- const result = await asyncLocalCtx.run({ typeOrmEntityManager: transactionEntityManager }, async () => {
111
- return await func.call(target, ...args);
112
- });
113
- // Save result
114
- try {
115
- if (!readOnly && wfid) {
116
- await this.#recordOutput(this.dataSource, wfid, funcnum, result);
117
- }
118
- }
119
- catch (e) {
120
- const error = e;
121
- await this.#recordError(this.dataSource, wfid, funcnum, error);
122
- // Aside from a connectivity error, two kinds of error are anticipated here:
123
- // 1. The transaction is marked failed, but the user code did not throw.
124
- // Bad on them. We will throw an error (this will get recorded) and not retry.
125
- // 2. There was a key conflict in the statement, and we need to use the fetched output
126
- if ((0, datasource_1.isPGFailedSqlTransactionError)(error)) {
127
- dbos_sdk_1.DBOS.logger.error(`In workflow ${wfid}, Postgres aborted a transaction but the function '${funcname}' did not raise an exception. Please ensure that the transaction method raises an exception if the database transaction is aborted.`);
128
- failedForRetriableReasons = false;
129
- throw new dbos_sdk_1.Error.DBOSFailedSqlTransactionError(wfid, funcname);
130
- }
131
- else if ((0, datasource_1.isPGKeyConflictError)(error)) {
132
- throw new dbos_sdk_1.Error.DBOSWorkflowConflictError(`In workflow ${wfid}, Postgres raised a key conflict error in transaction '${funcname}'. This is not retriable, but the output will be fetched from the database.`);
133
- }
134
- else {
135
- dbos_sdk_1.DBOS.logger.error(`Unexpected error raised in transaction '${funcname}: ${error}`);
136
- failedForRetriableReasons = false;
137
- throw error;
138
- }
139
- }
140
- return result;
141
- });
142
- return result;
143
- }
144
- catch (e) {
145
- const err = e;
146
- if (failedForRetriableReasons || (0, datasource_1.isPGRetriableTransactionError)(err)) {
147
- dbos_sdk_1.DBOS.span?.addEvent('TXN SERIALIZATION FAILURE', { retryWaitMillis: retryWaitMillis }, performance.now());
148
- // Retry serialization failures.
149
- await dbos_sdk_1.DBOS.sleepms(retryWaitMillis);
150
- retryWaitMillis *= backoffFactor;
151
- retryWaitMillis = retryWaitMillis < maxRetryWaitMs ? retryWaitMillis : maxRetryWaitMs;
152
- continue;
153
- }
154
- else {
155
- throw err;
156
- }
157
- }
158
- }
159
- }
160
- }
161
- class TypeOrmDataSource {
162
- name;
163
- config;
164
- entities;
165
- #provider;
166
- constructor(name, config,
167
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
168
- entities) {
169
- this.name = name;
170
- this.config = config;
171
- this.entities = entities;
172
- this.#provider = new TypeOrmDSTH(name, config, entities);
173
- (0, datasource_1.registerDataSource)(this.#provider);
174
- }
175
- // User calls this... DBOS not directly involved...
176
- static get entityManager() {
177
- const ctx = assertCurrentDSContextStore();
178
- if (!dbos_sdk_1.DBOS.isInTransaction())
179
- throw new dbos_sdk_1.Error.DBOSInvalidWorkflowTransitionError('Invalid use of `TypeOrmDataSource.entityManager` outside of a `transaction`');
180
- return ctx.typeOrmEntityManager;
181
- }
182
- async initializeInternalSchema() {
183
- const ds = await this.#provider.createInstance();
184
- try {
185
- await ds.query(datasource_1.createTransactionCompletionSchemaPG);
186
- await ds.query(datasource_1.createTransactionCompletionTablePG);
187
- }
188
- catch (e) {
189
- const error = e;
190
- throw new dbos_sdk_1.Error.DBOSError(`Unexpected error initializing schema: ${error.message}`);
191
- }
192
- finally {
193
- try {
194
- await ds.destroy();
195
- }
196
- catch (e) { }
197
- }
198
- }
199
- /**
200
- * Run `callback` as a transaction against this DataSource
201
- * @param callback Function to run within a transactional context
202
- * @param funcName Name to record for the transaction
203
- * @param config Transaction configuration (isolation, etc)
204
- * @returns Return value from `callback`
205
- */
206
- async runTransaction(callback, funcName, config) {
207
- return await (0, datasource_1.runTransaction)(callback, funcName, { dsName: this.name, config });
208
- }
209
- /**
210
- * Register function as DBOS transaction, to be called within the context
211
- * of a transaction on this data source.
212
- *
213
- * @param func Function to wrap
214
- * @param target Name of function
215
- * @param config Transaction settings
216
- * @returns Wrapped function, to be called instead of `func`
217
- */
218
- registerTransaction(func, name, config) {
219
- return (0, datasource_1.registerTransaction)(this.name, func, { name }, config);
220
- }
221
- /**
222
- * Decorator establishing function as a transaction
223
- */
224
- transaction(config) {
225
- // eslint-disable-next-line @typescript-eslint/no-this-alias
226
- const ds = this;
227
- return function decorator(_target, propertyKey, descriptor) {
228
- if (!descriptor.value) {
229
- throw new dbos_sdk_1.Error.DBOSError('Use of decorator when original method is undefined');
230
- }
231
- descriptor.value = ds.registerTransaction(descriptor.value, propertyKey.toString(), config);
232
- return descriptor;
233
- };
234
- }
235
- /**
236
- * For testing: Use DataSource.syncronize to install the user schema
237
- */
238
- async createSchema() {
239
- const ds = await this.#provider.createInstance();
240
- try {
241
- await ds.synchronize();
242
- }
243
- finally {
244
- await ds.destroy();
245
- }
246
- }
247
- }
248
- exports.TypeOrmDataSource = TypeOrmDataSource;
249
- //# sourceMappingURL=typeorm_datasource.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"typeorm_datasource.js","sourceRoot":"","sources":["../../src/typeorm_datasource.ts"],"names":[],"mappings":";;;AACA,iDAAiD;AACjD,8DAauC;AA2B9B,+FA/Ba,6BAAc,OA+Bb;AA1BvB,qCAAoD;AACpD,6CAAgD;AAChD,yCAAsC;AAKtC,MAAM,aAAa,GAAG,IAAI,+BAAiB,EAAuB,CAAC;AAEnE,SAAS,wBAAwB;IAC/B,OAAO,aAAa,CAAC,QAAQ,EAAE,CAAC;AAClC,CAAC;AAED,SAAS,2BAA2B;IAClC,MAAM,GAAG,GAAG,wBAAwB,EAAE,CAAC;IACvC,IAAI,CAAC,GAAG;QAAE,MAAM,IAAI,gBAAK,CAAC,kCAAkC,CAAC,qDAAqD,CAAC,CAAC;IACpH,OAAO,GAAG,CAAC;AACb,CAAC;AAWD,MAAM,WAAW;IAKJ;IACA;IAEA;IAPF,MAAM,GAAG,SAAS,CAAC;IAC5B,UAAU,CAAyB;IAEnC,YACW,IAAY,EACZ,MAAkB;IAC3B,sEAAsE;IAC7D,QAAoB;QAHpB,SAAI,GAAJ,IAAI,CAAQ;QACZ,WAAM,GAAN,MAAM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CAAC;IACH,CAAC;CACF;AAlGD,8CAkGC"}
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export { IsolationLevel, TypeOrmDataSource, TypeOrmTransactionConfig } from './typeorm_datasource';
@@ -1,332 +0,0 @@
1
- import { PoolConfig } from 'pg';
2
- import { DBOS, Error } from '@dbos-inc/dbos-sdk';
3
- import {
4
- type DataSourceTransactionHandler,
5
- createTransactionCompletionSchemaPG,
6
- createTransactionCompletionTablePG,
7
- isPGRetriableTransactionError,
8
- isPGKeyConflictError,
9
- isPGFailedSqlTransactionError,
10
- registerTransaction,
11
- runTransaction,
12
- PGIsolationLevel as IsolationLevel,
13
- PGTransactionConfig as TypeOrmTransactionConfig,
14
- DBOSDataSource,
15
- registerDataSource,
16
- } from '@dbos-inc/dbos-sdk/datasource';
17
- import { DataSource, EntityManager } from 'typeorm';
18
- import { AsyncLocalStorage } from 'async_hooks';
19
- import { SuperJSON } from 'superjson';
20
-
21
- interface DBOSTypeOrmLocalCtx {
22
- typeOrmEntityManager: EntityManager;
23
- }
24
- const asyncLocalCtx = new AsyncLocalStorage<DBOSTypeOrmLocalCtx>();
25
-
26
- function getCurrentDSContextStore(): DBOSTypeOrmLocalCtx | undefined {
27
- return asyncLocalCtx.getStore();
28
- }
29
-
30
- function assertCurrentDSContextStore(): DBOSTypeOrmLocalCtx {
31
- const ctx = getCurrentDSContextStore();
32
- if (!ctx) throw new Error.DBOSInvalidWorkflowTransitionError('Invalid use of TypeOrmDs outside of a `transaction`');
33
- return ctx;
34
- }
35
-
36
- interface transaction_completion {
37
- workflow_id: string;
38
- function_num: number;
39
- output: string | null;
40
- error: string | null;
41
- }
42
-
43
- export { IsolationLevel, TypeOrmTransactionConfig };
44
-
45
- class TypeOrmDSTH implements DataSourceTransactionHandler {
46
- readonly dsType = 'TypeOrm';
47
- dataSource: DataSource | undefined;
48
-
49
- constructor(
50
- readonly name: string,
51
- readonly config: PoolConfig,
52
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
53
- readonly entities: Function[],
54
- ) {}
55
-
56
- async createInstance(): Promise<DataSource> {
57
- const ds = new DataSource({
58
- type: 'postgres',
59
- url: this.config.connectionString,
60
- connectTimeoutMS: this.config.connectionTimeoutMillis,
61
- entities: this.entities,
62
- poolSize: this.config.max,
63
- });
64
- await ds.initialize();
65
- return ds;
66
- }
67
-
68
- async initialize(): Promise<void> {
69
- this.dataSource = await this.createInstance();
70
-
71
- return Promise.resolve();
72
- }
73
-
74
- async destroy(): Promise<void> {
75
- await this.dataSource?.destroy();
76
- }
77
-
78
- async #checkExecution<R>(
79
- client: DataSource,
80
- workflowID: string,
81
- funcNum: number,
82
- ): Promise<
83
- | {
84
- res: R;
85
- }
86
- | undefined
87
- > {
88
- type TxOutputRow = Pick<transaction_completion, 'output'> & {
89
- recorded: boolean;
90
- };
91
-
92
- const { rows } = await client.query<{ rows: TxOutputRow[] }>(
93
- `SELECT output
94
- FROM dbos.transaction_completion
95
- WHERE workflow_id=$1 AND function_num=$2;`,
96
- [workflowID, funcNum],
97
- );
98
-
99
- if (rows.length !== 1) {
100
- return undefined;
101
- }
102
-
103
- if (rows[0].output === null) {
104
- return undefined;
105
- }
106
-
107
- return { res: SuperJSON.parse(rows[0].output) };
108
- }
109
-
110
- async #recordOutput<R>(client: DataSource, workflowID: string, funcNum: number, output: R): Promise<void> {
111
- const serialOutput = SuperJSON.stringify(output);
112
- await client.query<{ rows: transaction_completion[] }>(
113
- `INSERT INTO dbos.transaction_completion (
114
- workflow_id,
115
- function_num,
116
- output,
117
- created_at
118
- ) VALUES ($1, $2, $3, $4)`,
119
- [workflowID, funcNum, serialOutput, Date.now()],
120
- );
121
- }
122
-
123
- async #recordError<R>(client: DataSource, workflowID: string, funcNum: number, error: R): Promise<void> {
124
- const serialError = SuperJSON.stringify(error);
125
- await client.query<{ rows: transaction_completion[] }>(
126
- `INSERT INTO dbos.transaction_completion (
127
- workflow_id,
128
- function_num,
129
- error,
130
- created_at
131
- ) VALUES ($1, $2, $3, $4)`,
132
- [workflowID, funcNum, serialError, Date.now()],
133
- );
134
- }
135
-
136
- /* Required by base class */
137
- async invokeTransactionFunction<This, Args extends unknown[], Return>(
138
- config: TypeOrmTransactionConfig,
139
- target: This,
140
- func: (this: This, ...args: Args) => Promise<Return>,
141
- ...args: Args
142
- ): Promise<Return> {
143
- const isolationLevel = config?.isolationLevel ?? 'SERIALIZABLE';
144
-
145
- const readOnly = config?.readOnly ? true : false;
146
-
147
- const wfid = DBOS.workflowID!;
148
- const funcnum = DBOS.stepID!;
149
- const funcname = func.name;
150
-
151
- // Retry loop if appropriate
152
- let retryWaitMillis = 1;
153
- const backoffFactor = 1.5;
154
- const maxRetryWaitMs = 2000; // Maximum wait 2 seconds.
155
- const shouldCheckOutput = false;
156
-
157
- if (this.dataSource === undefined) {
158
- throw new Error.DBOSInvalidWorkflowTransitionError('Invalid use of Datasource');
159
- }
160
-
161
- while (true) {
162
- let failedForRetriableReasons = false;
163
-
164
- try {
165
- const result = this.dataSource.transaction(isolationLevel, async (transactionEntityManager: EntityManager) => {
166
- if (this.dataSource === undefined) {
167
- throw new Error.DBOSInvalidWorkflowTransitionError('Invalid use of Datasource');
168
- }
169
-
170
- if (shouldCheckOutput && !readOnly && wfid) {
171
- const executionResult = await this.#checkExecution<Return>(this.dataSource, wfid, funcnum);
172
-
173
- if (executionResult) {
174
- DBOS.span?.setAttribute('cached', true);
175
- return executionResult.res;
176
- }
177
- }
178
-
179
- const result = await asyncLocalCtx.run({ typeOrmEntityManager: transactionEntityManager }, async () => {
180
- return await func.call(target, ...args);
181
- });
182
-
183
- // Save result
184
- try {
185
- if (!readOnly && wfid) {
186
- await this.#recordOutput(this.dataSource, wfid, funcnum, result);
187
- }
188
- } catch (e) {
189
- const error = e as Error;
190
- await this.#recordError(this.dataSource, wfid, funcnum, error);
191
-
192
- // Aside from a connectivity error, two kinds of error are anticipated here:
193
- // 1. The transaction is marked failed, but the user code did not throw.
194
- // Bad on them. We will throw an error (this will get recorded) and not retry.
195
- // 2. There was a key conflict in the statement, and we need to use the fetched output
196
- if (isPGFailedSqlTransactionError(error)) {
197
- DBOS.logger.error(
198
- `In workflow ${wfid}, Postgres aborted a transaction but the function '${funcname}' did not raise an exception. Please ensure that the transaction method raises an exception if the database transaction is aborted.`,
199
- );
200
- failedForRetriableReasons = false;
201
- throw new Error.DBOSFailedSqlTransactionError(wfid, funcname);
202
- } else if (isPGKeyConflictError(error)) {
203
- throw new Error.DBOSWorkflowConflictError(
204
- `In workflow ${wfid}, Postgres raised a key conflict error in transaction '${funcname}'. This is not retriable, but the output will be fetched from the database.`,
205
- );
206
- } else {
207
- DBOS.logger.error(`Unexpected error raised in transaction '${funcname}: ${error}`);
208
- failedForRetriableReasons = false;
209
- throw error;
210
- }
211
- }
212
-
213
- return result;
214
- });
215
-
216
- return result;
217
- } catch (e) {
218
- const err = e as Error;
219
- if (failedForRetriableReasons || isPGRetriableTransactionError(err)) {
220
- DBOS.span?.addEvent('TXN SERIALIZATION FAILURE', { retryWaitMillis: retryWaitMillis }, performance.now());
221
- // Retry serialization failures.
222
- await DBOS.sleepms(retryWaitMillis);
223
- retryWaitMillis *= backoffFactor;
224
- retryWaitMillis = retryWaitMillis < maxRetryWaitMs ? retryWaitMillis : maxRetryWaitMs;
225
- continue;
226
- } else {
227
- throw err;
228
- }
229
- }
230
- }
231
- }
232
- }
233
-
234
- export class TypeOrmDataSource implements DBOSDataSource<TypeOrmTransactionConfig> {
235
- #provider: TypeOrmDSTH;
236
- constructor(
237
- readonly name: string,
238
- readonly config: PoolConfig,
239
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
240
- readonly entities: Function[],
241
- ) {
242
- this.#provider = new TypeOrmDSTH(name, config, entities);
243
- registerDataSource(this.#provider);
244
- }
245
-
246
- // User calls this... DBOS not directly involved...
247
- static get entityManager(): EntityManager {
248
- const ctx = assertCurrentDSContextStore();
249
- if (!DBOS.isInTransaction())
250
- throw new Error.DBOSInvalidWorkflowTransitionError(
251
- 'Invalid use of `TypeOrmDataSource.entityManager` outside of a `transaction`',
252
- );
253
- return ctx.typeOrmEntityManager;
254
- }
255
-
256
- async initializeInternalSchema(): Promise<void> {
257
- const ds = await this.#provider.createInstance();
258
-
259
- try {
260
- await ds.query(createTransactionCompletionSchemaPG);
261
- await ds.query(createTransactionCompletionTablePG);
262
- } catch (e) {
263
- const error = e as Error;
264
- throw new Error.DBOSError(`Unexpected error initializing schema: ${error.message}`);
265
- } finally {
266
- try {
267
- await ds.destroy();
268
- } catch (e) {}
269
- }
270
- }
271
-
272
- /**
273
- * Run `callback` as a transaction against this DataSource
274
- * @param callback Function to run within a transactional context
275
- * @param funcName Name to record for the transaction
276
- * @param config Transaction configuration (isolation, etc)
277
- * @returns Return value from `callback`
278
- */
279
- async runTransaction<T>(callback: () => Promise<T>, funcName: string, config?: TypeOrmTransactionConfig) {
280
- return await runTransaction(callback, funcName, { dsName: this.name, config });
281
- }
282
-
283
- /**
284
- * Register function as DBOS transaction, to be called within the context
285
- * of a transaction on this data source.
286
- *
287
- * @param func Function to wrap
288
- * @param target Name of function
289
- * @param config Transaction settings
290
- * @returns Wrapped function, to be called instead of `func`
291
- */
292
- registerTransaction<This, Args extends unknown[], Return>(
293
- func: (this: This, ...args: Args) => Promise<Return>,
294
- name: string,
295
- config?: TypeOrmTransactionConfig,
296
- ): (this: This, ...args: Args) => Promise<Return> {
297
- return registerTransaction(this.name, func, { name }, config);
298
- }
299
-
300
- /**
301
- * Decorator establishing function as a transaction
302
- */
303
- transaction(config?: TypeOrmTransactionConfig) {
304
- // eslint-disable-next-line @typescript-eslint/no-this-alias
305
- const ds = this;
306
- return function decorator<This, Args extends unknown[], Return>(
307
- _target: object,
308
- propertyKey: string,
309
- descriptor: TypedPropertyDescriptor<(this: This, ...args: Args) => Promise<Return>>,
310
- ) {
311
- if (!descriptor.value) {
312
- throw new Error.DBOSError('Use of decorator when original method is undefined');
313
- }
314
-
315
- descriptor.value = ds.registerTransaction(descriptor.value, propertyKey.toString(), config);
316
-
317
- return descriptor;
318
- };
319
- }
320
-
321
- /**
322
- * For testing: Use DataSource.syncronize to install the user schema
323
- */
324
- async createSchema() {
325
- const ds = await this.#provider.createInstance();
326
- try {
327
- await ds.synchronize();
328
- } finally {
329
- await ds.destroy();
330
- }
331
- }
332
- }
@@ -1,30 +0,0 @@
1
- import { Client } from 'pg';
2
- import { DBOSConfig } from '@dbos-inc/dbos-sdk';
3
-
4
- export async function setUpDBOSTestDb(config: DBOSConfig) {
5
- const pgSystemClient = new Client({
6
- user: config.poolConfig?.user,
7
- port: config.poolConfig?.port,
8
- host: config.poolConfig?.host,
9
- password: config.poolConfig?.password,
10
- database: 'postgres',
11
- });
12
-
13
- try {
14
- await pgSystemClient.connect();
15
- await pgSystemClient.query(`DROP DATABASE IF EXISTS ${config.poolConfig?.database};`);
16
- await pgSystemClient.query(`CREATE DATABASE ${config.poolConfig?.database};`);
17
- await pgSystemClient.query(`DROP DATABASE IF EXISTS ${config.system_database};`);
18
- await pgSystemClient.end();
19
- } catch (e) {
20
- if (e instanceof AggregateError) {
21
- console.error(`Test database setup failed: AggregateError containing ${e.errors.length} errors:`);
22
- e.errors.forEach((err, index) => {
23
- console.error(` Error ${index + 1}:`, err);
24
- });
25
- } else {
26
- console.error(`Test database setup failed:`, e);
27
- }
28
- throw e;
29
- }
30
- }