@dascompany/database 4.0.6 → 4.0.8
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 +3 -4
- package/dist/Connection.js +8 -23
- package/dist/ConnectionFactory.js +16 -12
- package/dist/Database.d.ts +2 -2
- package/dist/Database.js +4 -4
- package/dist/types/StockObject.d.ts +2 -2
- package/dist/types/StockObject.js +2 -2
- package/package.json +1 -1
- package/dist/query/ResultsCorrector.d.ts +0 -4
- package/dist/query/ResultsCorrector.js +0 -14
package/dist/Connection.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Client } from 'pg';
|
|
2
2
|
import Query from './query/Query';
|
|
3
3
|
export default class Connection {
|
|
4
|
-
private
|
|
5
|
-
|
|
6
|
-
constructor(pgPool: pg.Pool);
|
|
4
|
+
private pgClient;
|
|
5
|
+
constructor(pgClient: Client);
|
|
7
6
|
createQuery(queryString: string): Query;
|
|
8
7
|
beginTransaction(): Promise<void>;
|
|
9
8
|
commitTransaction(): Promise<void>;
|
package/dist/Connection.js
CHANGED
|
@@ -1,37 +1,22 @@
|
|
|
1
1
|
import Query from './query/Query';
|
|
2
2
|
export default class Connection {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
this.pgPool = pgPool;
|
|
3
|
+
pgClient;
|
|
4
|
+
constructor(pgClient) {
|
|
5
|
+
this.pgClient = pgClient;
|
|
7
6
|
}
|
|
8
7
|
createQuery(queryString) {
|
|
9
|
-
return new Query(this.
|
|
8
|
+
return new Query(this.pgClient, queryString);
|
|
10
9
|
}
|
|
11
10
|
async beginTransaction() {
|
|
12
|
-
|
|
13
|
-
throw new Error('Transaction has already been started.');
|
|
14
|
-
}
|
|
15
|
-
this.client = await this.pgPool.connect();
|
|
16
|
-
await this.client.query('BEGIN');
|
|
11
|
+
await this.pgClient.query('BEGIN');
|
|
17
12
|
}
|
|
18
13
|
async commitTransaction() {
|
|
19
|
-
|
|
20
|
-
throw new Error('No transaction started to commit.');
|
|
21
|
-
}
|
|
22
|
-
await this.client.query('COMMIT');
|
|
23
|
-
this.client.release();
|
|
24
|
-
this.client = null;
|
|
14
|
+
await this.pgClient.query('COMMIT');
|
|
25
15
|
}
|
|
26
16
|
async rollBackTransaction() {
|
|
27
|
-
|
|
28
|
-
throw new Error('No transaction started to roll back.');
|
|
29
|
-
}
|
|
30
|
-
await this.client.query('ROLLBACK');
|
|
31
|
-
this.client.release();
|
|
32
|
-
this.client = null;
|
|
17
|
+
await this.pgClient.query('ROLLBACK');
|
|
33
18
|
}
|
|
34
19
|
async end() {
|
|
35
|
-
|
|
20
|
+
this.pgClient.end();
|
|
36
21
|
}
|
|
37
22
|
}
|
|
@@ -9,35 +9,39 @@ export default class ConnectionFactory {
|
|
|
9
9
|
static defaultDatabaseName = 'defaultDatabaseName';
|
|
10
10
|
static async create(name) {
|
|
11
11
|
name = name || this.defaultDatabaseName;
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
let connection = this.connections.get(name);
|
|
13
|
+
if (!connection) {
|
|
14
|
+
connection = await this.createConnection();
|
|
15
|
+
this.connections.set(name, connection);
|
|
14
16
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
else
|
|
18
|
+
throw new Error(`Connection for name: ${name} is already created! `);
|
|
17
19
|
return connection;
|
|
18
20
|
}
|
|
19
21
|
static isCreated(name) {
|
|
20
22
|
name = name || this.defaultDatabaseName;
|
|
21
|
-
|
|
23
|
+
let connection = this.connections.get(name);
|
|
24
|
+
if (!connection)
|
|
25
|
+
return false;
|
|
26
|
+
else
|
|
27
|
+
return true;
|
|
22
28
|
}
|
|
23
29
|
static get(name) {
|
|
24
30
|
name = name || this.defaultDatabaseName;
|
|
25
31
|
const connection = this.connections.get(name);
|
|
26
|
-
if (!connection)
|
|
27
|
-
throw new Error(`Connection with name: ${name} is not
|
|
28
|
-
}
|
|
32
|
+
if (!connection)
|
|
33
|
+
throw new Error(`Connection with name: ${name} is not create! `);
|
|
29
34
|
return connection;
|
|
30
35
|
}
|
|
31
36
|
static async createConnection() {
|
|
32
37
|
const databaseConfig = await this.getDatabaseConfig();
|
|
33
|
-
const
|
|
34
|
-
return new Connection(
|
|
38
|
+
const pgClient = await Database.connect(databaseConfig);
|
|
39
|
+
return new Connection(pgClient);
|
|
35
40
|
}
|
|
36
41
|
static async getDatabaseConfig() {
|
|
37
42
|
let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
|
|
38
|
-
if (this.configField)
|
|
43
|
+
if (this.configField)
|
|
39
44
|
config = config[this.configField];
|
|
40
|
-
}
|
|
41
45
|
return config;
|
|
42
46
|
}
|
|
43
47
|
}
|
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 pgClient = new pg.Client(connectionConfig);
|
|
6
|
+
await pgClient.connect();
|
|
7
|
+
return pgClient;
|
|
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
|
}
|
|
@@ -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():
|
|
6
|
-
setId(
|
|
5
|
+
getId(): number | null;
|
|
6
|
+
setId(clientId: number): void;
|
|
7
7
|
save(): Promise<void>;
|
|
8
8
|
delete(): Promise<void>;
|
|
9
9
|
}
|
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
|
-
}
|