@dascompany/database 4.0.5 → 4.0.7

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.
@@ -2,11 +2,8 @@ import pg from 'pg';
2
2
  import Query from './query/Query';
3
3
  export default class Connection {
4
4
  private pgPool;
5
- private client;
6
5
  constructor(pgPool: pg.Pool);
7
- createQuery(queryString: string): Query;
8
- beginTransaction(): Promise<void>;
9
- commitTransaction(): Promise<void>;
10
- rollBackTransaction(): Promise<void>;
6
+ createQuery(queryString: string, client: pg.PoolClient): Query;
7
+ getClient(): Promise<pg.PoolClient>;
11
8
  end(): Promise<void>;
12
9
  }
@@ -1,36 +1,14 @@
1
1
  import Query from './query/Query';
2
2
  export default class Connection {
3
3
  pgPool;
4
- client = null;
5
4
  constructor(pgPool) {
6
5
  this.pgPool = pgPool;
7
6
  }
8
- createQuery(queryString) {
9
- return new Query(this.client ?? this.pgPool, queryString);
7
+ createQuery(queryString, client) {
8
+ return new Query(client ?? this.pgPool, queryString);
10
9
  }
11
- async beginTransaction() {
12
- if (this.client) {
13
- throw new Error('Transaction has already been started.');
14
- }
15
- this.client = await this.pgPool.connect();
16
- await this.client.query('BEGIN');
17
- console.log('Transaction started');
18
- }
19
- async commitTransaction() {
20
- if (!this.client) {
21
- throw new Error('No transaction started to commit.');
22
- }
23
- await this.client.query('COMMIT');
24
- this.client.release();
25
- this.client = null;
26
- }
27
- async rollBackTransaction() {
28
- if (!this.client) {
29
- throw new Error('No transaction started to roll back.');
30
- }
31
- await this.client.query('ROLLBACK');
32
- this.client.release();
33
- this.client = null;
10
+ async getClient() {
11
+ return this.pgPool.connect();
34
12
  }
35
13
  async end() {
36
14
  await this.pgPool.end();
package/dist/Table.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import Connection from './Connection';
2
2
  import Query from './query/Query';
3
3
  import QueryOptions from './query/QueryOptions';
4
+ import type { PoolClient } from 'pg';
4
5
  export default abstract class Table {
5
6
  private connection;
6
- constructor(connection: Connection);
7
+ private client;
8
+ constructor(connection: Connection, client: PoolClient);
7
9
  createQuery(queryString: string, options?: QueryOptions): Query;
8
10
  }
package/dist/Table.js CHANGED
@@ -2,15 +2,17 @@ import QueryParams from './query/QueryParams';
2
2
  import BindParams from './query/BindParams';
3
3
  export default class Table {
4
4
  connection;
5
- constructor(connection) {
5
+ client;
6
+ constructor(connection, client) {
6
7
  this.connection = connection;
8
+ this.client = client;
7
9
  }
8
10
  createQuery(queryString, options) {
9
11
  if (!queryString)
10
12
  throw new Error('Nie podano queryString');
11
13
  if (options)
12
14
  queryString = QueryParams.createParams(queryString, options);
13
- const query = this.connection.createQuery(queryString);
15
+ const query = this.connection.createQuery(queryString, this.client);
14
16
  if (options)
15
17
  BindParams.bindParams(query, options);
16
18
  return query;
@@ -0,0 +1,9 @@
1
+ import pg from 'pg';
2
+ export default class TransactionClient {
3
+ private client;
4
+ private active;
5
+ constructor(client: pg.PoolClient);
6
+ beginTransaction(): Promise<void>;
7
+ commitTransaction(): Promise<void>;
8
+ rollBackTransaction(): Promise<void>;
9
+ }
@@ -0,0 +1,30 @@
1
+ export default class TransactionClient {
2
+ client;
3
+ active = false;
4
+ constructor(client) {
5
+ this.client = client;
6
+ }
7
+ async beginTransaction() {
8
+ if (this.active) {
9
+ throw new Error('Transaction already active');
10
+ }
11
+ await this.client.query('BEGIN');
12
+ this.active = true;
13
+ }
14
+ async commitTransaction() {
15
+ if (!this.active) {
16
+ throw new Error('No active transaction to commit');
17
+ }
18
+ await this.client.query('COMMIT');
19
+ this.client.release();
20
+ this.active = false;
21
+ }
22
+ async rollBackTransaction() {
23
+ if (!this.active) {
24
+ throw new Error('No active transaction to roll back');
25
+ }
26
+ await this.client.query('ROLLBACK');
27
+ this.client.release();
28
+ this.active = false;
29
+ }
30
+ }
package/dist/index.d.ts CHANGED
@@ -13,4 +13,5 @@ import OrderByI from "./types/OrderByI";
13
13
  import QueryOptions from './query/QueryOptions';
14
14
  import ConnectorE from "./types/ConnectorE";
15
15
  import Connection from "./Connection";
16
- export { ConnectionFactory, StockObject, StockObjectMapper, Table, Query, ParameterType, ParameterFilter, ConnectorE, ParameterI, QueryOptionsI, ResultLimitationI, Parameters, OrderByI, QueryOptions, Connection };
16
+ import TransactionClient from "./TransactionClient";
17
+ export { ConnectionFactory, StockObject, StockObjectMapper, Table, Query, ParameterType, ParameterFilter, ConnectorE, ParameterI, QueryOptionsI, ResultLimitationI, Parameters, OrderByI, QueryOptions, Connection, TransactionClient };
package/dist/index.js CHANGED
@@ -7,4 +7,5 @@ import Parameters from './utils/Parameters';
7
7
  import QueryOptions from './query/QueryOptions';
8
8
  import ConnectorE from "./types/ConnectorE";
9
9
  import Connection from "./Connection";
10
- export { ConnectionFactory, StockObject, Table, Query, ParameterType, ConnectorE, Parameters, QueryOptions, Connection };
10
+ import TransactionClient from "./TransactionClient";
11
+ export { ConnectionFactory, StockObject, Table, Query, ParameterType, ConnectorE, Parameters, QueryOptions, Connection, TransactionClient };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dascompany/database",
3
- "version": "4.0.5",
3
+ "version": "4.0.7",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "test": "vitest",