joist-utils 1.0.688 → 1.0.692
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/build/connection.d.ts +18 -12
- package/build/connection.js +33 -39
- package/build/connection.js.map +1 -1
- package/build/connection.test.js +10 -4
- package/build/connection.test.js.map +1 -1
- package/package.json +1 -1
package/build/connection.d.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import { ConnectionConfig } from "pg";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
declare type DatabaseUrl = {
|
|
3
|
+
DATABASE_URL: string;
|
|
4
|
+
};
|
|
5
|
+
declare type DbSettings = {
|
|
6
|
+
DB_USER: string;
|
|
7
|
+
DB_PASSWORD: string;
|
|
8
|
+
DB_HOST: string;
|
|
9
|
+
DB_DATABASE: string;
|
|
10
|
+
DB_PORT: string;
|
|
11
|
+
};
|
|
12
|
+
export declare type ConnectionEnv = DatabaseUrl | DbSettings;
|
|
9
13
|
/**
|
|
10
14
|
* Returns the `ConnectionConfig` that joist will use to connect to pg.
|
|
11
15
|
*
|
|
12
|
-
* This
|
|
16
|
+
* This reads environment variables, and can be either:
|
|
13
17
|
*
|
|
18
|
+
* - A single `DATABASE_URL` variable
|
|
19
|
+
* - Multiple `DB_USER`, `DB_PASSWORD`, `DB_DATABASE`, `DB_HOST`, `DB_PORT` variables
|
|
14
20
|
* The value can be either:
|
|
15
21
|
*
|
|
16
|
-
*
|
|
17
|
-
* -
|
|
22
|
+
* Note that users using a library for typed / validated environment variables, i.e.
|
|
23
|
+
* ts-app-env, you can pass in a specific `env` variable.
|
|
18
24
|
*/
|
|
19
|
-
export declare function newPgConnectionConfig(): ConnectionConfig;
|
|
20
|
-
export
|
|
25
|
+
export declare function newPgConnectionConfig(env?: ConnectionEnv): ConnectionConfig;
|
|
26
|
+
export {};
|
package/build/connection.js
CHANGED
|
@@ -1,52 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.newPgConnectionConfig = void 0;
|
|
4
4
|
const pg_connection_string_1 = require("pg-connection-string");
|
|
5
|
-
const index_1 = require("./index");
|
|
6
|
-
function readEnvVariable() {
|
|
7
|
-
return process.env.DATABASE_CONNECTION_INFO || (0, index_1.fail)("DATABASE_CONNECTION_INFO environment variable is not set");
|
|
8
|
-
}
|
|
9
|
-
/** Reads the RDS-style connection information from `process.env`. */
|
|
10
|
-
function parseAsRdsConnectionInfo(envVariable) {
|
|
11
|
-
if (envVariable.startsWith("{")) {
|
|
12
|
-
const { dbname: database, username: user, password, host, port } = JSON.parse(envVariable);
|
|
13
|
-
return { database, user, password, host, port };
|
|
14
|
-
}
|
|
15
|
-
return undefined;
|
|
16
|
-
}
|
|
17
5
|
/**
|
|
18
6
|
* Returns the `ConnectionConfig` that joist will use to connect to pg.
|
|
19
7
|
*
|
|
20
|
-
* This
|
|
8
|
+
* This reads environment variables, and can be either:
|
|
21
9
|
*
|
|
10
|
+
* - A single `DATABASE_URL` variable
|
|
11
|
+
* - Multiple `DB_USER`, `DB_PASSWORD`, `DB_DATABASE`, `DB_HOST`, `DB_PORT` variables
|
|
22
12
|
* The value can be either:
|
|
23
13
|
*
|
|
24
|
-
*
|
|
25
|
-
* -
|
|
14
|
+
* Note that users using a library for typed / validated environment variables, i.e.
|
|
15
|
+
* ts-app-env, you can pass in a specific `env` variable.
|
|
26
16
|
*/
|
|
27
|
-
function newPgConnectionConfig() {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
17
|
+
function newPgConnectionConfig(env) {
|
|
18
|
+
if (process.env.DATABASE_URL || (env && "DATABASE_URL" in env)) {
|
|
19
|
+
const url = process.env.DATABASE_URL ?? env.DATABASE_URL;
|
|
20
|
+
// It'd be great if `parse` returned ConnectionConfig directly
|
|
21
|
+
const options = (0, pg_connection_string_1.parse)(url);
|
|
22
|
+
const { database, port, host, user, password } = options;
|
|
23
|
+
return {
|
|
24
|
+
user,
|
|
25
|
+
password,
|
|
26
|
+
database: database ?? undefined,
|
|
27
|
+
host: host ?? undefined,
|
|
28
|
+
port: port ? Number(port) : undefined,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
else if (process.env.DB_DATABASE || (env && "DB_DATABASE" in env)) {
|
|
32
|
+
const e = process.env.DB_DATABASE ? process.env : env;
|
|
33
|
+
return {
|
|
34
|
+
user: e.DB_USER,
|
|
35
|
+
password: e.DB_PASSWORD,
|
|
36
|
+
database: e.DB_DATABASE,
|
|
37
|
+
host: e.DB_HOST,
|
|
38
|
+
port: e.DB_PORT ? Number(e.DB_PORT) : undefined,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
throw new Error("No DATABASE_URL or DB_DATABASE/etc. environment variable found");
|
|
36
43
|
}
|
|
37
|
-
const opts = (0, pg_connection_string_1.parse)(envVariable);
|
|
38
|
-
return {
|
|
39
|
-
...opts,
|
|
40
|
-
// Drop `| null` from the parse return type
|
|
41
|
-
database: opts.database || undefined,
|
|
42
|
-
host: opts.host || undefined,
|
|
43
|
-
port: opts.port !== undefined && opts.port !== null ? Number(opts.port) : undefined,
|
|
44
|
-
ssl: typeof opts.ssl === "boolean"
|
|
45
|
-
? opts.ssl
|
|
46
|
-
: typeof opts.ssl === "string"
|
|
47
|
-
? (0, index_1.fail)("parsing string ssl not implemented")
|
|
48
|
-
: undefined,
|
|
49
|
-
};
|
|
50
44
|
}
|
|
51
|
-
exports.
|
|
45
|
+
exports.newPgConnectionConfig = newPgConnectionConfig;
|
|
52
46
|
//# sourceMappingURL=connection.js.map
|
package/build/connection.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../src/connection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AC,CAAC,SAAS;SAChD,CAAC;KACH;SAAM;QACL,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAC;KACnF;AACH,CAAC;AAzBD,sDAyBC"}
|
package/build/connection.test.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const connection_1 = require("./connection");
|
|
4
4
|
describe("connection", () => {
|
|
5
|
-
it("should parse
|
|
6
|
-
const info = (0, connection_1.
|
|
5
|
+
it("should parse single DATABASE_URL", () => {
|
|
6
|
+
const info = (0, connection_1.newPgConnectionConfig)({ DATABASE_URL: "postgres://joist:local@db:5432/joist" });
|
|
7
7
|
expect(info).toEqual({
|
|
8
8
|
database: "joist",
|
|
9
9
|
host: "db",
|
|
@@ -12,8 +12,14 @@ describe("connection", () => {
|
|
|
12
12
|
user: "joist",
|
|
13
13
|
});
|
|
14
14
|
});
|
|
15
|
-
it("should parse
|
|
16
|
-
const info = (0, connection_1.
|
|
15
|
+
it("should parse multiple DB variables", () => {
|
|
16
|
+
const info = (0, connection_1.newPgConnectionConfig)({
|
|
17
|
+
DB_USER: "joist",
|
|
18
|
+
DB_PASSWORD: "local",
|
|
19
|
+
DB_DATABASE: "joist",
|
|
20
|
+
DB_HOST: "db",
|
|
21
|
+
DB_PORT: "5432",
|
|
22
|
+
});
|
|
17
23
|
expect(info).toEqual({
|
|
18
24
|
database: "joist",
|
|
19
25
|
host: "db",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.test.js","sourceRoot":"","sources":["../src/connection.test.ts"],"names":[],"mappings":";;AAAA,
|
|
1
|
+
{"version":3,"file":"connection.test.js","sourceRoot":"","sources":["../src/connection.test.ts"],"names":[],"mappings":";;AAAA,6CAAqD;AAErD,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE;IAC1B,EAAE,CAAC,kCAAkC,EAAE,GAAG,EAAE;QAC1C,MAAM,IAAI,GAAG,IAAA,kCAAqB,EAAC,EAAE,YAAY,EAAE,sCAAsC,EAAE,CAAC,CAAC;QAC7F,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC;YACnB,QAAQ,EAAE,OAAO;YACjB,IAAI,EAAE,IAAI;YACV,QAAQ,EAAE,OAAO;YACjB,IAAI,EAAE,IAAI;YACV,IAAI,EAAE,OAAO;SACd,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,oCAAoC,EAAE,GAAG,EAAE;QAC5C,MAAM,IAAI,GAAG,IAAA,kCAAqB,EAAC;YACjC,OAAO,EAAE,OAAO;YAChB,WAAW,EAAE,OAAO;YACpB,WAAW,EAAE,OAAO;YACpB,OAAO,EAAE,IAAI;YACb,OAAO,EAAE,MAAM;SAChB,CAAC,CAAC;QACH,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC;YACnB,QAAQ,EAAE,OAAO;YACjB,IAAI,EAAE,IAAI;YACV,QAAQ,EAAE,OAAO;YACjB,IAAI,EAAE,IAAI;YACV,GAAG,EAAE,SAAS;YACd,IAAI,EAAE,OAAO;SACd,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|