@dascompany/database 4.1.0 → 4.2.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 +8 -10
- package/dist/Connection.js +22 -22
- package/dist/ConnectionFactory.d.ts +2 -8
- package/dist/ConnectionFactory.js +5 -46
- package/dist/Table.d.ts +1 -3
- package/dist/Table.js +2 -4
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/package.json +1 -1
- package/dist/TESTConnection.d.ts +0 -12
- package/dist/TESTConnection.js +0 -25
- package/dist/TESTConnectionFactory.d.ts +0 -16
- package/dist/TESTConnectionFactory.js +0 -64
- package/dist/TESTTransactionClient.d.ts +0 -11
- package/dist/TESTTransactionClient.js +0 -36
- package/dist/TransactionClient.d.ts +0 -11
- package/dist/TransactionClient.js +0 -36
package/dist/Connection.d.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { PoolClient } from 'pg';
|
|
2
2
|
import Query from './query/Query';
|
|
3
|
-
import TransactionClient from './TransactionClient';
|
|
4
3
|
export default class Connection {
|
|
5
|
-
private
|
|
6
|
-
private
|
|
7
|
-
constructor(
|
|
8
|
-
createQuery(queryString: string
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
end(): Promise<void>;
|
|
4
|
+
private poolClient;
|
|
5
|
+
private activeTransaction;
|
|
6
|
+
constructor(poolClient: PoolClient);
|
|
7
|
+
createQuery(queryString: string): Query;
|
|
8
|
+
beginTransaction(): Promise<void>;
|
|
9
|
+
commitTransaction(): Promise<void>;
|
|
10
|
+
rollBackTransaction(): Promise<void>;
|
|
13
11
|
}
|
package/dist/Connection.js
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
import Query from './query/Query';
|
|
2
|
-
import TransactionClient from './TransactionClient';
|
|
3
2
|
export default class Connection {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
constructor(
|
|
7
|
-
this.
|
|
3
|
+
poolClient;
|
|
4
|
+
activeTransaction = false;
|
|
5
|
+
constructor(poolClient) {
|
|
6
|
+
this.poolClient = poolClient;
|
|
8
7
|
}
|
|
9
|
-
createQuery(queryString
|
|
10
|
-
|
|
11
|
-
return new Query(this.transactionClient.getClient(), queryString);
|
|
12
|
-
else
|
|
13
|
-
return new Query(client ?? this.pgPool, queryString);
|
|
8
|
+
createQuery(queryString) {
|
|
9
|
+
return new Query(this.poolClient, queryString);
|
|
14
10
|
}
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
async beginTransaction() {
|
|
12
|
+
await this.poolClient.query('BEGIN');
|
|
17
13
|
}
|
|
18
|
-
async
|
|
19
|
-
if (
|
|
20
|
-
throw new Error(
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
async commitTransaction() {
|
|
15
|
+
if (!this.activeTransaction) {
|
|
16
|
+
throw new Error('No active transaction to commit');
|
|
17
|
+
}
|
|
18
|
+
await this.poolClient.query('COMMIT');
|
|
19
|
+
this.poolClient.release();
|
|
20
|
+
this.activeTransaction = false;
|
|
23
21
|
}
|
|
24
|
-
async
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
await this.
|
|
22
|
+
async rollBackTransaction() {
|
|
23
|
+
if (!this.activeTransaction) {
|
|
24
|
+
throw new Error('No active transaction to roll back');
|
|
25
|
+
}
|
|
26
|
+
await this.poolClient.query('ROLLBACK');
|
|
27
|
+
this.poolClient.release();
|
|
28
|
+
this.activeTransaction = false;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
import DatabaseConfig from "./DatabaseConfig";
|
|
2
2
|
import Connection from "./Connection";
|
|
3
3
|
export default class ConnectionFactory {
|
|
4
|
-
private static numberOfAttemptsAllowed;
|
|
5
|
-
private static connections;
|
|
6
4
|
private static configName;
|
|
7
5
|
private static configField;
|
|
8
6
|
private static configPath;
|
|
9
|
-
private static
|
|
10
|
-
static
|
|
11
|
-
static test(name?: string, attempt?: number): Promise<Connection>;
|
|
12
|
-
static isCreated(name?: string): boolean;
|
|
13
|
-
static get(name?: string): Connection;
|
|
14
|
-
private static createConnection;
|
|
7
|
+
private static pool;
|
|
8
|
+
static createConnection(): Promise<Connection>;
|
|
15
9
|
protected static getDatabaseConfig(): Promise<DatabaseConfig>;
|
|
16
10
|
}
|
|
@@ -2,57 +2,16 @@ 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;
|
|
6
|
-
static connections = new Map();
|
|
7
5
|
static configName = 'config.json';
|
|
8
6
|
static configField = 'database';
|
|
9
7
|
static configPath = './server/';
|
|
10
|
-
static
|
|
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) {
|
|
21
|
-
name = name || this.defaultDatabaseName;
|
|
22
|
-
let connection = this.connections.get(name);
|
|
23
|
-
if (!connection) {
|
|
24
|
-
connection = await this.createConnection();
|
|
25
|
-
this.connections.set(name, connection);
|
|
26
|
-
}
|
|
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
|
-
}
|
|
39
|
-
}
|
|
40
|
-
static isCreated(name) {
|
|
41
|
-
name = name || this.defaultDatabaseName;
|
|
42
|
-
return this.connections.has(name);
|
|
43
|
-
}
|
|
44
|
-
static get(name) {
|
|
45
|
-
name = name || this.defaultDatabaseName;
|
|
46
|
-
const connection = this.connections.get(name);
|
|
47
|
-
if (!connection) {
|
|
48
|
-
throw new Error(`Connection with name: ${name} is not created!`);
|
|
49
|
-
}
|
|
50
|
-
return connection;
|
|
51
|
-
}
|
|
8
|
+
static pool;
|
|
52
9
|
static async createConnection() {
|
|
53
10
|
const databaseConfig = await this.getDatabaseConfig();
|
|
54
|
-
|
|
55
|
-
|
|
11
|
+
if (!this.pool || await this.pool.query('SELECT 1').catch(() => false))
|
|
12
|
+
this.pool = await Database.connect(databaseConfig);
|
|
13
|
+
const client = await this.pool.connect();
|
|
14
|
+
return new Connection(client);
|
|
56
15
|
}
|
|
57
16
|
static async getDatabaseConfig() {
|
|
58
17
|
let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
11
|
-
export { ConnectionFactory, StockObject, Table, Query, ParameterType, ConnectorE, Parameters, QueryOptions, Connection, TransactionClient };
|
|
10
|
+
export { ConnectionFactory, StockObject, Table, Query, ParameterType, ConnectorE, Parameters, QueryOptions, Connection, };
|
package/package.json
CHANGED
package/dist/TESTConnection.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
}
|
package/dist/TESTConnection.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import pg from 'pg';
|
|
2
|
-
export default class TransactionClient {
|
|
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
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
export default class TransactionClient {
|
|
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
|
-
}
|