@dbos-inc/drizzle-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,187 +0,0 @@
1
- /* eslint-disable */
2
- import { DBOS } from '@dbos-inc/dbos-sdk';
3
- import { DrizzleDataSource } from '../src';
4
- import { randomUUID } from 'node:crypto';
5
- import { setUpDBOSTestDb } from './testutils';
6
- import { pgTable, text } from 'drizzle-orm/pg-core';
7
- import { eq } from 'drizzle-orm/expressions';
8
-
9
- const kv = pgTable('kv', {
10
- id: text('id').primaryKey().default('t'),
11
- value: text('value').default('v'),
12
- });
13
-
14
- const dbPassword: string | undefined = process.env.DB_PASSWORD || process.env.PGPASSWORD;
15
- if (!dbPassword) {
16
- throw new Error('DB_PASSWORD or PGPASSWORD environment variable not set');
17
- }
18
-
19
- const databaseUrl = `postgresql://postgres:${dbPassword}@localhost:5432/drizzle_testdb?sslmode=disable`;
20
-
21
- const poolconfig = {
22
- connectionString: databaseUrl,
23
- user: 'postgres',
24
- password: dbPassword,
25
- database: 'drizzle_testdb',
26
-
27
- host: 'localhost',
28
- port: 5432,
29
- };
30
-
31
- const drizzleDS = new DrizzleDataSource('app-db', poolconfig, { kv });
32
-
33
- const dbosConfig = {
34
- name: 'dbos_drizzle_test',
35
- databaseUrl: databaseUrl,
36
- poolConfig: poolconfig,
37
- system_database: 'drizzle_testdb_dbos_sys',
38
- telemetry: {
39
- logs: {
40
- silent: true,
41
- },
42
- },
43
- };
44
-
45
- async function txFunctionGuts() {
46
- expect(DBOS.isInTransaction()).toBe(true);
47
- expect(DBOS.isWithinWorkflow()).toBe(true);
48
- const res = await DrizzleDataSource.drizzleClient.execute("SELECT 'Tx2 result' as a");
49
- return res.rows[0].a as string;
50
- }
51
-
52
- const txFunc = drizzleDS.registerTransaction(txFunctionGuts, 'MySecondTx', {});
53
-
54
- async function wfFunctionGuts() {
55
- // Transaction variant 2: Let DBOS run a code snippet as a step
56
- const p1 = await drizzleDS.runTransaction(
57
- async () => {
58
- return (await DrizzleDataSource.drizzleClient.execute("SELECT 'My first tx result' as a")).rows[0].a;
59
- },
60
- 'MyFirstTx',
61
- { readOnly: true },
62
- );
63
-
64
- // Transaction variant 1: Use a registered DBOS transaction function
65
- const p2 = await txFunc();
66
-
67
- return p1 + '|' + p2;
68
- }
69
-
70
- // Workflow functions must always be registered before launch; this
71
- // allows recovery to occur.
72
- const wfFunction = DBOS.registerWorkflow(wfFunctionGuts, 'workflow');
73
-
74
- class DBWFI {
75
- @drizzleDS.transaction({ readOnly: true })
76
- static async tx(): Promise<string> {
77
- let res = await DrizzleDataSource.drizzleClient.execute("SELECT 'My decorated tx result' as a");
78
- return res.rows[0].a as string;
79
- }
80
-
81
- @DBOS.workflow()
82
- static async wf(): Promise<string> {
83
- return await DBWFI.tx();
84
- }
85
- }
86
-
87
- describe('decoratorless-api-tests', () => {
88
- beforeAll(() => {
89
- DBOS.setConfig(dbosConfig);
90
- });
91
-
92
- beforeEach(async () => {
93
- await setUpDBOSTestDb(dbosConfig);
94
- await drizzleDS.initializeInternalSchema();
95
- await drizzleDS.createSchema();
96
- await DBOS.launch();
97
- });
98
-
99
- afterEach(async () => {
100
- await DBOS.shutdown();
101
- });
102
-
103
- test('bare-tx-wf-functions', async () => {
104
- const wfid = randomUUID();
105
-
106
- await DBOS.withNextWorkflowID(wfid, async () => {
107
- const res = await wfFunction();
108
- expect(res).toBe('My first tx result|Tx2 result');
109
- });
110
-
111
- const wfsteps = (await DBOS.listWorkflowSteps(wfid))!;
112
- expect(wfsteps.length).toBe(2);
113
- expect(wfsteps[0].functionID).toBe(0);
114
- expect(wfsteps[0].name).toBe('MyFirstTx');
115
- expect(wfsteps[1].functionID).toBe(1);
116
- expect(wfsteps[1].name).toBe('MySecondTx');
117
- });
118
-
119
- test('decorated-tx-wf-functions', async () => {
120
- const wfid = randomUUID();
121
-
122
- await DBOS.withNextWorkflowID(wfid, async () => {
123
- const res = await DBWFI.wf();
124
- expect(res).toBe('My decorated tx result');
125
- });
126
-
127
- const wfsteps = (await DBOS.listWorkflowSteps(wfid))!;
128
- expect(wfsteps.length).toBe(1);
129
- expect(wfsteps[0].functionID).toBe(0);
130
- expect(wfsteps[0].name).toBe('tx');
131
- });
132
- });
133
-
134
- class KVController {
135
- @drizzleDS.transaction()
136
- static async testTxn(id: string, value: string) {
137
- await drizzleDS.dataSource?.insert(kv).values({ id: id, value: value }).onConflictDoNothing().execute();
138
-
139
- return id;
140
- }
141
-
142
- static async readTxn(id: string): Promise<string> {
143
- const kvp = await drizzleDS.dataSource?.select().from(kv).where(eq(kv.id, id)).limit(1).execute();
144
-
145
- return kvp?.[0]?.value ?? '<Not Found>';
146
- }
147
-
148
- @DBOS.workflow()
149
- static async wf(id: string, value: string) {
150
- return await KVController.testTxn(id, value);
151
- }
152
- }
153
-
154
- const txFunc2 = drizzleDS.registerTransaction(KVController.readTxn, 'explicitRegister', {});
155
- async function explicitWf(id: string): Promise<string> {
156
- return await txFunc2(id);
157
- }
158
- const wfFunction2 = DBOS.registerWorkflow(explicitWf, 'explicitworkflow');
159
-
160
- describe('drizzle-tests', () => {
161
- beforeAll(() => {
162
- DBOS.setConfig(dbosConfig);
163
- });
164
-
165
- beforeEach(async () => {
166
- await setUpDBOSTestDb(dbosConfig);
167
- await drizzleDS.initializeInternalSchema();
168
- await drizzleDS.createSchema();
169
- await DBOS.launch();
170
- });
171
-
172
- afterEach(async () => {
173
- await DBOS.shutdown();
174
- });
175
-
176
- test('simple-drizzle', async () => {
177
- await KVController.wf('test', 'value');
178
- let read = await KVController.readTxn('test');
179
- expect(read).toBe('value');
180
- });
181
-
182
- test('drizzle-register', async () => {
183
- await expect(wfFunction2('test')).resolves.toBe('<Not Found>');
184
- await KVController.wf('test', 'value');
185
- await expect(wfFunction2('test')).resolves.toBe('value');
186
- });
187
- });
@@ -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
- }