dt-common-device 1.0.15 → 1.0.17

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.
@@ -7,7 +7,11 @@ export declare function ensureAuditInitialized(): void;
7
7
  * Returns the PostgreSQL DB URI from environment variables.
8
8
  * Throws an error if not set.
9
9
  */
10
- export declare function getPostgresDbUri(): string;
10
+ export declare function getPostgresDbUri(): {
11
+ baseUri: string;
12
+ dbName: string;
13
+ params: string;
14
+ };
11
15
  /**
12
16
  * Returns the Redis DB Host and port from environment variables.
13
17
  * Throws an error if not set.
@@ -62,11 +62,15 @@ function ensureAuditInitialized() {
62
62
  * Throws an error if not set.
63
63
  */
64
64
  function getPostgresDbUri() {
65
- const uri = process.env.DATABASE_URL;
66
- if (!uri) {
65
+ const fullUri = process.env.DATABASE_URL;
66
+ if (!fullUri) {
67
67
  throw new Error("dt-common-device: POSTGRESQL_DB_URI must be set in environment variables or .env file");
68
68
  }
69
- return uri;
69
+ const url = new URL(fullUri);
70
+ const baseUri = `${url.protocol}//${url.username}:${url.password}@${url.hostname}${url.port ? `:${url.port}` : ""}/`;
71
+ const dbName = url.pathname.replace(/^\//, "").split("?")[0];
72
+ const params = url.search || "";
73
+ return { baseUri, dbName, params };
70
74
  }
71
75
  /**
72
76
  * Returns the Redis DB Host and port from environment variables.
package/dist/db/db.js CHANGED
@@ -3,12 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getPostgresClient = getPostgresClient;
4
4
  const pg_1 = require("pg");
5
5
  const config_1 = require("../config/config");
6
- const dbUri = (0, config_1.getPostgresDbUri)();
6
+ const { baseUri, dbName, params } = (0, config_1.getPostgresDbUri)();
7
7
  let pool = null;
8
8
  function getPostgresClient() {
9
9
  if (!pool) {
10
10
  pool = new pg_1.Pool({
11
- connectionString: dbUri,
11
+ connectionString: `${baseUri}${dbName}${params}`,
12
12
  });
13
13
  }
14
14
  return pool;
@@ -39,13 +39,13 @@ export interface IConnectionConnectParams {
39
39
  propertyId?: string;
40
40
  [key: string]: unknown;
41
41
  }
42
- export interface ConnectionProvider {
43
- DEVICETHREAD: "Devicethread";
44
- SMARTTHINGS: "Smartthings";
45
- SALTOKS: "SaltoKS";
46
- TUYA: "Tuya";
47
- TTLOCK: "TTLock";
48
- SCHLAGE: "Schlage";
49
- YALEWIFI: "YaleWifi";
50
- SENSIBO: "Sensibo";
42
+ export declare enum ConnectionProvider {
43
+ Smartthings = "Smartthings",
44
+ SaltoKS = "SaltoKS",
45
+ TTLock = "TTLock",
46
+ Tuya = "Tuya",
47
+ Schlage = "Schlage",
48
+ YaleWifi = "YaleWifi",
49
+ Sensibo = "Sensibo",
50
+ Devicethread = "Devicethread"
51
51
  }
@@ -1,3 +1,15 @@
1
1
  "use strict";
2
2
  // Device Cloud Type Interfaces for DeviceThread Common Library
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.ConnectionProvider = void 0;
5
+ var ConnectionProvider;
6
+ (function (ConnectionProvider) {
7
+ ConnectionProvider["Smartthings"] = "Smartthings";
8
+ ConnectionProvider["SaltoKS"] = "SaltoKS";
9
+ ConnectionProvider["TTLock"] = "TTLock";
10
+ ConnectionProvider["Tuya"] = "Tuya";
11
+ ConnectionProvider["Schlage"] = "Schlage";
12
+ ConnectionProvider["YaleWifi"] = "YaleWifi";
13
+ ConnectionProvider["Sensibo"] = "Sensibo";
14
+ ConnectionProvider["Devicethread"] = "Devicethread";
15
+ })(ConnectionProvider || (exports.ConnectionProvider = ConnectionProvider = {}));
@@ -1,5 +1,6 @@
1
+ import { IConnection } from "../interfaces/IConnection";
1
2
  export declare class LocalConnectionService {
2
3
  private readonly pool;
3
4
  constructor();
4
- getConnection(connectionId: string): Promise<any>;
5
+ getConnection(connectionId: string): Promise<IConnection>;
5
6
  }
@@ -153,7 +153,7 @@ class LocalDeviceService {
153
153
  async getDevicesByAccessGroup(accessGroupId) {
154
154
  try {
155
155
  // Fetch from Postgres if not in cache
156
- const result = await this.postgres.query("SELECT * FROM access_groups WHERE id = $1", [accessGroupId]);
156
+ const result = await this.postgres.query("SELECT * FROM dt_group WHERE id = $1", [accessGroupId]);
157
157
  if (result.rows.length > 0) {
158
158
  return result.rows[0];
159
159
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dt-common-device",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -68,14 +68,24 @@ export function ensureAuditInitialized() {
68
68
  * Returns the PostgreSQL DB URI from environment variables.
69
69
  * Throws an error if not set.
70
70
  */
71
- export function getPostgresDbUri(): string {
72
- const uri = process.env.DATABASE_URL;
73
- if (!uri) {
71
+ export function getPostgresDbUri(): {
72
+ baseUri: string;
73
+ dbName: string;
74
+ params: string;
75
+ } {
76
+ const fullUri = process.env.DATABASE_URL;
77
+ if (!fullUri) {
74
78
  throw new Error(
75
79
  "dt-common-device: POSTGRESQL_DB_URI must be set in environment variables or .env file"
76
80
  );
77
81
  }
78
- return uri;
82
+ const url = new URL(fullUri);
83
+ const baseUri = `${url.protocol}//${url.username}:${url.password}@${
84
+ url.hostname
85
+ }${url.port ? `:${url.port}` : ""}/`;
86
+ const dbName = url.pathname.replace(/^\//, "").split("?")[0];
87
+ const params = url.search || "";
88
+ return { baseUri, dbName, params };
79
89
  }
80
90
 
81
91
  /**
package/src/db/db.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import { Pool } from "pg";
2
2
  import { getPostgresDbUri } from "../config/config";
3
3
 
4
- const dbUri = getPostgresDbUri();
4
+ const { baseUri, dbName, params } = getPostgresDbUri();
5
5
 
6
6
  let pool: Pool | null = null;
7
7
 
8
8
  export function getPostgresClient() {
9
9
  if (!pool) {
10
10
  pool = new Pool({
11
- connectionString: dbUri,
11
+ connectionString: `${baseUri}${dbName}${params}`,
12
12
  });
13
13
  }
14
14
  return pool;
@@ -45,13 +45,13 @@ export interface IConnectionConnectParams {
45
45
  [key: string]: unknown;
46
46
  }
47
47
 
48
- export interface ConnectionProvider {
49
- DEVICETHREAD: "Devicethread";
50
- SMARTTHINGS: "Smartthings";
51
- SALTOKS: "SaltoKS";
52
- TUYA: "Tuya";
53
- TTLOCK: "TTLock";
54
- SCHLAGE: "Schlage";
55
- YALEWIFI: "YaleWifi";
56
- SENSIBO: "Sensibo";
57
- }
48
+ export enum ConnectionProvider {
49
+ Smartthings = "Smartthings",
50
+ SaltoKS = "SaltoKS",
51
+ TTLock = "TTLock",
52
+ Tuya = "Tuya",
53
+ Schlage = "Schlage",
54
+ YaleWifi = "YaleWifi",
55
+ Sensibo = "Sensibo",
56
+ Devicethread = "Devicethread",
57
+ }
@@ -1,11 +1,12 @@
1
1
  import { getPostgresClient } from "../../../db";
2
+ import { IConnection } from "../interfaces/IConnection";
2
3
  export class LocalConnectionService {
3
4
  private readonly pool;
4
5
  constructor() {
5
6
  this.pool = getPostgresClient();
6
7
  }
7
8
 
8
- async getConnection(connectionId: string) {
9
+ async getConnection(connectionId: string): Promise<IConnection> {
9
10
  const result = await this.pool.query(
10
11
  "SELECT * FROM dt_connections WHERE id = $1",
11
12
  [connectionId]
@@ -203,7 +203,7 @@ export class LocalDeviceService {
203
203
  try {
204
204
  // Fetch from Postgres if not in cache
205
205
  const result = await this.postgres.query(
206
- "SELECT * FROM access_groups WHERE id = $1",
206
+ "SELECT * FROM dt_group WHERE id = $1",
207
207
  [accessGroupId]
208
208
  );
209
209
  if (result.rows.length > 0) {