Skip to content

Commit

Permalink
Merge pull request #37 from panates/implement-js-disposal
Browse files Browse the repository at this point in the history
Implement js disposal
  • Loading branch information
erayhanoglu committed Apr 22, 2024
2 parents 4060ea9 + d3df6f4 commit 816d817
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 19 deletions.
15 changes: 14 additions & 1 deletion DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,19 @@ connection.on('terminate', () => {
await connection.close(30000); // will wait 30 secs before terminate the connection
```

`Connection` already supports [TC30 Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management) proposal.

```ts
import { Connection } from 'postgresql-client';
{
// connection will be automatically closed when this scope ends
await using connection = new Connection('postgres://localhost');
await connection.connect();
}

```


##### .execute()

Executes single or multiple SQL scripts
Expand Down Expand Up @@ -795,7 +808,7 @@ import { Pool } from 'postgresql-client';
const pool = new Pool('postgres://localhost');
const connection = await pool.acquire();
// ...
await connection.relese();
await connection.close();
````

##### .close()
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Enterprise level PostgreSQL client for NodeJs
- Low memory utilization and boosted performance with Shared Buffers
- Supports Clear text, MD5 and SASL password algorithms
- Can return both array and object rows
- Auto disposal with "using" syntax ([TC30 Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management))


## Installation
Expand Down
6 changes: 5 additions & 1 deletion src/connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { PreparedStatement } from './prepared-statement.js';
export type NotificationMessage = Protocol.NotificationResponseMessage;
export type NotificationCallback = (msg: NotificationMessage) => any;

export class Connection extends SafeEventEmitter {
export class Connection extends SafeEventEmitter implements AsyncDisposable {
protected readonly _pool?: Pool;
protected readonly _intlCon: IntlConnection;
protected readonly _notificationListeners = new SafeEventEmitter();
Expand Down Expand Up @@ -305,4 +305,8 @@ export class Connection extends SafeEventEmitter {
throw e;
});
}

[Symbol.asyncDispose](): Promise<void> {
return this.close();
}
}
6 changes: 5 additions & 1 deletion src/connection/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { parseRow } from '../util/parse-row.js';
import { Portal } from './portal.js';
import { PreparedStatement } from './prepared-statement.js';

export class Cursor extends SafeEventEmitter {
export class Cursor extends SafeEventEmitter implements AsyncDisposable {
private readonly _statement: PreparedStatement;
private readonly _portal: Portal;
private readonly _parsers: AnyParseFunction[];
Expand Down Expand Up @@ -95,4 +95,8 @@ export class Cursor extends SafeEventEmitter {
})
.toPromise();
}

[Symbol.asyncDispose](): Promise<void> {
return this.close();
}
}
6 changes: 5 additions & 1 deletion src/connection/prepared-statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Portal } from './portal.js';
let statementCounter = 0;
let portalCounter = 0;

export class PreparedStatement extends SafeEventEmitter {
export class PreparedStatement extends SafeEventEmitter implements AsyncDisposable {
private readonly _connection: Connection;
private readonly _sql: string = '';
private readonly _name: string = '';
Expand Down Expand Up @@ -242,4 +242,8 @@ export class PreparedStatement extends SafeEventEmitter {
}
this.emit('close');
}

[Symbol.asyncDispose](): Promise<void> {
return this.close();
}
}
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Protocol } from './protocol/protocol.js';

import DataFormat = Protocol.DataFormat;

// @ts-ignore
Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose');

export { DataFormat };
export const DEFAULT_COLUMN_FORMAT = DataFormat.binary;

Expand Down
27 changes: 12 additions & 15 deletions test/B-connection/01-connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,18 @@ describe('Connection', function () {
const x = await connection.query('select count(*) from test.dummy_table1', { autoCommit: false });
expect(x.rows?.length).toStrictEqual(1);
expect(x.rows?.[0][0]).toStrictEqual(1);
/*
await connection.startTransaction();
try {
await connection.execute('savepoint sv1');
await connection.execute('invalid sql');
} catch (e) {
}
try {
await connection.execute('select 1');
} catch (e) {
await connection.execute('rollback to sv1');
}
await connection.execute('select 2');
await connection.execute('select 3');
*/

await connection.close();
});

it('should automatically close with "using" syntax', async function () {
let closed = false;
{
await using conn = new Connection();
await conn.connect();
conn.on('close', () => (closed = true));
expect(conn.state).toStrictEqual(ConnectionState.READY);
}
expect(closed).toStrictEqual(true);
});
});
13 changes: 13 additions & 0 deletions test/B-connection/06-cursor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ describe('Cursor support', function () {
await cursor?.close();
expect(count).toStrictEqual(10);
});

it('should automatically close with "using" syntax', async function () {
let closed = false;
{
const result = await connection.query(`select * from customers order by id`, { objectRows: false, cursor: true });
await using cursor = result.cursor!;
expect(cursor).toBeDefined();
cursor.on('close', () => (closed = true));
const row = await cursor?.next();
expect(row[0]).toStrictEqual(1);
}
expect(closed).toStrictEqual(true);
});
});

0 comments on commit 816d817

Please sign in to comment.