@dbos-inc/knex-datasource 3.0.10-preview → 3.0.11-preview.gc9233b8190
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.d.ts +4 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +61 -108
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/index.ts +75 -123
- package/package.json +5 -6
- package/tests/config.test.ts +7 -47
- package/tests/datasource.test.ts +42 -165
- package/tests/test-helpers.ts +2 -3
package/index.ts
CHANGED
|
@@ -9,8 +9,6 @@ import {
|
|
|
9
9
|
runTransaction,
|
|
10
10
|
DBOSDataSource,
|
|
11
11
|
registerDataSource,
|
|
12
|
-
createTransactionCompletionSchemaPG,
|
|
13
|
-
createTransactionCompletionTablePG,
|
|
14
12
|
} from '@dbos-inc/dbos-sdk/datasource';
|
|
15
13
|
import { AsyncLocalStorage } from 'async_hooks';
|
|
16
14
|
import knex, { type Knex } from 'knex';
|
|
@@ -20,7 +18,6 @@ interface transaction_completion {
|
|
|
20
18
|
workflow_id: string;
|
|
21
19
|
function_num: number;
|
|
22
20
|
output: string | null;
|
|
23
|
-
error: string | null;
|
|
24
21
|
}
|
|
25
22
|
|
|
26
23
|
interface KnexDataSourceContext {
|
|
@@ -31,79 +28,50 @@ export type TransactionConfig = Pick<Knex.TransactionConfig, 'isolationLevel' |
|
|
|
31
28
|
|
|
32
29
|
const asyncLocalCtx = new AsyncLocalStorage<KnexDataSourceContext>();
|
|
33
30
|
|
|
34
|
-
class
|
|
31
|
+
class KnexDSTH implements DataSourceTransactionHandler {
|
|
32
|
+
readonly name: string;
|
|
35
33
|
readonly dsType = 'KnexDataSource';
|
|
36
|
-
#
|
|
34
|
+
readonly #knexDB: Knex;
|
|
37
35
|
|
|
38
|
-
constructor(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
) {}
|
|
42
|
-
|
|
43
|
-
async initialize(): Promise<void> {
|
|
44
|
-
const knexDB = this.#knexDBField;
|
|
45
|
-
this.#knexDBField = knex(this.config);
|
|
46
|
-
await knexDB?.destroy();
|
|
36
|
+
constructor(name: string, config: Knex.Config) {
|
|
37
|
+
this.name = name;
|
|
38
|
+
this.#knexDB = knex(config);
|
|
47
39
|
}
|
|
48
40
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
this.#knexDBField = undefined;
|
|
52
|
-
await knexDB?.destroy();
|
|
41
|
+
initialize(): Promise<void> {
|
|
42
|
+
return Promise.resolve();
|
|
53
43
|
}
|
|
54
44
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
throw new Error(`DataSource ${this.name} is not initialized.`);
|
|
58
|
-
}
|
|
59
|
-
return this.#knexDBField;
|
|
45
|
+
destroy(): Promise<void> {
|
|
46
|
+
return this.#knexDB.destroy();
|
|
60
47
|
}
|
|
61
48
|
|
|
62
|
-
async #checkExecution(
|
|
49
|
+
static async #checkExecution(
|
|
50
|
+
client: Knex.Transaction,
|
|
63
51
|
workflowID: string,
|
|
64
|
-
|
|
65
|
-
): Promise<{ output: string | null } |
|
|
66
|
-
const result = await
|
|
52
|
+
functionNum: number,
|
|
53
|
+
): Promise<{ output: string | null } | undefined> {
|
|
54
|
+
const result = await client<transaction_completion>('transaction_completion')
|
|
67
55
|
.withSchema('dbos')
|
|
68
|
-
.select('output'
|
|
56
|
+
.select('output')
|
|
69
57
|
.where({
|
|
70
58
|
workflow_id: workflowID,
|
|
71
|
-
function_num:
|
|
59
|
+
function_num: functionNum,
|
|
72
60
|
})
|
|
73
61
|
.first();
|
|
74
|
-
|
|
75
|
-
return undefined;
|
|
76
|
-
}
|
|
77
|
-
const { output, error } = result;
|
|
78
|
-
return error !== null ? { error } : { output };
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
async #recordError(workflowID: string, stepID: number, error: string): Promise<void> {
|
|
82
|
-
try {
|
|
83
|
-
await this.#knexDB<transaction_completion>('transaction_completion').withSchema('dbos').insert({
|
|
84
|
-
workflow_id: workflowID,
|
|
85
|
-
function_num: stepID,
|
|
86
|
-
error,
|
|
87
|
-
});
|
|
88
|
-
} catch (error) {
|
|
89
|
-
if (isPGKeyConflictError(error)) {
|
|
90
|
-
throw new DBOSWorkflowConflictError(workflowID);
|
|
91
|
-
} else {
|
|
92
|
-
throw error;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
62
|
+
return result === undefined ? undefined : { output: result.output };
|
|
95
63
|
}
|
|
96
64
|
|
|
97
65
|
static async #recordOutput(
|
|
98
66
|
client: Knex.Transaction,
|
|
99
67
|
workflowID: string,
|
|
100
|
-
|
|
68
|
+
functionNum: number,
|
|
101
69
|
output: string | null,
|
|
102
70
|
): Promise<void> {
|
|
103
71
|
try {
|
|
104
72
|
await client<transaction_completion>('transaction_completion').withSchema('dbos').insert({
|
|
105
73
|
workflow_id: workflowID,
|
|
106
|
-
function_num:
|
|
74
|
+
function_num: functionNum,
|
|
107
75
|
output,
|
|
108
76
|
});
|
|
109
77
|
} catch (error) {
|
|
@@ -122,61 +90,67 @@ class KnexTransactionHandler implements DataSourceTransactionHandler {
|
|
|
122
90
|
...args: Args
|
|
123
91
|
): Promise<Return> {
|
|
124
92
|
const workflowID = DBOS.workflowID;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
93
|
+
if (workflowID === undefined) {
|
|
94
|
+
throw new Error('Workflow ID is not set.');
|
|
95
|
+
}
|
|
96
|
+
const functionNum = DBOS.stepID;
|
|
97
|
+
if (functionNum === undefined) {
|
|
98
|
+
throw new Error('Function Number is not set.');
|
|
128
99
|
}
|
|
129
100
|
|
|
130
101
|
const readOnly = config?.readOnly ?? false;
|
|
131
|
-
const saveResults = !readOnly && workflowID !== undefined;
|
|
132
|
-
|
|
133
|
-
// Retry loop if appropriate
|
|
134
102
|
let retryWaitMS = 1;
|
|
135
103
|
const backoffFactor = 1.5;
|
|
136
|
-
const maxRetryWaitMS = 2000;
|
|
104
|
+
const maxRetryWaitMS = 2000;
|
|
137
105
|
|
|
138
106
|
while (true) {
|
|
139
|
-
// Check to see if this tx has already been executed
|
|
140
|
-
const previousResult = saveResults ? await this.#checkExecution(workflowID, stepID!) : undefined;
|
|
141
|
-
if (previousResult) {
|
|
142
|
-
DBOS.span?.setAttribute('cached', true);
|
|
143
|
-
|
|
144
|
-
if ('error' in previousResult) {
|
|
145
|
-
throw SuperJSON.parse(previousResult.error);
|
|
146
|
-
}
|
|
147
|
-
return (previousResult.output ? SuperJSON.parse(previousResult.output) : null) as Return;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
107
|
try {
|
|
151
108
|
const result = await this.#knexDB.transaction<Return>(
|
|
152
109
|
async (client) => {
|
|
110
|
+
// Check to see if this tx has already been executed
|
|
111
|
+
const previousResult =
|
|
112
|
+
readOnly || !workflowID ? undefined : await KnexDSTH.#checkExecution(client, workflowID, functionNum);
|
|
113
|
+
if (previousResult) {
|
|
114
|
+
return (previousResult.output ? SuperJSON.parse(previousResult.output) : null) as Return;
|
|
115
|
+
}
|
|
116
|
+
|
|
153
117
|
// execute user's transaction function
|
|
154
118
|
const result = await asyncLocalCtx.run({ client }, async () => {
|
|
155
119
|
return (await func.call(target, ...args)) as Return;
|
|
156
120
|
});
|
|
157
121
|
|
|
158
122
|
// save the output of read/write transactions
|
|
159
|
-
if (
|
|
160
|
-
await
|
|
123
|
+
if (!readOnly && workflowID) {
|
|
124
|
+
await KnexDSTH.#recordOutput(client, workflowID, functionNum, SuperJSON.stringify(result));
|
|
125
|
+
|
|
126
|
+
// Note, existing code wraps #recordOutput call in a try/catch block that
|
|
127
|
+
// converts DB error with code 25P02 to DBOSFailedSqlTransactionError.
|
|
128
|
+
// However, existing code doesn't make any logic decisions based on that error type.
|
|
129
|
+
// DBOSFailedSqlTransactionError does stored WF ID and function name, so I assume that info is logged out somewhere
|
|
161
130
|
}
|
|
162
131
|
|
|
163
132
|
return result;
|
|
164
133
|
},
|
|
165
134
|
{ isolationLevel: config?.isolationLevel, readOnly: config?.readOnly },
|
|
166
135
|
);
|
|
136
|
+
// TODO: span.setStatus({ code: SpanStatusCode.OK });
|
|
137
|
+
// TODO: this.tracer.endSpan(span);
|
|
167
138
|
|
|
168
139
|
return result;
|
|
169
140
|
} catch (error) {
|
|
170
141
|
if (isPGRetriableTransactionError(error)) {
|
|
171
|
-
|
|
142
|
+
// TODO: span.addEvent('TXN SERIALIZATION FAILURE', { retryWaitMillis: retryWaitMillis }, performance.now());
|
|
172
143
|
await new Promise((resolve) => setTimeout(resolve, retryWaitMS));
|
|
173
144
|
retryWaitMS = Math.min(retryWaitMS * backoffFactor, maxRetryWaitMS);
|
|
174
145
|
continue;
|
|
175
146
|
} else {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
147
|
+
// TODO: span.setStatus({ code: SpanStatusCode.ERROR, message: e.message });
|
|
148
|
+
// TODO: this.tracer.endSpan(span);
|
|
149
|
+
|
|
150
|
+
// TODO: currently, we are *not* recording errors in the txOutput table.
|
|
151
|
+
// For normal execution, this is fine because we also store tx step results (output and error) in the sysdb operation output table.
|
|
152
|
+
// However, I'm concerned that we have a dueling execution hole where one tx fails while another succeeds.
|
|
153
|
+
// This implies that we can end up in a situation where the step output records an error but the txOutput table records success.
|
|
180
154
|
|
|
181
155
|
throw error;
|
|
182
156
|
}
|
|
@@ -185,10 +159,6 @@ class KnexTransactionHandler implements DataSourceTransactionHandler {
|
|
|
185
159
|
}
|
|
186
160
|
}
|
|
187
161
|
|
|
188
|
-
function isKnex(value: Knex | Knex.Config): value is Knex {
|
|
189
|
-
return 'raw' in value;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
162
|
export class KnexDataSource implements DBOSDataSource<TransactionConfig> {
|
|
193
163
|
static get client(): Knex.Transaction {
|
|
194
164
|
if (!DBOS.isInTransaction()) {
|
|
@@ -196,53 +166,35 @@ export class KnexDataSource implements DBOSDataSource<TransactionConfig> {
|
|
|
196
166
|
}
|
|
197
167
|
const ctx = asyncLocalCtx.getStore();
|
|
198
168
|
if (!ctx) {
|
|
199
|
-
throw new Error('
|
|
169
|
+
throw new Error('No async local context found.');
|
|
200
170
|
}
|
|
201
171
|
return ctx.client;
|
|
202
172
|
}
|
|
203
173
|
|
|
204
|
-
static async
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
await
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
async function $initSchema(knexDB: Knex) {
|
|
217
|
-
await knexDB.raw(createTransactionCompletionSchemaPG);
|
|
218
|
-
await knexDB.raw(createTransactionCompletionTablePG);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
static async uninitializeSchema(knexOrConfig: Knex.Config) {
|
|
223
|
-
if (isKnex(knexOrConfig)) {
|
|
224
|
-
await $uninitSchema(knexOrConfig);
|
|
225
|
-
} else {
|
|
226
|
-
const knexDB = knex(knexOrConfig);
|
|
227
|
-
try {
|
|
228
|
-
await $uninitSchema(knexDB);
|
|
229
|
-
} finally {
|
|
230
|
-
await knexDB.destroy();
|
|
174
|
+
static async initializeInternalSchema(config: Knex.Config) {
|
|
175
|
+
const knexDB = knex(config);
|
|
176
|
+
try {
|
|
177
|
+
await knexDB.schema.createSchemaIfNotExists('dbos');
|
|
178
|
+
const exists = await knexDB.schema.withSchema('dbos').hasTable('transaction_completion');
|
|
179
|
+
if (!exists) {
|
|
180
|
+
await knexDB.schema.withSchema('dbos').createTable('transaction_completion', (table) => {
|
|
181
|
+
table.string('workflow_id').notNullable();
|
|
182
|
+
table.integer('function_num').notNullable();
|
|
183
|
+
table.string('output').nullable();
|
|
184
|
+
table.primary(['workflow_id', 'function_num']);
|
|
185
|
+
});
|
|
231
186
|
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
async function $uninitSchema(knexDB: Knex) {
|
|
235
|
-
await knexDB.raw('DROP TABLE IF EXISTS dbos.transaction_completion; DROP SCHEMA IF EXISTS dbos;');
|
|
187
|
+
} finally {
|
|
188
|
+
await knexDB.destroy();
|
|
236
189
|
}
|
|
237
190
|
}
|
|
238
191
|
|
|
239
|
-
|
|
192
|
+
readonly name: string;
|
|
193
|
+
#provider: KnexDSTH;
|
|
240
194
|
|
|
241
|
-
constructor(
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
) {
|
|
245
|
-
this.#provider = new KnexTransactionHandler(name, config);
|
|
195
|
+
constructor(name: string, config: Knex.Config) {
|
|
196
|
+
this.name = name;
|
|
197
|
+
this.#provider = new KnexDSTH(name, config);
|
|
246
198
|
registerDataSource(this.#provider);
|
|
247
199
|
}
|
|
248
200
|
|
|
@@ -252,10 +204,10 @@ export class KnexDataSource implements DBOSDataSource<TransactionConfig> {
|
|
|
252
204
|
|
|
253
205
|
registerTransaction<This, Args extends unknown[], Return>(
|
|
254
206
|
func: (this: This, ...args: Args) => Promise<Return>,
|
|
207
|
+
name: string,
|
|
255
208
|
config?: TransactionConfig,
|
|
256
|
-
name?: string,
|
|
257
209
|
): (this: This, ...args: Args) => Promise<Return> {
|
|
258
|
-
return registerTransaction(this.name, func, { name
|
|
210
|
+
return registerTransaction(this.name, func, { name }, config);
|
|
259
211
|
}
|
|
260
212
|
|
|
261
213
|
transaction(config?: TransactionConfig) {
|
|
@@ -263,14 +215,14 @@ export class KnexDataSource implements DBOSDataSource<TransactionConfig> {
|
|
|
263
215
|
const ds = this;
|
|
264
216
|
return function decorator<This, Args extends unknown[], Return>(
|
|
265
217
|
_target: object,
|
|
266
|
-
propertyKey:
|
|
218
|
+
propertyKey: string,
|
|
267
219
|
descriptor: TypedPropertyDescriptor<(this: This, ...args: Args) => Promise<Return>>,
|
|
268
220
|
) {
|
|
269
221
|
if (!descriptor.value) {
|
|
270
222
|
throw Error('Use of decorator when original method is undefined');
|
|
271
223
|
}
|
|
272
224
|
|
|
273
|
-
descriptor.value = ds.registerTransaction(descriptor.value,
|
|
225
|
+
descriptor.value = ds.registerTransaction(descriptor.value, propertyKey.toString(), config);
|
|
274
226
|
|
|
275
227
|
return descriptor;
|
|
276
228
|
};
|
package/package.json
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dbos-inc/knex-datasource",
|
|
3
|
-
"version": "3.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.0.11-preview.gc9233b8190",
|
|
4
|
+
"description": "",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"homepage": "https://docs.dbos.dev/",
|
|
9
6
|
"repository": {
|
|
10
7
|
"type": "git",
|
|
11
8
|
"url": "https://github.com/dbos-inc/dbos-transact-ts",
|
|
12
9
|
"directory": "packages/knex-datasource"
|
|
13
10
|
},
|
|
11
|
+
"homepage": "https://docs.dbos.dev/",
|
|
12
|
+
"main": "index.js",
|
|
14
13
|
"scripts": {
|
|
15
14
|
"build": "tsc --project tsconfig.json",
|
|
16
15
|
"test": "jest --detectOpenHandles"
|
|
17
16
|
},
|
|
18
17
|
"dependencies": {
|
|
19
18
|
"knex": "^3.1.0",
|
|
20
|
-
"pg": "^8.11.3",
|
|
21
19
|
"superjson": "^1.13"
|
|
22
20
|
},
|
|
23
21
|
"peerDependencies": {
|
|
@@ -27,6 +25,7 @@
|
|
|
27
25
|
"@types/jest": "^29.5.14",
|
|
28
26
|
"@types/pg": "^8.15.2",
|
|
29
27
|
"jest": "^29.7.0",
|
|
28
|
+
"pg": "^8.11.3",
|
|
30
29
|
"typescript": "^5.4.5"
|
|
31
30
|
}
|
|
32
31
|
}
|
package/tests/config.test.ts
CHANGED
|
@@ -1,71 +1,31 @@
|
|
|
1
1
|
import { Client } from 'pg';
|
|
2
2
|
import { KnexDataSource } from '../index';
|
|
3
3
|
import { dropDB, ensureDB } from './test-helpers';
|
|
4
|
-
import knex from 'knex';
|
|
5
4
|
|
|
6
|
-
describe('KnexDataSource.
|
|
5
|
+
describe('KnexDataSource.configure', () => {
|
|
7
6
|
const config = { user: 'postgres', database: 'knex_ds_config_test' };
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
beforeAll(async () => {
|
|
10
9
|
const client = new Client({ ...config, database: 'postgres' });
|
|
11
10
|
try {
|
|
12
11
|
await client.connect();
|
|
13
|
-
await dropDB(client, config.database
|
|
12
|
+
await dropDB(client, config.database);
|
|
14
13
|
await ensureDB(client, config.database);
|
|
15
14
|
} finally {
|
|
16
15
|
await client.end();
|
|
17
16
|
}
|
|
18
17
|
});
|
|
19
18
|
|
|
20
|
-
async
|
|
21
|
-
|
|
22
|
-
'SELECT workflow_id, function_num, output, error FROM dbos.transaction_completion',
|
|
23
|
-
);
|
|
24
|
-
return result.rowCount;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async function txCompletionTableExists(client: Client) {
|
|
28
|
-
const result = await client.query<{ exists: boolean }>(
|
|
29
|
-
"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'dbos' AND table_name = 'transaction_completion');",
|
|
30
|
-
);
|
|
31
|
-
if (result.rowCount !== 1) throw new Error(`unexpected rowcount ${result.rowCount}`);
|
|
32
|
-
return result.rows[0].exists;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
test('initializeSchema-with-config', async () => {
|
|
36
|
-
const knexConfig = { client: 'pg', connection: config };
|
|
37
|
-
await KnexDataSource.initializeSchema(knexConfig);
|
|
19
|
+
test('configure creates tx outputs table', async () => {
|
|
20
|
+
await KnexDataSource.initializeInternalSchema({ client: 'pg', connection: config });
|
|
38
21
|
|
|
39
22
|
const client = new Client(config);
|
|
40
23
|
try {
|
|
41
24
|
await client.connect();
|
|
42
|
-
await
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
await KnexDataSource.uninitializeSchema(knexConfig);
|
|
46
|
-
|
|
47
|
-
await expect(txCompletionTableExists(client)).resolves.toBe(false);
|
|
48
|
-
} finally {
|
|
49
|
-
await client.end();
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test('initializeSchema-with-knex', async () => {
|
|
54
|
-
const knexDB = knex({ client: 'pg', connection: config });
|
|
55
|
-
const client = new Client(config);
|
|
56
|
-
try {
|
|
57
|
-
await KnexDataSource.initializeSchema(knexDB);
|
|
58
|
-
await client.connect();
|
|
59
|
-
|
|
60
|
-
await expect(txCompletionTableExists(client)).resolves.toBe(true);
|
|
61
|
-
await expect(queryTxCompletionTable(client)).resolves.toEqual(0);
|
|
62
|
-
|
|
63
|
-
await KnexDataSource.uninitializeSchema(knexDB);
|
|
64
|
-
|
|
65
|
-
await expect(txCompletionTableExists(client)).resolves.toBe(false);
|
|
25
|
+
const result = await client.query('SELECT workflow_id, function_num, output FROM dbos.transaction_completion');
|
|
26
|
+
expect(result.rows.length).toBe(0);
|
|
66
27
|
} finally {
|
|
67
28
|
await client.end();
|
|
68
|
-
await knexDB.destroy();
|
|
69
29
|
}
|
|
70
30
|
});
|
|
71
31
|
});
|