@dascompany/database 4.0.2 → 4.0.3

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,11 +1,11 @@
1
- import { Client } from 'pg';
1
+ import { Pool, PoolClient } from 'pg';
2
2
  import Query from './query/Query';
3
3
  export default class Connection {
4
- private pgClient;
5
- constructor(pgClient: Client);
4
+ private pgPool;
5
+ constructor(pgPool: Pool);
6
6
  createQuery(queryString: string): Query;
7
- beginTransaction(): Promise<void>;
8
- commitTransaction(): Promise<void>;
9
- rollBackTransaction(): Promise<void>;
7
+ beginTransaction(): Promise<PoolClient>;
8
+ commitTransaction(client: PoolClient): Promise<void>;
9
+ rollBackTransaction(client: PoolClient): Promise<void>;
10
10
  end(): Promise<void>;
11
11
  }
@@ -1,22 +1,26 @@
1
1
  import Query from './query/Query';
2
2
  export default class Connection {
3
- pgClient;
4
- constructor(pgClient) {
5
- this.pgClient = pgClient;
3
+ pgPool;
4
+ constructor(pgPool) {
5
+ this.pgPool = pgPool;
6
6
  }
7
7
  createQuery(queryString) {
8
- return new Query(this.pgClient, queryString);
8
+ return new Query(this.pgPool, queryString);
9
9
  }
10
10
  async beginTransaction() {
11
- await this.pgClient.query('BEGIN');
11
+ const client = await this.pgPool.connect();
12
+ await client.query('BEGIN');
13
+ return client;
12
14
  }
13
- async commitTransaction() {
14
- await this.pgClient.query('COMMIT');
15
+ async commitTransaction(client) {
16
+ await client.query('COMMIT');
17
+ client.release();
15
18
  }
16
- async rollBackTransaction() {
17
- await this.pgClient.query('ROLLBACK');
19
+ async rollBackTransaction(client) {
20
+ await client.query('ROLLBACK');
21
+ client.release();
18
22
  }
19
23
  async end() {
20
- this.pgClient.end();
24
+ await this.pgPool.end();
21
25
  }
22
26
  }
@@ -9,39 +9,35 @@ export default class ConnectionFactory {
9
9
  static defaultDatabaseName = 'defaultDatabaseName';
10
10
  static async create(name) {
11
11
  name = name || this.defaultDatabaseName;
12
- let connection = this.connections.get(name);
13
- if (!connection) {
14
- connection = await this.createConnection();
15
- this.connections.set(name, connection);
12
+ if (this.connections.has(name)) {
13
+ throw new Error(`Connection for name: ${name} is already created!`);
16
14
  }
17
- else
18
- throw new Error(`Connection for name: ${name} is already created! `);
15
+ const connection = await this.createConnection();
16
+ this.connections.set(name, connection);
19
17
  return connection;
20
18
  }
21
19
  static isCreated(name) {
22
20
  name = name || this.defaultDatabaseName;
23
- let connection = this.connections.get(name);
24
- if (!connection)
25
- return false;
26
- else
27
- return true;
21
+ return this.connections.has(name);
28
22
  }
29
23
  static get(name) {
30
24
  name = name || this.defaultDatabaseName;
31
25
  const connection = this.connections.get(name);
32
- if (!connection)
33
- throw new Error(`Connection with name: ${name} is not create! `);
26
+ if (!connection) {
27
+ throw new Error(`Connection with name: ${name} is not created!`);
28
+ }
34
29
  return connection;
35
30
  }
36
31
  static async createConnection() {
37
32
  const databaseConfig = await this.getDatabaseConfig();
38
- const pgClient = await Database.connect(databaseConfig);
39
- return new Connection(pgClient);
33
+ const pgPool = await Database.connect(databaseConfig);
34
+ return new Connection(pgPool);
40
35
  }
41
36
  static async getDatabaseConfig() {
42
37
  let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
43
- if (this.configField)
38
+ if (this.configField) {
44
39
  config = config[this.configField];
40
+ }
45
41
  return config;
46
42
  }
47
43
  }
@@ -1,4 +1,4 @@
1
- import { Client, type ConnectionConfig } from 'pg';
1
+ import { Pool, type PoolConfig } from 'pg';
2
2
  export default class Database {
3
- static connect(connectionConfig: ConnectionConfig): Promise<Client>;
3
+ static connect(connectionConfig: PoolConfig): Promise<Pool>;
4
4
  }
package/dist/Database.js CHANGED
@@ -1,13 +1,13 @@
1
- import pg from 'pg';
1
+ import { Pool } from 'pg';
2
2
  export default class Database {
3
3
  static async connect(connectionConfig) {
4
4
  try {
5
- const pgClient = new pg.Client(connectionConfig);
6
- await pgClient.connect();
7
- return pgClient;
5
+ const pool = new Pool(connectionConfig);
6
+ await pool.query('SELECT 1');
7
+ return pool;
8
8
  }
9
9
  catch (error) {
10
- error.message = `Database connection failed!\n ${error.message}`;
10
+ error.message = `Database connection failed!\n${error.message}`;
11
11
  throw error;
12
12
  }
13
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dascompany/database",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "test": "vitest",