@dascompany/database 3.1.4 → 4.0.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,11 +1,12 @@
1
- import { Client } from 'pg';
1
+ import { PoolClient } from 'pg';
2
2
  import Query from './query/Query';
3
3
  export default class Connection {
4
- private pgClient;
5
- constructor(pgClient: Client);
6
- createQuery(queryString: string): Query;
4
+ private client;
5
+ private inTransaction;
6
+ constructor(client: PoolClient);
7
7
  beginTransaction(): Promise<void>;
8
8
  commitTransaction(): Promise<void>;
9
9
  rollBackTransaction(): Promise<void>;
10
+ createQuery(queryString: string): Query;
10
11
  end(): Promise<void>;
11
12
  }
@@ -1,22 +1,37 @@
1
1
  import Query from './query/Query';
2
2
  export default class Connection {
3
- pgClient;
4
- constructor(pgClient) {
5
- this.pgClient = pgClient;
6
- }
7
- createQuery(queryString) {
8
- return new Query(this.pgClient, queryString);
3
+ client;
4
+ inTransaction = false;
5
+ constructor(client) {
6
+ this.client = client;
9
7
  }
10
8
  async beginTransaction() {
11
- await this.pgClient.query('BEGIN');
9
+ await this.client.query('BEGIN');
10
+ this.inTransaction = true;
12
11
  }
13
12
  async commitTransaction() {
14
- await this.pgClient.query('COMMIT');
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();
15
18
  }
16
19
  async rollBackTransaction() {
17
- await this.pgClient.query('ROLLBACK');
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);
18
28
  }
19
29
  async end() {
20
- this.pgClient.end();
30
+ if (!this.inTransaction) {
31
+ this.client.release();
32
+ }
33
+ else {
34
+ throw new Error('Cannot release client while transaction is active.');
35
+ }
21
36
  }
22
37
  }
@@ -1,14 +1,11 @@
1
1
  import DatabaseConfig from "./DatabaseConfig";
2
2
  import Connection from "./Connection";
3
3
  export default class ConnectionFactory {
4
- private static connections;
4
+ private static pool;
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;
13
10
  protected static getDatabaseConfig(): Promise<DatabaseConfig>;
14
11
  }
@@ -2,41 +2,18 @@ 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();
5
+ static pool = null;
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
- 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);
11
+ if (!this.pool) {
12
+ const config = await this.getDatabaseConfig();
13
+ this.pool = Database.connect(config);
16
14
  }
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);
15
+ const client = await this.pool.connect();
16
+ return new Connection(client);
40
17
  }
41
18
  static async getDatabaseConfig() {
42
19
  let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
@@ -1,4 +1,5 @@
1
- import { Client, type ConnectionConfig } from 'pg';
1
+ import { Pool, PoolConfig } from 'pg';
2
2
  export default class Database {
3
- static connect(connectionConfig: ConnectionConfig): Promise<Client>;
3
+ private static pool;
4
+ static connect(connectionConfig: PoolConfig): Pool;
4
5
  }
package/dist/Database.js CHANGED
@@ -1,14 +1,11 @@
1
- import pg from 'pg';
1
+ import { Pool } from 'pg';
2
2
  export default class Database {
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;
3
+ static pool = null;
4
+ static connect(connectionConfig) {
5
+ if (!this.pool) {
6
+ this.pool = new Pool(connectionConfig);
7
+ console.log('Database pool created');
12
8
  }
9
+ return this.pool;
13
10
  }
14
11
  }
@@ -18,7 +18,10 @@ export default class QueryParamCreator {
18
18
  }
19
19
  }
20
20
  static isArrayType(type) {
21
- if (type == ParameterType.arrayString || type == ParameterType.arrayNumber || type == ParameterType.arrayBoolean)
21
+ if (type == ParameterType.arrayString ||
22
+ type == ParameterType.arrayNumber ||
23
+ type == ParameterType.arrayBoolean ||
24
+ type == ParameterType.arrayBigint)
22
25
  return true;
23
26
  else
24
27
  return false;
@@ -0,0 +1,4 @@
1
+ export default class ResultsCorrector {
2
+ static correctRows(rows: Array<any>): Array<any>;
3
+ private static isNumber;
4
+ }
@@ -0,0 +1,14 @@
1
+ export default class ResultsCorrector {
2
+ static correctRows(rows) {
3
+ rows.forEach((row) => {
4
+ Object.keys(row).forEach((key) => {
5
+ if (this.isNumber(row[key]))
6
+ row[key] = Number(row[key]);
7
+ });
8
+ });
9
+ return rows;
10
+ }
11
+ static isNumber(n) {
12
+ return !isNaN(parseFloat(n)) && !isNaN(n - 0);
13
+ }
14
+ }
@@ -2,8 +2,8 @@ import StockObjectMapper from "./StockObjectMapper";
2
2
  export default abstract class StockObject {
3
3
  private id;
4
4
  protected abstract getMapper(): StockObjectMapper;
5
- getId(): number | null;
6
- setId(clientId: number): void;
5
+ getId(): string | null;
6
+ setId(id: string): void;
7
7
  save(): Promise<void>;
8
8
  delete(): Promise<void>;
9
9
  }
@@ -3,8 +3,8 @@ export default class StockObject {
3
3
  getId() {
4
4
  return this.id;
5
5
  }
6
- setId(clientId) {
7
- this.id = clientId;
6
+ setId(id) {
7
+ this.id = id;
8
8
  }
9
9
  async save() {
10
10
  await this.getMapper().save();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dascompany/database",
3
- "version": "3.1.4",
3
+ "version": "4.0.0",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "test": "vitest",