@gadicc/loom 1.21.0 → 1.21.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.
@@ -0,0 +1,26 @@
1
+ export declare const RUNTIME_DATABASE_URL_ENV_NAMES: readonly [
2
+ "DATABASE_URL_DIRECT",
3
+ "DATABASE_URL_UNPOOLED",
4
+ "POSTGRES_URL_NON_POOLING",
5
+ "DATABASE_URL",
6
+ ];
7
+
8
+ export type RuntimeDatabaseUrlEnvName =
9
+ (typeof RUNTIME_DATABASE_URL_ENV_NAMES)[number];
10
+
11
+ export interface RuntimeDatabaseUrl {
12
+ /** Environment variable selected by precedence. */
13
+ envName: RuntimeDatabaseUrlEnvName;
14
+ /** Database connection URL without logging or transformation. */
15
+ url: string;
16
+ /** Whether the selected variable represents a direct/unpooled connection. */
17
+ direct: boolean;
18
+ }
19
+
20
+ /**
21
+ * Resolves a transaction-capable runtime database URL, preferring explicit and
22
+ * platform-managed direct connections before the pooled application fallback.
23
+ */
24
+ export declare function resolveRuntimeDatabaseUrl(
25
+ env: Readonly<Record<string, string | undefined>>,
26
+ ): RuntimeDatabaseUrl;
@@ -0,0 +1,27 @@
1
+ export const RUNTIME_DATABASE_URL_ENV_NAMES = [
2
+ "DATABASE_URL_DIRECT",
3
+ "DATABASE_URL_UNPOOLED",
4
+ "POSTGRES_URL_NON_POOLING",
5
+ "DATABASE_URL",
6
+ ];
7
+
8
+ /**
9
+ * Resolves a transaction-capable runtime database URL, preferring explicit and
10
+ * platform-managed direct connections before the pooled application fallback.
11
+ */
12
+ export function resolveRuntimeDatabaseUrl(env) {
13
+ const envName = RUNTIME_DATABASE_URL_ENV_NAMES.find((name) =>
14
+ Boolean(env[name]?.trim()) && !/(?:sensitive|redacted)/i.test(env[name])
15
+ );
16
+ if (!envName) {
17
+ throw new Error(
18
+ `${RUNTIME_DATABASE_URL_ENV_NAMES.join(" or ")} is not set`,
19
+ );
20
+ }
21
+
22
+ return {
23
+ envName,
24
+ url: env[envName],
25
+ direct: envName !== "DATABASE_URL",
26
+ };
27
+ }
@@ -0,0 +1,72 @@
1
+ import { expect } from "@std/expect";
2
+ import { describe, it } from "@std/testing/bdd";
3
+ import { resolveRuntimeDatabaseUrl } from "./databaseUrl.js";
4
+
5
+ describe("resolveRuntimeDatabaseUrl", () => {
6
+ it("prefers explicit and integration-managed direct URLs", () => {
7
+ const resolved = resolveRuntimeDatabaseUrl({
8
+ DATABASE_URL_DIRECT: "postgresql://explicit.example/database",
9
+ DATABASE_URL_UNPOOLED: "postgresql://unpooled.example/database",
10
+ POSTGRES_URL_NON_POOLING: "postgresql://non-pooling.example/database",
11
+ DATABASE_URL: "postgresql://pooler.example/database",
12
+ });
13
+
14
+ expect(resolved).toEqual({
15
+ envName: "DATABASE_URL_DIRECT",
16
+ url: "postgresql://explicit.example/database",
17
+ direct: true,
18
+ });
19
+ });
20
+
21
+ it("uses Vercel's integration-managed unpooled URL without an alias", () => {
22
+ const resolved = resolveRuntimeDatabaseUrl({
23
+ DATABASE_URL_UNPOOLED: "postgresql://unpooled.example/database",
24
+ POSTGRES_URL_NON_POOLING: "postgresql://non-pooling.example/database",
25
+ DATABASE_URL: "postgresql://pooler.example/database",
26
+ });
27
+
28
+ expect(resolved.envName).toBe("DATABASE_URL_UNPOOLED");
29
+ expect(resolved.direct).toBe(true);
30
+ });
31
+
32
+ it("supports the legacy Neon non-pooling variable", () => {
33
+ const resolved = resolveRuntimeDatabaseUrl({
34
+ POSTGRES_URL_NON_POOLING: "postgresql://non-pooling.example/database",
35
+ DATABASE_URL: "postgresql://pooler.example/database",
36
+ });
37
+
38
+ expect(resolved.envName).toBe("POSTGRES_URL_NON_POOLING");
39
+ expect(resolved.direct).toBe(true);
40
+ });
41
+
42
+ it("keeps DATABASE_URL as the pooled fallback", () => {
43
+ const resolved = resolveRuntimeDatabaseUrl({
44
+ DATABASE_URL: "postgresql://pooler.example/database",
45
+ });
46
+
47
+ expect(resolved).toEqual({
48
+ envName: "DATABASE_URL",
49
+ url: "postgresql://pooler.example/database",
50
+ direct: false,
51
+ });
52
+ });
53
+
54
+ it("skips unreadable sensitive-value sentinels during external builds", () => {
55
+ const resolved = resolveRuntimeDatabaseUrl({
56
+ DATABASE_URL_UNPOOLED: "Vercel Sensitive Value",
57
+ DATABASE_URL: "postgresql://pooler.example/database",
58
+ });
59
+
60
+ expect(resolved.envName).toBe("DATABASE_URL");
61
+ expect(resolved.direct).toBe(false);
62
+ });
63
+
64
+ it("rejects empty and missing values", () => {
65
+ expect(() =>
66
+ resolveRuntimeDatabaseUrl({
67
+ DATABASE_URL_DIRECT: " ",
68
+ DATABASE_URL_UNPOOLED: "",
69
+ })
70
+ ).toThrow("DATABASE_URL_DIRECT or DATABASE_URL_UNPOOLED");
71
+ });
72
+ });
package/db/neonFull.js CHANGED
@@ -2,12 +2,9 @@ import "server-only";
2
2
  import { drizzle } from "drizzle-orm/postgres-js";
