@dascompany/database 4.2.7 → 5.0.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.
@@ -1,4 +1,4 @@
1
1
  import Connection from './Connection';
2
2
  export default class ConnectionFactory {
3
- static createConnection(): Promise<Connection>;
3
+ static createConnection(tenantName?: string): Promise<Connection>;
4
4
  }
@@ -1,8 +1,8 @@
1
1
  import Connection from './Connection';
2
2
  import PoolConnection from './PoolConnection';
3
3
  export default class ConnectionFactory {
4
- static async createConnection() {
5
- const client = await PoolConnection.getClient();
4
+ static async createConnection(tenantName) {
5
+ const client = await PoolConnection.getClient(tenantName);
6
6
  return new Connection(client);
7
7
  }
8
8
  }
@@ -1,11 +1,12 @@
1
1
  import { PoolClient } from 'pg';
2
2
  export default class PoolConnection {
3
- private static pool;
4
- private static poolPromise;
3
+ private static poolMap;
4
+ private static poolPromiseMap;
5
5
  private static readonly configName;
6
6
  private static readonly configField;
7
7
  private static readonly configPath;
8
- static getClient(): Promise<PoolClient>;
8
+ private static readonly defaultName;
9
+ static getClient(tenantName?: string): Promise<PoolClient>;
9
10
  private static getPool;
10
11
  private static ensurePool;
11
12
  private static init;
@@ -1,36 +1,42 @@
1
1
  import { readFile } from 'fs/promises';
2
2
  import Database from './Database';
3
3
  export default class PoolConnection {
4
- static pool;
5
- static poolPromise;
4
+ static poolMap = new Map();
5
+ static poolPromiseMap = new Map();
6
6
  static configName = 'config.json';
7
7
  static configField = 'database';
8
8
  static configPath = './server/';
9
- static async getClient() {
10
- const pool = await this.getPool();
9
+ static defaultName = 'default';
10
+ static async getClient(tenantName = this.defaultName) {
11
+ const pool = await this.getPool(tenantName);
11
12
  return pool.connect();
12
13
  }
13
- static async getPool() {
14
- if (this.pool) {
15
- return this.pool;
14
+ static async getPool(tenantName) {
15
+ if (this.poolMap.has(tenantName)) {
16
+ return this.poolMap.get(tenantName);
16
17
  }
17
- return this.pool = await this.ensurePool();
18
+ const pool = await this.ensurePool(tenantName);
19
+ this.poolMap.set(tenantName, pool);
20
+ return pool;
18
21
  }
19
- static async ensurePool() {
20
- if (!this.poolPromise) {
21
- this.poolPromise = this.init();
22
+ static async ensurePool(tenantName) {
23
+ if (!this.poolPromiseMap.has(tenantName)) {
24
+ this.poolPromiseMap.set(tenantName, this.init(tenantName));
22
25
  }
23
- return this.poolPromise;
26
+ return this.poolPromiseMap.get(tenantName);
24
27
  }
25
- static async init() {
26
- const config = await this.getDatabaseConfig();
28
+ static async init(tenantName) {
29
+ const config = await this.getDatabaseConfig(tenantName);
27
30
  return Database.connect(config);
28
31
  }
29
- static async getDatabaseConfig() {
32
+ static async getDatabaseConfig(tenantName) {
30
33
  let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
31
34
  if (this.configField) {
32
35
  config = config[this.configField];
33
36
  }
37
+ if (tenantName != this.defaultName) {
38
+ config = config[tenantName];
39
+ }
34
40
  return config;
35
41
  }
36
42
  }
@@ -24,7 +24,7 @@ export default class QueryParams {
24
24
  let sqlParams = [];
25
25
  parameters.forEach((parameter) => {
26
26
  if ((Array.isArray(parameter.value) && parameter.value.length > 0) ||
27
- (!Array.isArray(parameter.value) && parameter.value))
27
+ (!Array.isArray(parameter.value) && (parameter.value || parameter.value === false)))
28
28
  sqlParams.push(QueryParamsCreator.createParam(parameter));
29
29
  });
30
30
  return sqlParams;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dascompany/database",
3
- "version": "4.2.7",
3
+ "version": "5.0.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
package/dist/Pool.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import pg from 'pg';
2
- export declare function getPool(): Promise<pg.Pool>;
package/dist/Pool.js DELETED
@@ -1,17 +0,0 @@
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
- }