"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/create-pool.ts var _serverless = require('@neondatabase/serverless'); // src/error.ts var VercelPostgresError = class extends Error { constructor(code, message) { super(`VercelPostgresError - '${code}': ${message}`); this.code = code; this.name = "VercelPostgresError"; } }; // src/postgres-connection-string.ts function postgresConnectionString(type = "pool") { let connectionString; switch (type) { case "pool": { connectionString = process.env.POSTGRES_URL; break; } case "direct": { connectionString = process.env.POSTGRES_URL_NON_POOLING; break; } default: { const _exhaustiveCheck = type; const str = _exhaustiveCheck; throw new VercelPostgresError( "invalid_connection_type", `Unhandled type: ${str}` ); } } if (connectionString === "undefined") connectionString = void 0; return connectionString; } function isPooledConnectionString(connectionString) { return connectionString.includes("-pooler."); } function isDirectConnectionString(connectionString) { return !isPooledConnectionString(connectionString); } function isLocalhostConnectionString(connectionString) { try { const withHttpsProtocol = connectionString.startsWith("postgresql://") ? connectionString.replace("postgresql://", "https://") : connectionString; return new URL(withHttpsProtocol).hostname === "localhost"; } catch (err) { if (err instanceof TypeError) { return false; } if (typeof err === "object" && err !== null && "message" in err && typeof err.message === "string" && err.message === "Invalid URL") { return false; } throw err; } } // src/sql-template.ts function sqlTemplate(strings, ...values) { var _a, _b; if (!isTemplateStringsArray(strings) || !Array.isArray(values)) { throw new VercelPostgresError( "incorrect_tagged_template_call", "It looks like you tried to call `sql` as a function. Make sure to use it as a tagged template.\n Example: sql`SELECT * FROM users`, not sql('SELECT * FROM users')" ); } let result = (_a = strings[0]) != null ? _a : ""; for (let i = 1; i < strings.length; i++) { result += `$${i}${(_b = strings[i]) != null ? _b : ""}`; } return [result, values]; } function isTemplateStringsArray(strings) { return Array.isArray(strings) && "raw" in strings && Array.isArray(strings.raw); } // src/create-client.ts var VercelClient = class extends _serverless.Client { /** * A template literal tag providing safe, easy to use SQL parameterization. * Parameters are substituted using the underlying Postgres database, and so must follow * the rules of Postgres parameterization. * @example * ```ts * const pool = createClient(); * const userId = 123; * await client.connect(); * const result = await pool.sql`SELECT * FROM users WHERE id = ${userId}`; * // Equivalent to: await pool.query('SELECT * FROM users WHERE id = $1', [id]); * await client.end(); * ``` * @returns A promise that resolves to the query result. */ async sql(strings, ...values) { const [query, params] = sqlTemplate(strings, ...values); return this.query(query, params); } }; function createClient(config) { var _a; const connectionString = (_a = config == null ? void 0 : config.connectionString) != null ? _a : postgresConnectionString("direct"); if (!connectionString) throw new VercelPostgresError( "missing_connection_string", "You did not supply a 'connectionString' and no 'POSTGRES_URL_NON_POOLING' env var was found." ); if (!isLocalhostConnectionString(connectionString) && !isDirectConnectionString(connectionString)) throw new VercelPostgresError( "invalid_connection_string", "This connection string is meant to be used with a pooled connection. Try `createPool()` instead." ); return new VercelClient({ ...config, connectionString }); } // src/create-pool.ts var VercelPool = class extends _serverless.Pool { constructor(config) { var _a; super(config); this.Client = VercelClient; this.connectionString = (_a = config.connectionString) != null ? _a : ""; } /** * A template literal tag providing safe, easy to use SQL parameterization. * Parameters are substituted using the underlying Postgres database, and so must follow * the rules of Postgres parameterization. * @example * ```ts * const pool = createPool(); * const userId = 123; * const result = await pool.sql`SELECT * FROM users WHERE id = ${userId}`; * // Equivalent to: await pool.query('SELECT * FROM users WHERE id = $1', [id]); * ``` * @returns A promise that resolves to the query result. */ async sql(strings, ...values) { const [query, params] = sqlTemplate(strings, ...values); const sql2 = _serverless.neon.call(void 0, this.connectionString, { fullResults: true }); return sql2(query, params); } connect(callback) { return super.connect( callback ); } }; function createPool(config) { var _a; const connectionString = (_a = config == null ? void 0 : config.connectionString) != null ? _a : postgresConnectionString("pool"); if (!connectionString) throw new VercelPostgresError( "missing_connection_string", "You did not supply a 'connectionString' and no 'POSTGRES_URL' env var was found." ); if (!isLocalhostConnectionString(connectionString) && !isPooledConnectionString(connectionString)) throw new VercelPostgresError( "invalid_connection_string", "This connection string is meant to be used with a direct connection. Make sure to use a pooled connection string or try `createClient()` instead." ); let maxUses = config == null ? void 0 : config.maxUses; let max = config == null ? void 0 : config.max; if (typeof EdgeRuntime !== "undefined") { if (maxUses && maxUses !== 1) { console.warn( "@vercel/postgres: Overriding `maxUses` to 1 because the EdgeRuntime does not support client reuse." ); } if (max && max !== 1e4) { console.warn( "@vercel/postgres: Overriding `max` to 10,000 because the EdgeRuntime does not support client reuse." ); } maxUses = 1; max = 1e4; } const pool2 = new VercelPool({ ...config, connectionString, maxUses, max }); return pool2; } // src/index.ts var pool; var sql = new Proxy( // eslint-disable-next-line @typescript-eslint/no-empty-function -- [@vercel/style-guide@5 migration] () => { }, { get(_, prop) { if (!pool) { pool = createPool(); } const val = Reflect.get(pool, prop); if (typeof val === "function") { return val.bind(pool); } return val; }, apply(_, __, argumentsList) { if (!pool) { pool = createPool(); } return pool.sql(...argumentsList); } } ); var db = sql; exports.postgresConnectionString = postgresConnectionString; exports.VercelClient = VercelClient; exports.createClient = createClient; exports.VercelPool = VercelPool; exports.createPool = createPool; exports.sql = sql; exports.db = db; //# sourceMappingURL=chunk-PKSWROYW.cjs.map