@dascompany/database 4.0.2 → 4.0.4
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 +6 -6
- package/dist/Connection.js +14 -10
- package/dist/ConnectionFactory.js +12 -16
- package/dist/Database.d.ts +2 -2
- package/dist/Database.js +4 -4
- package/package.json +1 -1
package/dist/Connection.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import pg from 'pg';
|
|
2
2
|
import Query from './query/Query';
|
|
3
3
|
export default class Connection {
|
|
4
|
-
private
|
|
5
|
-
constructor(
|
|
4
|
+
private pgPool;
|
|
5
|
+
constructor(pgPool: pg.Pool);
|
|
6
6
|
createQuery(queryString: string): Query;
|
|
7
|
-
beginTransaction(): Promise<
|
|
8
|
-
commitTransaction(): Promise<void>;
|
|
9
|
-
rollBackTransaction(): Promise<void>;
|
|
7
|
+
beginTransaction(): Promise<pg.PoolClient>;
|
|
8
|
+
commitTransaction(client: pg.PoolClient): Promise<void>;
|
|
9
|
+
rollBackTransaction(client: pg.PoolClient): Promise<void>;
|
|
10
10
|
end(): Promise<void>;
|
|
11
11
|
}
|
package/dist/Connection.js
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
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
7
|
createQuery(queryString) {
|
|
8
|
-
return new Query(this.
|
|
8
|
+
return new Query(this.pgPool, queryString);
|
|
9
9
|
}
|
|
10
10
|
async beginTransaction() {
|
|
11
|
-
await this.
|
|
11
|
+
const client = await this.pgPool.connect();
|
|
12
|
+
await client.query('BEGIN');
|
|
13
|
+
return client;
|
|
12
14
|
}
|
|
13
|
-
async commitTransaction() {
|
|
14
|
-
await
|
|
15
|
+
async commitTransaction(client) {
|
|
16
|
+
await client.query('COMMIT');
|
|
17
|
+
client.release();
|
|
15
18
|
}
|
|
16
|
-
async rollBackTransaction() {
|
|
17
|
-
await
|
|
19
|
+
async rollBackTransaction(client) {
|
|
20
|
+
await client.query('ROLLBACK');
|
|
21
|
+
client.release();
|
|
18
22
|
}
|
|
19
23
|
async end() {
|
|
20
|
-
this.
|
|
24
|
+
await this.pgPool.end();
|
|
21
25
|
}
|
|
22
26
|
}
|
|
@@ -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
|
}
|