@dascompany/database 4.2.3 → 4.2.5

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.
@@ -1,10 +1,4 @@
1
- import DatabaseConfig from "./DatabaseConfig";
2
- import Connection from "./Connection";
1
+ import Connection from './Connection';
3
2
  export default class ConnectionFactory {
4
- private static configName;
5
- private static configField;
6
- private static configPath;
7
- private static pool;
8
3
  static createConnection(): Promise<Connection>;
9
- protected static getDatabaseConfig(): Promise<DatabaseConfig>;
10
4
  }
@@ -1,23 +1,8 @@
1
- import Database from "./Database";
2
- import Connection from "./Connection";
3
- import { readFile } from 'fs/promises';
1
+ import Connection from './Connection';
2
+ import PoolConnection from './PoolConnection';
4
3
  export default class ConnectionFactory {
5
- static configName = 'config.json';
6
- static configField = 'database';
7
- static configPath = './server/';
8
- static pool;
9
4
  static async createConnection() {
10
- const databaseConfig = await this.getDatabaseConfig();
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();
5
+ const client = await PoolConnection.getClient();
14
6
  return new Connection(client);
15
7
  }
16
- static async getDatabaseConfig() {
17
- let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
18
- if (this.configField) {
19
- config = config[this.configField];
20
- }
21
- return config;
22
- }
23
8
  }
package/dist/Pool.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import pg from 'pg';
2
+ export declare function getPool(): Promise<pg.Pool>;
package/dist/Pool.js ADDED
@@ -0,0 +1,17 @@
1
+ import Database from './Database';
2
+ import { readFile } from 'fs/promises';
3
+ let pool = null;
4
+ let poolPromise = null;
5
+ export async function getPool() {
6
+ if (pool)
7
+ return pool;
8
+ if (!poolPromise) {
9
+ poolPromise = (async () => {
10
+ const raw = await readFile('./server/config.json', 'utf8');
11
+ const config = JSON.parse(raw).database;
12
+ return Database.connect(config);
13
+ })();
14
+ }
15
+ pool = await poolPromise;
16
+ return pool;
17
+ }
@@ -0,0 +1,13 @@
1
+ import { PoolClient } from 'pg';
2
+ export default class PoolConnection {
3
+ private static pool;
4
+ private static poolPromise;
5
+ private static readonly configName;
6
+ private static readonly configField;
7
+ private static readonly configPath;
8
+ static getClient(): Promise<PoolClient>;
9
+ private static getPool;
10
+ private static ensurePool;
11
+ private static init;
12
+ private static getDatabaseConfig;
13
+ }
@@ -0,0 +1,36 @@
1
+ import { readFile } from 'fs/promises';
2
+ import Database from './Database';
3
+ export default class PoolConnection {
4
+ static pool;
5
+ static poolPromise;
6
+ static configName = 'config.json';
7
+ static configField = 'database';
8
+ static configPath = './server/';
9
+ static async getClient() {
10
+ const pool = await this.getPool();
11
+ return pool.connect();
12
+ }
13
+ static async getPool() {
14
+ if (this.pool) {
15
+ return this.pool;
16
+ }
17
+ return this.pool = await this.ensurePool();
18
+ }
19
+ static async ensurePool() {
20
+ if (!this.poolPromise) {
21
+ this.poolPromise = this.init();
22
+ }
23
+ return this.poolPromise;
24
+ }
25
+ static async init() {
26
+ const config = await this.getDatabaseConfig();
27
+ return Database.connect(config);
28
+ }
29
+ static async getDatabaseConfig() {
30
+ let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
31
+ if (this.configField) {
32
+ config = config[this.configField];
33
+ }
34
+ return config;
35
+ }
36
+ }
@@ -3,13 +3,11 @@ import OrderByI from "../types/OrderByI";
3
3
  import WhereOptionI from "../types/WhereOptionI";
4
4
  import ParameterI from "../types/ParameterI";
5
5
  import ConnectorE from "../types/ConnectorE";
6
- import QueryOptionsI from "../types/QueryOptionsI";
7
6
  export default class QueryOptions {
8
7
  private where;
9
8
  private orderBy;
10
9
  private limit;
11
10
  constructor();
12
- static create(data: QueryOptionsI): QueryOptions;
13
11
  getWhere(): WhereOptionI | null;
14
12
  getOrderBy(): OrderByI[] | null;
15
13
  getLimit(): ResultLimitationI | null;
@@ -1,4 +1,4 @@
1
- import ConnectorE, { getFromValue } from "../types/ConnectorE";
1
+ import ConnectorE from "../types/ConnectorE";
2
2
  export default class QueryOptions {
3
3
  where;
4
4
  orderBy;
@@ -8,20 +8,6 @@ export default class QueryOptions {
8
8
  this.orderBy = null;
9
9
  this.limit = null;
10
10
  }
11
- static create(data) {
12
- const queryOptions = new QueryOptions();
13
- if (data.where) {
14
- const connector = data.where.connector ? getFromValue(data.where.connector) : ConnectorE.and;
15
- queryOptions.setWhere(data.where.parameters, connector);
16
- }
17
- if (data.orderBy) {
18
- queryOptions.setOrderBy(data.orderBy);
19
- }
20
- if (data.limit) {
21
- queryOptions.setLimit(data.limit.limit, data.limit.offset);
22
- }
23
- return queryOptions;
24
- }
25
11
  getWhere() {
26
12
  return this.where;
27
13
  }
@@ -31,8 +31,9 @@ export default class QueryParams {
31
31
  }
32
32
  static createOrderParams(sql, orderBy) {
33
33
  let sqlString = `ORDER BY `;
34
- orderBy.forEach(item => {
35
- sqlString += `${item.key} ${item.order} `;
34
+ orderBy.forEach((item, i) => {
35
+ sqlString += `${item.key} ${item.order}`;
36
+ sqlString += orderBy.length - 1 !== i ? ', ' : ' ';
36
37
  });
37
38
  sql = `${sql} ${sqlString}`;
38
39
  return sql;
@@ -2,6 +2,4 @@ declare enum ConnectorE {
2
2
  or = "OR ",
3
3
  and = "AND "
4
4
  }
5
- declare function getFromValue(value: string): ConnectorE;
6
5
  export default ConnectorE;
7
- export { getFromValue };
@@ -3,14 +3,4 @@ var ConnectorE;
3
3
  ConnectorE["or"] = "OR ";
4
4
  ConnectorE["and"] = "AND ";
5
5
  })(ConnectorE || (ConnectorE = {}));
6
- function getFromValue(value) {
7
- switch (value) {
8
- case "OR ":
9
- return ConnectorE.or;
10
- case "AND ":
11
- default:
12
- return ConnectorE.and;
13
- }
14
- }
15
6
  export default ConnectorE;
16
- export { getFromValue };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dascompany/database",
3
- "version": "4.2.3",
3
+ "version": "4.2.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -1,13 +0,0 @@
1
- import OrderByI from "./OrderByI";
2
- import ParameterI from "./ParameterI";
3
- export default interface QueryOptionsConfig {
4
- where: {
5
- connector: string;
6
- parameters: ParameterI[];
7
- };
8
- orderBy: OrderByI[];
9
- limit: {
10
- limit: number;
11
- offset: number;
12
- };
13
- }
@@ -1 +0,0 @@
1
- export {};