@dbos-inc/kysely-datasource 4.3.6-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/index.ts ADDED
@@ -0,0 +1,326 @@
1
+ // using https://kysely.dev/
2
+ import { DBOS, DBOSWorkflowConflictError, FunctionName } from '@dbos-inc/dbos-sdk';
3
+ import {
4
+ type DataSourceTransactionHandler,
5
+ isPGRetriableTransactionError,
6
+ isPGKeyConflictError,
7
+ registerTransaction,
8
+ runTransaction,
9
+ DBOSDataSource,
10
+ registerDataSource,
11
+ createTransactionCompletionSchemaPG,
12
+ createTransactionCompletionTablePG,
13
+ CheckSchemaInstallationReturn,
14
+ checkSchemaInstallationPG,
15
+ } from '@dbos-inc/dbos-sdk/datasource';
16
+ import { AsyncLocalStorage } from 'async_hooks';
17
+ import { Kysely, sql, Transaction, IsolationLevel, PostgresDialect } from 'kysely';
18
+ import { Pool, PoolConfig } from 'pg';
19
+ import { SuperJSON } from 'superjson';
20
+
21
+ export interface transaction_completion {
22
+ workflow_id: string;
23
+ function_num: number;
24
+ output: string | null;
25
+ error: string | null;
26
+ }
27
+
28
+ // Define a database interface for tables used in this datasource
29
+ interface DBOSKyselyTables {
30
+ 'dbos.transaction_completion': transaction_completion;
31
+ }
32
+
33
+ export interface TransactionConfig {
34
+ name?: string;
35
+ isolationLevel?: IsolationLevel;
36
+ readOnly?: boolean;
37
+ }
38
+
39
+ interface KyselyDataSourceContext<DB> {
40
+ client: Transaction<DB>;
41
+ owner: KyselyTransactionHandler;
42
+ }
43
+
44
+ const asyncLocalCtx = new AsyncLocalStorage();
45
+
46
+ class KyselyTransactionHandler implements DataSourceTransactionHandler {
47
+ readonly dsType = 'KyselyDataSource';
48
+ #kyselyDBField: Kysely<DBOSKyselyTables>;
49
+
50
+ constructor(
51
+ readonly name: string,
52
+ private readonly poolConfig: PoolConfig,
53
+ ) {
54
+ this.#kyselyDBField = new Kysely<DBOSKyselyTables>({
55
+ dialect: new PostgresDialect({
56
+ pool: new Pool(poolConfig),
57
+ }),
58
+ });
59
+ }
60
+
61
+ async initialize(): Promise<void> {
62
+ const kyselyDB = this.#kyselyDBField;
63
+ this.#kyselyDBField = new Kysely<DBOSKyselyTables>({
64
+ dialect: new PostgresDialect({
65
+ pool: new Pool(this.poolConfig),
66
+ }),
67
+ });
68
+ await kyselyDB?.destroy();
69
+
70
+ // Check for connectivity & the schema
71
+ let installed = false;
72
+ try {
73
+ const { rows } = await sql
74
+ .raw<CheckSchemaInstallationReturn>(checkSchemaInstallationPG)
75
+ .execute(this.#kyselyDBField);
76
+ const { schema_exists, table_exists } = rows[0];
77
+ installed = !!schema_exists && !!table_exists;
78
+ } catch (e) {
79
+ throw new Error(
80
+ `In initialization of 'KyselyDataSource' ${this.name}: Database could not be reached: ${(e as Error).message}`,
81
+ );
82
+ }
83
+
84
+ if (!installed) {
85
+ try {
86
+ await sql.raw(createTransactionCompletionSchemaPG).execute(this.#kyselyDBField);
87
+ await sql.raw(createTransactionCompletionTablePG).execute(this.#kyselyDBField);
88
+ } catch (err) {
89
+ throw new Error(
90
+ `In initialization of 'KyselyDataSource' ${this.name}: The 'dbos.transaction_completion' table does not exist, and could not be created. This should be added to your database migrations.
91
+ See: https://docs.dbos.dev/typescript/tutorials/transaction-tutorial#installing-the-dbos-schema`,
92
+ );
93
+ }
94
+ }
95
+ }
96
+
97
+ async destroy(): Promise<void> {
98
+ await this.#kyselyDBField.destroy();
99
+ }
100
+
101
+ get #kyselyDB() {
102
+ if (!this.#kyselyDBField) {
103
+ throw new Error(`DataSource ${this.name} is not initialized.`);
104
+ }
105
+ return this.#kyselyDBField;
106
+ }
107
+
108
+ async #checkExecution(
109
+ workflowID: string,
110
+ stepID: number,
111
+ ): Promise<{ output: string | null } | { error: string } | undefined> {
112
+ const result = await this.#kyselyDB
113
+ .selectFrom('dbos.transaction_completion')
114
+ .select(['output', 'error'])
115
+ .where('workflow_id', '=', workflowID)
116
+ .where('function_num', '=', stepID)
117
+ .executeTakeFirst();
118
+ if (result === undefined) {
119
+ return undefined;
120
+ }
121
+ const { output, error } = result;
122
+ return error !== null ? { error } : { output };
123
+ }
124
+
125
+ async #recordError(workflowID: string, stepID: number, error: string): Promise<void> {
126
+ try {
127
+ await this.#kyselyDB
128
+ .insertInto('dbos.transaction_completion')
129
+ .values({
130
+ workflow_id: workflowID,
131
+ function_num: stepID,
132
+ error,
133
+ output: null,
134
+ })
135
+ .execute();
136
+ } catch (error) {
137
+ if (isPGKeyConflictError(error)) {
138
+ throw new DBOSWorkflowConflictError(workflowID);
139
+ } else {
140
+ throw error;
141
+ }
142
+ }
143
+ }
144
+
145
+ static async #recordOutput(
146
+ client: Transaction<DBOSKyselyTables>,
147
+ workflowID: string,
148
+ stepID: number,
149
+ output: string | null,
150
+ ): Promise<void> {
151
+ try {
152
+ await client
153
+ .insertInto('dbos.transaction_completion')
154
+ .values({
155
+ workflow_id: workflowID,
156
+ function_num: stepID,
157
+ output,
158
+ error: null,
159
+ })
160
+ .execute();
161
+ } catch (error) {
162
+ if (isPGKeyConflictError(error)) {
163
+ throw new DBOSWorkflowConflictError(workflowID);
164
+ } else {
165
+ throw error;
166
+ }
167
+ }
168
+ }
169
+
170
+ async invokeTransactionFunction<This, Args extends unknown[], Return>(
171
+ config: TransactionConfig | undefined,
172
+ target: This,
173
+ func: (this: This, ...args: Args) => Promise<Return>,
174
+ ...args: Args
175
+ ): Promise<Return> {
176
+ const workflowID = DBOS.workflowID;
177
+ const stepID = DBOS.stepID;
178
+ if (workflowID !== undefined && stepID === undefined) {
179
+ throw new Error('DBOS.stepID is undefined inside a workflow.');
180
+ }
181
+
182
+ const readOnly = config?.readOnly ?? false;
183
+ const saveResults = !readOnly && workflowID !== undefined;
184
+
185
+ // Retry loop if appropriate
186
+ let retryWaitMS = 1;
187
+ const backoffFactor = 1.5;
188
+ const maxRetryWaitMS = 2000; // Maximum wait 2 seconds.
189
+
190
+ while (true) {
191
+ // Check to see if this tx has already been executed
192
+ const previousResult = saveResults ? await this.#checkExecution(workflowID, stepID!) : undefined;
193
+ if (previousResult) {
194
+ DBOS.span?.setAttribute('cached', true);
195
+
196
+ if ('error' in previousResult) {
197
+ throw SuperJSON.parse(previousResult.error);
198
+ }
199
+ return (previousResult.output ? SuperJSON.parse(previousResult.output) : null) as Return;
200
+ }
201
+
202
+ try {
203
+ let trx = this.#kyselyDB.transaction();
204
+ if (config?.readOnly) {
205
+ trx = trx.setAccessMode('read only');
206
+ }
207
+ if (config?.isolationLevel) {
208
+ trx = trx.setIsolationLevel(config.isolationLevel);
209
+ }
210
+ const result = await trx.execute(async (client) => {
211
+ // execute user's transaction function
212
+ const result = await asyncLocalCtx.run({ client, owner: this }, async () => {
213
+ return (await func.call(target, ...args)) as Return;
214
+ });
215
+
216
+ // save the output of read/write transactions
217
+ if (saveResults) {
218
+ await KyselyTransactionHandler.#recordOutput(client, workflowID, stepID!, SuperJSON.stringify(result));
219
+ }
220
+
221
+ return result;
222
+ });
223
+
224
+ return result;
225
+ } catch (error) {
226
+ if (isPGRetriableTransactionError(error)) {
227
+ DBOS.span?.addEvent('TXN SERIALIZATION FAILURE', { retryWaitMillis: retryWaitMS }, performance.now());
228
+ await new Promise((resolve) => setTimeout(resolve, retryWaitMS));
229
+ retryWaitMS = Math.min(retryWaitMS * backoffFactor, maxRetryWaitMS);
230
+ continue;
231
+ } else {
232
+ if (saveResults) {
233
+ const message = SuperJSON.stringify(error);
234
+ await this.#recordError(workflowID, stepID!, message);
235
+ }
236
+
237
+ throw error;
238
+ }
239
+ }
240
+ }
241
+ }
242
+ }
243
+
244
+ export class KyselyDataSource<DB> implements DBOSDataSource<TransactionConfig> {
245
+ static #getClient<DB>(p?: KyselyTransactionHandler): Kysely<DB> {
246
+ if (!DBOS.isInTransaction()) {
247
+ throw new Error('invalid use of KyselyDataSource.client outside of a DBOS transaction.');
248
+ }
249
+ const ctx = asyncLocalCtx.getStore() as KyselyDataSourceContext<DB>;
250
+ if (!ctx) {
251
+ throw new Error('invalid use of KyselyDataSource.client outside of a DBOS transaction.');
252
+ }
253
+ if (p && p !== ctx.owner) throw new Error('Request of `KyselyDataSource.client` from the wrong object.');
254
+ return ctx.client;
255
+ }
256
+
257
+ get client(): Kysely<DB> {
258
+ return KyselyDataSource.#getClient(this.#provider);
259
+ }
260
+
261
+ static async initializeDBOSSchema(poolConfig: PoolConfig) {
262
+ const client = new Kysely({
263
+ dialect: new PostgresDialect({
264
+ pool: new Pool(poolConfig),
265
+ }),
266
+ });
267
+ await sql.raw(createTransactionCompletionSchemaPG).execute(client);
268
+ await sql.raw(createTransactionCompletionTablePG).execute(client);
269
+ await client.destroy();
270
+ }
271
+
272
+ static async uninitializeDBOSSchema(poolConfig: PoolConfig) {
273
+ const client = new Kysely({
274
+ dialect: new PostgresDialect({
275
+ pool: new Pool(poolConfig),
276
+ }),
277
+ });
278
+ await sql
279
+ .raw('DROP TABLE IF EXISTS dbos.transaction_completion; DROP SCHEMA IF EXISTS dbos CASCADE;')
280
+ .execute(client);
281
+ await client.destroy();
282
+ }
283
+
284
+ #provider: KyselyTransactionHandler;
285
+
286
+ constructor(
287
+ readonly name: string,
288
+ poolConfig: PoolConfig,
289
+ ) {
290
+ this.#provider = new KyselyTransactionHandler(name, poolConfig);
291
+ registerDataSource(this.#provider);
292
+ }
293
+
294
+ async runTransaction<R>(func: () => Promise<R>, config?: TransactionConfig) {
295
+ return await runTransaction(func, config?.name ?? func.name, { dsName: this.name, config });
296
+ }
297
+
298
+ registerTransaction<This, Args extends unknown[], Return>(
299
+ func: (this: This, ...args: Args) => Promise<Return>,
300
+ config?: TransactionConfig & FunctionName,
301
+ ): (this: This, ...args: Args) => Promise<Return> {
302
+ return registerTransaction(this.name, func, config);
303
+ }
304
+
305
+ transaction(config?: TransactionConfig) {
306
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
307
+ const ds = this;
308
+ return function decorator<This, Args extends unknown[], Return>(
309
+ target: object,
310
+ propertyKey: PropertyKey,
311
+ descriptor: TypedPropertyDescriptor<(this: This, ...args: Args) => Promise<Return>>,
312
+ ) {
313
+ if (!descriptor.value) {
314
+ throw Error('Use of decorator when original method is undefined');
315
+ }
316
+
317
+ descriptor.value = ds.registerTransaction(descriptor.value, {
318
+ ...config,
319
+ name: config?.name ?? String(propertyKey),
320
+ ctorOrProto: target,
321
+ });
322
+
323
+ return descriptor;
324
+ };
325
+ }
326
+ }
package/jest.config.js ADDED
@@ -0,0 +1,8 @@
1
+ /** @type {import('ts-jest').JestConfigWithTsJest} */
2
+ module.exports = {
3
+ preset: 'ts-jest',
4
+ testEnvironment: 'node',
5
+ testRegex: '((\\.|/)(test|spec))\\.ts?$',
6
+ moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
7
+ modulePaths: ['./'],
8
+ };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@dbos-inc/kysely-datasource",
3
+ "version": "4.3.6-preview",
4
+ "description": "DBOS DataSource library for Kysely Query Builder with PostgreSQL support",
5
+ "license": "MIT",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "homepage": "https://docs.dbos.dev/",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/dbos-inc/dbos-transact-ts",
12
+ "directory": "packages/kysely-datasource"
13
+ },
14
+ "scripts": {
15
+ "build": "tsc --project tsconfig.json",
16
+ "test": "jest --detectOpenHandles"
17
+ },
18
+ "dependencies": {
19
+ "kysely": "^0.28.8",
20
+ "pg": "^8.11.3",
21
+ "superjson": "^1.13.3"
22
+ },
23
+ "peerDependencies": {
24
+ "@dbos-inc/dbos-sdk": "*"
25
+ },
26
+ "devDependencies": {
27
+ "@types/jest": "^29.5.14",
28
+ "@types/pg": "^8.15.2",
29
+ "jest": "^29.7.0",
30
+ "typescript": "^5.4.5"
31
+ }
32
+ }
@@ -0,0 +1,51 @@
1
+ import { Client, Pool, PoolClient } from 'pg';
2
+ import { KyselyDataSource } from '..';
3
+ import { dropDB, ensureDB } from './test-helpers';
4
+
5
+ const config = { user: 'postgres', database: 'kysely_ds_config_test' };
6
+
7
+ describe('KyselyDataSource.initializeDBOSSchema', () => {
8
+ beforeEach(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
+ async function queryTxCompletionTable(client: PoolClient) {
20
+ const result = await client.query(
21
+ 'SELECT workflow_id, function_num, output, error FROM dbos.transaction_completion',
22
+ );
23
+ return result.rowCount;
24
+ }
25
+
26
+ async function txCompletionTableExists(client: PoolClient) {
27
+ const result = await client.query<{ exists: boolean }>(
28
+ "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'dbos' AND table_name = 'transaction_completion');",
29
+ );
30
+ if (result.rowCount !== 1) throw new Error(`unexpected rowcount ${result.rowCount}`);
31
+ return result.rows[0].exists;
32
+ }
33
+
34
+ test('initializeDBOSSchema', async () => {
35
+ const db = new Pool(config);
36
+ await KyselyDataSource.initializeDBOSSchema(config);
37
+
38
+ try {
39
+ const client = await db.connect();
40
+ await expect(txCompletionTableExists(client)).resolves.toBe(true);
41
+ await expect(queryTxCompletionTable(client)).resolves.toEqual(0);
42
+
43
+ await KyselyDataSource.uninitializeDBOSSchema(config);
44
+
45
+ await expect(txCompletionTableExists(client)).resolves.toBe(false);
46
+ client.release();
47
+ } finally {
48
+ await db.end();
49
+ }
50
+ });
51
+ });