Skip to content

Commit b7f9806

Browse files
committed
vibe
1 parent 922c28a commit b7f9806

7 files changed

Lines changed: 138 additions & 119 deletions

File tree

.eslintrc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"extends": ["eslint:recommended", "plugin:prettier/recommended", "prettier"],
55
"ignorePatterns": ["node_modules", "coverage", "packages/pg-protocol/dist/**/*", "packages/pg-query-stream/dist/**/*"],
66
"parserOptions": {
7-
"ecmaVersion": 2017,
7+
"ecmaVersion": 2022,
88
"sourceType": "module"
99
},
1010
"env": {
@@ -30,6 +30,15 @@
3030
"rules": {
3131
"no-undef": "off"
3232
}
33+
},
34+
{
35+
"files": ["packages/pg-transaction/src/**/*.ts"],
36+
"parser": "@typescript-eslint/parser",
37+
"parserOptions": {
38+
"ecmaVersion": 2022,
39+
"sourceType": "module",
40+
"project": "./packages/pg-transaction/tsconfig.eslint.json"
41+
}
3342
}
3443
]
3544
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
},
2222
"devDependencies": {
2323
"@typescript-eslint/eslint-plugin": "^7.0.0",
24-
"@typescript-eslint/parser": "^6.17.0",
24+
"@typescript-eslint/parser": "^7.0.0",
2525
"eslint": "^8.56.0",
2626
"eslint-config-prettier": "^10.1.2",
2727
"eslint-plugin-node": "^11.1.0",
2828
"eslint-plugin-prettier": "^5.1.2",
2929
"lerna": "^3.19.0",
3030
"prettier": "3.0.3",
31-
"typescript": "^4.0.3"
31+
"typescript": "^5.2.0"
3232
},
3333
"prettier": {
3434
"semi": false,

packages/pg-transaction/.eslintrc

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,74 @@
1-
import { strict as assert } from 'assert';
2-
import { Client } from 'pg';
3-
import { transaction } from '.';
1+
import { strict as assert } from 'assert'
2+
import { Client } from 'pg'
3+
import { transaction } from '.'
44

55
class DisposableClient extends Client {
66
// overwrite the query method and log the arguments and then dispatch to the original method
7-
override query(queryText: string, values?: any[]): Promise<any>;
8-
override query(queryConfig: any): Promise<any>;
9-
override query(queryStream: any): any;
107
override query(...args: any[]): any {
118
// console.log('Executing query:', ...args);
129
// @ts-ignore
13-
return super.query(...args);
10+
return super.query(...args)
1411
}
1512

1613
async [Symbol.asyncDispose]() {
17-
await this.end();
14+
await this.end()
1815
}
1916
}
2017

2118
async function getClient(): Promise<DisposableClient> {
2219
const client = new DisposableClient()
23-
await client.connect();
24-
await client.query('CREATE TEMP TABLE test_table (id SERIAL PRIMARY KEY, name TEXT)');
20+
await client.connect()
21+
await client.query('CREATE TEMP TABLE test_table (id SERIAL PRIMARY KEY, name TEXT)')
2522
return client
2623
}
2724

28-
2925
describe('transaction', () => {
3026
it('should create a client with an empty temp table', async () => {
31-
await using client = await getClient();
32-
const { rowCount } = await client.query('SELECT * FROM test_table');
33-
assert.equal(rowCount, 0, 'Temp table should be empty on creation');
34-
});
27+
await using client = await getClient()
28+
const { rowCount } = await client.query('SELECT * FROM test_table')
29+
assert.equal(rowCount, 0, 'Temp table should be empty on creation')
30+
})
3531

3632
it('automatically commits on success', async () => {
37-
await using client = await getClient();
38-
33+
await using client = await getClient()
34+
3935
const result = await transaction(client, async () => {
40-
await client.query('INSERT INTO test_table (name) VALUES ($1)', ['test']);
41-
const { rows } = await client.query('SELECT * FROM test_table');
42-
return rows[0].name; // Should return 'test'
43-
});
44-
45-
assert.equal(result, 'test');
46-
});
36+
await client.query('INSERT INTO test_table (name) VALUES ($1)', ['test'])
37+
const { rows } = await client.query('SELECT * FROM test_table')
38+
return rows[0].name // Should return 'test'
39+
})
40+
41+
assert.equal(result, 'test')
42+
})
4743

