{"version":3,"file":"session.cjs","names":["PgAsyncSession","entityKind","NoopLogger","NoopCache","PgAsyncPreparedQuery"],"sources":["../../src/neon-http/session.ts"],"sourcesContent":["import type { FullQueryResults, NeonQueryFunction, NeonQueryPromise } from '@neondatabase/serverless';\nimport type { BatchItem } from '~/batch.ts';\nimport { type Cache, NoopCache } from '~/cache/core/index.ts';\nimport type { WithCacheConfig } from '~/cache/core/types.ts';\nimport { entityKind } from '~/entity.ts';\nimport type { Logger } from '~/logger.ts';\nimport { NoopLogger } from '~/logger.ts';\nimport { PgAsyncPreparedQuery, PgAsyncSession, type PgAsyncTransaction } from '~/pg-core/async/session.ts';\nimport type { PgDialect } from '~/pg-core/dialect.ts';\nimport type { PgQueryResultHKT, PgTransactionConfig, PreparedQueryConfig } from '~/pg-core/session.ts';\nimport type { AnyRelations } from '~/relations.ts';\nimport type { Query } from '~/sql/sql.ts';\nimport type { NeonAuthToken } from '~/utils.ts';\n\nexport type NeonHttpClient = NeonQueryFunction<any, any>;\n\nexport interface NeonHttpSessionOptions {\n\tlogger?: Logger;\n\tcache?: Cache;\n\tauthToken?: NeonAuthToken;\n}\n\nexport class NeonHttpSession<TRelations extends AnyRelations>\n\textends PgAsyncSession<NeonHttpQueryResultHKT, TRelations>\n{\n\tstatic override readonly [entityKind]: string = 'NeonHttpSession';\n\n\tprivate logger: Logger;\n\tprivate cache: Cache;\n\n\t/** @internal */\n\treadonly client: NeonHttpClient;\n\tconstructor(\n\t\tclient: NeonHttpClient,\n\t\tdialect: PgDialect,\n\t\tprivate relations: AnyRelations,\n\t\treadonly options: NeonHttpSessionOptions = {},\n\t) {\n\t\tsuper(dialect);\n\t\tthis.client = client;\n\t\t// `client.query` is for @neondatabase/serverless v1.0.0 and up, where the\n\t\t// root query function `client` is only usable as a template function;\n\t\t// `client` is a fallback for earlier versions\n\t\tthis.logger = options.logger ?? new NoopLogger();\n\t\tthis.cache = options.cache ?? new NoopCache();\n\t}\n\n\tprepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tmode: 'arrays' | 'objects' | 'raw',\n\t\t_name: string | boolean,\n\t\tmapper: ((rows: any[]) => any) | undefined,\n\t\tqueryMetadata?: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t},\n\t\tcacheConfig?: WithCacheConfig,\n\t): PgAsyncPreparedQuery<T> {\n\t\tconst executor = (params?: unknown[]) => {\n\t\t\t// `client.query` is for @neondatabase/serverless v1.0.0 and up, where the\n\t\t\t// root query function `httpClient` is only usable as a template function;\n\t\t\t// `client` is a fallback for earlier versions\n\t\t\tconst q = ((this.client as any).query ?? this.client as any) as NeonHttpClient;\n\t\t\tif (mode === 'raw') {\n\t\t\t\t// otherwise raw queries with .then crash due to .then not existing on raw mode queries\n\t\t\t\treturn (async () =>\n\t\t\t\t\tq(query.sql, params, {\n\t\t\t\t\t\tarrayMode: false,\n\t\t\t\t\t\tfullResults: true,\n\t\t\t\t\t\tauthToken: this.options.authToken,\n\t\t\t\t\t}))();\n\t\t\t}\n\n\t\t\treturn q(query.sql, params, {\n\t\t\t\tarrayMode: mode === 'arrays',\n\t\t\t\tfullResults: true,\n\t\t\t\tauthToken: this.options.authToken,\n\t\t\t}).then((it: any) => it.rows);\n\t\t};\n\n\t\treturn new PgAsyncPreparedQuery(executor, query, mapper, mode, this.logger, this.cache, queryMetadata, cacheConfig);\n\t}\n\n\tasync batch<U extends BatchItem<'pg'>, T extends Readonly<[U, ...U[]]>>(queries: T) {\n\t\tconst preparedQueries: PgAsyncPreparedQuery<any>[] = [];\n\t\tconst builtQueries: NeonQueryPromise<any, true>[] = [];\n\t\tconst q = (this.client as any).query ?? this.client as any;\n\n\t\tfor (const query of queries) {\n\t\t\tconst preparedQuery = query._prepare() as PgAsyncPreparedQuery<any>;\n\t\t\tconst builtQuery = preparedQuery.getQuery();\n\t\t\tpreparedQueries.push(preparedQuery);\n\t\t\tbuiltQueries.push(\n\t\t\t\tq(builtQuery.sql, builtQuery.params, {\n\t\t\t\t\tfullResults: true,\n\t\t\t\t\tarrayMode: preparedQuery.mode === 'arrays',\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\n\t\tconst batchResults = await this.client.transaction(builtQueries, {\n\t\t\tauthToken: this.options.authToken,\n\t\t\tfullResults: true,\n\t\t\tarrayMode: true,\n\t\t});\n\t\treturn batchResults.map((result, i) =>\n\t\t\tpreparedQueries[i]!.mapper ? preparedQueries[i]!.mapper(result.rows) : result\n\t\t) as any;\n\t}\n\n\toverride async transaction<T>(\n\t\t_transaction: (tx: PgAsyncTransaction<NeonHttpQueryResultHKT, TRelations>) => Promise<T>,\n\t\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\t\t_config: PgTransactionConfig = {},\n\t): Promise<T> {\n\t\tthrow new Error('No transactions support in neon-http driver');\n\t}\n}\n\nexport type NeonHttpQueryResult<T> = Omit<FullQueryResults<false>, 'rows'> & { rows: T[] };\n\nexport interface NeonHttpQueryResultHKT extends PgQueryResultHKT {\n\ttype: NeonHttpQueryResult<this['row']>;\n}\n"],"mappings":";;;;;;;;AAsBA,IAAa,kBAAb,cACSA,0CACT;CACC,QAA0BC,0BAAsB;CAEhD,AAAQ;CACR,AAAQ;;CAGR,AAAS;CACT,YACC,QACA,SACA,AAAQ,WACR,AAAS,UAAkC,EAAE,EAC5C;AACD,QAAM,QAAQ;EAHN;EACC;AAGT,OAAK,SAAS;AAId,OAAK,SAAS,QAAQ,UAAU,IAAIC,wBAAY;AAChD,OAAK,QAAQ,QAAQ,SAAS,IAAIC,iCAAW;;CAG9C,aACC,OACA,MACA,OACA,QACA,eAIA,aAC0B;EAC1B,MAAM,YAAY,WAAuB;GAIxC,MAAM,IAAM,KAAK,OAAe,SAAS,KAAK;AAC9C,OAAI,SAAS,MAEZ,SAAQ,YACP,EAAE,MAAM,KAAK,QAAQ;IACpB,WAAW;IACX,aAAa;IACb,WAAW,KAAK,QAAQ;IACxB,CAAC,GAAG;AAGP,UAAO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