@dbos-inc/typeorm-datasource 3.0.8-preview → 3.0.8-preview.g493d2d1c2b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js DELETED
@@ -1,226 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TypeOrmDataSource = void 0;
4
- const dbos_sdk_1 = require("@dbos-inc/dbos-sdk");
5
- const datasource_1 = require("@dbos-inc/dbos-sdk/datasource");
6
- const typeorm_1 = require("typeorm");
7
- const async_hooks_1 = require("async_hooks");
8
- const superjson_1 = require("superjson");
9
- const asyncLocalCtx = new async_hooks_1.AsyncLocalStorage();
10
- class TypeOrmTransactionHandler {
11
- name;
12
- config;
13
- entities;
14
- dsType = 'TypeOrm';
15
- #dataSourceField;
16
- constructor(name, config,
17
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
18
- entities) {
19
- this.name = name;
20
- this.config = config;
21
- this.entities = entities;
22
- }
23
- static async createInstance(config,
24
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
25
- entities) {
26
- const ds = new typeorm_1.DataSource({
27
- type: 'postgres',
28
- entities: entities,
29
- url: config.connectionString,
30
- host: config.host,
31
- port: config.port,
32
- username: config.user,
33
- // password: config.password,
34
- database: config.database,
35
- // ssl: config.ssl,
36
- connectTimeoutMS: config.connectionTimeoutMillis,
37
- poolSize: config.max,
38
- });
39
- await ds.initialize();
40
- return ds;
41
- }
42
- async initialize() {
43
- const ds = this.#dataSourceField;
44
- this.#dataSourceField = await TypeOrmTransactionHandler.createInstance(this.config, this.entities);
45
- await ds?.destroy();
46
- }
47
- async destroy() {
48
- const ds = this.#dataSourceField;
49
- this.#dataSourceField = undefined;
50
- await ds?.destroy();
51
- }
52
- get #dataSource() {
53
- if (!this.#dataSourceField) {
54
- throw new Error(`DataSource ${this.name} is not initialized.`);
55
- }
56
- return this.#dataSourceField;
57
- }
58
- async #checkExecution(workflowID, stepID) {
59
- const rows = await this.#dataSource.query(`SELECT output, error FROM dbos.transaction_completion
60
- WHERE workflow_id=$1 AND function_num=$2;`, [workflowID, stepID]);
61
- if (rows.length !== 1) {
62
- return undefined;
63
- }
64
- if (rows[0].output === null) {
65
- return undefined;
66
- }
67
- const { output, error } = rows[0];
68
- return error !== null ? { error } : { output };
69
- }
70
- static async #recordOutput(entityManager, workflowID, stepID, output) {
71
- try {
72
- await entityManager.query(`INSERT INTO dbos.transaction_completion (workflow_id, function_num, output)
73
- VALUES ($1, $2, $3)`, [workflowID, stepID, output]);
74
- }
75
- catch (error) {
76
- if ((0, datasource_1.isPGKeyConflictError)(error)) {
77
- throw new dbos_sdk_1.DBOSWorkflowConflictError(workflowID);
78
- }
79
- else {
80
- throw error;
81
- }
82
- }
83
- }
84
- async #recordError(workflowID, stepID, error) {
85
- try {
86
- await this.#dataSource.query(`INSERT INTO dbos.transaction_completion (workflow_id, function_num, error)
87
- VALUES ($1, $2, $3)`, [workflowID, stepID, error]);
88
- }
89
- catch (error) {
90
- if ((0, datasource_1.isPGKeyConflictError)(error)) {
91
- throw new dbos_sdk_1.DBOSWorkflowConflictError(workflowID);
92
- }
93
- else {
94
- throw error;
95
- }
96
- }
97
- }
98
- /* Required by base class */
99
- async invokeTransactionFunction(config, target, func, ...args) {
100
- const workflowID = dbos_sdk_1.DBOS.workflowID;
101
- const stepID = dbos_sdk_1.DBOS.stepID;
102
- if (workflowID !== undefined && stepID === undefined) {
103
- throw new Error('DBOS.stepID is undefined inside a workflow.');
104
- }
105
- const isolationLevel = config?.isolationLevel ?? 'READ COMMITTED';
106
- const readOnly = config?.readOnly ? true : false;
107
- const saveResults = !readOnly && workflowID !== undefined;
108
- // Retry loop if appropriate
109
- let retryWaitMS = 1;
110
- const backoffFactor = 1.5;
111
- const maxRetryWaitMS = 2000; // Maximum wait 2 seconds.
112
- while (true) {
113
- // Check to see if this tx has already been executed
114
- const previousResult = saveResults ? await this.#checkExecution(workflowID, stepID) : undefined;
115
- if (previousResult) {
116
- dbos_sdk_1.DBOS.span?.setAttribute('cached', true);
117
- if ('error' in previousResult) {
118
- throw superjson_1.SuperJSON.parse(previousResult.error);
119
- }
120
- return (previousResult.output ? superjson_1.SuperJSON.parse(previousResult.output) : null);
121
- }
122
- try {
123
- const result = await this.#dataSource.transaction(isolationLevel, async (entityManager) => {
124
- if (readOnly) {
125
- await entityManager.query('SET TRANSACTION READ ONLY');
126
- }
127
- const result = await asyncLocalCtx.run({ entityManager: entityManager }, async () => {
128
- return await func.call(target, ...args);
129
- });
130
- // save the output of read/write transactions
131
- if (saveResults) {
132
- await TypeOrmTransactionHandler.#recordOutput(entityManager, workflowID, stepID, superjson_1.SuperJSON.stringify(result));
133
- }
134
- return result;
135
- });
136
- return result;
137
- }
138
- catch (error) {
139
- if ((0, datasource_1.isPGRetriableTransactionError)(error)) {
140
- dbos_sdk_1.DBOS.span?.addEvent('TXN SERIALIZATION FAILURE', { retryWaitMillis: retryWaitMS }, performance.now());
141
- // Retry serialization failures.
142
- await new Promise((resolve) => setTimeout(resolve, retryWaitMS));
143
- retryWaitMS = Math.min(retryWaitMS * backoffFactor, maxRetryWaitMS);
144
- continue;
145
- }
146
- else {
147
- if (saveResults) {
148
- const message = superjson_1.SuperJSON.stringify(error);
149
- await this.#recordError(workflowID, stepID, message);
150
- }
151
- throw error;
152
- }
153
- }
154
- }
155
- }
156
- }
157
- class TypeOrmDataSource {
158
- name;
159
- // User calls this... DBOS not directly involved...
160
- static get entityManager() {
161
- if (!dbos_sdk_1.DBOS.isInTransaction()) {
162
- throw new Error('Invalid use of TypeOrmDataSource.entityManager outside of a DBOS transaction');
163
- }
164
- const ctx = asyncLocalCtx.getStore();
165
- if (!ctx) {
166
- throw new Error('Invalid use of TypeOrmDataSource.entityManager outside of a DBOS transaction');
167
- }
168
- return ctx.entityManager;
169
- }
170
- static async initializeInternalSchema(config) {
171
- const ds = await TypeOrmTransactionHandler.createInstance(config, []);
172
- try {
173
- await ds.query(datasource_1.createTransactionCompletionSchemaPG);
174
- await ds.query(datasource_1.createTransactionCompletionTablePG);
175
- }
176
- finally {
177
- await ds.destroy();
178
- }
179
- }
180
- #provider;
181
- constructor(name, config,
182
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
183
- entities) {
184
- this.name = name;
185
- this.#provider = new TypeOrmTransactionHandler(name, config, entities);
186
- (0, datasource_1.registerDataSource)(this.#provider);
187
- }
188
- /**
189
- * Run `callback` as a transaction against this DataSource
190
- * @param callback Function to run within a transactional context
191
- * @param funcName Name to record for the transaction
192
- * @param config Transaction configuration (isolation, etc)
193
- * @returns Return value from `callback`
194
- */
195
- async runTransaction(callback, funcName, config) {
196
- return await (0, datasource_1.runTransaction)(callback, funcName, { dsName: this.name, config });
197
- }
198
- /**
199
- * Register function as DBOS transaction, to be called within the context
200
- * of a transaction on this data source.
201
- *
202
- * @param func Function to wrap
203
- * @param target Name of function
204
- * @param config Transaction settings
205
- * @returns Wrapped function, to be called instead of `func`
206
- */
207
- registerTransaction(func, config, name) {
208
- return (0, datasource_1.registerTransaction)(this.name, func, { name: name ?? func.name }, config);
209
- }
210
- /**
211
- * Decorator establishing function as a transaction
212
- */
213
- transaction(config) {
214
- // eslint-disable-next-line @typescript-eslint/no-this-alias
215
- const ds = this;
216
- return function decorator(_target, propertyKey, descriptor) {
217
- if (!descriptor.value) {
218
- throw new Error('Use of decorator when original method is undefined');
219
- }
220
- descriptor.value = ds.registerTransaction(descriptor.value, config, String(propertyKey));
221
- return descriptor;
222
- };
223
- }
224
- }
225
- exports.TypeOrmDataSource = TypeOrmDataSource;
226
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AACA,iDAAqE;AACrE,8DAWuC;AACvC,qCAAoD;AACpD,6CAAgD;AAChD,yCAAsC;AAMtC,MAAM,aAAa,GAAG,IAAI,+BAAiB,EAAuB,CAAC;AASnE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AE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;YAEzF,OAAO,UAAU,CAAC;QACpB,CAAC,CAAC;IACJ,CAAC;CACF;AApFD,8CAoFC"}
package/index.ts DELETED
@@ -1,306 +0,0 @@
1
- import { PoolConfig } from 'pg';
2
- import { DBOS, DBOSWorkflowConflictError } from '@dbos-inc/dbos-sdk';
3
- import {
4
- type DataSourceTransactionHandler,
5
- createTransactionCompletionSchemaPG,
6
- createTransactionCompletionTablePG,
7
- isPGRetriableTransactionError,
8
- isPGKeyConflictError,
9
- registerTransaction,
10
- runTransaction,
11
- DBOSDataSource,
12
- registerDataSource,
13
- PGTransactionConfig,
14
- } from '@dbos-inc/dbos-sdk/datasource';
15
- import { DataSource, EntityManager } from 'typeorm';
16
- import { AsyncLocalStorage } from 'async_hooks';
17
- import { SuperJSON } from 'superjson';
18
-
19
- interface DBOSTypeOrmLocalCtx {
20
- entityManager: EntityManager;
21
- }
22
-
23
- const asyncLocalCtx = new AsyncLocalStorage<DBOSTypeOrmLocalCtx>();
24
-
25
- interface transaction_completion {
26
- workflow_id: string;
27
- function_num: number;
28
- output: string | null;
29
- error: string | null;
30
- }
31
-
32
- class TypeOrmTransactionHandler implements DataSourceTransactionHandler {
33
- readonly dsType = 'TypeOrm';
34
- #dataSourceField: DataSource | undefined;
35
-
36
- constructor(
37
- readonly name: string,
38
- private readonly config: PoolConfig,
39
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
40
- private readonly entities: Function[],
41
- ) {}
42
-
43
- static async createInstance(
44
- config: PoolConfig,
45
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
46
- entities: Function[],
47
- ): Promise<DataSource> {
48
- const ds = new DataSource({
49
- type: 'postgres',
50
- entities: entities,
51
- url: config.connectionString,
52
- host: config.host,
53
- port: config.port,
54
- username: config.user,
55
- // password: config.password,
56
- database: config.database,
57
- // ssl: config.ssl,
58
- connectTimeoutMS: config.connectionTimeoutMillis,
59
- poolSize: config.max,
60
- });
61
- await ds.initialize();
62
- return ds;
63
- }
64
-
65
- async initialize(): Promise<void> {
66
- const ds = this.#dataSourceField;
67
- this.#dataSourceField = await TypeOrmTransactionHandler.createInstance(this.config, this.entities);
68
- await ds?.destroy();
69
- }
70
-
71
- async destroy(): Promise<void> {
72
- const ds = this.#dataSourceField;
73
- this.#dataSourceField = undefined;
74
- await ds?.destroy();
75
- }
76
-
77
- get #dataSource() {
78
- if (!this.#dataSourceField) {
79
- throw new Error(`DataSource ${this.name} is not initialized.`);
80
- }
81
- return this.#dataSourceField;
82
- }
83
-
84
- async #checkExecution(
85
- workflowID: string,
86
- stepID: number,
87
- ): Promise<{ output: string | null } | { error: string } | undefined> {
88
- type TxOutputRow = Pick<transaction_completion, 'output' | 'error'>;
89
- const rows = await this.#dataSource.query<TxOutputRow[]>(
90
- `SELECT output, error FROM dbos.transaction_completion
91
- WHERE workflow_id=$1 AND function_num=$2;`,
92
- [workflowID, stepID],
93
- );
94
-
95
- if (rows.length !== 1) {
96
- return undefined;
97
- }
98
-
99
- if (rows[0].output === null) {
100
- return undefined;
101
- }
102
-
103
- const { output, error } = rows[0];
104
- return error !== null ? { error } : { output };
105
- }
106
-
107
- static async #recordOutput(
108
- entityManager: EntityManager,
109
- workflowID: string,
110
- stepID: number,
111
- output: string,
112
- ): Promise<void> {
113
- try {
114
- await entityManager.query(
115
- `INSERT INTO dbos.transaction_completion (workflow_id, function_num, output)
116
- VALUES ($1, $2, $3)`,
117
- [workflowID, stepID, output],
118
- );
119
- } catch (error) {
120
- if (isPGKeyConflictError(error)) {
121
- throw new DBOSWorkflowConflictError(workflowID);
122
- } else {
123
- throw error;
124
- }
125
- }
126
- }
127
-
128
- async #recordError(workflowID: string, stepID: number, error: string): Promise<void> {
129
- try {
130
- await this.#dataSource.query(
131
- `INSERT INTO dbos.transaction_completion (workflow_id, function_num, error)
132
- VALUES ($1, $2, $3)`,
133
- [workflowID, stepID, error],
134
- );
135
- } catch (error) {
136
- if (isPGKeyConflictError(error)) {
137
- throw new DBOSWorkflowConflictError(workflowID);
138
- } else {
139
- throw error;
140
- }
141
- }
142
- }
143
-
144
- /* Required by base class */
145
- async invokeTransactionFunction<This, Args extends unknown[], Return>(
146
- config: PGTransactionConfig | undefined,
147
- target: This,
148
- func: (this: This, ...args: Args) => Promise<Return>,
149
- ...args: Args
150
- ): Promise<Return> {
151
- const workflowID = DBOS.workflowID;
152
- const stepID = DBOS.stepID;
153
- if (workflowID !== undefined && stepID === undefined) {
154
- throw new Error('DBOS.stepID is undefined inside a workflow.');
155
- }
156
-
157
- const isolationLevel = config?.isolationLevel ?? 'READ COMMITTED';
158
- const readOnly = config?.readOnly ? true : false;
159
- const saveResults = !readOnly && workflowID !== undefined;
160
-
161
- // Retry loop if appropriate
162
- let retryWaitMS = 1;
163
- const backoffFactor = 1.5;
164
- const maxRetryWaitMS = 2000; // Maximum wait 2 seconds.
165
-
166
- while (true) {
167
- // Check to see if this tx has already been executed
168
- const previousResult = saveResults ? await this.#checkExecution(workflowID, stepID!) : undefined;
169
- if (previousResult) {
170
- DBOS.span?.setAttribute('cached', true);
171
-
172
- if ('error' in previousResult) {
173
- throw SuperJSON.parse(previousResult.error);
174
- }
175
- return (previousResult.output ? SuperJSON.parse(previousResult.output) : null) as Return;
176
- }
177
-
178
- try {
179
- const result = await this.#dataSource.transaction(isolationLevel, async (entityManager: EntityManager) => {
180
- if (readOnly) {
181
- await entityManager.query('SET TRANSACTION READ ONLY');
182
- }
183
-
184
- const result = await asyncLocalCtx.run({ entityManager: entityManager }, async () => {
185
- return await func.call(target, ...args);
186
- });
187
-
188
- // save the output of read/write transactions
189
- if (saveResults) {
190
- await TypeOrmTransactionHandler.#recordOutput(
191
- entityManager,
192
- workflowID,
193
- stepID!,
194
- SuperJSON.stringify(result),
195
- );
196
- }
197
-
198
- return result;
199
- });
200
-
201
- return result;
202
- } catch (error) {
203
- if (isPGRetriableTransactionError(error)) {
204
- DBOS.span?.addEvent('TXN SERIALIZATION FAILURE', { retryWaitMillis: retryWaitMS }, performance.now());
205
- // Retry serialization failures.
206
- await new Promise((resolve) => setTimeout(resolve, retryWaitMS));
207
- retryWaitMS = Math.min(retryWaitMS * backoffFactor, maxRetryWaitMS);
208
- continue;
209
- } else {
210
- if (saveResults) {
211
- const message = SuperJSON.stringify(error);
212
- await this.#recordError(workflowID, stepID!, message);
213
- }
214
-
215
- throw error;
216
- }
217
- }
218
- }
219
- }
220
- }
221
-
222
- export class TypeOrmDataSource implements DBOSDataSource<PGTransactionConfig> {
223
- // User calls this... DBOS not directly involved...
224
- static get entityManager(): EntityManager {
225
- if (!DBOS.isInTransaction()) {
226
- throw new Error('Invalid use of TypeOrmDataSource.entityManager outside of a DBOS transaction');
227
- }
228
- const ctx = asyncLocalCtx.getStore();
229
- if (!ctx) {
230
- throw new Error('Invalid use of TypeOrmDataSource.entityManager outside of a DBOS transaction');
231
- }
232
-
233
- return ctx.entityManager;
234
- }
235
-
236
- static async initializeInternalSchema(config: PoolConfig): Promise<void> {
237
- const ds = await TypeOrmTransactionHandler.createInstance(config, []);
238
- try {
239
- await ds.query(createTransactionCompletionSchemaPG);
240
- await ds.query(createTransactionCompletionTablePG);
241
- } finally {
242
- await ds.destroy();
243
- }
244
- }
245
-
246
- #provider: TypeOrmTransactionHandler;
247
-
248
- constructor(
249
- readonly name: string,
250
- config: PoolConfig,
251
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
252
- entities: Function[],
253
- ) {
254
- this.#provider = new TypeOrmTransactionHandler(name, config, entities);
255
- registerDataSource(this.#provider);
256
- }
257
-
258
- /**
259
- * Run `callback` as a transaction against this DataSource
260
- * @param callback Function to run within a transactional context
261
- * @param funcName Name to record for the transaction
262
- * @param config Transaction configuration (isolation, etc)
263
- * @returns Return value from `callback`
264
- */
265
- async runTransaction<T>(callback: () => Promise<T>, funcName: string, config?: PGTransactionConfig) {
266
- return await runTransaction(callback, funcName, { dsName: this.name, config });
267
- }
268
-
269
- /**
270
- * Register function as DBOS transaction, to be called within the context
271
- * of a transaction on this data source.
272
- *
273
- * @param func Function to wrap
274
- * @param target Name of function
275
- * @param config Transaction settings
276
- * @returns Wrapped function, to be called instead of `func`
277
- */
278
- registerTransaction<This, Args extends unknown[], Return>(
279
- func: (this: This, ...args: Args) => Promise<Return>,
280
- config?: PGTransactionConfig,
281
- name?: string,
282
- ): (this: This, ...args: Args) => Promise<Return> {
283
- return registerTransaction(this.name, func, { name: name ?? func.name }, config);
284
- }
285
-
286
- /**
287
- * Decorator establishing function as a transaction
288
- */
289
- transaction(config?: PGTransactionConfig) {
290
- // eslint-disable-next-line @typescript-eslint/no-this-alias
291
- const ds = this;
292
- return function decorator<This, Args extends unknown[], Return>(
293
- _target: object,
294
- propertyKey: PropertyKey,
295
- descriptor: TypedPropertyDescriptor<(this: This, ...args: Args) => Promise<Return>>,
296
- ) {
297
- if (!descriptor.value) {
298
- throw new Error('Use of decorator when original method is undefined');
299
- }
300
-
301
- descriptor.value = ds.registerTransaction(descriptor.value, config, String(propertyKey));
302
-
303
- return descriptor;
304
- };
305
- }
306
- }
@@ -1,31 +0,0 @@
1
- import { Client } from 'pg';
2
- import { TypeOrmDataSource } from '../index';
3
- import { dropDB, ensureDB } from './test-helpers';
4
-
5
- describe('TypeOrmDataSource.configure', () => {
6
- const config = { user: 'postgres', database: 'typeorm_ds_config_test' };
7
-
8
- beforeAll(async () => {
9
- const client = new Client({ ...config, database: 'postgres' });
10
- try {
11
- await client.connect();
12
- await dropDB(client, config.database, true);
13
- await ensureDB(client, config.database);
14
- } finally {
15
- await client.end();
16
- }
17
- });
18
-
19
- test('configure creates tx outputs table', async () => {
20
- await TypeOrmDataSource.initializeInternalSchema(config);
21
-
22
- const client = new Client(config);
23
- try {
24
- await client.connect();
25
- const result = await client.query('SELECT workflow_id, function_num, output FROM dbos.transaction_completion');
26
- expect(result.rows.length).toBe(0);
27
- } finally {
28
- await client.end();
29
- }
30
- });
31
- });