@dascompany/database 4.0.0 → 4.0.2

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