@schemavaults/dbh 0.5.1

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.
Files changed (32) hide show
  1. package/.npmrc.github +3 -0
  2. package/.npmrc.npmjs +3 -0
  3. package/README.md +24 -0
  4. package/dist/PostgresDatabaseCredentials.d.ts +22 -0
  5. package/dist/PostgresDatabaseCredentials.js +2 -0
  6. package/dist/PostgresDatabaseCredentials.js.map +1 -0
  7. package/dist/SchemaVaultsAppEnvironment.d.ts +9 -0
  8. package/dist/SchemaVaultsAppEnvironment.js +10 -0
  9. package/dist/SchemaVaultsAppEnvironment.js.map +1 -0
  10. package/dist/index.d.ts +3 -0
  11. package/dist/index.js +3 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/schemavaults-postgres-neon-proxy-adapter.d.ts +17 -0
  14. package/dist/schemavaults-postgres-neon-proxy-adapter.js +74 -0
  15. package/dist/schemavaults-postgres-neon-proxy-adapter.js.map +1 -0
  16. package/dist/utils/getAppEnvironment.d.ts +3 -0
  17. package/dist/utils/getAppEnvironment.js +10 -0
  18. package/dist/utils/getAppEnvironment.js.map +1 -0
  19. package/dist/utils/getPostgresNeonWsProxyUrl.d.ts +8 -0
  20. package/dist/utils/getPostgresNeonWsProxyUrl.js +40 -0
  21. package/dist/utils/getPostgresNeonWsProxyUrl.js.map +1 -0
  22. package/dist/utils/isDbhInDebugMode.d.ts +10 -0
  23. package/dist/utils/isDbhInDebugMode.js +22 -0
  24. package/dist/utils/isDbhInDebugMode.js.map +1 -0
  25. package/dist/utils/maybeStripQuotes.d.ts +2 -0
  26. package/dist/utils/maybeStripQuotes.js +14 -0
  27. package/dist/utils/maybeStripQuotes.js.map +1 -0
  28. package/dist/utils/parseDatabaseCredentialsFromEnv.d.ts +3 -0
  29. package/dist/utils/parseDatabaseCredentialsFromEnv.js +57 -0
  30. package/dist/utils/parseDatabaseCredentialsFromEnv.js.map +1 -0
  31. package/package.json +37 -0
  32. package/tsconfig.json +24 -0
