{"version":3,"sources":["../src/create-pool.ts","../src/error.ts","../src/postgres-connection-string.ts","../src/sql-template.ts","../src/create-client.ts","../src/index.ts"],"names":["sql","pool"],"mappings":";AAKA,SAAS,MAAM,YAAY;;;ACCpB,IAAM,sBAAN,cAAkC,MAAM;AAAA,EACtC,YAAmB,MAA+B,SAAiB;AACxE,UAAM,0BAA0B,UAAU,SAAS;AAD3B;AAExB,SAAK,OAAO;AAAA,EACd;AACF;;;ACPO,SAAS,yBACd,OAA6B,QACT;AACpB,MAAI;AAEJ,UAAQ,MAAM;AAAA,IACZ,KAAK,QAAQ;AACX,yBAAmB,QAAQ,IAAI;AAC/B;AAAA,IACF;AAAA,IACA,KAAK,UAAU;AACb,yBAAmB,QAAQ,IAAI;AAC/B;AAAA,IACF;AAAA,IACA,SAAS;AACP,YAAM,mBAA0B;AAChC,YAAM,MAAM;AACZ,YAAM,IAAI;AAAA,QACR;AAAA,QACA,mBAAmB;AAAA,MACrB;AAAA,IACF;AAAA,EACF;AAEA,MAAI,qBAAqB;AAAa,uBAAmB;AACzD,SAAO;AACT;AAEO,SAAS,yBAAyB,kBAAmC;AAC1E,SAAO,iBAAiB,SAAS,UAAU;AAC7C;AAEO,SAAS,yBAAyB,kBAAmC;AAC1E,SAAO,CAAC,yBAAyB,gBAAgB;AACnD;AAEO,SAAS,4BAA4B,kBAAmC;AAC7E,MAAI;AAGF,UAAM,oBAAoB,iBAAiB,WAAW,eAAe,IACjE,iBAAiB,QAAQ,iBAAiB,UAAU,IACpD;AACJ,WAAO,IAAI,IAAI,iBAAiB,EAAE,aAAa;AAAA,EACjD,SAAS,KAAP;AACA,QAAI,eAAe,WAAW;AAC5B,aAAO;AAAA,IACT;AAEA,QACE,OAAO,QAAQ,YACf,QAAQ,QACR,aAAa,OACb,OAAO,IAAI,YAAY,YACvB,IAAI,YAAY,eAChB;AACA,aAAO;AAAA,IACT;AACA,UAAM;AAAA,EACR;AACF;;;AC5DO,SAAS,YACd,YACG,QACoB;AAPzB;AA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AM,IAAI;AAClC,UAAI,OAAO,QAAQ,YAAY;AAE7B,eAAO,IAAI,KAAK,IAAI;AAAA,MACtB;AAEA,aAAO;AAAA,IACT;AAAA,IACA,MAAM,GAAG,IAAI,eAAe;AAC1B,UAAI,CAAC,MAAM;AACT,eAAO,WAAW;AAAA,MACpB;AAIA,aAAO,KAAK,IAAI,GAAG,aAAa;AAAA,IAClC;AAAA,EACF;AACF;AAMO,IAAM,KAAK","sourcesContent":["import type {\n PoolClient,\n QueryResult,\n QueryResultRow,\n} from '@neondatabase/serverless';\nimport { Pool, neon } from '@neondatabase/serverless';\nimport type { VercelPoolClient, VercelPostgresPoolConfig } from './types';\nimport {\n isLocalhostConnectionString,\n isPooledConnectionString,\n postgresConnectionString,\n} from './postgres-connection-string';\nimport { VercelPostgresError } from './error';\nimport type { Primitive } from './sql-template';\nimport { sqlTemplate } from './sql-template';\nimport { VercelClient } from './create-client';\n\nexport class VercelPool extends Pool {\n Client = VercelClient;\n private connectionString: string;\n\n constructor(config: VercelPostgresPoolConfig) {\n super(config);\n this.connectionString = config.connectionString ?? '';\n }\n\n /**\n * A template literal tag providing safe, easy to use SQL parameterization.\n * Parameters are substituted using the underlying Postgres database, and so must follow\n * the rules of Postgres parameterization.\n * @example\n * ```ts\n * const pool = createPool();\n * const userId = 123;\n * const result = await pool.sql`SELECT * FROM users WHERE id = ${userId}`;\n * // Equivalent to: await pool.query('SELECT * FROM users WHERE id = $1', [id]);\n * ```\n * @returns A promise that resolves to the query result.\n */\n async sql(\n strings: TemplateStringsArray,\n ...values: Primitive[]\n ): Promise> {\n const [query, params] = sqlTemplate(strings, ...values);\n\n const sql = neon(this.connectionString, {\n fullResults: true,\n });\n return sql(query, params) as unknown as Promise>;\n }\n\n connect(): Promise;\n connect(\n callback: (\n err: Error,\n client: VercelPoolClient,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- [@vercel/style-guide@5 migration]\n done: (release?: any) => void\n ) => void\n ): void;\n connect(\n callback?: (\n err: Error,\n client: VercelPoolClient,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- [@vercel/style-guide@5 migration]\n done: (release?: any) => void\n ) => void\n // eslint-disable-next-line @typescript-eslint/no-invalid-void-type -- [@vercel/style-guide@5 migration]\n ): void | Promise {\n // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression -- [@vercel/style-guide@5 migration]\n return super.connect(\n callback as (\n err: Error,\n client: PoolClient,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- [@vercel/style-guide@5 migration]\n done: (release?: any) => void\n ) => void\n );\n }\n}\n\nexport function createPool(config?: VercelPostgresPoolConfig): VercelPool {\n const connectionString =\n config?.connectionString ?? postgresConnectionString('pool');\n if (!connectionString)\n throw new VercelPostgresError(\n 'missing_connection_string',\n \"You did not supply a 'connectionString' and no 'POSTGRES_URL' env var was found.\"\n );\n\n if (\n !isLocalhostConnectionString(connectionString) &&\n !isPooledConnectionString(connectionString)\n )\n throw new VercelPostgresError(\n 'invalid_connection_string',\n 'This connection string is meant to be used with a direct connection. Make sure to use a pooled connection string or try `createClient()` instead.'\n );\n\n let maxUses = config?.maxUses;\n let max = config?.max;\n if (typeof EdgeRuntime !== 'undefined') {\n if (maxUses && maxUses !== 1) {\n // eslint-disable-next-line no-console -- [@vercel/style-guide@5 migration]\n console.warn(\n '@vercel/postgres: Overriding `maxUses` to 1 because the EdgeRuntime does not support client reuse.'\n );\n }\n if (max && max !== 10_000) {\n // eslint-disable-next-line no-console -- [@vercel/style-guide@5 migration]\n console.warn(\n '@vercel/postgres: Overriding `max` to 10,000 because the EdgeRuntime does not support client reuse.'\n );\n }\n // Client reuse is not supported in the EdgeRuntime because it does not support IO across requests.\n maxUses = 1;\n // Since we do not allow client reuse, we need a higher max number of clients to avoid running out of\n // connections.\n // Usecase: a website that needs more than 10 concurrent connections\n // https://node-postgres.com/apis/pool#new-pool\n max = 10_000;\n }\n\n const pool = new VercelPool({\n ...config,\n connectionString,\n maxUses,\n max,\n });\n return pool;\n}\n","type VercelPostgresErrorCode =\n | 'invalid_connection_string'\n | 'missing_connection_string'\n | 'invalid_connection_type'\n | 'incorrect_tagged_template_call';\n\nexport class VercelPostgresError extends Error {\n public constructor(public code: VercelPostgresErrorCode, message: string) {\n super(`VercelPostgresError - '${code}': ${message}`);\n this.name = 'VercelPostgresError';\n }\n}\n","import { VercelPostgresError } from './error';\n\nexport type ConnectionStringType = 'pool' | 'direct';\n\nexport function postgresConnectionString(\n type: ConnectionStringType = 'pool'\n): string | undefined {\n let connectionString: string | undefined;\n\n switch (type) {\n case 'pool': {\n connectionString = process.env.POSTGRES_URL;\n break;\n }\n case 'direct': {\n connectionString = process.env.POSTGRES_URL_NON_POOLING;\n break;\n }\n default: {\n const _exhaustiveCheck: never = type;\n const str = _exhaustiveCheck as string;\n throw new VercelPostgresError(\n 'invalid_connection_type',\n `Unhandled type: ${str}`\n );\n }\n }\n\n if (connectionString === 'undefined') connectionString = undefined;\n return connectionString;\n}\n\nexport function isPooledConnectionString(connectionString: string): boolean {\n return connectionString.includes('-pooler.');\n}\n\nexport function isDirectConnectionString(connectionString: string): boolean {\n return !isPooledConnectionString(connectionString);\n}\n\nexport function isLocalhostConnectionString(connectionString: string): boolean {\n try {\n // This seems silly, but we can use all of the hard work put into URL parsing\n // if we just convert `postgresql://` to `https://` and then parse it as a URL.\n const withHttpsProtocol = connectionString.startsWith('postgresql://')\n ? connectionString.replace('postgresql://', 'https://')\n : connectionString;\n return new URL(withHttpsProtocol).hostname === 'localhost';\n } catch (err) {\n if (err instanceof TypeError) {\n return false;\n }\n // ok typescript\n if (\n typeof err === 'object' &&\n err !== null &&\n 'message' in err &&\n typeof err.message === 'string' &&\n err.message === 'Invalid URL'\n ) {\n return false;\n }\n throw err;\n }\n}\n","import { VercelPostgresError } from './error';\n\nexport type Primitive = string | number | boolean | undefined | null;\n\nexport function sqlTemplate(\n strings: TemplateStringsArray,\n ...values: Primitive[]\n): [string, Primitive[]] {\n if (!isTemplateStringsArray(strings) || !Array.isArray(values)) {\n throw new VercelPostgresError(\n 'incorrect_tagged_template_call',\n \"It looks like you tried to call `sql` as a function. Make sure to use it as a tagged template.\\n\\tExample: sql`SELECT * FROM users`, not sql('SELECT * FROM users')\"\n );\n }\n\n let result = strings[0] ?? '';\n\n for (let i = 1; i < strings.length; i++) {\n result += `$${i}${strings[i] ?? ''}`;\n }\n\n return [result, values];\n}\n\nfunction isTemplateStringsArray(\n strings: unknown\n): strings is TemplateStringsArray {\n return (\n Array.isArray(strings) && 'raw' in strings && Array.isArray(strings.raw)\n );\n}\n","import type { QueryResult, QueryResultRow } from '@neondatabase/serverless';\nimport { Client } from '@neondatabase/serverless';\nimport type { VercelPostgresClientConfig } from './types';\nimport {\n isDirectConnectionString,\n isLocalhostConnectionString,\n postgresConnectionString,\n} from './postgres-connection-string';\nimport { VercelPostgresError } from './error';\nimport type { Primitive } from './sql-template';\nimport { sqlTemplate } from './sql-template';\n\nexport class VercelClient extends Client {\n /**\n * A template literal tag providing safe, easy to use SQL parameterization.\n * Parameters are substituted using the underlying Postgres database, and so must follow\n * the rules of Postgres parameterization.\n * @example\n * ```ts\n * const pool = createClient();\n * const userId = 123;\n * await client.connect();\n * const result = await pool.sql`SELECT * FROM users WHERE id = ${userId}`;\n * // Equivalent to: await pool.query('SELECT * FROM users WHERE id = $1', [id]);\n * await client.end();\n * ```\n * @returns A promise that resolves to the query result.\n */\n async sql(\n strings: TemplateStringsArray,\n ...values: Primitive[]\n ): Promise> {\n const [query, params] = sqlTemplate(strings, ...values);\n return this.query(query, params);\n }\n}\n\nexport function createClient(\n config?: VercelPostgresClientConfig\n): VercelClient {\n const connectionString =\n config?.connectionString ?? postgresConnectionString('direct');\n if (!connectionString)\n throw new VercelPostgresError(\n 'missing_connection_string',\n \"You did not supply a 'connectionString' and no 'POSTGRES_URL_NON_POOLING' env var was found.\"\n );\n if (\n !isLocalhostConnectionString(connectionString) &&\n !isDirectConnectionString(connectionString)\n )\n throw new VercelPostgresError(\n 'invalid_connection_string',\n 'This connection string is meant to be used with a pooled connection. Try `createPool()` instead.'\n );\n return new VercelClient({\n ...config,\n connectionString,\n });\n}\n","import type { QueryResult, QueryResultRow } from '@neondatabase/serverless';\nimport { type VercelPool, createPool } from './create-pool';\nimport type { Primitive } from './sql-template';\n\nexport * from './create-client';\nexport * from './create-pool';\nexport * from './types';\nexport { postgresConnectionString } from './postgres-connection-string';\n\nlet pool: VercelPool | undefined;\n\n// for future peons who aren't briliant like Malte, this means\n// \"make an object that will pretend to be a pool but not initialize itself\n// until someone tries to access a property on it\"\n// this also makes it callable, so you can call `sql` as a function\nexport const sql = new Proxy(\n // eslint-disable-next-line @typescript-eslint/no-empty-function -- [@vercel/style-guide@5 migration]\n () => {},\n {\n get(_, prop) {\n if (!pool) {\n pool = createPool();\n }\n\n // keep an eye on this -- it'll fail on certain cases, like private property access, which can\n // require weird things like binding or calling with an explicit `this` arg.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- [@vercel/style-guide@5 migration]\n const val = Reflect.get(pool, prop);\n if (typeof val === 'function') {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call -- [@vercel/style-guide@5 migration]\n return val.bind(pool);\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- [@vercel/style-guide@5 migration]\n return val;\n },\n apply(_, __, argumentsList) {\n if (!pool) {\n pool = createPool();\n }\n\n // @ts-expect-error - we're breaking all kinds of rules\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- [@vercel/style-guide@5 migration]\n return pool.sql(...argumentsList);\n },\n }\n) as unknown as VercelPool &\n ((\n strings: TemplateStringsArray,\n ...values: Primitive[]\n ) => Promise>);\n\nexport const db = sql;\n"]}