@dascompany/database 4.0.8 → 4.0.10

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,9 @@
1
- import { Client } from 'pg';
1
+ import pg 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;
7
- beginTransaction(): Promise<void>;
8
- commitTransaction(): Promise<void>;
9
- rollBackTransaction(): Promise<void>;
4
+ private pgPool;
5
+ constructor(pgPool: pg.Pool);
6
+ createQuery(queryString: string, client: pg.PoolClient): Query;
7
+ getClient(): Promise<pg.PoolClient>;
10
8
  end(): Promise<void>;
11
9
  }
@@ -1,22 +1,16 @@
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
- createQuery(queryString) {
8
- return new Query(this.pgClient, queryString);
7
+ createQuery(queryString, client) {
8
+ return new Query(client ?? this.pgPool, queryString);
9
9
  }
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');
10
+ async getClient() {
11
+ return this.pgPool.connect();
18
12
  }
19
13
  async end() {
20
- this.pgClient.end();
14
+ await this.pgPool.end();
21
15
  }
22
16
  }
@@ -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 pg from 'pg';
2
2
  export default class Database {
3
- static connect(connectionConfig: ConnectionConfig): Promise<Client>;
3
+ static connect(connectionConfig: pg.PoolConfig): Promise<pg.Pool>;
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 pgClient = new pg.Client(connectionConfig);
6
- await pgClient.connect();
7
- return pgClient;
5
+ const pool = new pg.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/dist/Table.d.ts CHANGED
@@ -1,8 +1,10 @@
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';
4
5
  export default abstract class Table {
5
6
  private connection;
6
- constructor(connection: Connection);
7
+ private client;
8
+ constructor(connection: Connection, client: PoolClient);
7
9
  createQuery(queryString: string, options?: QueryOptions): Query;
8
10
  }
package/dist/Table.js CHANGED
@@ -2,15 +2,17 @@ import QueryParams from './query/QueryParams';
2
2
  import BindParams from './query/BindParams';
3
3
  export default class Table {
4
4
  connection;
5
- constructor(connection) {
5
+ client;
6
+ constructor(connection, client) {
6
7
  this.connection = connection;
8
+ this.client = client;
7
9
  }
8
10
  createQuery(queryString, options) {
9
11
  if (!queryString)
10
12
  throw new Error('Nie podano queryString');
11
13
  if (options)
12
14
  queryString = QueryParams.createParams(queryString, options);
13
- const query = this.connection.createQuery(queryString);
15
+ const query = this.connection.createQuery(queryString, this.client);
14
16
  if (options)
15
17
  BindParams.bindParams(query, options);
16
18
  return query;
@@ -0,0 +1,9 @@
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
+ }
@@ -0,0 +1,30 @@
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
+ }
package/dist/index.d.ts CHANGED
@@ -13,4 +13,5 @@ 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
- export { ConnectionFactory, StockObject, StockObjectMapper, Table, Query, ParameterType, ParameterFilter, ConnectorE, ParameterI, QueryOptionsI, ResultLimitationI, Parameters, OrderByI, QueryOptions, Connection };
16
+ import TransactionClient from "./TransactionClient";
17
+ export { ConnectionFactory, StockObject, StockObjectMapper, Table, Query, ParameterType, ParameterFilter, ConnectorE, ParameterI, QueryOptionsI, ResultLimitationI, Parameters, OrderByI, QueryOptions, Connection, TransactionClient };
package/dist/index.js CHANGED
@@ -7,4 +7,5 @@ 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
- export { ConnectionFactory, StockObject, Table, Query, ParameterType, ConnectorE, Parameters, QueryOptions, Connection };
10
+ import TransactionClient from "./TransactionClient";
11
+ export { ConnectionFactory, StockObject, Table, Query, ParameterType, ConnectorE, Parameters, QueryOptions, Connection, TransactionClient };
@@ -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,7 +1,8 @@
1
1
  {
2
2
  "name": "@dascompany/database",
3
- "version": "4.0.8",
3
+ "version": "4.0.10",
4
4
  "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
5
6
  "scripts": {
6
7
  "test": "vitest",
7
8
  "build": "tsc --declaration",