package/.npmrc.github ADDED
@@ -0,0 +1,3 @@
1
+ @schemavaults:registry=https://npm.pkg.github.com
2
+ //https://npm.pkg.github.com/:username=${SCHEMAVAULTS_GITHUB_PACKAGE_REGISTRY_USER}
3
+ //https://npm.pkg.github.com/:_authToken=${SCHEMAVAULTS_GITHUB_PACKAGE_REGISTRY_TOKEN}
package/.npmrc.npmjs ADDED
@@ -0,0 +1,3 @@
1
+ @schemavaults:registry=https://registry.npmjs.org
2
+ //https://registry.npmjs.org/:username=${NPM_PACKAGE_PUBLISH_USER}
3
+ //https://registry.npmjs.org/:_authToken=${NPM_PACKAGE_PUBLISH_TOKEN}
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # @schemavaults/dbh
2
+
3
+ ## About
4
+
5
+ This package makes it easy to connect to a Postgres instance and run queries using the 'Kysely' type-safe query builder-- whether it's a local Postgres container or a serverless [Neon](https://neon.com/)-hosted Postgres container.
6
+
7
+ ## Highlighted Dependencies
8
+
9
+ - [kysely](https://www.kysely.dev/) - TypeScript SQL query builder ([GitHub Repo](https://github.com/kysely-org/kysely))
10
+ - [neon](https://neon.com/) - Serverless Postgres ([@neondatabase/serverless driver GitHub Repo](https://github.com/neondatabase/serverless))
11
+ - [kysely-neon](https://github.com/seveibar/kysely-neon) - Kysely Dialect for using @neondatabase/serverless
12
+
13
+ ## Required Environment Variables
14
+
15
+ Ensure that the following environment variables are defined:
16
+ ```bash
17
+ POSTGRES_USER=""
18
+ POSTGRES_PASSWORD=""
19
+ POSTGRES_URL=""
20
+ POSTGRES_URL_NON_POOLING=""
21
+ POSTGRES_HOST=""
22
+ POSTGRES_PORT="5432"
23
+ POSTGRES_DATABASE=""
24
+ ```
@@ -0,0 +1,22 @@
1
+ declare global {
2
+ namespace NodeJS {
3
+ interface ProcessEnv {
4
+ POSTGRES_USER: string;
5
+ POSTGRES_PASSWORD: string;
6
+ POSTGRES_URL: string;
7
+ POSTGRES_URL_NON_POOLING?: string | undefined;
8
+ POSTGRES_HOST: string;
9
+ POSTGRES_PORT: string;
10
+ POSTGRES_DATABASE: string;
11
+ }
12
+ }
13
+ }
14
+ export interface PostgresDatabaseCredentials {
15
+ POSTGRES_USER: string;
16
+ POSTGRES_PASSWORD: string;
17
+ POSTGRES_URL: string;
18
+ POSTGRES_URL_NON_POOLING?: string;
19
+ POSTGRES_HOST: string;
20
+ POSTGRES_PORT: number;
21
+ POSTGRES_DATABASE: string;
22
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=PostgresDatabaseCredentials.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PostgresDatabaseCredentials.js","sourceRoot":"","sources":["../src/PostgresDatabaseCredentials.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ export type SchemaVaultsAppEnvironment = 'development' | 'test' | 'staging' | 'production';
2
+ declare global {
3
+ namespace NodeJS {
4
+ interface ProcessEnv {
5
+ SCHEMAVAULTS_APP_ENVIRONMENT?: SchemaVaultsAppEnvironment;
6
+ }
7
+ }
8
+ }
9
+ export declare function isValidAppEnvironment(env: unknown): env is SchemaVaultsAppEnvironment;
@@ -0,0 +1,10 @@
1
+ export function isValidAppEnvironment(env) {
2
+ if (typeof env !== "string") {
3
+ return false;
4
+ }
5
+ if (env === "development" || env === "test" || env === "staging" || env === "production") {
6
+ return true;
7
+ }
8
+ return false;
9
+ }
10
+ //# sourceMappingURL=SchemaVaultsAppEnvironment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaVaultsAppEnvironment.js","sourceRoot":"","sources":["../src/SchemaVaultsAppEnvironment.ts"],"names":[],"mappings":"AAUA,MAAM,UAAU,qBAAqB,CAAC,GAAY;IAChD,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,GAAG,KAAK,aAAa,IAAI,GAAG,KAAK,MAAM,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,YAAY,EAAE,CAAC;QACzF,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { SchemaVaultsPostgresNeonProxyAdapter, SchemaVaultsPostgresNeonProxyAdapter as default } from "./schemavaults-postgres-neon-proxy-adapter";
2
+ export { sql } from "kysely";
3
+ export type * from "kysely";
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { SchemaVaultsPostgresNeonProxyAdapter, SchemaVaultsPostgresNeonProxyAdapter as default } from "./schemavaults-postgres-neon-proxy-adapter";
2
+ export { sql } from "kysely";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oCAAoC,EAAE,oCAAoC,IAAI,OAAO,EAAE,MAAM,4CAA4C,CAAC;AACnJ,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { Kysely } from "kysely";
2
+ import type { SchemaVaultsAppEnvironment } from "./SchemaVaultsAppEnvironment";
3
+ import type { PostgresDatabaseCredentials } from "./PostgresDatabaseCredentials";
4
+ export interface ISchemaVaultsPostgresNeonProxyAdapterConstructorOpts {
5
+ environment: SchemaVaultsAppEnvironment;
6
+ credentials?: PostgresDatabaseCredentials;
7
+ }
8
+ export declare class SchemaVaultsPostgresNeonProxyAdapter<KyselyTablesType extends object> {
9
+ private readonly kysely_db;
10
+ private readonly env;
11
+ private readonly debug;
12
+ private static maybeStripQuotes;
13
+ private static getPostgresNeonWsProxyUrl;
14
+ constructor(opts: ISchemaVaultsPostgresNeonProxyAdapterConstructorOpts);
15
+ get db(): Kysely<KyselyTablesType>;
16
+ destroy(): Promise<void>;
17
+ }
@@ -0,0 +1,74 @@
1
+ // This file sets up kysely to connect to postgress
2
+ import { NeonDialect } from "kysely-neon";
3
+ import { Kysely } from "kysely";
4
+ import maybeStripQuotes from "./utils/maybeStripQuotes";
5
+ import getPostgresNeonWsProxyUrl from "./utils/getPostgresNeonWsProxyUrl";
6
+ import isDbhInDebugMode from "./utils/isDbhInDebugMode";
7
+ import parseDatabaseCredentialsFromEnv from "./utils/parseDatabaseCredentialsFromEnv";
8
+ export class SchemaVaultsPostgresNeonProxyAdapter {
9
+ kysely_db;
10
+ env;
11
+ debug;
12
+ static maybeStripQuotes(maybeQuotes) {
13
+ return maybeStripQuotes(maybeQuotes);
14
+ }
15
+ static getPostgresNeonWsProxyUrl(opts) {
16
+ return getPostgresNeonWsProxyUrl(opts);
17
+ } // end of getPostgresNeonWsProxyUrl()
18
+ // Initialized from getInstance() (Singleton pattern)
19
+ constructor(opts) {
20
+ const environment = opts.environment;
21
+ this.env = environment;
22
+ // checks if 'SCHEMAVAULTS_DBH_DEBUG="true"' is set in env vars, or defaults to yes if in dev/test/staging environment
23
+ this.debug = isDbhInDebugMode(this.env);
24
+ const debug = this.debug;
25
+ const credentials = opts.credentials ?? parseDatabaseCredentialsFromEnv(process.env, debug);
26
+ const kysely_neon_dialect_config = {
27
+ connectionString: credentials.POSTGRES_URL,
28
+ host: credentials.POSTGRES_HOST,
29
+ user: credentials.POSTGRES_USER,
30
+ password: credentials.POSTGRES_PASSWORD,
31
+ database: credentials.POSTGRES_DATABASE,
32
+ useSecureWebSocket: (this.env === "production"),
33
+ port: credentials.POSTGRES_PORT,
34
+ wsProxy: (pg_host) => {
35
+ if (debug) {
36
+ console.log(`[SchemaVaultsPostgresNeonProxyAdapter] NeonDialectConfig.wsProxy("${pg_host}")`);
37
+ }
38
+ // Calculate the wsProxy url from host
39
+ return SchemaVaultsPostgresNeonProxyAdapter.getPostgresNeonWsProxyUrl({
40
+ pg_host,
41
+ environment,
42
+ debug,
43
+ });
44
+ },
45
+ };
46
+ if (this.debug) {
47
+ kysely_neon_dialect_config.pipelineTLS = false;
48
+ kysely_neon_dialect_config.pipelineConnect = false;
49
+ }
50
+ if (this.debug) {
51
+ console.log("[SchemaVaultsPostgresNeonProxyAdapter] Kysely Neon DB adapter configuration: ", kysely_neon_dialect_config);
52
+ }
53
+ if (this.debug) {
54
+ console.log("[SchemaVaultsPostgresNeonProxyAdapter] Initializing SchemaVaults x Kysely x Neon database adapter...");
55
+ }
56
+ const dialect = new NeonDialect(kysely_neon_dialect_config);
57
+ const kysely_configuration = {
58
+ dialect,
59
+ log: ["query", "error"],
60
+ };
61
+ this.kysely_db = new Kysely(kysely_configuration);
62
+ if (this.debug) {
63
+ console.log("[SchemaVaultsPostgresNeonProxyAdapter] Initialized SchemaVaults x Kysely x Neon database adapter in environment: ", this.env);
64
+ }
65
+ }
66
+ get db() {
67
+ return this.kysely_db;
68
+ }
69
+ async destroy() {
70
+ await this.kysely_db.destroy();
71
+ return;
72
+ }
73
+ }
74
+ //# sourceMappingURL=schemavaults-postgres-neon-proxy-adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemavaults-postgres-neon-proxy-adapter.js","sourceRoot":"","sources":["../src/schemavaults-postgres-neon-proxy-adapter.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,3 @@
1
+ import { type SchemaVaultsAppEnvironment } from "../SchemaVaultsAppEnvironment";
2
+ export declare function getAppEnvironment(): SchemaVaultsAppEnvironment;
3
+ export default getAppEnvironment;
@@ -0,0 +1,10 @@
1
+ import { isValidAppEnvironment } from "../SchemaVaultsAppEnvironment";
2
+ export function getAppEnvironment() {
3
+ const env = process.env["SCHEMAVAULTS_APP_ENVIRONMENT"];
4
+ if (isValidAppEnvironment(env)) {
5
+ return env;
6
+ }
7
+ return "production"; // default to production if not set or invalid
8
+ }
9
+ export default getAppEnvironment;
10
+ //# sourceMappingURL=getAppEnvironment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getAppEnvironment.js","sourceRoot":"","sources":["../../src/utils/getAppEnvironment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAmC,MAAM,8BAA8B,CAAC;AAEtG,MAAM,UAAU,iBAAiB;IAC/B,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,8BAA8B,CAAC,CAAC;IACxD,IAAI,qBAAqB,CAAC,GAAG,CAAC,EAAE,CAAC;QAC/B,OAAO,GAAG,CAAC;IACb,CAAC;IACD,OAAO,YAAY,CAAC,CAAC,8CAA8C;AACrE,CAAC;AAED,eAAe,iBAAiB,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { type SchemaVaultsAppEnvironment } from "../SchemaVaultsAppEnvironment";
2
+ export interface IGetPostgresNeonWsProxyUrlOpts {
3
+ pg_host: string;
4
+ environment: SchemaVaultsAppEnvironment;
5
+ debug?: boolean;
6
+ }
7
+ export declare function getPostgresNeonWsProxyUrl({ pg_host, environment, ...opts }: IGetPostgresNeonWsProxyUrlOpts): string;
8
+ export default getPostgresNeonWsProxyUrl;
@@ -0,0 +1,40 @@
1
+ import { isValidAppEnvironment, } from "../SchemaVaultsAppEnvironment";
2
+ export function getPostgresNeonWsProxyUrl({ pg_host, environment, ...opts }) {
3
+ const debug = opts.debug ?? false;
4
+ if (debug) {
5
+ console.log(`[getPostgresNeonWsProxyUrl] Loading websocket proxy URL in environment "${environment}" from base 'pg_host' url:`, pg_host);
6
+ }
7
+ let ws_host;
8
+ if (environment === "production") {
9
+ ws_host = pg_host;
10
+ }
11
+ else if (environment === "development") {
12
+ if (pg_host.includes("localhost")) {
13
+ ws_host = "localhost";
14
+ }
15
+ else {
16
+ ws_host = "schemavaults-postgres-proxy-dev";
17
+ }
18
+ }
19
+ else if (environment === "test" || environment === "staging") {
20
+ ws_host = "schemavaults-postgres-proxy-dev";
21
+ }
22
+ else {
23
+ throw new Error("Failed to build 'ws_host' url for Postgres Neon proxy!");
24
+ }
25
+ console.assert(isValidAppEnvironment(environment), "Invalid app environment to determine postgres neon websocket proxy URL for!");
26
+ const neon_api_version_suffix = environment === "production"
27
+ ? "v2"
28
+ : "v1";
29
+ const usingLocalNeonProxyContainer = environment !== "production";
30
+ const shouldAppendPort = usingLocalNeonProxyContainer;
31
+ const postgresNeonWsProxyUrl = `${ws_host}${shouldAppendPort ? ":5433" : ""}/${neon_api_version_suffix}`;
32
+ console.assert(!postgresNeonWsProxyUrl.startsWith("ws://") &&
33
+ !postgresNeonWsProxyUrl.startsWith("wss://"), "Expected built postgres neon proxy websocket URL to not include protocol (ws:// or wss://)!");
34
+ if (debug) {
35
+ console.log(`[getPostgresNeonWsProxyUrl] Loaded websocket proxy URL: `, postgresNeonWsProxyUrl);
36
+ }
37
+ return postgresNeonWsProxyUrl;
38
+ }
39
+ export default getPostgresNeonWsProxyUrl;
40
+ //# sourceMappingURL=getPostgresNeonWsProxyUrl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPostgresNeonWsProxyUrl.js","sourceRoot":"","sources":["../../src/utils/getPostgresNeonWsProxyUrl.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,GAEtB,MAAM,8BAA8B,CAAC;AAQtC,MAAM,UAAU,yBAAyB,CAAC,EACxC,OAAO,EACP,WAAW,EACX,GAAG,IAAI,EACwB;IAC/B,MAAM,KAAK,GAAY,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC;IAC3C,IAAI,KAAK,EAAE,CAAC;QACV,OAAO,CAAC,GAAG,CACT,2EAA2E,WAAW,4BAA4B,EAClH,OAAO,CACR,CAAC;IACJ,CAAC;IAED,IAAI,OAAe,CAAC;IACpB,IAAI,WAAW,KAAK,YAAY,EAAE,CAAC;QACjC,OAAO,GAAG,OAAO,CAAC;IACpB,CAAC;SAAM,IAAI,WAAW,KAAK,aAAa,EAAE,CAAC;QACzC,IAAI,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;YAClC,OAAO,GAAG,WAAW,CAAC;QACxB,CAAC;aAAM,CAAC;YACN,OAAO,GAAG,iCAAiC,CAAC;QAC9C,CAAC;IACH,CAAC;SAAM,IAAI,WAAW,KAAK,MAAM,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;QAC/D,OAAO,GAAG,iCAAiC,CAAC;IAC9C,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;IAC5E,CAAC;IAED,OAAO,CAAC,MAAM,CACZ,qBAAqB,CAAC,WAAW,CAAC,EAClC,6EAA6E,CAC9E,CAAC;IAEF,MAAM,uBAAuB,GAC3B,WAAW,KAAK,YAAY;QAC1B,CAAC,CAAE,IAA+B;QAClC,CAAC,CAAE,IAA+B,CAAC;IAEvC,MAAM,4BAA4B,GAAY,WAAW,KAAK,YAAY,CAAC;IAC3E,MAAM,gBAAgB,GAAY,4BAA4B,CAAC;IAE/D,MAAM,sBAAsB,GAC1B,GAAG,OAAO,GAAG,gBAAgB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,IAAI,uBAAuB,EAA4B,CAAC;IAEtG,OAAO,CAAC,MAAM,CACZ,CAAC,sBAAsB,CAAC,UAAU,CAAC,OAAO,CAAC;QACzC,CAAC,sBAAsB,CAAC,UAAU,CAAC,QAAQ,CAAC,EAC9C,6FAA6F,CAC9F,CAAC;IAEF,IAAI,KAAK,EAAE,CAAC;QACV,OAAO,CAAC,GAAG,CACT,0DAA0D,EAC1D,sBAAsB,CACvB,CAAC;IACJ,CAAC;IAED,OAAO,sBAAsB,CAAC;AAChC,CAAC;AAED,eAAe,yBAAyB,CAAC"}
@@ -0,0 +1,10 @@
1
+ import type { SchemaVaultsAppEnvironment } from "../SchemaVaultsAppEnvironment";
2
+ declare global {
3
+ namespace NodeJS {
4
+ interface ProcessEnv {
5
+ SCHEMAVAULTS_DBH_DEBUG?: 'true' | 'false';
6
+ }
7
+ }
8
+ }
9
+ export declare function isDbhInDebugMode(environment: SchemaVaultsAppEnvironment): boolean;
10
+ export default isDbhInDebugMode;
@@ -0,0 +1,22 @@
1
+ export function isDbhInDebugMode(environment) {
2
+ const SCHEMAVAULTS_DBH_DEBUG_ENV_VAR = process.env["SCHEMAVAULTS_DBH_DEBUG"];
3
+ if (typeof SCHEMAVAULTS_DBH_DEBUG_ENV_VAR === "string") {
4
+ if (SCHEMAVAULTS_DBH_DEBUG_ENV_VAR.toLowerCase().includes("true")) {
5
+ return true;
6
+ }
7
+ else {
8
+ return false;
9
+ }
10
+ }
11
+ else {
12
+ // 'SCHEMAVAULTS_DBH_DEBUG' is not set in environment variables, get default debug state based on current environment
13
+ if (environment !== "production") {
14
+ return true;
15
+ }
16
+ else {
17
+ return false;
18
+ }
19
+ }
20
+ }
21
+ export default isDbhInDebugMode;
22
+ //# sourceMappingURL=isDbhInDebugMode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isDbhInDebugMode.js","sourceRoot":"","sources":["../../src/utils/isDbhInDebugMode.ts"],"names":[],"mappings":"AAUA,MAAM,UAAU,gBAAgB,CAC9B,WAAuC;IAEvC,MAAM,8BAA8B,GAClC,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC;IAExC,IAAI,OAAO,8BAA8B,KAAK,QAAQ,EAAE,CAAC;QACvD,IAAI,8BAA8B,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;YAClE,OAAO,IAAI,CAAC;QACd,CAAC;aAAM,CAAC;YACN,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;SAAM,CAAC;QACN,qHAAqH;QACrH,IAAI,WAAW,KAAK,YAAY,EAAE,CAAC;YACjC,OAAO,IAAI,CAAC;QACd,CAAC;aAAM,CAAC;YACN,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;AACH,CAAC;AAED,eAAe,gBAAgB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function maybeStripQuotes(maybeQuotes?: string | undefined): string | undefined;
2
+ export default maybeStripQuotes;
@@ -0,0 +1,14 @@
1
+ export function maybeStripQuotes(maybeQuotes) {
2
+ if (!maybeQuotes)
3
+ return maybeQuotes;
4
+ const trimmed = maybeQuotes.trim();
5
+ if (trimmed.startsWith('"') && trimmed.endsWith('"')) {
6
+ return trimmed.slice(1, -1);
7
+ }
8
+ if (trimmed.startsWith("'") && trimmed.endsWith("'")) {
9
+ return trimmed.slice(1, -1);
10
+ }
11
+ return trimmed;
12
+ }
13
+ export default maybeStripQuotes;
14
+ //# sourceMappingURL=maybeStripQuotes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"maybeStripQuotes.js","sourceRoot":"","sources":["../../src/utils/maybeStripQuotes.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,gBAAgB,CAC9B,WAAgC;IAEhC,IAAI,CAAC,WAAW;QAAE,OAAO,WAAW,CAAC;IACrC,MAAM,OAAO,GAAG,WAAW,CAAC,IAAI,EAAE,CAAC;IACnC,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACrD,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAC9B,CAAC;IACD,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACrD,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAC9B,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,eAAe,gBAAgB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { PostgresDatabaseCredentials } from "../PostgresDatabaseCredentials";
2
+ export declare function parseDatabaseCredentialsFromEnv(env: NodeJS.ProcessEnv, debug?: boolean): PostgresDatabaseCredentials;
3
+ export default parseDatabaseCredentialsFromEnv;
@@ -0,0 +1,57 @@
1
+ import maybeStripQuotes from "./maybeStripQuotes";
2
+ export function parseDatabaseCredentialsFromEnv(env, debug = false) {
3
+ const POSTGRES_URL = maybeStripQuotes(env.POSTGRES_URL);
4
+ if (!POSTGRES_URL) {
5
+ throw new Error("POSTGRES_URL is not set in environment variables!");
6
+ }
7
+ else {
8
+ if (debug) {
9
+ console.log(`[SchemaVaultsPostgresNeonProxyAdapter] Using postgres connection url POSTGRES_URL: \"${POSTGRES_URL}\"`);
10
+ }
11
+ }
12
+ const POSTGRES_URL_NON_POOLING = maybeStripQuotes(env.POSTGRES_URL_NON_POOLING);
13
+ if (!POSTGRES_URL_NON_POOLING) {
14
+ console.warn("POSTGRES_URL_NON_POOLING is not set in environment variables!");
15
+ }
16
+ else {
17
+ if (debug) {
18
+ console.log(`[SchemaVaultsPostgresNeonProxyAdapter] Using postgres no-pooling connection url POSTGRES_URL_NON_POOLING: \"${POSTGRES_URL_NON_POOLING}\"`);
19
+ }
20
+ }
21
+ const POSTGRES_HOST = maybeStripQuotes(env.POSTGRES_HOST);
22
+ if (!POSTGRES_HOST) {
23
+ throw new Error("POSTGRES_HOST is not set in environment variables!");
24
+ }
25
+ else {
26
+ if (debug) {
27
+ console.log(`[SchemaVaultsPostgresNeonProxyAdapter] Using database POSTGRES_HOST: \"${POSTGRES_HOST}\"`);
28
+ }
29
+ }
30
+ const POSTGRES_PORT = Number.parseInt(maybeStripQuotes(env.POSTGRES_PORT) ?? "NaN");
31
+ if (isNaN(POSTGRES_PORT)) {
32
+ throw new Error("Failed to load POSTGRES_PORT from environment variables! NaN error!");
33
+ }
34
+ const POSTGRES_DATABASE = maybeStripQuotes(env.POSTGRES_DATABASE);
35
+ if (!POSTGRES_DATABASE) {
36
+ throw new Error("Failed to load database name from POSTGRES_DATABASE environment variable!");
37
+ }
38
+ const POSTGRES_USER = maybeStripQuotes(env.POSTGRES_USER);
39
+ if (!POSTGRES_USER) {
40
+ throw new Error("POSTGRES_USER is not defined in environment variables!");
41
+ }
42
+ const POSTGRES_PASSWORD = maybeStripQuotes(env.POSTGRES_PASSWORD);
43
+ if (!POSTGRES_PASSWORD) {
44
+ throw new Error("POSTGRES_PASSWORD is not defined in environment variables!");
45
+ }
46
+ return {
47
+ POSTGRES_USER,
48
+ POSTGRES_PASSWORD,
49
+ POSTGRES_URL,
50
+ POSTGRES_URL_NON_POOLING,
51
+ POSTGRES_HOST,
52
+ POSTGRES_PORT,
53
+ POSTGRES_DATABASE,
54
+ };
55
+ }
56
+ export default parseDatabaseCredentialsFromEnv;
57
+ //# sourceMappingURL=parseDatabaseCredentialsFromEnv.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parseDatabaseCredentialsFromEnv.js","sourceRoot":"","sources":["../../src/utils/parseDatabaseCredentialsFromEnv.ts"],"names":[],"mappings":"AACA,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAElD,MAAM,UAAU,+BAA+B,CAAC,GAAsB,EAAE,QAAiB,KAAK;IAC5F,MAAM,YAAY,GAAG,gBAAgB,CACjC,GAAG,CAAC,YAAY,CACjB,CAAC;IACF,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;IACvE,CAAC;SAAM,CAAC;QACN,IAAI,KAAK,EAAE,CAAC;YACV,OAAO,CAAC,GAAG,CACT,wFAAwF,YAAY,IAAI,CACzG,CAAC;QACJ,CAAC;IACH,CAAC;IAED,MAAM,wBAAwB,GAC5B,gBAAgB,CACd,GAAG,CAAC,wBAAwB,CAC7B,CAAC;IACJ,IAAI,CAAC,wBAAwB,EAAE,CAAC;QAC9B,OAAO,CAAC,IAAI,CACV,+DAA+D,CAChE,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,IAAI,KAAK,EAAE,CAAC;YACV,OAAO,CAAC,GAAG,CACT,+GAA+G,wBAAwB,IAAI,CAC5I,CAAC;QACJ,CAAC;IACH,CAAC;IAED,MAAM,aAAa,GAAG,gBAAgB,CACpC,GAAG,CAAC,aAAa,CAClB,CAAC;IACF,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;IACxE,CAAC;SAAM,CAAC;QACN,IAAI,KAAK,EAAE,CAAC;YACV,OAAO,CAAC,GAAG,CACT,0EAA0E,aAAa,IAAI,CAC5F,CAAC;QACJ,CAAC;IACH,CAAC;IAED,MAAM,aAAa,GAAW,MAAM,CAAC,QAAQ,CAC3C,gBAAgB,CACd,GAAG,CAAC,aAAa,CAClB,IAAI,KAAK,CACX,CAAC;IACF,IAAI,KAAK,CAAC,aAAa,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CACb,qEAAqE,CACtE,CAAC;IACJ,CAAC;IAED,MAAM,iBAAiB,GACrB,gBAAgB,CACd,GAAG,CAAC,iBAAiB,CACtB,CAAC;IACJ,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CACb,2EAA2E,CAC5E,CAAC;IACJ,CAAC;IAED,MAAM,aAAa,GAAG,gBAAgB,CACpC,GAAG,CAAC,aAAa,CAClB,CAAC;IACF,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;IAC5E,CAAC;IAED,MAAM,iBAAiB,GACrB,gBAAgB,CACd,GAAG,CAAC,iBAAiB,CACtB,CAAC;IACJ,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CACb,4DAA4D,CAC7D,CAAC;IACJ,CAAC;IAED,OAAO;QACL,aAAa;QACb,iBAAiB;QACjB,YAAY;QACZ,wBAAwB;QACxB,aAAa;QACb,aAAa;QACb,iBAAiB;KAClB,CAAA;AACL,CAAC;AAED,eAAe,+BAA+B,CAAC"}
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@schemavaults/dbh",
3
+ "version": "0.5.1",
4
+ "description": "Easily connect to PostgresDB from serverless environment",
5
+ "license": "UNLICENSED",
6
+ "private": false,
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/schemavaults/dbh.git"
10
+ },
11
+ "dependencies": {
12
+ "kysely": "0.28.2",
13
+ "kysely-neon": "1.3.0",
14
+ "@neondatabase/serverless": "1.0.1"
15
+ },
16
+ "devDependencies": {
17
+ "typescript": "5.9.3",
18
+ "bun-types": "1.3.3",
19
+ "tsc-alias": "1.8.16"
20
+ },
21
+ "scripts": {
22
+ "build": "tsc --project tsconfig.json && tsc-alias --project tsconfig.json",
23
+ "test": "bun test",
24
+ "test:watch": "bun test --watch",
25
+ "cleanup:compiled_tests_in_dist_directory": "find ./dist -type f \\( -name \"*.test.js\" -o -name \"*.test.js.map\" -o -name \"*.test.d.ts\" \\) -delete",
26
+ "cleanup": "bun run cleanup:compiled_tests_in_dist_directory",
27
+ "postbuild": "bun run cleanup"
28
+ },
29
+ "main": "dist/index.js",
30
+ "module": "dist/index.js",
31
+ "types": "dist/index.d.ts",
32
+ "type": "module",
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "packageManager": "bun@1.3.3"
37
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "compilerOptions": {
3
+ "strict": true,
4
+ "outDir": "dist",
5
+ "target": "ESNext",
6
+ "moduleResolution": "Bundler",
7
+ "isolatedModules": true,
8
+ "module": "ESNext",
9
+ "skipLibCheck": true,
10
+ "declaration": true,
11
+ "emitDeclarationOnly": false,
12
+ "sourceMap": true,
13
+ "esModuleInterop": true,
14
+ "noImplicitAny": true,
15
+ "strictNullChecks": true,
16
+ "types": ["bun-types"],
17
+ "paths": {
18
+ "@/*": ["src/*"]
19
+ },
20
+ "baseUrl": "."
21
+ },
22
+ "include": ["src/**/*.ts"],
23
+ "exclude": ["node_modules", "dist"]
24
+ }