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