@dascompany/database 4.0.7 → 4.0.8

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