@dascompany/database 4.0.10 → 4.2.0

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,9 +1,11 @@
1
- import pg from 'pg';
1
+ import { PoolClient } from 'pg';
2
2
  import Query from './query/Query';
3
3
  export default class Connection {
4
- private pgPool;
5
- constructor(pgPool: pg.Pool);
6
- createQuery(queryString: string, client: pg.PoolClient): Query;
7
- getClient(): Promise<pg.PoolClient>;
8
- end(): Promise<void>;
4
+ private poolClient;
5
+ private activeTransaction;
6
+ constructor(poolClient: PoolClient);
7
+ createQuery(queryString: string): Query;
8
+ beginTransaction(): Promise<void>;
9
+ commitTransaction(): Promise<void>;
10
+ rollBackTransaction(): Promise<void>;
9
11
  }
@@ -1,16 +1,30 @@
1
1
  import Query from './query/Query';
2
2
  export default class Connection {
3
- pgPool;
4
- constructor(pgPool) {
5
- this.pgPool = pgPool;
3
+ poolClient;
4
+ activeTransaction = false;
5
+ constructor(poolClient) {
6
+ this.poolClient = poolClient;
6
7
  }
7
- createQuery(queryString, client) {
8
- return new Query(client ?? this.pgPool, queryString);
8
+ createQuery(queryString) {
9
+ return new Query(this.poolClient, queryString);
9
10
  }
10
- async getClient() {
11
- return this.pgPool.connect();
11
+ async beginTransaction() {
12
+ await this.poolClient.query('BEGIN');
12
13
  }
13
- async end() {
14
- await this.pgPool.end();
14
+ async commitTransaction() {
15
+ if (!this.activeTransaction) {
16
+ throw new Error('No active transaction to commit');
17
+ }
18
+ await this.poolClient.query('COMMIT');
19
+ this.poolClient.release();
20
+ this.activeTransaction = false;
21
+ }
22
+ async rollBackTransaction() {
23
+ if (!this.activeTransaction) {
24
+ throw new Error('No active transaction to roll back');
25
+ }
26
+ await this.poolClient.query('ROLLBACK');
27
+ this.poolClient.release();
28
+ this.activeTransaction = false;
15
29
  }
16
30
  }
@@ -1,14 +1,10 @@
1
1
  import DatabaseConfig from "./DatabaseConfig";
2
2
  import Connection from "./Connection";
3
3
  export default class ConnectionFactory {
4
- private static connections;
5
4
  private static configName;
6
5
  private static configField;
7
6
  private static configPath;
8
- private static defaultDatabaseName;
9
- static create(name?: string): Promise<Connection>;
10
- static isCreated(name?: string): boolean;
11
- static get(name?: string): Connection;
12
- private static createConnection;
7
+ private static pool;
8
+ static createConnection(): Promise<Connection>;
13
9
  protected static getDatabaseConfig(): Promise<DatabaseConfig>;
14
10
  }
@@ -2,36 +2,16 @@ import Database from "./Database";
2
2
  import Connection from "./Connection";
3
3
  import { readFile } from 'fs/promises';
4
4
  export default class ConnectionFactory {
5
- static connections = new Map();
6
5
  static configName = 'config.json';
7
6
  static configField = 'database';
8
7
  static configPath = './server/';
9
- static defaultDatabaseName = 'defaultDatabaseName';
10
- static async create(name) {
11
- name = name || this.defaultDatabaseName;
12
- if (this.connections.has(name)) {
13
- throw new Error(`Connection for name: ${name} is already created!`);
14
- }
15
- const connection = await this.createConnection();
16
- this.connections.set(name, connection);
17
- return connection;
18
- }
19
- static isCreated(name) {
20
- name = name || this.defaultDatabaseName;
21
- return this.connections.has(name);
22
- }
23
- static get(name) {
24
- name = name || this.defaultDatabaseName;
25
- const connection = this.connections.get(name);
26
- if (!connection) {
27
- throw new Error(`Connection with name: ${name} is not created!`);
28
- }
29
- return connection;
30
- }
8
+ static pool;
31
9
  static async createConnection() {
32
10
  const databaseConfig = await this.getDatabaseConfig();
33
- const pgPool = await Database.connect(databaseConfig);
34
- return new Connection(pgPool);
11
+ if (!this.pool || await this.pool.query('SELECT 1').catch(() => false))
12
+ this.pool = await Database.connect(databaseConfig);
13
+ const client = await this.pool.connect();
14
+ return new Connection(client);
35
15
  }
36
16
  static async getDatabaseConfig() {
37
17
  let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
package/dist/Table.d.ts CHANGED
@@ -1,10 +1,8 @@
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';
5
4
  export default abstract class Table {
6
5
  private connection;
7
- private client;
8
- constructor(connection: Connection, client: PoolClient);
6
+ constructor(connection: Connection);
9
7
  createQuery(queryString: string, options?: QueryOptions): Query;
10
8
  }
package/dist/Table.js CHANGED
@@ -2,17 +2,15 @@ import QueryParams from './query/QueryParams';
2
2
  import BindParams from './query/BindParams';
3
3
  export default class Table {
4
4
  connection;
5
- client;
6
- constructor(connection, client) {
5
+ constructor(connection) {
7
6
  this.connection = connection;
8
- this.client = client;
9
7
  }
10
8
  createQuery(queryString, options) {
11
9
  if (!queryString)
12
10
  throw new Error('Nie podano queryString');
13
11
  if (options)
14
12
  queryString = QueryParams.createParams(queryString, options);
15
- const query = this.connection.createQuery(queryString, this.client);
13
+ const query = this.connection.createQuery(queryString);
16
14
  if (options)
17
15
  BindParams.bindParams(query, options);
18
16
  return query;
package/dist/index.d.ts CHANGED
@@ -13,5 +13,4 @@ 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
- import TransactionClient from "./TransactionClient";
17
- export { ConnectionFactory, StockObject, StockObjectMapper, Table, Query, ParameterType, ParameterFilter, ConnectorE, ParameterI, QueryOptionsI, ResultLimitationI, Parameters, OrderByI, QueryOptions, Connection, TransactionClient };
16
+ export { ConnectionFactory, StockObject, StockObjectMapper, Table, Query, ParameterType, ParameterFilter, ConnectorE, ParameterI, QueryOptionsI, ResultLimitationI, Parameters, OrderByI, QueryOptions, Connection, };
package/dist/index.js CHANGED
@@ -7,5 +7,4 @@ 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
- import TransactionClient from "./TransactionClient";
11
- export { ConnectionFactory, StockObject, Table, Query, ParameterType, ConnectorE, Parameters, QueryOptions, Connection, TransactionClient };
10
+ export { ConnectionFactory, StockObject, Table, Query, ParameterType, ConnectorE, Parameters, QueryOptions, Connection, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dascompany/database",
3
- "version": "4.0.10",
3
+ "version": "4.2.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -1,9 +0,0 @@
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
- }
@@ -1,30 +0,0 @@
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
- }