@saws/postgres-client 2.0.0-beta.3

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/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@saws/postgres-client",
3
+ "version": "2.0.0-beta.3",
4
+ "description": "",
5
+ "license": "ISC",
6
+ "author": "",
7
+ "type": "module",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "dependencies": {
16
+ "@types/pg": "^8.15.5",
17
+ "pg": "^8.16.3"
18
+ }
19
+ }
@@ -0,0 +1,57 @@
1
+ import { Pool, type PoolConfig } from "pg";
2
+
3
+ export interface PostgresClientOptions extends PoolConfig {
4
+ /** Explicit environment source for application adapters. */
5
+ environment?: Record<string, string | undefined>;
6
+ }
7
+
8
+ export class PostgresClient extends Pool {
9
+ constructor(serviceName: string, options: PostgresClientOptions = {}) {
10
+ const { environment, ...poolOptions } = options;
11
+
12
+ super({
13
+ connectionString: resolvePostgresServiceUrl(serviceName, environment),
14
+ ...poolOptions,
15
+ });
16
+ }
17
+ }
18
+
19
+ export function resolvePostgresServiceUrl(
20
+ serviceName: string,
21
+ environment?: Record<string, string | undefined>,
22
+ ) {
23
+ const variableName = postgresServiceUrlEnvironmentVariable(serviceName);
24
+ const value =
25
+ environment?.[variableName] ??
26
+ getRuntimeEnvironment()?.[variableName] ??
27
+ getProcessEnvironment()?.[variableName];
28
+
29
+ if (value == null || value.trim().length === 0) {
30
+ throw new Error(
31
+ `Postgres service "${serviceName}" is not configured: ` +
32
+ `${variableName} must be present in the SAWS environment`,
33
+ );
34
+ }
35
+
36
+ return value;
37
+ }
38
+
39
+ export function postgresServiceUrlEnvironmentVariable(serviceName: string) {
40
+ return `${serviceName.replace(/[^a-zA-Z\d]/g, "_").toUpperCase()}_DATABASE_URL`;
41
+ }
42
+
43
+ function getRuntimeEnvironment() {
44
+ return (
45
+ globalThis as typeof globalThis & {
46
+ ENV?: Record<string, string | undefined>;
47
+ }
48
+ ).ENV;
49
+ }
50
+
51
+ function getProcessEnvironment() {
52
+ return (
53
+ globalThis as typeof globalThis & {
54
+ process?: { env?: Record<string, string | undefined> };
55
+ }
56
+ ).process?.env;
57
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./PostgresClient.js";
2
+ export * from "pg";
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig-node.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src",
6
+ "tsBuildInfoFile": "./dist/.tsbuildinfo"
7
+ }
8
+ }