@dbos-inc/typeorm-datasource 3.0.11-preview.gc9233b8190 → 3.0.13-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.
- package/dist/{src/typeorm_datasource.d.ts → index.d.ts} +8 -15
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +226 -0
- package/dist/index.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/index.ts +306 -0
- package/package.json +7 -8
- package/tests/config.test.ts +31 -0
- package/tests/datasource.test.ts +445 -0
- package/tests/test-helpers.ts +13 -0
- package/tsconfig.json +3 -2
- package/dist/src/index.d.ts +0 -2
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -7
- package/dist/src/index.js.map +0 -1
- package/dist/src/typeorm_datasource.d.ts.map +0 -1
- package/dist/src/typeorm_datasource.js +0 -249
- package/dist/src/typeorm_datasource.js.map +0 -1
- package/src/index.ts +0 -1
- package/src/typeorm_datasource.ts +0 -332
- package/tests/testutils.ts +0 -30
- package/tests/typeormds.test.ts +0 -203
package/index.ts
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dbos-inc/typeorm-datasource",
|
|
3
|
-
"version": "3.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "3.0.13-preview",
|
|
4
|
+
"description": "DBOS DataSource library for TypeORM with PostgreSQL support",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"homepage": "https://docs.dbos.dev/",
|
|
6
9
|
"repository": {
|
|
7
10
|
"type": "git",
|
|
8
11
|
"url": "https://github.com/dbos-inc/dbos-transact-ts",
|
|
@@ -12,10 +15,6 @@
|
|
|
12
15
|
"build": "tsc --project tsconfig.json",
|
|
13
16
|
"test": "npm run build && jest --detectOpenHandles"
|
|
14
17
|
},
|
|
15
|
-
"keywords": [],
|
|
16
|
-
"author": "",
|
|
17
|
-
"license": "MIT",
|
|
18
|
-
"description": "",
|
|
19
18
|
"dependencies": {
|
|
20
19
|
"typeorm": "^0.3.24",
|
|
21
20
|
"pg": "^8.11.3",
|
|
@@ -31,6 +30,6 @@
|
|
|
31
30
|
"jest": "^29.7.0",
|
|
32
31
|
"supertest": "^7.0.0",
|
|
33
32
|
"ts-jest": "^29.1.4",
|
|
34
|
-
"typescript": "^5.
|
|
33
|
+
"typescript": "^5.4.5"
|
|
35
34
|
}
|
|
36
35
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
});
|