@dascompany/database 4.0.9 → 4.1.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.
- package/dist/Connection.d.ts +9 -7
- package/dist/Connection.js +20 -12
- package/dist/ConnectionFactory.d.ts +2 -0
- package/dist/ConnectionFactory.js +30 -13
- package/dist/Database.d.ts +2 -2
- package/dist/Database.js +4 -4
- package/dist/TESTConnection.d.ts +12 -0
- package/dist/TESTConnection.js +25 -0
- package/dist/TESTConnectionFactory.d.ts +16 -0
- package/dist/TESTConnectionFactory.js +64 -0
- package/dist/TESTTransactionClient.d.ts +11 -0
- package/dist/TESTTransactionClient.js +36 -0
- package/dist/Table.d.ts +3 -1
- package/dist/Table.js +4 -2
- package/dist/TransactionClient.d.ts +2 -0
- package/dist/TransactionClient.js +6 -0
- 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,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import pg from 'pg';
|
|
2
2
|
import Query from './query/Query';
|
|
3
|
+
import TransactionClient from './TransactionClient';
|
|
3
4
|
export default class Connection {
|
|
4
|
-
private
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
private pgPool;
|
|
6
|
+
private transactionClient;
|
|
7
|
+
constructor(pgPool: pg.Pool);
|
|
8
|
+
createQuery(queryString: string, client?: pg.PoolClient): Query;
|
|
9
|
+
getTransaction(): TransactionClient;
|
|
10
|
+
createTransaction(): Promise<TransactionClient>;
|
|
11
|
+
getClient(): Promise<pg.PoolClient>;
|
|
10
12
|
end(): Promise<void>;
|
|
11
13
|
}
|
package/dist/Connection.js
CHANGED
|
@@ -1,22 +1,30 @@
|
|
|
1
1
|
import Query from './query/Query';
|
|
2
|
+
import TransactionClient from './TransactionClient';
|
|
2
3
|
export default class Connection {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
pgPool;
|
|
5
|
+
transactionClient;
|
|
6
|
+
constructor(pgPool) {
|
|
7
|
+
this.pgPool = pgPool;
|
|
6
8
|
}
|
|
7
|
-
createQuery(queryString) {
|
|
8
|
-
|
|
9
|
+
createQuery(queryString, client) {
|
|
10
|
+
if (this.transactionClient && this.transactionClient.pending())
|
|
11
|
+
return new Query(this.transactionClient.getClient(), queryString);
|
|
12
|
+
else
|
|
13
|
+
return new Query(client ?? this.pgPool, queryString);
|
|
9
14
|
}
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
getTransaction() {
|
|
16
|
+
return this.transactionClient;
|
|
12
17
|
}
|
|
13
|
-
async
|
|
14
|
-
|
|
18
|
+
async createTransaction() {
|
|
19
|
+
if (!!this.transactionClient && this.transactionClient.pending())
|
|
20
|
+
throw new Error("there is no free client ");
|
|
21
|
+
this.transactionClient = new TransactionClient(await this.getClient());
|
|
22
|
+
return this.transactionClient;
|
|
15
23
|
}
|
|
16
|
-
async
|
|
17
|
-
|
|
24
|
+
async getClient() {
|
|
25
|
+
return this.pgPool.connect();
|
|
18
26
|
}
|
|
19
27
|
async end() {
|
|
20
|
-
this.
|
|
28
|
+
await this.pgPool.end();
|
|
21
29
|
}
|
|
22
30
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import DatabaseConfig from "./DatabaseConfig";
|
|
2
2
|
import Connection from "./Connection";
|
|
3
3
|
export default class ConnectionFactory {
|
|
4
|
+
private static numberOfAttemptsAllowed;
|
|
4
5
|
private static connections;
|
|
5
6
|
private static configName;
|
|
6
7
|
private static configField;
|
|
7
8
|
private static configPath;
|
|
8
9
|
private static defaultDatabaseName;
|
|
9
10
|
static create(name?: string): Promise<Connection>;
|
|
11
|
+
static test(name?: string, attempt?: number): Promise<Connection>;
|
|
10
12
|
static isCreated(name?: string): boolean;
|
|
11
13
|
static get(name?: string): Connection;
|
|
12
14
|
private static createConnection;
|
|
@@ -2,46 +2,63 @@ 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 numberOfAttemptsAllowed = 3;
|
|
5
6
|
static connections = new Map();
|
|
6
7
|
static configName = 'config.json';
|
|
7
8
|
static configField = 'database';
|
|
8
9
|
static configPath = './server/';
|
|
9
10
|
static defaultDatabaseName = 'defaultDatabaseName';
|
|
10
11
|
static async create(name) {
|
|
12
|
+
name = name || this.defaultDatabaseName;
|
|
13
|
+
if (this.connections.has(name)) {
|
|
14
|
+
throw new Error(`Connection for name: ${name} is already created!`);
|
|
15
|
+
}
|
|
16
|
+
const connection = await this.createConnection();
|
|
17
|
+
this.connections.set(name, connection);
|
|
18
|
+
return connection;
|
|
19
|
+
}
|
|
20
|
+
static async test(name, attempt = 1) {
|
|
11
21
|
name = name || this.defaultDatabaseName;
|
|
12
22
|
let connection = this.connections.get(name);
|
|
13
23
|
if (!connection) {
|
|
14
24
|
connection = await this.createConnection();
|
|
15
25
|
this.connections.set(name, connection);
|
|
16
26
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
27
|
+
try {
|
|
28
|
+
const client = await connection.getClient();
|
|
29
|
+
connection?.createQuery("SELECT 1", client);
|
|
30
|
+
return connection;
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
this.connections.delete(name);
|
|
34
|
+
if (this.numberOfAttemptsAllowed >= attempt)
|
|
35
|
+
return this.test('', attempt + 1);
|
|
36
|
+
else
|
|
37
|
+
throw new Error("Testing connection failed. Could not re-establish connection. Message: " + err);
|
|
38
|
+
}
|
|
20
39
|
}
|
|
21
40
|
static isCreated(name) {
|
|
22
41
|
name = name || this.defaultDatabaseName;
|
|
23
|
-
|
|
24
|
-
if (!connection)
|
|
25
|
-
return false;
|
|
26
|
-
else
|
|
27
|
-
return true;
|
|
42
|
+
return this.connections.has(name);
|
|
28
43
|
}
|
|
29
44
|
static get(name) {
|
|
30
45
|
name = name || this.defaultDatabaseName;
|
|
31
46
|
const connection = this.connections.get(name);
|
|
32
|
-
if (!connection)
|
|
33
|
-
throw new Error(`Connection with name: ${name} is not
|
|
47
|
+
if (!connection) {
|
|
48
|
+
throw new Error(`Connection with name: ${name} is not created!`);
|
|
49
|
+
}
|
|
34
50
|
return connection;
|
|
35
51
|
}
|
|
36
52
|
static async createConnection() {
|
|
37
53
|
const databaseConfig = await this.getDatabaseConfig();
|
|
38
|
-
const
|
|
39
|
-
return new Connection(
|
|
54
|
+
const pgPool = await Database.connect(databaseConfig);
|
|
55
|
+
return new Connection(pgPool);
|
|
40
56
|
}
|
|
41
57
|
static async getDatabaseConfig() {
|
|
42
58
|
let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
|
|
43
|
-
if (this.configField)
|
|
59
|
+
if (this.configField) {
|
|
44
60
|
config = config[this.configField];
|
|
61
|
+
}
|
|
45
62
|
return config;
|
|
46
63
|
}
|
|
47
64
|
}
|
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
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import pg from 'pg';
|
|
2
|
+
import Query from './query/Query';
|
|
3
|
+
import TESTTransactionClient from './TESTTransactionClient';
|
|
4
|
+
export default class TESTConnection {
|
|
5
|
+
private pgPool;
|
|
6
|
+
private TESTtransactionClient;
|
|
7
|
+
constructor(pgPool: pg.Pool);
|
|
8
|
+
createTransaction(): Promise<TESTTransactionClient>;
|
|
9
|
+
createQuery(queryString: string, client: pg.PoolClient): Query;
|
|
10
|
+
getClient(): Promise<pg.PoolClient>;
|
|
11
|
+
end(): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import Query from './query/Query';
|
|
2
|
+
import TESTTransactionClient from './TESTTransactionClient';
|
|
3
|
+
export default class TESTConnection {
|
|
4
|
+
pgPool;
|
|
5
|
+
TESTtransactionClient;
|
|
6
|
+
constructor(pgPool) {
|
|
7
|
+
this.pgPool = pgPool;
|
|
8
|
+
}
|
|
9
|
+
async createTransaction() {
|
|
10
|
+
this.TESTtransactionClient = new TESTTransactionClient(await this.getClient());
|
|
11
|
+
return this.TESTtransactionClient;
|
|
12
|
+
}
|
|
13
|
+
createQuery(queryString, client) {
|
|
14
|
+
if (this.TESTtransactionClient.pending())
|
|
15
|
+
return new Query(this.TESTtransactionClient.getClient(), queryString);
|
|
16
|
+
else
|
|
17
|
+
return new Query(client ?? this.pgPool, queryString);
|
|
18
|
+
}
|
|
19
|
+
async getClient() {
|
|
20
|
+
return this.pgPool.connect();
|
|
21
|
+
}
|
|
22
|
+
async end() {
|
|
23
|
+
await this.pgPool.end();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import DatabaseConfig from "./DatabaseConfig";
|
|
2
|
+
import TESTConnection from "./TESTConnection";
|
|
3
|
+
export default class TESTConnectionFactory {
|
|
4
|
+
private static numberOfAttemptsAllowed;
|
|
5
|
+
private static TESTConnections;
|
|
6
|
+
private static configName;
|
|
7
|
+
private static configField;
|
|
8
|
+
private static configPath;
|
|
9
|
+
private static defaultDatabaseName;
|
|
10
|
+
static create(name?: string): Promise<TESTConnection>;
|
|
11
|
+
static test(name?: string, attempt?: number): Promise<TESTConnection>;
|
|
12
|
+
static isCreated(name?: string): boolean;
|
|
13
|
+
static get(name?: string): TESTConnection;
|
|
14
|
+
private static createTESTConnection;
|
|
15
|
+
protected static getDatabaseConfig(): Promise<DatabaseConfig>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import Database from "./Database";
|
|
2
|
+
import TESTConnection from "./TESTConnection";
|
|
3
|
+
import { readFile } from 'fs/promises';
|
|
4
|
+
export default class TESTConnectionFactory {
|
|
5
|
+
static numberOfAttemptsAllowed = 3;
|
|
6
|
+
static TESTConnections = new Map();
|
|
7
|
+
static configName = 'config.json';
|
|
8
|
+
static configField = 'database';
|
|
9
|
+
static configPath = './server/';
|
|
10
|
+
static defaultDatabaseName = 'defaultDatabaseName';
|
|
11
|
+
static async create(name) {
|
|
12
|
+
name = name || this.defaultDatabaseName;
|
|
13
|
+
if (this.TESTConnections.has(name)) {
|
|
14
|
+
throw new Error(`TESTConnection for name: ${name} is already created!`);
|
|
15
|
+
}
|
|
16
|
+
const TESTConnection = await this.createTESTConnection();
|
|
17
|
+
this.TESTConnections.set(name, TESTConnection);
|
|
18
|
+
return TESTConnection;
|
|
19
|
+
}
|
|
20
|
+
static async test(name, attempt = 1) {
|
|
21
|
+
name = name || this.defaultDatabaseName;
|
|
22
|
+
const TESTConnection = this.TESTConnections.get(name);
|
|
23
|
+
if (!TESTConnection) {
|
|
24
|
+
const TESTConnection = await this.create(name);
|
|
25
|
+
return TESTConnection;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
const client = await TESTConnection.getClient();
|
|
29
|
+
TESTConnection?.createQuery("SELECT 1", client);
|
|
30
|
+
return TESTConnection;
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
this.TESTConnections.delete(name);
|
|
34
|
+
if (this.numberOfAttemptsAllowed >= attempt)
|
|
35
|
+
return this.test('', attempt + 1);
|
|
36
|
+
else
|
|
37
|
+
throw new Error("Testing TESTConnection failed. Could not re-establish TESTConnection.");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
static isCreated(name) {
|
|
41
|
+
name = name || this.defaultDatabaseName;
|
|
42
|
+
return this.TESTConnections.has(name);
|
|
43
|
+
}
|
|
44
|
+
static get(name) {
|
|
45
|
+
name = name || this.defaultDatabaseName;
|
|
46
|
+
const TESTConnection = this.TESTConnections.get(name);
|
|
47
|
+
if (!TESTConnection) {
|
|
48
|
+
throw new Error(`TESTConnection with name: ${name} is not created!`);
|
|
49
|
+
}
|
|
50
|
+
return TESTConnection;
|
|
51
|
+
}
|
|
52
|
+
static async createTESTConnection() {
|
|
53
|
+
const databaseConfig = await this.getDatabaseConfig();
|
|
54
|
+
const pgPool = await Database.connect(databaseConfig);
|
|
55
|
+
return new TESTConnection(pgPool);
|
|
56
|
+
}
|
|
57
|
+
static async getDatabaseConfig() {
|
|
58
|
+
let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
|
|
59
|
+
if (this.configField) {
|
|
60
|
+
config = config[this.configField];
|
|
61
|
+
}
|
|
62
|
+
return config;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import pg from 'pg';
|
|
2
|
+
export default class TESTTransactionClient {
|
|
3
|
+
private client;
|
|
4
|
+
private active;
|
|
5
|
+
constructor(client: pg.PoolClient);
|
|
6
|
+
pending(): boolean;
|
|
7
|
+
getClient(): pg.PoolClient;
|
|
8
|
+
beginTransaction(): Promise<void>;
|
|
9
|
+
commitTransaction(): Promise<void>;
|
|
10
|
+
rollBackTransaction(): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export default class TESTTransactionClient {
|
|
2
|
+
client;
|
|
3
|
+
active = false;
|
|
4
|
+
constructor(client) {
|
|
5
|
+
this.client = client;
|
|
6
|
+
}
|
|
7
|
+
pending() {
|
|
8
|
+
return this.active;
|
|
9
|
+
}
|
|
10
|
+
getClient() {
|
|
11
|
+
return this.client;
|
|
12
|
+
}
|
|
13
|
+
async beginTransaction() {
|
|
14
|
+
if (this.active) {
|
|
15
|
+
throw new Error('Transaction already active');
|
|
16
|
+
}
|
|
17
|
+
await this.client.query('BEGIN');
|
|
18
|
+
this.active = true;
|
|
19
|
+
}
|
|
20
|
+
async commitTransaction() {
|
|
21
|
+
if (!this.active) {
|
|
22
|
+
throw new Error('No active transaction to commit');
|
|
23
|
+
}
|
|
24
|
+
await this.client.query('COMMIT');
|
|
25
|
+
this.client.release();
|
|
26
|
+
this.active = false;
|
|
27
|
+
}
|
|
28
|
+
async rollBackTransaction() {
|
|
29
|
+
if (!this.active) {
|
|
30
|
+
throw new Error('No active transaction to roll back');
|
|
31
|
+
}
|
|
32
|
+
await this.client.query('ROLLBACK');
|
|
33
|
+
this.client.release();
|
|
34
|
+
this.active = false;
|
|
35
|
+
}
|
|
36
|
+
}
|
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;
|
|
@@ -3,6 +3,8 @@ export default class TransactionClient {
|
|
|
3
3
|
private client;
|
|
4
4
|
private active;
|
|
5
5
|
constructor(client: pg.PoolClient);
|
|
6
|
+
pending(): boolean;
|
|
7
|
+
getClient(): pg.PoolClient;
|
|
6
8
|
beginTransaction(): Promise<void>;
|
|
7
9
|
commitTransaction(): Promise<void>;
|
|
8
10
|
rollBackTransaction(): Promise<void>;
|
|
@@ -4,6 +4,12 @@ export default class TransactionClient {
|
|
|
4
4
|
constructor(client) {
|
|
5
5
|
this.client = client;
|
|
6
6
|
}
|
|
7
|
+
pending() {
|
|
8
|
+
return this.active;
|
|
9
|
+
}
|
|
10
|
+
getClient() {
|
|
11
|
+
return this.client;
|
|
12
|
+
}
|
|
7
13
|
async beginTransaction() {
|
|
8
14
|
if (this.active) {
|
|
9
15
|
throw new Error('Transaction already active');
|
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
|
-
}
|