@dascompany/database 4.0.9 → 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.
- package/dist/Connection.d.ts +5 -7
- package/dist/Connection.js +8 -14
- package/dist/ConnectionFactory.js +12 -16
- package/dist/Database.d.ts +2 -2
- package/dist/Database.js +4 -4
- package/dist/Table.d.ts +3 -1
- package/dist/Table.js +4 -2
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +2 -1
- package/dist/query/ResultsCorrector.d.ts +0 -4
- package/dist/query/ResultsCorrector.js +0 -14
package/dist/Connection.d.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import pg from 'pg';
|
|
2
2
|
import Query from './query/Query';
|
|
3
3
|
export default class Connection {
|
|
4
|
-
private
|
|
5
|
-
constructor(
|
|
6
|
-
createQuery(queryString: string): Query;
|
|
7
|
-
|
|
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
|
}
|
package/dist/Connection.js
CHANGED
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
import Query from './query/Query';
|
|
2
2
|
export default class Connection {
|
|
3
|
-
|
|
4
|
-
constructor(
|
|
5
|
-
this.
|
|
3
|
+
pgPool;
|
|
4
|
+
constructor(pgPool) {
|
|
5
|
+
this.pgPool = pgPool;
|
|
6
6
|
}
|
|
7
|
-
createQuery(queryString) {
|
|
8
|
-
return new Query(this.
|
|
7
|
+
createQuery(queryString, client) {
|
|
8
|
+
return new Query(client ?? this.pgPool, queryString);
|
|
9
9
|
}
|
|
10
|
-
async
|
|
11
|
-
|
|
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.
|
|
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
|
-
|
|
13
|
-
|
|
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
|
-
|
|
18
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
39
|
-
return new Connection(
|
|
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
|
}
|
package/dist/Database.d.ts
CHANGED
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
|
|
6
|
-
await
|
|
7
|
-
return
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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;
|
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
|
-
|
|
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
|
-
|
|
10
|
+
import TransactionClient from "./TransactionClient";
|
|
11
|
+
export { ConnectionFactory, StockObject, Table, Query, ParameterType, ConnectorE, Parameters, QueryOptions, Connection, TransactionClient };
|
package/package.json
CHANGED
|
@@ -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
|
-
}
|