3
3
  import postgres from "postgres";
4
4
  import process from "node:process";
5
+ import { resolveRuntimeDatabaseUrl } from "./databaseUrl.js";
5
6
 
6
- const databaseUrl = process.env.DATABASE_URL_DIRECT ??
7
- process.env.DATABASE_URL ?? "";
8
- if (!databaseUrl) {
9
- throw new Error("DATABASE_URL is not set");
10
- }
7
+ const { direct, url: databaseUrl } = resolveRuntimeDatabaseUrl(process.env);
11
8
 
12
9
  const parsedUrl = new URL(databaseUrl);
13
10
  const isLocal = parsedUrl.hostname === "db.localtest.me" ||
@@ -16,9 +13,9 @@ const isLocal = parsedUrl.hostname === "db.localtest.me" ||
16
13
  const isPooler = parsedUrl.hostname.includes("pooler");
17
14
  const ssl = isLocal ? false : "require";
18
15
 
19
- if (isPooler && !process.env.DATABASE_URL_DIRECT) {
16
+ if (isPooler && !direct) {
20
17
  console.warn(
21
- "[db] DATABASE_URL points at a pooler. Set DATABASE_URL_DIRECT for postgres-js direct connections.",
18
+ "[db] DATABASE_URL points at a pooler and no direct database URL is available. Prefer DATABASE_URL_UNPOOLED or POSTGRES_URL_NON_POOLING from the platform integration, or set DATABASE_URL_DIRECT explicitly.",
22
19
  );
23
20
  }
24
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gadicc/loom",
3
- "version": "1.21.0",
3
+ "version": "1.21.1",
4
4
  "description": "Deno-first shared helpers for extracting common Next.js app code.",
5
5
  "homepage": "https://github.com/gadicc/loom#readme",
6
6
  "repository": {
@@ -77,6 +77,12 @@
77
77
  "require": "./db/drizzle-config.js",
78
78
  "default": "./db/drizzle-config.js"
79
79
  },
80
+ "./db/database-url": {
81
+ "types": "./db/databaseUrl.d.ts",
82
+ "import": "./db/databaseUrl.js",
83
+ "require": "./db/databaseUrl.js",
84
+ "default": "./db/databaseUrl.js"
85
+ },
80
86
  "./db/fixtures": {
81
87
  "types": "./db/fixtures.d.ts",
82
88
  "import": "./db/fixtures.js",
@@ -225,7 +231,7 @@
225
231
  },
226
232
  "scripts": {},
227
233
  "loom": {
228
- "gitCommit": "c48e1a784fc82ef4ad7280b19e15fa62363c2712"
234
+ "gitCommit": "5e5f4af45c0081d4010f2b033cf71b45f818005e"
229
235
  },
230
236
  "bin": {
231
237
  "loom": "script/bin/loom.js"