4844
it('automatically rolls back on error', async () => {
49-
await using client = await getClient();
50-
45+
await using client = await getClient()
46+
5147
// Assert that the transaction function rejects with the expected error
5248
await assert.rejects(
5349
async () => {
5450
await transaction(client, async () => {
55-
await client.query('INSERT INTO test_table (name) VALUES ($1)', ['test']);
56-
const { rows } = await client.query('SELECT * FROM test_table');
57-
throw new Error('Simulated error'); // This will trigger a rollback
58-
});
51+
await client.query('INSERT INTO test_table (name) VALUES ($1)', ['test'])
52+
await client.query('SELECT * FROM test_table')
53+
throw new Error('Simulated error') // This will trigger a rollback
54+
})
5955
},
6056
{
6157
name: 'Error',
62-
message: 'Simulated error'
58+
message: 'Simulated error',
6359
}
64-
);
60+
)
6561

6662
// Verify that the transaction rolled back
67-
const { rowCount } = await client.query('SELECT * FROM test_table');
68-
assert.equal(rowCount, 0, 'Table should be empty after rollback');
69-
});
63+
const { rowCount } = await client.query('SELECT * FROM test_table')
64+
assert.equal(rowCount, 0, 'Table should be empty after rollback')
65+
})
7066

7167
it('can return nothing from the transaction with correct type', async () => {
72-
await using client = await getClient();
73-
74-
const _nothing: void = await transaction(client, async () => {
75-
await client.query('INSERT INTO test_table (name) VALUES ($1)', ['test']);
76-
});
77-
});
78-
});
68+
await using client = await getClient()
69+
70+
const _: void = await transaction(client, async () => {
71+
await client.query('INSERT INTO test_table (name) VALUES ($1)', ['test'])
72+
})
73+
})
74+
})
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
import { Client } from 'pg';
1+
import { Client } from 'pg'
22

33
async function doTransaction(client: Client) {
4-
await client.query('BEGIN');
5-
6-
let shouldRollback = false;
7-
let disposed = false;
8-
4+
await client.query('BEGIN')
5+
6+
let shouldRollback = false
7+
let disposed = false
8+
99
return {
1010
async [Symbol.asyncDispose]() {
11-
if (disposed) return;
12-
disposed = true;
13-
11+
if (disposed) return
12+
disposed = true
13+
1414
if (shouldRollback) {
15-
await client.query('ROLLBACK');
15+
await client.query('ROLLBACK')
1616
} else {
17-
await client.query('COMMIT');
17+
await client.query('COMMIT')
1818
}
1919
},
20-
20+
2121
rollback() {
22-
shouldRollback = true;
23-
}
24-
};
22+
shouldRollback = true
23+
},
24+
}
2525
}
2626

2727
// Auto-rollback wrapper that catches errors automatically
2828
async function transaction<T>(client: Client, fn: () => Promise<T>): Promise<T> {
29-
await using txn = await doTransaction(client);
30-
29+
await using txn = await doTransaction(client)
30+
3131
try {
32-
const result = await fn();
32+
const result = await fn()
3333
// If we get here, success - transaction will auto-commit
34-
return result;
34+
return result
3535
} catch (error) {
3636
// If error occurs, mark for rollback
37-
txn.rollback();
38-
throw error;
37+
txn.rollback()
38+
throw error
3939
}
4040
}
4141

42-
export { transaction as transaction };
42+
export { transaction as transaction }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2022",
4+
"module": "commonjs",
5+
"lib": ["es2022"],
6+
"outDir": "./dist",
7+
"rootDir": "./src",
8+
"declaration": true,
9+
"esModuleInterop": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"strict": true,
12+
"skipLibCheck": true
13+
},
14+
"include": ["src/**/*"],
15+
"exclude": ["node_modules", "dist", "test"]
16+
}

0 commit comments

Comments
 (0)