pg-env 1.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.
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
4
+ Copyright (c) 2025 Hyperweb <developers@hyperweb.io>
5
+ Copyright (c) 2020-present, Interweb, Inc.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # pg-env
2
+
3
+ <p align="center" width="100%">
4
+ <img height="250" src="https://raw.githubusercontent.com/launchql/launchql/refs/heads/main/assets/outline-logo.svg" />
5
+ </p>
6
+
7
+ PostgreSQL environment configuration utilities for managing database connection settings.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install pg-env
13
+ ```
14
+
15
+ ## Features
16
+
17
+ - Parse PostgreSQL environment variables (PGHOST, PGPORT, etc.)
18
+ - Convert configuration objects to environment variables
19
+ - Merge environment variables with configuration overrides
20
+ - Create spawn environments with PostgreSQL settings
21
+
22
+ ## Usage
23
+
24
+ ### Basic Configuration
25
+
26
+ ```typescript
27
+ import { PgConfig, getPgEnvOptions } from 'pg-env';
28
+
29
+ // Get PostgreSQL config from environment with defaults
30
+ const config = getPgEnvOptions();
31
+ console.log(config);
32
+ // { host: 'localhost', port: 5432, user: 'postgres', ... }
33
+
34
+ // Override specific values
35
+ const customConfig = getPgEnvOptions({
36
+ database: 'myapp',
37
+ port: 5433
38
+ });
39
+ ```
40
+
41
+ ### Environment Variables
42
+
43
+ ```typescript
44
+ import { getPgEnvVars, toPgEnvVars } from 'pg-env';
45
+
46
+ // Read current PostgreSQL environment variables
47
+ const envVars = getPgEnvVars();
48
+ // Returns partial PgConfig from PGHOST, PGPORT, etc.
49
+
50
+ // Convert config to environment variables
51
+ const config: PgConfig = {
52
+ host: 'db.example.com',
53
+ port: 5432,
54
+ user: 'appuser',
55
+ password: 'secret',
56
+ database: 'myapp'
57
+ };
58
+
59
+ const envVars = toPgEnvVars(config);
60
+ // { PGHOST: 'db.example.com', PGPORT: '5432', ... }
61
+ ```
62
+
63
+ ### Spawning Processes
64
+
65
+ ```typescript
66
+ import { getSpawnEnvWithPg } from 'pg-env';
67
+ import { spawn } from 'child_process';
68
+
69
+ // Create environment for spawning processes
70
+ const env = getSpawnEnvWithPg({
71
+ database: 'testdb',
72
+ user: 'testuser'
73
+ });
74
+
75
+ // Use with child processes
76
+ const child = spawn('psql', [], { env });
77
+ ```
78
+
79
+ ## API
80
+
81
+ ### Types
82
+
83
+ #### `PgConfig`
84
+
85
+ ```typescript
86
+ interface PgConfig {
87
+ host: string;
88
+ port: number;
89
+ user: string;
90
+ password: string;
91
+ database: string;
92
+ }
93
+ ```
94
+
95
+ ### Functions
96
+
97
+ - `getPgEnvOptions(overrides?: Partial<PgConfig>): PgConfig` - Get config from environment with overrides
98
+ - `getPgEnvVars(): Partial<PgConfig>` - Parse PostgreSQL environment variables
99
+ - `toPgEnvVars(config: Partial<PgConfig>): Record<string, string>` - Convert config to env vars
100
+ - `getSpawnEnvWithPg(config: Partial<PgConfig>, baseEnv?: NodeJS.ProcessEnv): NodeJS.ProcessEnv` - Create spawn environment
101
+
102
+ ### Constants
103
+
104
+ - `defaultPgConfig: PgConfig` - Default PostgreSQL configuration
105
+
106
+ ## Environment Variables
107
+
108
+ The package reads the following environment variables:
109
+
110
+ - `PGHOST` - Database host
111
+ - `PGPORT` - Database port (parsed as number)
112
+ - `PGUSER` - Database user
113
+ - `PGPASSWORD` - Database password
114
+ - `PGDATABASE` - Database name
package/dist/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # pg-env
2
+
3
+ <p align="center" width="100%">
4
+ <img height="250" src="https://raw.githubusercontent.com/launchql/launchql/refs/heads/main/assets/outline-logo.svg" />
5
+ </p>
6
+
7
+ PostgreSQL environment configuration utilities for managing database connection settings.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install pg-env
13
+ ```
14
+
15
+ ## Features
16
+
17
+ - Parse PostgreSQL environment variables (PGHOST, PGPORT, etc.)
18
+ - Convert configuration objects to environment variables
19
+ - Merge environment variables with configuration overrides
20
+ - Create spawn environments with PostgreSQL settings
21
+
22
+ ## Usage
23
+
24
+ ### Basic Configuration
25
+
26
+ ```typescript
27
+ import { PgConfig, getPgEnvOptions } from 'pg-env';
28
+
29
+ // Get PostgreSQL config from environment with defaults
30
+ const config = getPgEnvOptions();
31
+ console.log(config);
32
+ // { host: 'localhost', port: 5432, user: 'postgres', ... }
33
+
34
+ // Override specific values
35
+ const customConfig = getPgEnvOptions({
36
+ database: 'myapp',
37
+ port: 5433
38
+ });
39
+ ```
40
+
41
+ ### Environment Variables
42
+
43
+ ```typescript
44
+ import { getPgEnvVars, toPgEnvVars } from 'pg-env';
45
+
46
+ // Read current PostgreSQL environment variables
47
+ const envVars = getPgEnvVars();
48
+ // Returns partial PgConfig from PGHOST, PGPORT, etc.
49
+
50
+ // Convert config to environment variables
51
+ const config: PgConfig = {
52
+ host: 'db.example.com',
53
+ port: 5432,
54
+ user: 'appuser',
55
+ password: 'secret',
56
+ database: 'myapp'
57
+ };
58
+
59
+ const envVars = toPgEnvVars(config);
60
+ // { PGHOST: 'db.example.com', PGPORT: '5432', ... }
61
+ ```
62
+
63
+ ### Spawning Processes
64
+
65
+ ```typescript
66
+ import { getSpawnEnvWithPg } from 'pg-env';
67
+ import { spawn } from 'child_process';
68
+
69
+ // Create environment for spawning processes
70
+ const env = getSpawnEnvWithPg({
71
+ database: 'testdb',
72
+ user: 'testuser'
73
+ });
74
+
75
+ // Use with child processes
76
+ const child = spawn('psql', [], { env });
77
+ ```
78
+
79
+ ## API
80
+
81
+ ### Types
82
+
83
+ #### `PgConfig`
84
+
85
+ ```typescript
86
+ interface PgConfig {
87
+ host: string;
88
+ port: number;
89
+ user: string;
90
+ password: string;
91
+ database: string;
92
+ }
93
+ ```
94
+
95
+ ### Functions
96
+
97
+ - `getPgEnvOptions(overrides?: Partial<PgConfig>): PgConfig` - Get config from environment with overrides
98
+ - `getPgEnvVars(): Partial<PgConfig>` - Parse PostgreSQL environment variables
99
+ - `toPgEnvVars(config: Partial<PgConfig>): Record<string, string>` - Convert config to env vars
100
+ - `getSpawnEnvWithPg(config: Partial<PgConfig>, baseEnv?: NodeJS.ProcessEnv): NodeJS.ProcessEnv` - Create spawn environment
101
+
102
+ ### Constants
103
+
104
+ - `defaultPgConfig: PgConfig` - Default PostgreSQL configuration
105
+
106
+ ## Environment Variables
107
+
108
+ The package reads the following environment variables:
109
+
110
+ - `PGHOST` - Database host
111
+ - `PGPORT` - Database port (parsed as number)
112
+ - `PGUSER` - Database user
113
+ - `PGPASSWORD` - Database password
114
+ - `PGDATABASE` - Database name
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "pg-env",
3
+ "version": "1.0.0",
4
+ "author": "Dan Lynch <pyramation@gmail.com>",
5
+ "description": "PostgreSQL environment configuration utilities",
6
+ "main": "index.js",
7
+ "module": "esm/index.js",
8
+ "types": "index.d.ts",
9
+ "homepage": "https://github.com/launchql/launchql",
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/launchql/launchql"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/launchql/launchql/issues"
21
+ },
22
+ "scripts": {
23
+ "copy": "copyfiles -f ../../LICENSE README.md package.json dist",
24
+ "clean": "rimraf dist/**",
25
+ "prepare": "npm run build",
26
+ "build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy",
27
+ "build:dev": "npm run clean; tsc --declarationMap; tsc -p tsconfig.esm.json; npm run copy",
28
+ "lint": "eslint . --fix",
29
+ "test": "jest",
30
+ "test:watch": "jest --watch"
31
+ },
32
+ "keywords": [
33
+ "postgres",
34
+ "postgresql",
35
+ "environment",
36
+ "config",
37
+ "launchql"
38
+ ]
39
+ }
package/env.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { PgConfig } from './pg-config';
2
+ export declare const getPgEnvVars: () => Partial<PgConfig>;
3
+ export declare const getPgEnvOptions: (overrides?: Partial<PgConfig>) => PgConfig;
4
+ export declare function toPgEnvVars(config: Partial<PgConfig>): Record<string, string>;
5
+ export declare function getSpawnEnvWithPg(config: Partial<PgConfig>, baseEnv?: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
package/env.js ADDED
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPgEnvOptions = exports.getPgEnvVars = void 0;
4
+ exports.toPgEnvVars = toPgEnvVars;
5
+ exports.getSpawnEnvWithPg = getSpawnEnvWithPg;
6
+ const pg_config_1 = require("./pg-config");
7
+ const parseEnvNumber = (val) => {
8
+ const num = Number(val);
9
+ return !isNaN(num) ? num : undefined;
10
+ };
11
+ const getPgEnvVars = () => {
12
+ const { PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE } = process.env;
13
+ return {
14
+ ...(PGHOST && { host: PGHOST }),
15
+ ...(PGPORT && { port: parseEnvNumber(PGPORT) }),
16
+ ...(PGUSER && { user: PGUSER }),
17
+ ...(PGPASSWORD && { password: PGPASSWORD }),
18
+ ...(PGDATABASE && { database: PGDATABASE }),
19
+ };
20
+ };
21
+ exports.getPgEnvVars = getPgEnvVars;
22
+ const getPgEnvOptions = (overrides = {}) => {
23
+ const envOpts = (0, exports.getPgEnvVars)();
24
+ const merged = { ...pg_config_1.defaultPgConfig, ...envOpts, ...overrides };
25
+ return merged;
26
+ };
27
+ exports.getPgEnvOptions = getPgEnvOptions;
28
+ function toPgEnvVars(config) {
29
+ const opts = { ...pg_config_1.defaultPgConfig, ...config };
30
+ return {
31
+ ...(opts.host && { PGHOST: opts.host }),
32
+ ...(opts.port && { PGPORT: String(opts.port) }),
33
+ ...(opts.user && { PGUSER: opts.user }),
34
+ ...(opts.password && { PGPASSWORD: opts.password }),
35
+ ...(opts.database && { PGDATABASE: opts.database }),
36
+ };
37
+ }
38
+ function getSpawnEnvWithPg(config, baseEnv = process.env) {
39
+ return {
40
+ ...baseEnv,
41
+ ...toPgEnvVars(config)
42
+ };
43
+ }
package/esm/env.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { PgConfig } from './pg-config';
2
+ export declare const getPgEnvVars: () => Partial<PgConfig>;
3
+ export declare const getPgEnvOptions: (overrides?: Partial<PgConfig>) => PgConfig;
4
+ export declare function toPgEnvVars(config: Partial<PgConfig>): Record<string, string>;
5
+ export declare function getSpawnEnvWithPg(config: Partial<PgConfig>, baseEnv?: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
package/esm/env.js ADDED
@@ -0,0 +1,36 @@
1
+ import { defaultPgConfig } from './pg-config';
2
+ const parseEnvNumber = (val) => {
3
+ const num = Number(val);
4
+ return !isNaN(num) ? num : undefined;
5
+ };
6
+ export const getPgEnvVars = () => {
7
+ const { PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE } = process.env;
8
+ return {
9
+ ...(PGHOST && { host: PGHOST }),
10
+ ...(PGPORT && { port: parseEnvNumber(PGPORT) }),
11
+ ...(PGUSER && { user: PGUSER }),
12
+ ...(PGPASSWORD && { password: PGPASSWORD }),
13
+ ...(PGDATABASE && { database: PGDATABASE }),
14
+ };
15
+ };
16
+ export const getPgEnvOptions = (overrides = {}) => {
17
+ const envOpts = getPgEnvVars();
18
+ const merged = { ...defaultPgConfig, ...envOpts, ...overrides };
19
+ return merged;
20
+ };
21
+ export function toPgEnvVars(config) {
22
+ const opts = { ...defaultPgConfig, ...config };
23
+ return {
24
+ ...(opts.host && { PGHOST: opts.host }),
25
+ ...(opts.port && { PGPORT: String(opts.port) }),
26
+ ...(opts.user && { PGUSER: opts.user }),
27
+ ...(opts.password && { PGPASSWORD: opts.password }),
28
+ ...(opts.database && { PGDATABASE: opts.database }),
29
+ };
30
+ }
31
+ export function getSpawnEnvWithPg(config, baseEnv = process.env) {
32
+ return {
33
+ ...baseEnv,
34
+ ...toPgEnvVars(config)
35
+ };
36
+ }
package/esm/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { PgConfig, defaultPgConfig } from './pg-config';
2
+ export { getPgEnvVars, getPgEnvOptions, toPgEnvVars, getSpawnEnvWithPg } from './env';
package/esm/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { defaultPgConfig } from './pg-config';
2
+ export { getPgEnvVars, getPgEnvOptions, toPgEnvVars, getSpawnEnvWithPg } from './env';
@@ -0,0 +1,8 @@
1
+ export interface PgConfig {
2
+ host: string;
3
+ port: number;
4
+ user: string;
5
+ password: string;
6
+ database: string;
7
+ }
8
+ export declare const defaultPgConfig: PgConfig;
@@ -0,0 +1,7 @@
1
+ export const defaultPgConfig = {
2
+ host: 'localhost',
3
+ port: 5432,
4
+ user: 'postgres',
5
+ password: 'password',
6
+ database: 'postgres'
7
+ };
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { PgConfig, defaultPgConfig } from './pg-config';
2
+ export { getPgEnvVars, getPgEnvOptions, toPgEnvVars, getSpawnEnvWithPg } from './env';
package/index.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSpawnEnvWithPg = exports.toPgEnvVars = exports.getPgEnvOptions = exports.getPgEnvVars = exports.defaultPgConfig = void 0;
4
+ var pg_config_1 = require("./pg-config");
5
+ Object.defineProperty(exports, "defaultPgConfig", { enumerable: true, get: function () { return pg_config_1.defaultPgConfig; } });
6
+ var env_1 = require("./env");
7
+ Object.defineProperty(exports, "getPgEnvVars", { enumerable: true, get: function () { return env_1.getPgEnvVars; } });
8
+ Object.defineProperty(exports, "getPgEnvOptions", { enumerable: true, get: function () { return env_1.getPgEnvOptions; } });
9
+ Object.defineProperty(exports, "toPgEnvVars", { enumerable: true, get: function () { return env_1.toPgEnvVars; } });
10
+ Object.defineProperty(exports, "getSpawnEnvWithPg", { enumerable: true, get: function () { return env_1.getSpawnEnvWithPg; } });
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "pg-env",
3
+ "version": "1.0.0",
4
+ "author": "Dan Lynch <pyramation@gmail.com>",
5
+ "description": "PostgreSQL environment configuration utilities",
6
+ "main": "index.js",
7
+ "module": "esm/index.js",
8
+ "types": "index.d.ts",
9
+ "homepage": "https://github.com/launchql/launchql",
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/launchql/launchql"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/launchql/launchql/issues"
21
+ },
22
+ "scripts": {
23
+ "copy": "copyfiles -f ../../LICENSE README.md package.json dist",
24
+ "clean": "rimraf dist/**",
25
+ "prepare": "npm run build",
26
+ "build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy",
27
+ "build:dev": "npm run clean; tsc --declarationMap; tsc -p tsconfig.esm.json; npm run copy",
28
+ "lint": "eslint . --fix",
29
+ "test": "jest",
30
+ "test:watch": "jest --watch"
31
+ },
32
+ "keywords": [
33
+ "postgres",
34
+ "postgresql",
35
+ "environment",
36
+ "config",
37
+ "launchql"
38
+ ]
39
+ }
package/pg-config.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export interface PgConfig {
2
+ host: string;
3
+ port: number;
4
+ user: string;
5
+ password: string;
6
+ database: string;
7
+ }
8
+ export declare const defaultPgConfig: PgConfig;
package/pg-config.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultPgConfig = void 0;
4
+ exports.defaultPgConfig = {
5
+ host: 'localhost',
6
+ port: 5432,
7
+ user: 'postgres',
8
+ password: 'password',
9
+ database: 'postgres'
10
+ };