@nextlyhq/adapter-postgres 0.0.2-alpha.65 → 0.0.2-alpha.67
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.
- package/README.md +5 -5
- package/dist/index.cjs +104 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.mjs +104 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -76,11 +76,11 @@ Most users do not need this. The framework's runtime does it automatically.
|
|
|
76
76
|
|
|
77
77
|
## Compatibility
|
|
78
78
|
|
|
79
|
-
| Tool | Version
|
|
80
|
-
| -------- |
|
|
81
|
-
| Node.js | 20+
|
|
82
|
-
| `pg` | 8.10+
|
|
83
|
-
| `nextly` | 0.0.x
|
|
79
|
+
| Tool | Version |
|
|
80
|
+
| -------- | --------------------- |
|
|
81
|
+
| Node.js | 20.19+, 22.12+ or 24+ |
|
|
82
|
+
| `pg` | 8.10+ |
|
|
83
|
+
| `nextly` | 0.0.x |
|
|
84
84
|
|
|
85
85
|
## Documentation
|
|
86
86
|
|
package/dist/index.cjs
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
var net = require('net');
|
|
4
4
|
var adapterDrizzle = require('@nextlyhq/adapter-drizzle');
|
|
5
|
+
var schemaName = require('@nextlyhq/adapter-drizzle/schema-name');
|
|
5
6
|
var types = require('@nextlyhq/adapter-drizzle/types');
|
|
6
7
|
var versionCheck = require('@nextlyhq/adapter-drizzle/version-check');
|
|
7
8
|
var nodePostgres = require('drizzle-orm/node-postgres');
|
|
9
|
+
var pgCore = require('drizzle-orm/pg-core');
|
|
8
10
|
var pg = require('pg');
|
|
9
11
|
|
|
10
12
|
function _interopNamespace(e) {
|
|
@@ -118,6 +120,15 @@ var PG_ERROR_CODES = {
|
|
|
118
120
|
"57P02": "connection",
|
|
119
121
|
"57P03": "connection"
|
|
120
122
|
};
|
|
123
|
+
async function ensureSchemaExists(client, schema) {
|
|
124
|
+
const name = schemaName.assertSchemaName(schema);
|
|
125
|
+
const existing = await client.query(
|
|
126
|
+
"SELECT 1 FROM pg_namespace WHERE nspname = $1",
|
|
127
|
+
[name]
|
|
128
|
+
);
|
|
129
|
+
if (existing.rows.length > 0) return;
|
|
130
|
+
await client.query(`CREATE SCHEMA IF NOT EXISTS ${name}`);
|
|
131
|
+
}
|
|
121
132
|
function delay(ms) {
|
|
122
133
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
123
134
|
}
|
|
@@ -136,6 +147,7 @@ function applyHappyEyeballsTimeoutOnce() {
|
|
|
136
147
|
} catch {
|
|
137
148
|
}
|
|
138
149
|
}
|
|
150
|
+
var PG_WALL_CLOCK_FORMAT = 'YYYY-MM-DD"T"HH24:MI:SS.MS';
|
|
139
151
|
var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
140
152
|
// getDrizzle memoization: drizzle v1's constructor builds a relational
|
|
141
153
|
// query builder per table in the relations config (~40 tables), and the
|
|
@@ -231,6 +243,9 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
231
243
|
const client = await this.pool.connect();
|
|
232
244
|
try {
|
|
233
245
|
await client.query("SELECT 1");
|
|
246
|
+
if (this.config.schema) {
|
|
247
|
+
await ensureSchemaExists(client, this.config.schema);
|
|
248
|
+
}
|
|
234
249
|
await versionCheck.checkDialectVersion(client, "postgresql", {
|
|
235
250
|
// Why: route any future variant warnings through the adapter's
|
|
236
251
|
// logger. PG has no recognized variants today, but this keeps
|
|
@@ -371,6 +386,12 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
371
386
|
* @throws The callback's own error, unchanged, when it carries that brand
|
|
372
387
|
*/
|
|
373
388
|
async transaction(callback, options) {
|
|
389
|
+
return this.runInFlowTransaction(
|
|
390
|
+
() => this.runTransactionAttempts(callback, options)
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
/** The attempts `transaction` makes, each on a connection of its own. */
|
|
394
|
+
async runTransactionAttempts(callback, options) {
|
|
374
395
|
const pool = this.ensurePool();
|
|
375
396
|
const maxAttempts = (options?.retryCount ?? 0) + 1;
|
|
376
397
|
const retryDelayMs = options?.retryDelayMs ?? 100;
|
|
@@ -378,6 +399,7 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
378
399
|
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
379
400
|
const client = await pool.connect();
|
|
380
401
|
const startTime = Date.now();
|
|
402
|
+
let discard;
|
|
381
403
|
try {
|
|
382
404
|
await this.beginTransaction(client, options);
|
|
383
405
|
const ctx = this.createTransactionContext(client);
|
|
@@ -392,7 +414,8 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
392
414
|
}
|
|
393
415
|
return result;
|
|
394
416
|
} catch (error) {
|
|
395
|
-
await client.query("ROLLBACK").catch(() => {
|
|
417
|
+
await client.query("ROLLBACK").catch((rollbackError) => {
|
|
418
|
+
discard = rollbackError instanceof Error ? rollbackError : true;
|
|
396
419
|
});
|
|
397
420
|
lastError = error;
|
|
398
421
|
if (types.isApplicationError(error)) throw error;
|
|
@@ -411,7 +434,7 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
411
434
|
}
|
|
412
435
|
throw this.classifyError(error);
|
|
413
436
|
} finally {
|
|
414
|
-
client.release();
|
|
437
|
+
client.release(discard);
|
|
415
438
|
}
|
|
416
439
|
}
|
|
417
440
|
throw this.classifyError(lastError);
|
|
@@ -441,6 +464,20 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
441
464
|
// PostgreSQL default
|
|
442
465
|
};
|
|
443
466
|
}
|
|
467
|
+
/**
|
|
468
|
+
* The schema set as `search_path` on every pooled connection.
|
|
469
|
+
*
|
|
470
|
+
* @remarks
|
|
471
|
+
* Answers with the same test `buildPoolConfig` applies, so an empty string
|
|
472
|
+
* reports `undefined` exactly as it sets no `search_path`: the value
|
|
473
|
+
* reported is the one the connections actually use.
|
|
474
|
+
*
|
|
475
|
+
* @returns The configured schema, or `undefined` when the server's default
|
|
476
|
+
* `search_path` applies
|
|
477
|
+
*/
|
|
478
|
+
getConfiguredSchema() {
|
|
479
|
+
return this.config.schema ? this.config.schema : void 0;
|
|
480
|
+
}
|
|
444
481
|
/**
|
|
445
482
|
* Returns connection pool statistics.
|
|
446
483
|
*
|
|
@@ -527,16 +564,38 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
527
564
|
}
|
|
528
565
|
const pool = this.ensurePool();
|
|
529
566
|
if (!relations) {
|
|
530
|
-
this.drizzleBare ??=
|
|
567
|
+
this.drizzleBare ??= this.poolDrizzle(pool);
|
|
531
568
|
return this.drizzleBare;
|
|
532
569
|
}
|
|
533
570
|
let cached = this.drizzleByRelations.get(relations);
|
|
534
571
|
if (!cached) {
|
|
535
|
-
cached =
|
|
572
|
+
cached = this.poolDrizzle(pool, relations);
|
|
536
573
|
this.drizzleByRelations.set(relations, cached);
|
|
537
574
|
}
|
|
538
575
|
return cached;
|
|
539
576
|
}
|
|
577
|
+
/**
|
|
578
|
+
* A Drizzle handle on the pool, for `getDrizzle`.
|
|
579
|
+
*
|
|
580
|
+
* Its `transaction` runs on a client this adapter checks out and returns
|
|
581
|
+
* itself (`pinDrizzleTransactions`): node-postgres destroys a client
|
|
582
|
+
* released with `true`, which is what happens when no ROLLBACK could be
|
|
583
|
+
* confirmed on it. And it counts for `transactionOpenInFlow`, as the
|
|
584
|
+
* adapter's does.
|
|
585
|
+
*/
|
|
586
|
+
poolDrizzle(pool, relations) {
|
|
587
|
+
const onClient = (client) => relations ? nodePostgres.drizzle({ client, relations }) : nodePostgres.drizzle({ client });
|
|
588
|
+
return this.trackFlowTransactions(
|
|
589
|
+
this.pinDrizzleTransactions(onClient(pool), async () => {
|
|
590
|
+
const client = await pool.connect();
|
|
591
|
+
return {
|
|
592
|
+
drizzle: onClient(client),
|
|
593
|
+
rollback: () => client.query("ROLLBACK"),
|
|
594
|
+
release: (dirty) => client.release(dirty)
|
|
595
|
+
};
|
|
596
|
+
})
|
|
597
|
+
);
|
|
598
|
+
}
|
|
540
599
|
/**
|
|
541
600
|
* Builds pg Pool configuration from adapter config.
|
|
542
601
|
*/
|
|
@@ -579,6 +638,9 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
579
638
|
if (this.config.applicationName) {
|
|
580
639
|
config.application_name = this.config.applicationName;
|
|
581
640
|
}
|
|
641
|
+
if (this.config.schema) {
|
|
642
|
+
config.options = `-c search_path=${schemaName.assertSchemaName(this.config.schema)}`;
|
|
643
|
+
}
|
|
582
644
|
const effectiveStatementTimeout = this.config.statementTimeout ?? this.providerDefaults.statementTimeoutMs;
|
|
583
645
|
if (effectiveStatementTimeout !== void 0) {
|
|
584
646
|
config.statement_timeout = effectiveStatementTimeout;
|
|
@@ -616,11 +678,30 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
616
678
|
/**
|
|
617
679
|
* Creates a TransactionContext for the given client.
|
|
618
680
|
*/
|
|
681
|
+
/**
|
|
682
|
+
* The RETURNING entries that spell each date column's wall clock out as
|
|
683
|
+
* text, one per alias `dateWallClockAliases` chose. `to_char` renders the
|
|
684
|
+
* stored value without a zone, which is the only form node-postgres cannot
|
|
685
|
+
* shift on the way back.
|
|
686
|
+
*/
|
|
687
|
+
wallClockSpelling(aliases) {
|
|
688
|
+
return aliases.map(
|
|
689
|
+
(a) => `to_char(${this.escapeIdentifier(a.sqlName)}, '${PG_WALL_CLOCK_FORMAT}') AS ${this.escapeIdentifier(a.alias)}`
|
|
690
|
+
).join(", ");
|
|
691
|
+
}
|
|
692
|
+
/** A table-level `primaryKey({ columns })`, read through the PostgreSQL table config. */
|
|
693
|
+
compositePrimaryKey(tableObj) {
|
|
694
|
+
return pgCore.getTableConfig(tableObj).primaryKeys.flatMap(
|
|
695
|
+
(key) => key.columns
|
|
696
|
+
);
|
|
697
|
+
}
|
|
619
698
|
createTransactionContext(client) {
|
|
620
|
-
const
|
|
621
|
-
|
|
622
|
-
|
|
699
|
+
const txHandles = this.transactionDrizzleHandles(
|
|
700
|
+
(relations) => relations ? nodePostgres.drizzle({ client, relations }) : nodePostgres.drizzle({ client })
|
|
701
|
+
);
|
|
702
|
+
const txDb = txHandles.bare;
|
|
623
703
|
return {
|
|
704
|
+
drizzleWithRelations: txHandles.withRelations,
|
|
624
705
|
execute: async (sql, params = []) => {
|
|
625
706
|
const result = await client.query(sql, params);
|
|
626
707
|
return result.rows;
|
|
@@ -655,9 +736,7 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
655
736
|
const aliases = returningEmpty ? [] : this.dateWallClockAliases(tableObj, ret ?? "*");
|
|
656
737
|
if (!returningEmpty) {
|
|
657
738
|
const returning = !ret || ret === "*" ? "*" : this.mapColumnNamesToSql(tableObj, ret).map((col) => this.escapeIdentifier(col)).join(", ");
|
|
658
|
-
const spelled =
|
|
659
|
-
(a) => `to_char(${this.escapeIdentifier(a.sqlName)}, 'YYYY-MM-DD"T"HH24:MI:SS.MS') AS ${this.escapeIdentifier(a.alias)}`
|
|
660
|
-
).join(", ");
|
|
739
|
+
const spelled = this.wallClockSpelling(aliases);
|
|
661
740
|
sql += ` RETURNING ${returning}${spelled ? `, ${spelled}` : ""}`;
|
|
662
741
|
}
|
|
663
742
|
const result = await client.query(sql, values);
|
|
@@ -684,9 +763,7 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
684
763
|
const aliases = returningEmpty ? [] : this.dateWallClockAliases(tableObj, ret ?? "*");
|
|
685
764
|
if (!returningEmpty) {
|
|
686
765
|
const returning = !ret || ret === "*" ? "*" : this.mapColumnNamesToSql(tableObj, ret).map((col) => this.escapeIdentifier(col)).join(", ");
|
|
687
|
-
const spelled =
|
|
688
|
-
(a) => `to_char(${this.escapeIdentifier(a.sqlName)}, 'YYYY-MM-DD"T"HH24:MI:SS.MS') AS ${this.escapeIdentifier(a.alias)}`
|
|
689
|
-
).join(", ");
|
|
766
|
+
const spelled = this.wallClockSpelling(aliases);
|
|
690
767
|
sql += ` RETURNING ${returning}${spelled ? `, ${spelled}` : ""}`;
|
|
691
768
|
}
|
|
692
769
|
const result = await client.query(sql, params);
|
|
@@ -694,6 +771,16 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
694
771
|
(r) => this.mapRowFromRawSql(tableObj, r, aliases)
|
|
695
772
|
);
|
|
696
773
|
},
|
|
774
|
+
// Adapter-built, as `insert` above is; `transactionUpdate` says why.
|
|
775
|
+
// A column the model does not declare binds natively, as every value
|
|
776
|
+
// on this path's insert does.
|
|
777
|
+
update: this.transactionUpdate(
|
|
778
|
+
txDb,
|
|
779
|
+
async (statement) => (await txDb().execute(statement)).rows,
|
|
780
|
+
// Structured values as JSON text: node-postgres would spell an array
|
|
781
|
+
// as a PostgreSQL array literal, which no JSON column accepts.
|
|
782
|
+
(value) => this.bindUnmodeledStructuredAsJson(value)
|
|
783
|
+
),
|
|
697
784
|
...this.createTransactionForwarders(txDb),
|
|
698
785
|
savepoint: async (name) => {
|
|
699
786
|
await client.query(`SAVEPOINT ${this.escapeIdentifier(name)}`);
|
|
@@ -717,7 +804,10 @@ var PostgresAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
717
804
|
*/
|
|
718
805
|
classifyError(error, sql) {
|
|
719
806
|
if (types.isDatabaseError(error)) return error;
|
|
720
|
-
const pgError =
|
|
807
|
+
const pgError = this.driverErrorIn(
|
|
808
|
+
error,
|
|
809
|
+
(candidate) => typeof candidate.code === "string" && candidate.code !== ""
|
|
810
|
+
);
|
|
721
811
|
const kind = pgError.code && PG_ERROR_CODES[pgError.code] || "unknown";
|
|
722
812
|
let message = pgError.message ?? String(error);
|
|
723
813
|
if (sql && kind === "query") {
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/provider.ts","../src/index.ts"],"names":["net","DrizzleAdapter","Pool","checkDialectVersion","isApplicationError","createDatabaseError","drizzle","isDatabaseError"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBO,SAAS,sBAAA,CACd,KACA,gBAAA,EACkB;AAElB,EAAA,IAAI,gBAAA,EAAkB;AACpB,IAAA,MAAM,UAAA,GAAa,iBAAiB,WAAA,EAAY;AAChD,IAAA,IACE,UAAA,KAAe,MAAA,IACf,UAAA,KAAe,UAAA,IACf,eAAe,UAAA,EACf;AACA,MAAA,OAAO,UAAA;AAAA,IACT;AAAA,EACF;AAGA,EAAA,IAAI,IAAI,QAAA,CAAS,YAAY,KAAK,GAAA,CAAI,QAAA,CAAS,OAAO,CAAA,EAAG;AACvD,IAAA,OAAO,MAAA;AAAA,EACT;AACA,EAAA,IAAI,IAAI,QAAA,CAAS,YAAY,KAAK,GAAA,CAAI,QAAA,CAAS,WAAW,CAAA,EAAG;AAC3D,IAAA,OAAO,UAAA;AAAA,EACT;AAEA,EAAA,OAAO,UAAA;AACT;AAGO,SAAS,oBACd,QAAA,EACkB;AAClB,EAAA,QAAQ,QAAA;AAAU,IAChB,KAAK,MAAA;AAEH,MAAA,OAAO;AAAA,QACL,GAAA,EAAK,IAAA;AAAA,QACL,OAAA,EAAS,CAAA;AAAA,QACT,OAAA,EAAS,CAAA;AAAA,QACT,aAAA,EAAe,GAAA;AAAA,QACf,mBAAA,EAAqB,GAAA;AAAA,QACrB,kBAAA,EAAoB,GAAA;AAAA,QACpB,aAAA,EAAe;AAAA,OACjB;AAAA,IACF,KAAK,UAAA;AAEH,MAAA,OAAO;AAAA,QACL,GAAA,EAAK,IAAA;AAAA,QACL,OAAA,EAAS,CAAA;AAAA,QACT,OAAA,EAAS,CAAA;AAAA,QACT,aAAA,EAAe,GAAA;AAAA,QACf,mBAAA,EAAqB,IAAA;AAAA,QACrB,kBAAA,EAAoB,IA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AAQ,UAAA;AAAA,MACpB,OAAO,OAAA,CAAQ,KAAA;AAAA,MACf,QAAQ,OAAA,CAAQ,MAAA;AAAA,MAChB,QAAQ,OAAA,CAAQ,MAAA;AAAA,MAChB,MAAM,OAAA,CAAQ,IAAA;AAAA,MACd,KAAA,EAAO,KAAA,YAAiB,KAAA,GAAQ,KAAA,GAAQ;AAAA,KACzC,CAAA;AAAA,EACH;AACF;AA+CO,SAAS,sBACd,MAAA,EACiB;AACjB,EAAA,OAAO,IAAI,gBAAgB,MAAM,CAAA;AACnC;AAoBO,SAAS,kBAAkB,KAAA,EAA0C;AAC1E,EAAA,OAAO,KAAA,YAAiB,eAAA;AAC1B","file":"index.cjs","sourcesContent":["// Auto-detects PostgreSQL provider from DATABASE_URL pattern\n// and returns appropriate connection defaults.\n// Standard pg driver works with all providers - only config changes.\n\nexport type PostgresProvider = \"standard\" | \"neon\" | \"supabase\";\n\nexport interface ProviderDefaults {\n ssl: boolean;\n poolMax: number;\n poolMin: number;\n idleTimeoutMs: number;\n connectionTimeoutMs: number;\n statementTimeoutMs: number;\n retryAttempts: number;\n}\n\n// Detect provider from URL pattern or explicit override\nexport function detectPostgresProvider(\n url: string,\n explicitProvider?: string\n): PostgresProvider {\n // Explicit override takes priority (if valid)\n if (explicitProvider) {\n const normalized = explicitProvider.toLowerCase();\n if (\n normalized === \"neon\" ||\n normalized === \"supabase\" ||\n normalized === \"standard\"\n ) {\n return normalized;\n }\n }\n\n // Auto-detect from URL hostname\n if (url.includes(\".neon.tech\") || url.includes(\"neon.\")) {\n return \"neon\";\n }\n if (url.includes(\".supabase.\") || url.includes(\"supabase.\")) {\n return \"supabase\";\n }\n\n return \"standard\";\n}\n\n// Get connection defaults for a provider\nexport function getProviderDefaults(\n provider: PostgresProvider\n): ProviderDefaults {\n switch (provider) {\n case \"neon\":\n // Neon: serverless PG with auto-suspend, cold starts need more retries\n return {\n ssl: true,\n poolMax: 5,\n poolMin: 0,\n idleTimeoutMs: 10000,\n connectionTimeoutMs: 20000,\n statementTimeoutMs: 30000,\n retryAttempts: 5,\n };\n case \"supabase\":\n // Supabase: managed PG with Supavisor pooler, SSL required\n return {\n ssl: true,\n poolMax: 5,\n poolMin: 0,\n idleTimeoutMs: 30000,\n connectionTimeoutMs: 15000,\n statementTimeoutMs: 15000,\n retryAttempts: 3,\n };\n case \"standard\":\n default:\n // Standard: Docker, self-hosted, direct PG connections\n return {\n ssl: false,\n poolMax: 10,\n poolMin: 0,\n idleTimeoutMs: 30000,\n connectionTimeoutMs: 15000,\n statementTimeoutMs: 15000,\n retryAttempts: 3,\n };\n }\n}\n","/**\n * @nextlyhq/adapter-postgres\n *\n * PostgreSQL database adapter for Nextly.\n * Extends the base DrizzleAdapter from @nextlyhq/adapter-drizzle to provide\n * PostgreSQL-specific functionality.\n *\n * @remarks\n * This adapter uses the `pg` (node-postgres) driver for database connections\n * and integrates with Drizzle ORM for type-safe queries.\n *\n * Features:\n * - Connection pooling via pg.Pool\n * - Full transaction support with savepoints\n * - RETURNING clause support for all CRUD operations\n * - PostgreSQL-specific error classification\n * - JSONB support\n * - Full-text search capabilities\n * - Automatic retry for serialization failures and deadlocks\n *\n * @example\n * Simple usage with connection string:\n * ```typescript\n * import { createPostgresAdapter } from '@nextlyhq/adapter-postgres';\n *\n * const adapter = createPostgresAdapter({\n * url: process.env.DATABASE_URL!,\n * });\n *\n * await adapter.connect();\n * ```\n *\n * @example\n * Full configuration:\n * ```typescript\n * import { createPostgresAdapter } from '@nextlyhq/adapter-postgres';\n *\n * const adapter = createPostgresAdapter({\n * url: process.env.DATABASE_URL!,\n * pool: {\n * min: 2,\n * max: 20,\n * idleTimeoutMs: 30000,\n * },\n * ssl: {\n * rejectUnauthorized: true,\n * },\n * applicationName: 'my-nextly-app',\n * });\n * ```\n *\n * @example\n * Using the adapter class directly:\n * ```typescript\n * import { PostgresAdapter } from '@nextlyhq/adapter-postgres';\n * import type { PostgresAdapterConfig } from '@nextlyhq/adapter-postgres';\n *\n * const config: PostgresAdapterConfig = {\n * url: process.env.DATABASE_URL!,\n * };\n *\n * const adapter = new PostgresAdapter(config);\n * await adapter.connect();\n * ```\n *\n * @packageDocumentation\n */\n\nimport * as net from \"node:net\";\n\nimport { DrizzleAdapter } from \"@nextlyhq/adapter-drizzle\";\n// F17: connect-time DB version check shared across all adapters.\nimport type {\n PostgresAdapterConfig,\n DatabaseCapabilities,\n PoolStats,\n TransactionContext,\n TransactionOptions,\n SqlParam,\n InsertOptions,\n DatabaseError,\n DatabaseErrorKind,\n} from \"@nextlyhq/adapter-drizzle/types\";\nimport {\n createDatabaseError,\n isDatabaseError,\n isApplicationError,\n} from \"@nextlyhq/adapter-drizzle/types\";\nimport { checkDialectVersion } from \"@nextlyhq/adapter-drizzle/version-check\";\nimport type { AnyRelations, SQL } from \"drizzle-orm\";\nimport { drizzle, type NodePgDatabase } from \"drizzle-orm/node-postgres\";\nimport type { PoolClient, PoolConfig } from \"pg\";\nimport { Pool } from \"pg\";\n\nimport {\n detectPostgresProvider,\n getProviderDefaults,\n type PostgresProvider,\n type ProviderDefaults,\n} from \"./provider\";\n\n// Re-export types from @nextlyhq/adapter-drizzle for convenience\nexport type {\n PostgresAdapterConfig,\n DatabaseCapabilities,\n PoolStats,\n TransactionContext,\n TransactionOptions,\n SqlParam,\n // Additional types users might need\n BaseAdapterConfig,\n AdapterLogger,\n PoolConfig as AdapterPoolConfig,\n SslConfig,\n WhereClause,\n WhereCondition,\n WhereOperator,\n SelectOptions,\n InsertOptions,\n UpdateOptions,\n DeleteOptions,\n UpsertOptions,\n OrderBySpec,\n JoinSpec,\n DatabaseError,\n DatabaseErrorKind,\n} from \"@nextlyhq/adapter-drizzle/types\";\n\n/**\n * Package version.\n */\nexport const VERSION = \"0.1.0\";\n\n/**\n * Default pool configuration values.\n *\n * min: 0 – Never eagerly create background connections. This is critical\n * for cold-start recovery (e.g. Neon auto-suspend): after the\n * initial smoke-test connection, the pool won't immediately try\n * to create additional connections that might still fail while\n * the DB is waking up.\n *\n * max: 5 – Conservative default to avoid overwhelming cloud databases\n * (Neon free-tier limit is ~25-30 simultaneous connections).\n * With Next.js spawning up to 7 build workers this keeps the\n * total connection count safely under typical limits.\n */\nconst DEFAULT_POOL_CONFIG = {\n min: 0,\n max: 5,\n idleTimeoutMs: 30000,\n connectionTimeoutMs: 15000,\n};\n\n/**\n * PostgreSQL error codes mapping to DatabaseErrorKind.\n *\n * @see https://www.postgresql.org/docs/current/errcodes-appendix.html\n */\nconst PG_ERROR_CODES: Record<string, DatabaseErrorKind> = {\n // Class 08 - Connection Exception\n \"08000\": \"connection\",\n \"08003\": \"connection\",\n \"08006\": \"connection\",\n \"08001\": \"connection\",\n \"08004\": \"connection\",\n \"08007\": \"connection\",\n \"08P01\": \"connection\",\n\n // Class 23 - Integrity Constraint Violation\n \"23000\": \"constraint\",\n \"23001\": \"constraint\",\n \"23502\": \"not_null_violation\",\n \"23503\": \"foreign_key_violation\",\n \"23505\": \"unique_violation\",\n \"23514\": \"check_violation\",\n \"23P01\": \"constraint\",\n\n // Class 40 - Transaction Rollback\n \"40000\": \"query\",\n \"40001\": \"serialization_failure\",\n \"40002\": \"constraint\",\n \"40003\": \"query\",\n \"40P01\": \"deadlock\",\n\n // Class 57 - Operator Intervention\n \"57014\": \"timeout\",\n \"57P01\": \"connection\",\n \"57P02\": \"connection\",\n \"57P03\": \"connection\",\n};\n\n/**\n * Delay helper for retry logic.\n */\nfunction delay(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n}\n\n// Node 20+ defaults the Happy Eyeballs per-address connection-attempt timeout\n// to 250ms. For consumers connecting from a region with RTT > 250ms to their\n// database (typical transcontinental: Pakistan→Singapore is ~310ms),\n// every TCP attempt is killed mid-handshake and the pg driver reports\n// ETIMEDOUT after exhausting all DNS-resolved addresses — even though the\n// network is fine.\n//\n// We bump the timeout to 5000ms on first adapter connect. This is process-\n// global state but the change is purely permissive (only increases the\n// per-address budget; cannot break anything that worked at 250ms). If the\n// process has already set a higher value, we leave it alone.\n//\n// References:\n// https://nodejs.org/api/net.html#netsetdefaultautoselectfamilyattempttimeoutvalue\n// https://www.rfc-editor.org/rfc/rfc8305\nconst HAPPY_EYEBALLS_MIN_TIMEOUT_MS = 5000;\nlet happyEyeballsTimeoutApplied = false;\nfunction applyHappyEyeballsTimeoutOnce(): void {\n if (happyEyeballsTimeoutApplied) return;\n happyEyeballsTimeoutApplied = true;\n try {\n const current =\n typeof net.getDefaultAutoSelectFamilyAttemptTimeout === \"function\"\n ? net.getDefaultAutoSelectFamilyAttemptTimeout()\n : 0;\n if (current < HAPPY_EYEBALLS_MIN_TIMEOUT_MS) {\n net.setDefaultAutoSelectFamilyAttemptTimeout(\n HAPPY_EYEBALLS_MIN_TIMEOUT_MS\n );\n }\n } catch {\n // Older Node versions (< 20) don't have these APIs. Swallow — the\n // 250ms default they have predates Happy Eyeballs anyway, so they\n // don't suffer this bug.\n }\n}\n\n/**\n * PostgreSQL database adapter for Nextly.\n *\n * @remarks\n * This class extends the base DrizzleAdapter to provide PostgreSQL-specific\n * functionality including:\n *\n * - Connection pooling with pg.Pool\n * - Transaction support with savepoints\n * - PostgreSQL-specific error codes\n * - JSONB and array type support\n * - Full-text search\n * - Automatic retry for serialization failures and deadlocks\n *\n * For most use cases, use the `createPostgresAdapter` factory function\n * instead of instantiating this class directly.\n *\n * @example\n * ```typescript\n * import { PostgresAdapter } from '@nextlyhq/adapter-postgres';\n *\n * const adapter = new PostgresAdapter({\n * url: 'postgres://user:pass@localhost:5432/mydb',\n * });\n *\n * await adapter.connect();\n *\n * // Use the adapter\n * const users = await adapter.select('users', {\n * where: { and: [{ column: 'status', op: '=', value: 'active' }] },\n * });\n *\n * await adapter.disconnect();\n * ```\n *\n * @public\n */\nexport class PostgresAdapter extends DrizzleAdapter {\n // getDrizzle memoization: drizzle v1's constructor builds a relational\n // query builder per table in the relations config (~40 tables), and the\n // service layer resolves an instance on every db access — construct once\n // per relations object (identity-stable: the schema registry caches it\n // and hands out a NEW object on invalidation, which naturally misses\n // this cache and produces a fresh instance).\n private drizzleByRelations = new WeakMap<AnyRelations, unknown>();\n private drizzleBare: unknown;\n\n /**\n * The database dialect - always 'postgresql' for this adapter.\n */\n public readonly dialect = \"postgresql\" as const;\n\n /**\n * Adapter configuration.\n */\n protected readonly config: PostgresAdapterConfig;\n\n /**\n * Connection pool instance.\n */\n private pool: Pool | null = null;\n\n /**\n * Connection state flag.\n */\n private connected = false;\n\n /**\n * Auto-detected provider (Neon, Supabase, or standard).\n * Set during connect() from DATABASE_URL pattern or DB_PROVIDER env var.\n */\n private detectedProvider: PostgresProvider = \"standard\";\n\n /**\n * Provider-specific connection defaults. Applied as fallbacks when\n * user config doesn't specify a value.\n */\n private providerDefaults: ProviderDefaults = getProviderDefaults(\"standard\");\n\n /**\n * Creates a new PostgreSQL adapter instance.\n *\n * @param config - Adapter configuration\n */\n constructor(config: PostgresAdapterConfig) {\n super();\n this.config = config;\n }\n\n /**\n * Establishes a connection to the PostgreSQL database.\n *\n * @remarks\n * This method initializes the connection pool and verifies connectivity\n * by executing a simple query. It is idempotent - calling it multiple\n * times will not create multiple pools.\n *\n * @throws {DatabaseError} If connection fails\n */\n async connect(): Promise<void> {\n // Fix Node 20+ Happy Eyeballs default before constructing the pool —\n // see applyHappyEyeballsTimeoutOnce above.\n applyHappyEyeballsTimeoutOnce();\n\n if (this.connected && this.pool) {\n return;\n }\n\n // Auto-detect provider from URL and apply defaults\n const url = this.config.url || \"\";\n this.detectedProvider = detectPostgresProvider(\n url,\n process.env.DB_PROVIDER\n );\n this.providerDefaults = getProviderDefaults(this.detectedProvider);\n\n // Log detected provider for developer awareness\n if (this.config.logger?.info) {\n const source = process.env.DB_PROVIDER ? \"(explicit)\" : \"(auto-detected)\";\n this.config.logger.info(\n `PostgreSQL provider: ${this.detectedProvider} ${source}`,\n {}\n );\n }\n\n // Node.js network error codes that are safe to retry (transient failures).\n // These cover cloud databases (e.g. Neon) waking from auto-suspend, brief\n // network hiccups, and DNS resolution races during build parallelism.\n const retryableNodeCodes = new Set([\n \"ETIMEDOUT\",\n \"ECONNREFUSED\",\n \"ECONNRESET\",\n \"ENOTFOUND\",\n \"EAI_AGAIN\",\n ]);\n\n // Use provider-specific retry count (Neon needs more for cold starts)\n const maxAttempts = this.providerDefaults.retryAttempts;\n\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n const poolConfig = this.buildPoolConfig();\n this.pool = new Pool(poolConfig);\n\n // Handle pool errors (background errors on idle clients)\n this.pool.on(\"error\", (err: Error) => {\n // Log through adapter logger if configured\n if (this.config.logger?.error) {\n this.config.logger.error(err, {\n context: \"pool_error\",\n message: \"Unexpected error on idle client\",\n });\n }\n // Client is automatically removed from pool, no action needed\n });\n\n // Verify connection with smoke test, then check dialect version.\n // Why: F17 hard-fails at connect on unsupported PG versions (<15.0)\n // so users see a clear upgrade pointer instead of cryptic errors\n // mid-apply later. The version query runs inside the existing retry\n // loop's try block, so transient network failures (Neon cold start,\n // EAI_AGAIN) continue to retry; only confirmed version mismatch\n // surfaces as UnsupportedDialectVersionError and exits the loop.\n const client = await this.pool.connect();\n try {\n await client.query(\"SELECT 1\");\n await checkDialectVersion(client, \"postgresql\", {\n // Why: route any future variant warnings through the adapter's\n // logger. PG has no recognized variants today, but this keeps\n // the integration symmetric with MySQL.\n onWarning: msg => this.config.logger?.warn?.(msg),\n });\n this.connected = true;\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"PostgreSQL connection established\", {\n host: this.config.host ?? \"from URL\",\n database: this.config.database ?? \"from URL\",\n });\n }\n\n return; // Success — exit retry loop\n } finally {\n client.release();\n }\n } catch (error) {\n // Clean up the failed pool before deciding whether to retry\n if (this.pool) {\n await this.pool.end().catch(() => {});\n this.pool = null;\n }\n\n const nodeError = error as { code?: string };\n const isRetryable =\n nodeError.code != null && retryableNodeCodes.has(nodeError.code);\n\n if (isRetryable && attempt < maxAttempts) {\n // Exponential back-off with jitter: ~1 s, ~2 s, ~3 s, ~4 s\n const waitMs = 1000 * attempt;\n const msg = `PostgreSQL connection attempt ${attempt}/${maxAttempts} failed with ${nodeError.code}, retrying in ${waitMs}ms...`;\n if (this.config.logger?.warn) {\n this.config.logger.warn(msg);\n } else {\n console.warn(`[PostgresAdapter] ${msg}`);\n }\n await delay(waitMs);\n continue;\n }\n\n // Non-retryable error or exhausted retries — surface to caller\n throw this.classifyError(error);\n }\n }\n }\n\n /**\n * Closes the database connection and releases all pool resources.\n *\n * @remarks\n * This method is idempotent - calling it multiple times is safe.\n * It waits for all checked-out clients to be returned before shutting down.\n */\n async disconnect(): Promise<void> {\n // Detach the pool FIRST, then drop the memoized drizzle instances —\n // this closes the repopulation window where a concurrent getDrizzle()\n // call during the (async) pool.end() could cache an instance wrapping\n // the closing pool.\n const pool = this.pool;\n this.pool = null;\n this.drizzleBare = undefined;\n this.drizzleByRelations = new WeakMap();\n if (!pool) {\n return;\n }\n\n try {\n await pool.end();\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"PostgreSQL connection closed\");\n }\n } finally {\n this.connected = false;\n }\n }\n\n /**\n * Checks if the adapter is currently connected.\n *\n * @returns True if connected and pool is available\n */\n override isConnected(): boolean {\n return this.connected && this.pool !== null;\n }\n\n /**\n * Executes a raw SQL query.\n *\n * @param sql - SQL statement with $1, $2, ... placeholders\n * @param params - Query parameters\n * @returns Array of result rows\n *\n * @throws {DatabaseError} If query execution fails\n */\n async executeQuery<T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> {\n const pool = this.ensurePool();\n const startTime = Date.now();\n\n // Transient network errors that are safe to retry at the query level.\n // These occur when Neon auto-suspends between the pool smoke-test and the\n // actual query, or when multiple build workers compete for cold-start.\n const retryableNodeCodes = new Set([\n \"ETIMEDOUT\",\n \"ECONNRESET\",\n \"ECONNREFUSED\",\n ]);\n const maxQueryAttempts = 3;\n\n for (let attempt = 1; attempt <= maxQueryAttempts; attempt++) {\n try {\n const result = await pool.query(sql, params as unknown[]);\n\n // Log query if logger configured\n if (this.config.logger?.query) {\n const durationMs = Date.now() - startTime;\n this.config.logger.query(sql, params, durationMs);\n }\n\n return result.rows as T[];\n } catch (error) {\n const nodeError = error as { code?: string };\n const isRetryable =\n nodeError.code != null && retryableNodeCodes.has(nodeError.code);\n\n if (isRetryable && attempt < maxQueryAttempts) {\n const waitMs = 500 * attempt;\n console.warn(\n `[PostgresAdapter] Query attempt ${attempt}/${maxQueryAttempts} failed with ${nodeError.code}, retrying in ${waitMs}ms...`\n );\n await delay(waitMs);\n continue;\n }\n\n throw this.classifyError(error, sql);\n }\n }\n\n // Unreachable — loop always returns or throws\n throw this.classifyError(new Error(\"executeQuery: exhausted retries\"));\n }\n\n /**\n * Executes a callback within a database transaction.\n *\n * @remarks\n * PostgreSQL supports full ACID transactions with savepoints.\n * If the callback throws, the transaction is rolled back.\n *\n * Supports automatic retry for serialization failures (40001) and\n * deadlocks (40P01) when `retryCount` is specified in options.\n *\n * An error carrying the Nextly application brand is neither retried nor\n * classified: it is the application refusing the write, so it arrives at the\n * caller as it was thrown. Retrying it would repeat work whose verdict has\n * already been given. Every other error, including an unbranded one raised by\n * the callback, is retried when it is a serialization failure or deadlock and\n * classified on the way out.\n *\n * @param callback - Function to execute within the transaction\n * @param options - Transaction options (isolation level, timeout, retry)\n * @returns The result of the callback\n *\n * @throws {DatabaseError} If the transaction itself fails after all retries,\n * or if the callback threw an error that does not carry the application brand\n * @throws The callback's own error, unchanged, when it carries that brand\n */\n async transaction<T>(\n callback: (ctx: TransactionContext) => Promise<T>,\n options?: TransactionOptions\n ): Promise<T> {\n const pool = this.ensurePool();\n const maxAttempts = (options?.retryCount ?? 0) + 1;\n const retryDelayMs = options?.retryDelayMs ?? 100;\n\n let lastError: unknown;\n\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n const client = await pool.connect();\n const startTime = Date.now();\n\n try {\n // Begin transaction with options\n await this.beginTransaction(client, options);\n\n // Create transaction context\n const ctx = this.createTransactionContext(client);\n\n // Execute callback\n const result = await callback(ctx);\n\n // Commit transaction\n await client.query(\"COMMIT\");\n\n // Log success\n if (this.config.logger?.debug) {\n const durationMs = Date.now() - startTime;\n this.config.logger.debug(\"Transaction committed\", {\n attempt,\n durationMs,\n });\n }\n\n return result;\n } catch (error) {\n // Rollback transaction\n await client.query(\"ROLLBACK\").catch(() => {});\n\n lastError = error;\n\n // Work inside a transaction may throw to roll the write back — a\n // refused value, a denied permission — and that is the application's\n // verdict, not the driver's failure. Rethrown before the retry check as\n // well as before classification: a refusal re-run is the same refusal,\n // and the caller must receive the code and payload it raised rather\n // than a generic database error with the detail stripped out.\n if (isApplicationError(error)) throw error;\n\n // Check if error is retryable\n const pgError = error as { code?: string };\n const isRetryable =\n pgError.code === \"40001\" || // serialization_failure\n pgError.code === \"40P01\"; // deadlock\n\n if (isRetryable && attempt < maxAttempts) {\n if (this.config.logger?.warn) {\n this.config.logger.warn(\n `Transaction failed with ${pgError.code}, retrying (${attempt}/${maxAttempts})`,\n { code: pgError.code, attempt }\n );\n }\n await delay(retryDelayMs * attempt); // Exponential backoff\n continue;\n }\n\n throw this.classifyError(error);\n } finally {\n client.release();\n }\n }\n\n // Should not reach here, but handle just in case\n throw this.classifyError(lastError);\n }\n\n /**\n * Returns the database capabilities for PostgreSQL.\n *\n * @remarks\n * PostgreSQL has the most comprehensive feature set of all supported\n * databases, including JSONB, arrays, full-text search, and more.\n */\n getCapabilities(): DatabaseCapabilities {\n return {\n dialect: \"postgresql\",\n supportsJsonb: true,\n supportsJson: true,\n supportsArrays: true,\n supportsGeneratedColumns: true,\n supportsFts: true,\n supportsIlike: true,\n supportsReturning: true,\n supportsSavepoints: true,\n supportsOnConflict: true,\n maxParamsPerQuery: 65535, // PostgreSQL limit\n maxIdentifierLength: 63, // PostgreSQL default\n };\n }\n\n /**\n * Returns connection pool statistics.\n *\n * @returns Pool stats or null if not connected\n */\n override getPoolStats(): PoolStats | null {\n if (!this.pool) {\n return null;\n }\n\n return {\n total: this.pool.totalCount,\n idle: this.pool.idleCount,\n waiting: this.pool.waitingCount,\n active: this.pool.totalCount - this.pool.idleCount,\n };\n }\n\n /**\n * Override insertMany for bulk insert optimization.\n *\n * @remarks\n * Uses a single multi-row INSERT statement for better performance\n * when inserting multiple records.\n */\n override async insertMany<T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> {\n if (data.length === 0) {\n return [];\n }\n\n // For single record, use parent implementation\n if (data.length === 1) {\n const result = await this.insert<T>(table, data[0], options);\n return [result];\n }\n\n // Build multi-row INSERT\n const columns = Object.keys(data[0]);\n const params: SqlParam[] = [];\n const valuesClauses: string[] = [];\n\n for (let i = 0; i < data.length; i++) {\n const record = data[i];\n const placeholders: string[] = [];\n\n for (const col of columns) {\n params.push(record[col] as SqlParam);\n placeholders.push(`$${params.length}`);\n }\n\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n const columnList = columns\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columnList}) VALUES ${valuesClauses.join(\", \")}`;\n\n // Add RETURNING clause\n if (options?.returning) {\n const returning =\n options.returning === \"*\"\n ? \"*\"\n : options.returning.map(col => this.escapeIdentifier(col)).join(\", \");\n sql += ` RETURNING ${returning}`;\n }\n\n try {\n return await this.executeQuery<T>(sql, params);\n } catch (error) {\n throw this.handleQueryError(error, \"insertMany\", table);\n }\n }\n\n // ============================================================\n // Protected Helper Methods\n // ============================================================\n\n /**\n * Ensures pool is connected and returns it.\n *\n * @throws {DatabaseError} If not connected\n */\n private ensurePool(): Pool {\n if (!this.pool) {\n throw createDatabaseError({\n kind: \"connection\",\n message: \"PostgresAdapter is not connected. Call connect() first.\",\n });\n }\n return this.pool;\n }\n\n /**\n * Return the typed Drizzle instance for PostgreSQL.\n * Guarded for server-only usage and requires an active connection.\n *\n * @param schema - Optional schema for relational queries (db.query.*)\n * @returns Drizzle ORM instance wrapping the pg pool connection\n * @throws {Error} If called in browser or not connected\n */\n getDrizzle<T = NodePgDatabase<AnyRelations>>(relations?: AnyRelations): T {\n if (typeof window !== \"undefined\") {\n throw new Error(\"getDrizzle() is server-only\");\n }\n const pool = this.ensurePool();\n // drizzle v1 removed the positional (client, config) form — the object\n // form is REQUIRED (positional silently treats the client as config).\n // `relations` (defineRelations output, assembled by Nextly's schema\n // registry) is what powers db.query in v1; a bare instance serves the\n // select-builder CRUD paths.\n if (!relations) {\n this.drizzleBare ??= drizzle({ client: pool });\n return this.drizzleBare as T;\n }\n let cached = this.drizzleByRelations.get(relations);\n if (!cached) {\n cached = drizzle({ client: pool, relations });\n this.drizzleByRelations.set(relations, cached);\n }\n return cached as T;\n }\n\n /**\n * Builds pg Pool configuration from adapter config.\n */\n private buildPoolConfig(): PoolConfig {\n const config: PoolConfig = {};\n\n // Connection string or explicit options\n if (this.config.url) {\n config.connectionString = this.config.url;\n } else {\n if (this.config.host) config.host = this.config.host;\n if (this.config.port) config.port = this.config.port;\n if (this.config.database) config.database = this.config.database;\n if (this.config.user) config.user = this.config.user;\n if (this.config.password) config.password = this.config.password;\n }\n\n // Pool settings (user config > provider defaults > hardcoded defaults)\n config.min =\n this.config.pool?.min ??\n this.providerDefaults.poolMin ??\n DEFAULT_POOL_CONFIG.min;\n config.max =\n this.config.pool?.max ??\n this.providerDefaults.poolMax ??\n DEFAULT_POOL_CONFIG.max;\n config.idleTimeoutMillis =\n this.config.pool?.idleTimeoutMs ??\n this.providerDefaults.idleTimeoutMs ??\n DEFAULT_POOL_CONFIG.idleTimeoutMs;\n config.connectionTimeoutMillis =\n this.config.pool?.connectionTimeoutMs ??\n this.providerDefaults.connectionTimeoutMs ??\n DEFAULT_POOL_CONFIG.connectionTimeoutMs;\n\n // TCP keepalive - prevents cloud databases (e.g. Neon) from silently\n // dropping idle connections between the pool smoke-test and the first\n // real query, which manifests as ETIMEDOUT on the query itself.\n config.keepAlive = true;\n config.keepAliveInitialDelayMillis = 10000;\n\n // SSL configuration (user config > provider default)\n if (this.config.ssl) {\n if (typeof this.config.ssl === \"boolean\") {\n config.ssl = this.config.ssl;\n } else {\n if (this.config.ssl.rejectUnauthorized === false) {\n console.warn(\n \"[nextly/adapter-postgres] ssl.rejectUnauthorized is set to false — \" +\n \"TLS certificates will not be validated. This is unsafe on untrusted networks. \" +\n \"Provide a trusted `ca` cert instead, or remove the rejectUnauthorized override.\"\n );\n }\n config.ssl = {\n rejectUnauthorized: this.config.ssl.rejectUnauthorized,\n ca: this.config.ssl.ca,\n cert: this.config.ssl.cert,\n key: this.config.ssl.key,\n };\n }\n } else if (this.providerDefaults.ssl) {\n // Provider requires SSL but user didn't explicitly configure it.\n // Default to validating certs; the user can opt out by setting\n // `ssl: { rejectUnauthorized: false }` explicitly (which logs a warning above).\n config.ssl = { rejectUnauthorized: true };\n }\n\n // PostgreSQL-specific options\n if (this.config.applicationName) {\n config.application_name = this.config.applicationName;\n }\n\n // User config beats provider default; both beat \"unset\".\n // Why: provider.ts:58 declared a Neon statementTimeoutMs default but the\n // adapter previously ignored it, leaving stuck queries to pin pool slots\n // indefinitely. Wire the fallback chain here so the declared defaults\n // actually fire.\n const effectiveStatementTimeout =\n this.config.statementTimeout ?? this.providerDefaults.statementTimeoutMs;\n if (effectiveStatementTimeout !== undefined) {\n config.statement_timeout = effectiveStatementTimeout;\n }\n\n if (this.config.queryTimeout) {\n config.query_timeout = this.config.queryTimeout;\n }\n\n return config;\n }\n\n /**\n * Begins a transaction with the specified options.\n */\n private async beginTransaction(\n client: PoolClient,\n options?: TransactionOptions\n ): Promise<void> {\n let beginSql = \"BEGIN\";\n\n // Add isolation level if specified\n if (options?.isolationLevel) {\n const isolationMap: Record<string, string> = {\n \"read uncommitted\": \"READ UNCOMMITTED\",\n \"read committed\": \"READ COMMITTED\",\n \"repeatable read\": \"REPEATABLE READ\",\n serializable: \"SERIALIZABLE\",\n };\n const level = isolationMap[options.isolationLevel];\n if (level) {\n beginSql += ` ISOLATION LEVEL ${level}`;\n }\n }\n\n // Add read-only mode if specified\n if (options?.readOnly) {\n beginSql += \" READ ONLY\";\n }\n\n await client.query(beginSql);\n\n // Set statement timeout if specified\n if (options?.timeoutMs) {\n await client.query(`SET LOCAL statement_timeout = ${options.timeoutMs}`);\n }\n }\n\n /**\n * Creates a TransactionContext for the given client.\n */\n private createTransactionContext(client: PoolClient): TransactionContext {\n // Bind a Drizzle instance to this transaction's checked-out client so the\n // delegated CRUD methods run inside the transaction and see its uncommitted\n // rows. getDrizzle() wraps the pool, which would use a different connection.\n // Built lazily and memoized: transactions that use only raw execute/insert\n // never construct it.\n const buildTxExecutor = () => drizzle({ client });\n let txExecutor: ReturnType<typeof buildTxExecutor> | undefined;\n const txDb = () => (txExecutor ??= buildTxExecutor());\n return {\n execute: async <T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> => {\n const result = await client.query(sql, params as unknown[]);\n return result.rows as T[];\n },\n\n // Run on the transaction-bound Drizzle instance rather than the pool, so\n // the statement is part of this transaction and sees its uncommitted rows.\n runStatement: async (statement: SQL): Promise<void> => {\n await txDb().execute(statement);\n },\n\n // node-postgres answers `{ rows }`; the transaction-bound instance keeps\n // the read inside this transaction so it sees its uncommitted writes.\n queryStatement: async <T = Record<string, unknown>>(\n statement: SQL\n ): Promise<T[]> => {\n const result = await txDb().execute(statement);\n return result.rows as T[];\n },\n\n lockRow: async (table: string, id: SqlParam): Promise<void> => {\n const idColumn = this.escapeIdentifier(\"id\");\n await client.query(\n `SELECT ${idColumn} FROM ${this.escapeIdentifier(table)} ` +\n `WHERE ${idColumn} = $1 FOR UPDATE`,\n [id] as unknown[]\n );\n },\n\n insert: async <T = unknown>(\n table: string,\n data: Record<string, unknown>,\n options?: InsertOptions\n ): Promise<T> => {\n const mapped = this.mapRowToRawSql(this.getTableObject(table), data);\n const columns = Object.keys(mapped);\n const values = Object.values(mapped);\n const placeholders = values.map((_, i) => `$${i + 1}`).join(\", \");\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES (${placeholders})`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n const tableObj = this.getTableObject(table);\n // Each returned timestamp's wall clock, spelled out by the database.\n // node-postgres turns one into a `Date` in the LOCAL zone before this\n // code sees it, and that conversion cannot be undone: a wall clock\n // inside a daylight-saving gap is normalized away.\n const aliases = returningEmpty\n ? []\n : this.dateWallClockAliases(tableObj, ret ?? \"*\");\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(tableObj, ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n const spelled = aliases\n .map(\n a =>\n `to_char(${this.escapeIdentifier(a.sqlName)}, 'YYYY-MM-DD\"T\"HH24:MI:SS.MS') AS ${this.escapeIdentifier(a.alias)}`\n )\n .join(\", \");\n sql += ` RETURNING ${returning}${spelled ? `, ${spelled}` : \"\"}`;\n }\n\n const result = await client.query(sql, values);\n // Return JS property-named keys AND dates read as the UTC the statement\n // wrote, to match the non-transactional insert.\n return this.mapRowFromRawSql(tableObj, result.rows[0] as T, aliases);\n },\n\n insertMany: async <T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> => {\n if (data.length === 0) return [];\n\n const tableObj = this.getTableObject(table);\n const mappedRecords = data.map(r => this.mapRowToRawSql(tableObj, r));\n const columns = Object.keys(mappedRecords[0]);\n const params: unknown[] = [];\n const valuesClauses: string[] = [];\n\n for (const record of mappedRecords) {\n const placeholders: string[] = [];\n for (const col of columns) {\n params.push(record[col]);\n placeholders.push(`$${params.length}`);\n }\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES ${valuesClauses.join(\", \")}`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n const aliases = returningEmpty\n ? []\n : this.dateWallClockAliases(tableObj, ret ?? \"*\");\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(tableObj, ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n const spelled = aliases\n .map(\n a =>\n `to_char(${this.escapeIdentifier(a.sqlName)}, 'YYYY-MM-DD\"T\"HH24:MI:SS.MS') AS ${this.escapeIdentifier(a.alias)}`\n )\n .join(\", \");\n sql += ` RETURNING ${returning}${spelled ? `, ${spelled}` : \"\"}`;\n }\n\n const result = await client.query(sql, params);\n return (result.rows as T[]).map(r =>\n this.mapRowFromRawSql(tableObj, r, aliases)\n );\n },\n\n ...this.createTransactionForwarders(txDb),\n\n savepoint: async (name: string): Promise<void> => {\n await client.query(`SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n rollbackToSavepoint: async (name: string): Promise<void> => {\n await client.query(\n `ROLLBACK TO SAVEPOINT ${this.escapeIdentifier(name)}`\n );\n },\n\n releaseSavepoint: async (name: string): Promise<void> => {\n await client.query(`RELEASE SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n };\n }\n\n /**\n * Classifies a PostgreSQL error into a DatabaseError.\n *\n * @param error - Original error from pg\n * @param sql - SQL statement that caused the error (optional)\n * @returns DatabaseError with proper classification\n */\n protected override classifyError(\n error: unknown,\n sql?: string\n ): DatabaseError {\n // Why short-circuit on existing DatabaseError: F17's\n // UnsupportedDialectVersionError is already a typed DatabaseError with\n // kind: \"unsupported_version\" plus detectedVersion/requiredVersion\n // fields. Re-wrapping it here would erase those fields and re-tag it\n // as kind: \"unknown\".\n if (isDatabaseError(error)) return error;\n\n const pgError = error as {\n code?: string;\n message?: string;\n constraint?: string;\n table?: string;\n column?: string;\n detail?: string;\n hint?: string;\n severity?: string;\n };\n\n // Determine error kind from PostgreSQL error code\n const kind: DatabaseErrorKind =\n (pgError.code && PG_ERROR_CODES[pgError.code]) || \"unknown\";\n\n // Build error message\n let message = pgError.message ?? String(error);\n if (sql && kind === \"query\") {\n message = `Query failed: ${message}`;\n }\n\n return createDatabaseError({\n kind,\n message,\n code: pgError.code,\n constraint: pgError.constraint,\n table: pgError.table,\n column: pgError.column,\n detail: pgError.detail,\n hint: pgError.hint,\n cause: error instanceof Error ? error : undefined,\n });\n }\n}\n\n/**\n * Creates a PostgreSQL database adapter instance.\n *\n * @remarks\n * This is the recommended way to create a PostgreSQL adapter.\n * The adapter is not connected after creation - call `connect()` to\n * establish the database connection.\n *\n * @param config - Adapter configuration\n * @returns A new PostgresAdapter instance\n *\n * @example\n * Simple usage:\n * ```typescript\n * import { createPostgresAdapter } from '@nextlyhq/adapter-postgres';\n *\n * const adapter = createPostgresAdapter({\n * url: process.env.DATABASE_URL!,\n * });\n *\n * await adapter.connect();\n * ```\n *\n * @example\n * With full configuration:\n * ```typescript\n * const adapter = createPostgresAdapter({\n * url: process.env.DATABASE_URL!,\n * pool: {\n * min: 5,\n * max: 20,\n * idleTimeoutMs: 30000,\n * connectionTimeoutMs: 10000,\n * },\n * ssl: {\n * rejectUnauthorized: true,\n * ca: process.env.CA_CERT,\n * },\n * applicationName: 'my-app',\n * statementTimeout: 30000,\n * });\n * ```\n *\n * @public\n */\nexport function createPostgresAdapter(\n config: PostgresAdapterConfig\n): PostgresAdapter {\n return new PostgresAdapter(config);\n}\n\n/**\n * Type guard to check if a value is a PostgresAdapter instance.\n *\n * @param value - Value to check\n * @returns True if value is a PostgresAdapter\n *\n * @example\n * ```typescript\n * import { isPostgresAdapter } from '@nextlyhq/adapter-postgres';\n *\n * if (isPostgresAdapter(adapter)) {\n * // TypeScript knows adapter is PostgresAdapter\n * console.log(adapter.dialect); // 'postgresql'\n * }\n * ```\n *\n * @public\n */\nexport function isPostgresAdapter(value: unknown): value is PostgresAdapter {\n return value instanceof PostgresAdapter;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/provider.ts","../src/index.ts"],"names":["assertSchemaName","net","DrizzleAdapter","Pool","checkDialectVersion","isApplicationError","createDatabaseError","dr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QAAQ,OAAA,CAAQ,MAAA;AAAA,MAChB,MAAM,OAAA,CAAQ,IAAA;AAAA,MACd,KAAA,EAAO,KAAA,YAAiB,KAAA,GAAQ,KAAA,GAAQ;AAAA,KACzC,CAAA;AAAA,EACH;AACF;AA+CO,SAAS,sBACd,MAAA,EACiB;AACjB,EAAA,OAAO,IAAI,gBAAgB,MAAM,CAAA;AACnC;AAoBO,SAAS,kBAAkB,KAAA,EAA0C;AAC1E,EAAA,OAAO,KAAA,YAAiB,eAAA;AAC1B","file":"index.cjs","sourcesContent":["// Auto-detects PostgreSQL provider from DATABASE_URL pattern\n// and returns appropriate connection defaults.\n// Standard pg driver works with all providers - only config changes.\n\nexport type PostgresProvider = \"standard\" | \"neon\" | \"supabase\";\n\nexport interface ProviderDefaults {\n ssl: boolean;\n poolMax: number;\n poolMin: number;\n idleTimeoutMs: number;\n connectionTimeoutMs: number;\n statementTimeoutMs: number;\n retryAttempts: number;\n}\n\n// Detect provider from URL pattern or explicit override\nexport function detectPostgresProvider(\n url: string,\n explicitProvider?: string\n): PostgresProvider {\n // Explicit override takes priority (if valid)\n if (explicitProvider) {\n const normalized = explicitProvider.toLowerCase();\n if (\n normalized === \"neon\" ||\n normalized === \"supabase\" ||\n normalized === \"standard\"\n ) {\n return normalized;\n }\n }\n\n // Auto-detect from URL hostname\n if (url.includes(\".neon.tech\") || url.includes(\"neon.\")) {\n return \"neon\";\n }\n if (url.includes(\".supabase.\") || url.includes(\"supabase.\")) {\n return \"supabase\";\n }\n\n return \"standard\";\n}\n\n// Get connection defaults for a provider\nexport function getProviderDefaults(\n provider: PostgresProvider\n): ProviderDefaults {\n switch (provider) {\n case \"neon\":\n // Neon: serverless PG with auto-suspend, cold starts need more retries\n return {\n ssl: true,\n poolMax: 5,\n poolMin: 0,\n idleTimeoutMs: 10000,\n connectionTimeoutMs: 20000,\n statementTimeoutMs: 30000,\n retryAttempts: 5,\n };\n case \"supabase\":\n // Supabase: managed PG with Supavisor pooler, SSL required\n return {\n ssl: true,\n poolMax: 5,\n poolMin: 0,\n idleTimeoutMs: 30000,\n connectionTimeoutMs: 15000,\n statementTimeoutMs: 15000,\n retryAttempts: 3,\n };\n case \"standard\":\n default:\n // Standard: Docker, self-hosted, direct PG connections\n return {\n ssl: false,\n poolMax: 10,\n poolMin: 0,\n idleTimeoutMs: 30000,\n connectionTimeoutMs: 15000,\n statementTimeoutMs: 15000,\n retryAttempts: 3,\n };\n }\n}\n","/**\n * @nextlyhq/adapter-postgres\n *\n * PostgreSQL database adapter for Nextly.\n * Extends the base DrizzleAdapter from @nextlyhq/adapter-drizzle to provide\n * PostgreSQL-specific functionality.\n *\n * @remarks\n * This adapter uses the `pg` (node-postgres) driver for database connections\n * and integrates with Drizzle ORM for type-safe queries.\n *\n * Features:\n * - Connection pooling via pg.Pool\n * - Full transaction support with savepoints\n * - RETURNING clause support for all CRUD operations\n * - PostgreSQL-specific error classification\n * - JSONB support\n * - Full-text search capabilities\n * - Automatic retry for serialization failures and deadlocks\n *\n * @example\n * Simple usage with connection string:\n * ```typescript\n * import { createPostgresAdapter } from '@nextlyhq/adapter-postgres';\n *\n * const adapter = createPostgresAdapter({\n * url: process.env.DATABASE_URL!,\n * });\n *\n * await adapter.connect();\n * ```\n *\n * @example\n * Full configuration:\n * ```typescript\n * import { createPostgresAdapter } from '@nextlyhq/adapter-postgres';\n *\n * const adapter = createPostgresAdapter({\n * url: process.env.DATABASE_URL!,\n * pool: {\n * min: 2,\n * max: 20,\n * idleTimeoutMs: 30000,\n * },\n * ssl: {\n * rejectUnauthorized: true,\n * },\n * applicationName: 'my-nextly-app',\n * });\n * ```\n *\n * @example\n * Using the adapter class directly:\n * ```typescript\n * import { PostgresAdapter } from '@nextlyhq/adapter-postgres';\n * import type { PostgresAdapterConfig } from '@nextlyhq/adapter-postgres';\n *\n * const config: PostgresAdapterConfig = {\n * url: process.env.DATABASE_URL!,\n * };\n *\n * const adapter = new PostgresAdapter(config);\n * await adapter.connect();\n * ```\n *\n * @packageDocumentation\n */\n\nimport * as net from \"node:net\";\n\nimport { DrizzleAdapter } from \"@nextlyhq/adapter-drizzle\";\n// F17: connect-time DB version check shared across all adapters.\nimport { assertSchemaName } from \"@nextlyhq/adapter-drizzle/schema-name\";\nimport type {\n PostgresAdapterConfig,\n DatabaseCapabilities,\n PoolStats,\n TransactionContext,\n TransactionOptions,\n SqlParam,\n InsertOptions,\n DatabaseError,\n DatabaseErrorKind,\n} from \"@nextlyhq/adapter-drizzle/types\";\nimport {\n createDatabaseError,\n isDatabaseError,\n isApplicationError,\n} from \"@nextlyhq/adapter-drizzle/types\";\nimport { checkDialectVersion } from \"@nextlyhq/adapter-drizzle/version-check\";\nimport type { AnyRelations, SQL } from \"drizzle-orm\";\nimport { drizzle, type NodePgDatabase } from \"drizzle-orm/node-postgres\";\nimport { getTableConfig, type PgTable } from \"drizzle-orm/pg-core\";\nimport type { PoolClient, PoolConfig } from \"pg\";\nimport { Pool } from \"pg\";\n\nimport {\n detectPostgresProvider,\n getProviderDefaults,\n type PostgresProvider,\n type ProviderDefaults,\n} from \"./provider\";\n\n// Re-export types from @nextlyhq/adapter-drizzle for convenience\nexport type {\n PostgresAdapterConfig,\n DatabaseCapabilities,\n PoolStats,\n TransactionContext,\n TransactionOptions,\n SqlParam,\n // Additional types users might need\n BaseAdapterConfig,\n AdapterLogger,\n PoolConfig as AdapterPoolConfig,\n SslConfig,\n WhereClause,\n WhereCondition,\n WhereOperator,\n SelectOptions,\n InsertOptions,\n UpdateOptions,\n DeleteOptions,\n UpsertOptions,\n OrderBySpec,\n JoinSpec,\n DatabaseError,\n DatabaseErrorKind,\n} from \"@nextlyhq/adapter-drizzle/types\";\n\n/**\n * Package version.\n */\nexport const VERSION = \"0.1.0\";\n\n/**\n * Default pool configuration values.\n *\n * min: 0 – Never eagerly create background connections. This is critical\n * for cold-start recovery (e.g. Neon auto-suspend): after the\n * initial smoke-test connection, the pool won't immediately try\n * to create additional connections that might still fail while\n * the DB is waking up.\n *\n * max: 5 – Conservative default to avoid overwhelming cloud databases\n * (Neon free-tier limit is ~25-30 simultaneous connections).\n * With Next.js spawning up to 7 build workers this keeps the\n * total connection count safely under typical limits.\n */\nconst DEFAULT_POOL_CONFIG = {\n min: 0,\n max: 5,\n idleTimeoutMs: 30000,\n connectionTimeoutMs: 15000,\n};\n\n/**\n * PostgreSQL error codes mapping to DatabaseErrorKind.\n *\n * @see https://www.postgresql.org/docs/current/errcodes-appendix.html\n */\nconst PG_ERROR_CODES: Record<string, DatabaseErrorKind> = {\n // Class 08 - Connection Exception\n \"08000\": \"connection\",\n \"08003\": \"connection\",\n \"08006\": \"connection\",\n \"08001\": \"connection\",\n \"08004\": \"connection\",\n \"08007\": \"connection\",\n \"08P01\": \"connection\",\n\n // Class 23 - Integrity Constraint Violation\n \"23000\": \"constraint\",\n \"23001\": \"constraint\",\n \"23502\": \"not_null_violation\",\n \"23503\": \"foreign_key_violation\",\n \"23505\": \"unique_violation\",\n \"23514\": \"check_violation\",\n \"23P01\": \"constraint\",\n\n // Class 40 - Transaction Rollback\n \"40000\": \"query\",\n \"40001\": \"serialization_failure\",\n \"40002\": \"constraint\",\n \"40003\": \"query\",\n \"40P01\": \"deadlock\",\n\n // Class 57 - Operator Intervention\n \"57014\": \"timeout\",\n \"57P01\": \"connection\",\n \"57P02\": \"connection\",\n \"57P03\": \"connection\",\n};\n\n/**\n * Create the configured schema when the database does not have it yet.\n *\n * Looked up before it is created, because `CREATE SCHEMA IF NOT EXISTS` is not\n * a no-op for a schema that exists: PostgreSQL checks the database-level\n * `CREATE` privilege before it checks for the schema, so a role that may use\n * and create objects in `public` but may not create schemas would fail to\n * connect over a schema that is already there. Reading `pg_namespace` needs no\n * privilege, so only a schema that is genuinely missing asks for one.\n */\nasync function ensureSchemaExists(\n client: PoolClient,\n schema: string\n): Promise<void> {\n const name = assertSchemaName(schema);\n const existing = await client.query(\n \"SELECT 1 FROM pg_namespace WHERE nspname = $1\",\n [name]\n );\n if (existing.rows.length > 0) return;\n // Still `IF NOT EXISTS`: another process connecting at the same moment may\n // create it between the lookup and this statement.\n await client.query(`CREATE SCHEMA IF NOT EXISTS ${name}`);\n}\n\n/**\n * Delay helper for retry logic.\n */\nfunction delay(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n}\n\n// Node 20+ defaults the Happy Eyeballs per-address connection-attempt timeout\n// to 250ms. For consumers connecting from a region with RTT > 250ms to their\n// database (typical transcontinental: Pakistan→Singapore is ~310ms),\n// every TCP attempt is killed mid-handshake and the pg driver reports\n// ETIMEDOUT after exhausting all DNS-resolved addresses — even though the\n// network is fine.\n//\n// We bump the timeout to 5000ms on first adapter connect. This is process-\n// global state but the change is purely permissive (only increases the\n// per-address budget; cannot break anything that worked at 250ms). If the\n// process has already set a higher value, we leave it alone.\n//\n// References:\n// https://nodejs.org/api/net.html#netsetdefaultautoselectfamilyattempttimeoutvalue\n// https://www.rfc-editor.org/rfc/rfc8305\nconst HAPPY_EYEBALLS_MIN_TIMEOUT_MS = 5000;\nlet happyEyeballsTimeoutApplied = false;\nfunction applyHappyEyeballsTimeoutOnce(): void {\n if (happyEyeballsTimeoutApplied) return;\n happyEyeballsTimeoutApplied = true;\n try {\n const current =\n typeof net.getDefaultAutoSelectFamilyAttemptTimeout === \"function\"\n ? net.getDefaultAutoSelectFamilyAttemptTimeout()\n : 0;\n if (current < HAPPY_EYEBALLS_MIN_TIMEOUT_MS) {\n net.setDefaultAutoSelectFamilyAttemptTimeout(\n HAPPY_EYEBALLS_MIN_TIMEOUT_MS\n );\n }\n } catch {\n // Older Node versions (< 20) don't have these APIs. Swallow — the\n // 250ms default they have predates Happy Eyeballs anyway, so they\n // don't suffer this bug.\n }\n}\n\n/**\n * How a date column's wall clock is spelled out when a write reads its row\n * back: the stored value to the millisecond, with no zone for the driver to\n * shift.\n */\nconst PG_WALL_CLOCK_FORMAT = 'YYYY-MM-DD\"T\"HH24:MI:SS.MS';\n\n/**\n * PostgreSQL database adapter for Nextly.\n *\n * @remarks\n * This class extends the base DrizzleAdapter to provide PostgreSQL-specific\n * functionality including:\n *\n * - Connection pooling with pg.Pool\n * - Transaction support with savepoints\n * - PostgreSQL-specific error codes\n * - JSONB and array type support\n * - Full-text search\n * - Automatic retry for serialization failures and deadlocks\n *\n * For most use cases, use the `createPostgresAdapter` factory function\n * instead of instantiating this class directly.\n *\n * @example\n * ```typescript\n * import { PostgresAdapter } from '@nextlyhq/adapter-postgres';\n *\n * const adapter = new PostgresAdapter({\n * url: 'postgres://user:pass@localhost:5432/mydb',\n * });\n *\n * await adapter.connect();\n *\n * // Use the adapter\n * const users = await adapter.select('users', {\n * where: { and: [{ column: 'status', op: '=', value: 'active' }] },\n * });\n *\n * await adapter.disconnect();\n * ```\n *\n * @public\n */\nexport class PostgresAdapter extends DrizzleAdapter {\n // getDrizzle memoization: drizzle v1's constructor builds a relational\n // query builder per table in the relations config (~40 tables), and the\n // service layer resolves an instance on every db access — construct once\n // per relations object (identity-stable: the schema registry caches it\n // and hands out a NEW object on invalidation, which naturally misses\n // this cache and produces a fresh instance).\n private drizzleByRelations = new WeakMap<AnyRelations, unknown>();\n private drizzleBare: unknown;\n\n /**\n * The database dialect - always 'postgresql' for this adapter.\n */\n public readonly dialect = \"postgresql\" as const;\n\n /**\n * Adapter configuration.\n */\n protected readonly config: PostgresAdapterConfig;\n\n /**\n * Connection pool instance.\n */\n private pool: Pool | null = null;\n\n /**\n * Connection state flag.\n */\n private connected = false;\n\n /**\n * Auto-detected provider (Neon, Supabase, or standard).\n * Set during connect() from DATABASE_URL pattern or DB_PROVIDER env var.\n */\n private detectedProvider: PostgresProvider = \"standard\";\n\n /**\n * Provider-specific connection defaults. Applied as fallbacks when\n * user config doesn't specify a value.\n */\n private providerDefaults: ProviderDefaults = getProviderDefaults(\"standard\");\n\n /**\n * Creates a new PostgreSQL adapter instance.\n *\n * @param config - Adapter configuration\n */\n constructor(config: PostgresAdapterConfig) {\n super();\n this.config = config;\n }\n\n /**\n * Establishes a connection to the PostgreSQL database.\n *\n * @remarks\n * This method initializes the connection pool and verifies connectivity\n * by executing a simple query. It is idempotent - calling it multiple\n * times will not create multiple pools.\n *\n * @throws {DatabaseError} If connection fails\n */\n async connect(): Promise<void> {\n // Fix Node 20+ Happy Eyeballs default before constructing the pool —\n // see applyHappyEyeballsTimeoutOnce above.\n applyHappyEyeballsTimeoutOnce();\n\n if (this.connected && this.pool) {\n return;\n }\n\n // Auto-detect provider from URL and apply defaults\n const url = this.config.url || \"\";\n this.detectedProvider = detectPostgresProvider(\n url,\n process.env.DB_PROVIDER\n );\n this.providerDefaults = getProviderDefaults(this.detectedProvider);\n\n // Log detected provider for developer awareness\n if (this.config.logger?.info) {\n const source = process.env.DB_PROVIDER ? \"(explicit)\" : \"(auto-detected)\";\n this.config.logger.info(\n `PostgreSQL provider: ${this.detectedProvider} ${source}`,\n {}\n );\n }\n\n // Node.js network error codes that are safe to retry (transient failures).\n // These cover cloud databases (e.g. Neon) waking from auto-suspend, brief\n // network hiccups, and DNS resolution races during build parallelism.\n const retryableNodeCodes = new Set([\n \"ETIMEDOUT\",\n \"ECONNREFUSED\",\n \"ECONNRESET\",\n \"ENOTFOUND\",\n \"EAI_AGAIN\",\n ]);\n\n // Use provider-specific retry count (Neon needs more for cold starts)\n const maxAttempts = this.providerDefaults.retryAttempts;\n\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n const poolConfig = this.buildPoolConfig();\n this.pool = new Pool(poolConfig);\n\n // Handle pool errors (background errors on idle clients)\n this.pool.on(\"error\", (err: Error) => {\n // Log through adapter logger if configured\n if (this.config.logger?.error) {\n this.config.logger.error(err, {\n context: \"pool_error\",\n message: \"Unexpected error on idle client\",\n });\n }\n // Client is automatically removed from pool, no action needed\n });\n\n // Verify connection with smoke test, then check dialect version.\n // Why: F17 hard-fails at connect on unsupported PG versions (<15.0)\n // so users see a clear upgrade pointer instead of cryptic errors\n // mid-apply later. The version query runs inside the existing retry\n // loop's try block, so transient network failures (Neon cold start,\n // EAI_AGAIN) continue to retry; only confirmed version mismatch\n // surfaces as UnsupportedDialectVersionError and exits the loop.\n const client = await this.pool.connect();\n try {\n await client.query(\"SELECT 1\");\n // Before anything writes. `search_path` naming a schema that is not\n // there does not fail — it falls through to whatever else is on the\n // path — so the first migration would create its tables in `public`\n // and the setting would appear to do nothing.\n if (this.config.schema) {\n await ensureSchemaExists(client, this.config.schema);\n }\n await checkDialectVersion(client, \"postgresql\", {\n // Why: route any future variant warnings through the adapter's\n // logger. PG has no recognized variants today, but this keeps\n // the integration symmetric with MySQL.\n onWarning: msg => this.config.logger?.warn?.(msg),\n });\n this.connected = true;\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"PostgreSQL connection established\", {\n host: this.config.host ?? \"from URL\",\n database: this.config.database ?? \"from URL\",\n });\n }\n\n return; // Success — exit retry loop\n } finally {\n client.release();\n }\n } catch (error) {\n // Clean up the failed pool before deciding whether to retry\n if (this.pool) {\n await this.pool.end().catch(() => {});\n this.pool = null;\n }\n\n const nodeError = error as { code?: string };\n const isRetryable =\n nodeError.code != null && retryableNodeCodes.has(nodeError.code);\n\n if (isRetryable && attempt < maxAttempts) {\n // Exponential back-off with jitter: ~1 s, ~2 s, ~3 s, ~4 s\n const waitMs = 1000 * attempt;\n const msg = `PostgreSQL connection attempt ${attempt}/${maxAttempts} failed with ${nodeError.code}, retrying in ${waitMs}ms...`;\n if (this.config.logger?.warn) {\n this.config.logger.warn(msg);\n } else {\n console.warn(`[PostgresAdapter] ${msg}`);\n }\n await delay(waitMs);\n continue;\n }\n\n // Non-retryable error or exhausted retries — surface to caller\n throw this.classifyError(error);\n }\n }\n }\n\n /**\n * Closes the database connection and releases all pool resources.\n *\n * @remarks\n * This method is idempotent - calling it multiple times is safe.\n * It waits for all checked-out clients to be returned before shutting down.\n */\n async disconnect(): Promise<void> {\n // Detach the pool FIRST, then drop the memoized drizzle instances —\n // this closes the repopulation window where a concurrent getDrizzle()\n // call during the (async) pool.end() could cache an instance wrapping\n // the closing pool.\n const pool = this.pool;\n this.pool = null;\n this.drizzleBare = undefined;\n this.drizzleByRelations = new WeakMap();\n if (!pool) {\n return;\n }\n\n try {\n await pool.end();\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"PostgreSQL connection closed\");\n }\n } finally {\n this.connected = false;\n }\n }\n\n /**\n * Checks if the adapter is currently connected.\n *\n * @returns True if connected and pool is available\n */\n override isConnected(): boolean {\n return this.connected && this.pool !== null;\n }\n\n /**\n * Executes a raw SQL query.\n *\n * @param sql - SQL statement with $1, $2, ... placeholders\n * @param params - Query parameters\n * @returns Array of result rows\n *\n * @throws {DatabaseError} If query execution fails\n */\n async executeQuery<T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> {\n const pool = this.ensurePool();\n const startTime = Date.now();\n\n // Transient network errors that are safe to retry at the query level.\n // These occur when Neon auto-suspends between the pool smoke-test and the\n // actual query, or when multiple build workers compete for cold-start.\n const retryableNodeCodes = new Set([\n \"ETIMEDOUT\",\n \"ECONNRESET\",\n \"ECONNREFUSED\",\n ]);\n const maxQueryAttempts = 3;\n\n for (let attempt = 1; attempt <= maxQueryAttempts; attempt++) {\n try {\n const result = await pool.query(sql, params as unknown[]);\n\n // Log query if logger configured\n if (this.config.logger?.query) {\n const durationMs = Date.now() - startTime;\n this.config.logger.query(sql, params, durationMs);\n }\n\n return result.rows as T[];\n } catch (error) {\n const nodeError = error as { code?: string };\n const isRetryable =\n nodeError.code != null && retryableNodeCodes.has(nodeError.code);\n\n if (isRetryable && attempt < maxQueryAttempts) {\n const waitMs = 500 * attempt;\n console.warn(\n `[PostgresAdapter] Query attempt ${attempt}/${maxQueryAttempts} failed with ${nodeError.code}, retrying in ${waitMs}ms...`\n );\n await delay(waitMs);\n continue;\n }\n\n throw this.classifyError(error, sql);\n }\n }\n\n // Unreachable — loop always returns or throws\n throw this.classifyError(new Error(\"executeQuery: exhausted retries\"));\n }\n\n /**\n * Executes a callback within a database transaction.\n *\n * @remarks\n * PostgreSQL supports full ACID transactions with savepoints.\n * If the callback throws, the transaction is rolled back.\n *\n * Supports automatic retry for serialization failures (40001) and\n * deadlocks (40P01) when `retryCount` is specified in options.\n *\n * An error carrying the Nextly application brand is neither retried nor\n * classified: it is the application refusing the write, so it arrives at the\n * caller as it was thrown. Retrying it would repeat work whose verdict has\n * already been given. Every other error, including an unbranded one raised by\n * the callback, is retried when it is a serialization failure or deadlock and\n * classified on the way out.\n *\n * @param callback - Function to execute within the transaction\n * @param options - Transaction options (isolation level, timeout, retry)\n * @returns The result of the callback\n *\n * @throws {DatabaseError} If the transaction itself fails after all retries,\n * or if the callback threw an error that does not carry the application brand\n * @throws The callback's own error, unchanged, when it carries that brand\n */\n async transaction<T>(\n callback: (ctx: TransactionContext) => Promise<T>,\n options?: TransactionOptions\n ): Promise<T> {\n // The calling flow's open transaction from BEGIN until COMMIT or ROLLBACK\n // has run, for `transactionOpenInFlow`.\n return this.runInFlowTransaction(() =>\n this.runTransactionAttempts(callback, options)\n );\n }\n\n /** The attempts `transaction` makes, each on a connection of its own. */\n private async runTransactionAttempts<T>(\n callback: (ctx: TransactionContext) => Promise<T>,\n options?: TransactionOptions\n ): Promise<T> {\n const pool = this.ensurePool();\n const maxAttempts = (options?.retryCount ?? 0) + 1;\n const retryDelayMs = options?.retryDelayMs ?? 100;\n\n let lastError: unknown;\n\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n const client = await pool.connect();\n const startTime = Date.now();\n // Set when the ROLLBACK failed. The client may then still be inside the\n // transaction, so it is destroyed rather than pooled: the next request\n // handed it would otherwise write into a transaction nobody commits.\n let discard: Error | true | undefined;\n\n try {\n // Begin transaction with options\n await this.beginTransaction(client, options);\n\n // Create transaction context\n const ctx = this.createTransactionContext(client);\n\n // Execute callback\n const result = await callback(ctx);\n\n // Commit transaction\n await client.query(\"COMMIT\");\n\n // Log success\n if (this.config.logger?.debug) {\n const durationMs = Date.now() - startTime;\n this.config.logger.debug(\"Transaction committed\", {\n attempt,\n durationMs,\n });\n }\n\n return result;\n } catch (error) {\n // Rollback transaction\n await client.query(\"ROLLBACK\").catch((rollbackError: unknown) => {\n discard = rollbackError instanceof Error ? rollbackError : true;\n });\n\n lastError = error;\n\n // Work inside a transaction may throw to roll the write back — a\n // refused value, a denied permission — and that is the application's\n // verdict, not the driver's failure. Rethrown before the retry check as\n // well as before classification: a refusal re-run is the same refusal,\n // and the caller must receive the code and payload it raised rather\n // than a generic database error with the detail stripped out.\n if (isApplicationError(error)) throw error;\n\n // Check if error is retryable\n const pgError = error as { code?: string };\n const isRetryable =\n pgError.code === \"40001\" || // serialization_failure\n pgError.code === \"40P01\"; // deadlock\n\n if (isRetryable && attempt < maxAttempts) {\n if (this.config.logger?.warn) {\n this.config.logger.warn(\n `Transaction failed with ${pgError.code}, retrying (${attempt}/${maxAttempts})`,\n { code: pgError.code, attempt }\n );\n }\n await delay(retryDelayMs * attempt); // Exponential backoff\n continue;\n }\n\n throw this.classifyError(error);\n } finally {\n // node-postgres destroys a client released with an error or `true`,\n // and pools one released with nothing.\n client.release(discard);\n }\n }\n\n // Should not reach here, but handle just in case\n throw this.classifyError(lastError);\n }\n\n /**\n * Returns the database capabilities for PostgreSQL.\n *\n * @remarks\n * PostgreSQL has the most comprehensive feature set of all supported\n * databases, including JSONB, arrays, full-text search, and more.\n */\n getCapabilities(): DatabaseCapabilities {\n return {\n dialect: \"postgresql\",\n supportsJsonb: true,\n supportsJson: true,\n supportsArrays: true,\n supportsGeneratedColumns: true,\n supportsFts: true,\n supportsIlike: true,\n supportsReturning: true,\n supportsSavepoints: true,\n supportsOnConflict: true,\n maxParamsPerQuery: 65535, // PostgreSQL limit\n maxIdentifierLength: 63, // PostgreSQL default\n };\n }\n\n /**\n * The schema set as `search_path` on every pooled connection.\n *\n * @remarks\n * Answers with the same test `buildPoolConfig` applies, so an empty string\n * reports `undefined` exactly as it sets no `search_path`: the value\n * reported is the one the connections actually use.\n *\n * @returns The configured schema, or `undefined` when the server's default\n * `search_path` applies\n */\n getConfiguredSchema(): string | undefined {\n return this.config.schema ? this.config.schema : undefined;\n }\n\n /**\n * Returns connection pool statistics.\n *\n * @returns Pool stats or null if not connected\n */\n override getPoolStats(): PoolStats | null {\n if (!this.pool) {\n return null;\n }\n\n return {\n total: this.pool.totalCount,\n idle: this.pool.idleCount,\n waiting: this.pool.waitingCount,\n active: this.pool.totalCount - this.pool.idleCount,\n };\n }\n\n /**\n * Override insertMany for bulk insert optimization.\n *\n * @remarks\n * Uses a single multi-row INSERT statement for better performance\n * when inserting multiple records.\n */\n override async insertMany<T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> {\n if (data.length === 0) {\n return [];\n }\n\n // For single record, use parent implementation\n if (data.length === 1) {\n const result = await this.insert<T>(table, data[0], options);\n return [result];\n }\n\n // Build multi-row INSERT\n const columns = Object.keys(data[0]);\n const params: SqlParam[] = [];\n const valuesClauses: string[] = [];\n\n for (let i = 0; i < data.length; i++) {\n const record = data[i];\n const placeholders: string[] = [];\n\n for (const col of columns) {\n params.push(record[col] as SqlParam);\n placeholders.push(`$${params.length}`);\n }\n\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n const columnList = columns\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columnList}) VALUES ${valuesClauses.join(\", \")}`;\n\n // Add RETURNING clause\n if (options?.returning) {\n const returning =\n options.returning === \"*\"\n ? \"*\"\n : options.returning.map(col => this.escapeIdentifier(col)).join(\", \");\n sql += ` RETURNING ${returning}`;\n }\n\n try {\n return await this.executeQuery<T>(sql, params);\n } catch (error) {\n throw this.handleQueryError(error, \"insertMany\", table);\n }\n }\n\n // ============================================================\n // Protected Helper Methods\n // ============================================================\n\n /**\n * Ensures pool is connected and returns it.\n *\n * @throws {DatabaseError} If not connected\n */\n private ensurePool(): Pool {\n if (!this.pool) {\n throw createDatabaseError({\n kind: \"connection\",\n message: \"PostgresAdapter is not connected. Call connect() first.\",\n });\n }\n return this.pool;\n }\n\n /**\n * Return the typed Drizzle instance for PostgreSQL.\n * Guarded for server-only usage and requires an active connection.\n *\n * @param schema - Optional schema for relational queries (db.query.*)\n * @returns Drizzle ORM instance wrapping the pg pool connection\n * @throws {Error} If called in browser or not connected\n */\n getDrizzle<T = NodePgDatabase<AnyRelations>>(relations?: AnyRelations): T {\n if (typeof window !== \"undefined\") {\n throw new Error(\"getDrizzle() is server-only\");\n }\n const pool = this.ensurePool();\n // drizzle v1 removed the positional (client, config) form — the object\n // form is REQUIRED (positional silently treats the client as config).\n // `relations` (defineRelations output, assembled by Nextly's schema\n // registry) is what powers db.query in v1; a bare instance serves the\n // select-builder CRUD paths.\n if (!relations) {\n this.drizzleBare ??= this.poolDrizzle(pool);\n return this.drizzleBare as T;\n }\n let cached = this.drizzleByRelations.get(relations);\n if (!cached) {\n cached = this.poolDrizzle(pool, relations);\n this.drizzleByRelations.set(relations, cached);\n }\n return cached as T;\n }\n\n /**\n * A Drizzle handle on the pool, for `getDrizzle`.\n *\n * Its `transaction` runs on a client this adapter checks out and returns\n * itself (`pinDrizzleTransactions`): node-postgres destroys a client\n * released with `true`, which is what happens when no ROLLBACK could be\n * confirmed on it. And it counts for `transactionOpenInFlow`, as the\n * adapter's does.\n */\n private poolDrizzle(pool: Pool, relations?: AnyRelations) {\n const onClient = (client: Pool | PoolClient) =>\n relations ? drizzle({ client, relations }) : drizzle({ client });\n return this.trackFlowTransactions(\n this.pinDrizzleTransactions(onClient(pool), async () => {\n const client = await pool.connect();\n return {\n drizzle: onClient(client),\n rollback: () => client.query(\"ROLLBACK\"),\n release: dirty => client.release(dirty),\n };\n })\n );\n }\n\n /**\n * Builds pg Pool configuration from adapter config.\n */\n private buildPoolConfig(): PoolConfig {\n const config: PoolConfig = {};\n\n // Connection string or explicit options\n if (this.config.url) {\n config.connectionString = this.config.url;\n } else {\n if (this.config.host) config.host = this.config.host;\n if (this.config.port) config.port = this.config.port;\n if (this.config.database) config.database = this.config.database;\n if (this.config.user) config.user = this.config.user;\n if (this.config.password) config.password = this.config.password;\n }\n\n // Pool settings (user config > provider defaults > hardcoded defaults)\n config.min =\n this.config.pool?.min ??\n this.providerDefaults.poolMin ??\n DEFAULT_POOL_CONFIG.min;\n config.max =\n this.config.pool?.max ??\n this.providerDefaults.poolMax ??\n DEFAULT_POOL_CONFIG.max;\n config.idleTimeoutMillis =\n this.config.pool?.idleTimeoutMs ??\n this.providerDefaults.idleTimeoutMs ??\n DEFAULT_POOL_CONFIG.idleTimeoutMs;\n config.connectionTimeoutMillis =\n this.config.pool?.connectionTimeoutMs ??\n this.providerDefaults.connectionTimeoutMs ??\n DEFAULT_POOL_CONFIG.connectionTimeoutMs;\n\n // TCP keepalive - prevents cloud databases (e.g. Neon) from silently\n // dropping idle connections between the pool smoke-test and the first\n // real query, which manifests as ETIMEDOUT on the query itself.\n config.keepAlive = true;\n config.keepAliveInitialDelayMillis = 10000;\n\n // SSL configuration (user config > provider default)\n if (this.config.ssl) {\n if (typeof this.config.ssl === \"boolean\") {\n config.ssl = this.config.ssl;\n } else {\n if (this.config.ssl.rejectUnauthorized === false) {\n console.warn(\n \"[nextly/adapter-postgres] ssl.rejectUnauthorized is set to false — \" +\n \"TLS certificates will not be validated. This is unsafe on untrusted networks. \" +\n \"Provide a trusted `ca` cert instead, or remove the rejectUnauthorized override.\"\n );\n }\n config.ssl = {\n rejectUnauthorized: this.config.ssl.rejectUnauthorized,\n ca: this.config.ssl.ca,\n cert: this.config.ssl.cert,\n key: this.config.ssl.key,\n };\n }\n } else if (this.providerDefaults.ssl) {\n // Provider requires SSL but user didn't explicitly configure it.\n // Default to validating certs; the user can opt out by setting\n // `ssl: { rejectUnauthorized: false }` explicitly (which logs a warning above).\n config.ssl = { rejectUnauthorized: true };\n }\n\n // PostgreSQL-specific options\n if (this.config.applicationName) {\n config.application_name = this.config.applicationName;\n }\n\n // The schema every unqualified name resolves in, set as a STARTUP option\n // rather than by issuing `SET search_path`. A SET belongs to whichever\n // pooled connection ran it, so the next checkout would see the default\n // again — and the query that noticed would be whichever one happened to\n // land on a fresh connection, which is the least reproducible failure\n // this setting could have.\n //\n // Validated before it gets here: the value is interpolated, and a startup\n // option is not a place to discover that it needed quoting.\n if (this.config.schema) {\n config.options = `-c search_path=${assertSchemaName(this.config.schema)}`;\n }\n\n // User config beats provider default; both beat \"unset\".\n // Why: provider.ts:58 declared a Neon statementTimeoutMs default but the\n // adapter previously ignored it, leaving stuck queries to pin pool slots\n // indefinitely. Wire the fallback chain here so the declared defaults\n // actually fire.\n const effectiveStatementTimeout =\n this.config.statementTimeout ?? this.providerDefaults.statementTimeoutMs;\n if (effectiveStatementTimeout !== undefined) {\n config.statement_timeout = effectiveStatementTimeout;\n }\n\n if (this.config.queryTimeout) {\n config.query_timeout = this.config.queryTimeout;\n }\n\n return config;\n }\n\n /**\n * Begins a transaction with the specified options.\n */\n private async beginTransaction(\n client: PoolClient,\n options?: TransactionOptions\n ): Promise<void> {\n let beginSql = \"BEGIN\";\n\n // Add isolation level if specified\n if (options?.isolationLevel) {\n const isolationMap: Record<string, string> = {\n \"read uncommitted\": \"READ UNCOMMITTED\",\n \"read committed\": \"READ COMMITTED\",\n \"repeatable read\": \"REPEATABLE READ\",\n serializable: \"SERIALIZABLE\",\n };\n const level = isolationMap[options.isolationLevel];\n if (level) {\n beginSql += ` ISOLATION LEVEL ${level}`;\n }\n }\n\n // Add read-only mode if specified\n if (options?.readOnly) {\n beginSql += \" READ ONLY\";\n }\n\n await client.query(beginSql);\n\n // Set statement timeout if specified\n if (options?.timeoutMs) {\n await client.query(`SET LOCAL statement_timeout = ${options.timeoutMs}`);\n }\n }\n\n /**\n * Creates a TransactionContext for the given client.\n */\n /**\n * The RETURNING entries that spell each date column's wall clock out as\n * text, one per alias `dateWallClockAliases` chose. `to_char` renders the\n * stored value without a zone, which is the only form node-postgres cannot\n * shift on the way back.\n */\n private wallClockSpelling(\n aliases: ReadonlyArray<{ sqlName: string; alias: string }>\n ): string {\n return aliases\n .map(\n a =>\n `to_char(${this.escapeIdentifier(a.sqlName)}, '${PG_WALL_CLOCK_FORMAT}') AS ${this.escapeIdentifier(a.alias)}`\n )\n .join(\", \");\n }\n\n /** A table-level `primaryKey({ columns })`, read through the PostgreSQL table config. */\n protected override compositePrimaryKey(\n tableObj: Record<string, unknown>\n ): object[] {\n return getTableConfig(tableObj as unknown as PgTable).primaryKeys.flatMap(\n key => key.columns\n );\n }\n\n private createTransactionContext(client: PoolClient): TransactionContext {\n // Bind a Drizzle instance to this transaction's checked-out client so the\n // delegated CRUD methods run inside the transaction and see its uncommitted\n // rows. getDrizzle() wraps the pool, which would use a different connection.\n // Built lazily and memoized: transactions that use only raw execute/insert\n // never construct it.\n const txHandles = this.transactionDrizzleHandles(relations =>\n relations ? drizzle({ client, relations }) : drizzle({ client })\n );\n const txDb = txHandles.bare;\n return {\n drizzleWithRelations: txHandles.withRelations,\n\n execute: async <T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> => {\n const result = await client.query(sql, params as unknown[]);\n return result.rows as T[];\n },\n\n // Run on the transaction-bound Drizzle instance rather than the pool, so\n // the statement is part of this transaction and sees its uncommitted rows.\n runStatement: async (statement: SQL): Promise<void> => {\n await txDb().execute(statement);\n },\n\n // node-postgres answers `{ rows }`; the transaction-bound instance keeps\n // the read inside this transaction so it sees its uncommitted writes.\n queryStatement: async <T = Record<string, unknown>>(\n statement: SQL\n ): Promise<T[]> => {\n const result = await txDb().execute(statement);\n return result.rows as T[];\n },\n\n lockRow: async (table: string, id: SqlParam): Promise<void> => {\n const idColumn = this.escapeIdentifier(\"id\");\n await client.query(\n `SELECT ${idColumn} FROM ${this.escapeIdentifier(table)} ` +\n `WHERE ${idColumn} = $1 FOR UPDATE`,\n [id] as unknown[]\n );\n },\n\n insert: async <T = unknown>(\n table: string,\n data: Record<string, unknown>,\n options?: InsertOptions\n ): Promise<T> => {\n const mapped = this.mapRowToRawSql(this.getTableObject(table), data);\n const columns = Object.keys(mapped);\n const values = Object.values(mapped);\n const placeholders = values.map((_, i) => `$${i + 1}`).join(\", \");\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES (${placeholders})`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n const tableObj = this.getTableObject(table);\n // Each returned timestamp's wall clock, spelled out by the database.\n // node-postgres turns one into a `Date` in the LOCAL zone before this\n // code sees it, and that conversion cannot be undone: a wall clock\n // inside a daylight-saving gap is normalized away.\n const aliases = returningEmpty\n ? []\n : this.dateWallClockAliases(tableObj, ret ?? \"*\");\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(tableObj, ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n const spelled = this.wallClockSpelling(aliases);\n sql += ` RETURNING ${returning}${spelled ? `, ${spelled}` : \"\"}`;\n }\n\n const result = await client.query(sql, values);\n // Return JS property-named keys AND dates read as the UTC the statement\n // wrote, to match the non-transactional insert.\n return this.mapRowFromRawSql(tableObj, result.rows[0] as T, aliases);\n },\n\n insertMany: async <T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> => {\n if (data.length === 0) return [];\n\n const tableObj = this.getTableObject(table);\n const mappedRecords = data.map(r => this.mapRowToRawSql(tableObj, r));\n const columns = Object.keys(mappedRecords[0]);\n const params: unknown[] = [];\n const valuesClauses: string[] = [];\n\n for (const record of mappedRecords) {\n const placeholders: string[] = [];\n for (const col of columns) {\n params.push(record[col]);\n placeholders.push(`$${params.length}`);\n }\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES ${valuesClauses.join(\", \")}`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n const aliases = returningEmpty\n ? []\n : this.dateWallClockAliases(tableObj, ret ?? \"*\");\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(tableObj, ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n const spelled = this.wallClockSpelling(aliases);\n sql += ` RETURNING ${returning}${spelled ? `, ${spelled}` : \"\"}`;\n }\n\n const result = await client.query(sql, params);\n return (result.rows as T[]).map(r =>\n this.mapRowFromRawSql(tableObj, r, aliases)\n );\n },\n\n // Adapter-built, as `insert` above is; `transactionUpdate` says why.\n // A column the model does not declare binds natively, as every value\n // on this path's insert does.\n update: this.transactionUpdate(\n txDb,\n async statement => (await txDb().execute(statement)).rows,\n // Structured values as JSON text: node-postgres would spell an array\n // as a PostgreSQL array literal, which no JSON column accepts.\n value => this.bindUnmodeledStructuredAsJson(value)\n ),\n\n ...this.createTransactionForwarders(txDb),\n\n savepoint: async (name: string): Promise<void> => {\n await client.query(`SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n rollbackToSavepoint: async (name: string): Promise<void> => {\n await client.query(\n `ROLLBACK TO SAVEPOINT ${this.escapeIdentifier(name)}`\n );\n },\n\n releaseSavepoint: async (name: string): Promise<void> => {\n await client.query(`RELEASE SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n };\n }\n\n /**\n * Classifies a PostgreSQL error into a DatabaseError.\n *\n * @param error - Original error from pg\n * @param sql - SQL statement that caused the error (optional)\n * @returns DatabaseError with proper classification\n */\n protected override classifyError(\n error: unknown,\n sql?: string\n ): DatabaseError {\n // Why short-circuit on existing DatabaseError: F17's\n // UnsupportedDialectVersionError is already a typed DatabaseError with\n // kind: \"unsupported_version\" plus detectedVersion/requiredVersion\n // fields. Re-wrapping it here would erase those fields and re-tag it\n // as kind: \"unknown\".\n if (isDatabaseError(error)) return error;\n\n // Classified from node-postgres's own error, which Drizzle puts on `cause`\n // when it wraps a failed query (`driverErrorIn`); it is the one carrying\n // the SQLSTATE `code`.\n const pgError = this.driverErrorIn<{\n code?: string;\n message?: string;\n constraint?: string;\n table?: string;\n column?: string;\n detail?: string;\n hint?: string;\n severity?: string;\n cause?: unknown;\n }>(\n error,\n candidate => typeof candidate.code === \"string\" && candidate.code !== \"\"\n );\n\n // Determine error kind from PostgreSQL error code\n const kind: DatabaseErrorKind =\n (pgError.code && PG_ERROR_CODES[pgError.code]) || \"unknown\";\n\n // Build error message\n let message = pgError.message ?? String(error);\n if (sql && kind === \"query\") {\n message = `Query failed: ${message}`;\n }\n\n return createDatabaseError({\n kind,\n message,\n code: pgError.code,\n constraint: pgError.constraint,\n table: pgError.table,\n column: pgError.column,\n detail: pgError.detail,\n hint: pgError.hint,\n cause: error instanceof Error ? error : undefined,\n });\n }\n}\n\n/**\n * Creates a PostgreSQL database adapter instance.\n *\n * @remarks\n * This is the recommended way to create a PostgreSQL adapter.\n * The adapter is not connected after creation - call `connect()` to\n * establish the database connection.\n *\n * @param config - Adapter configuration\n * @returns A new PostgresAdapter instance\n *\n * @example\n * Simple usage:\n * ```typescript\n * import { createPostgresAdapter } from '@nextlyhq/adapter-postgres';\n *\n * const adapter = createPostgresAdapter({\n * url: process.env.DATABASE_URL!,\n * });\n *\n * await adapter.connect();\n * ```\n *\n * @example\n * With full configuration:\n * ```typescript\n * const adapter = createPostgresAdapter({\n * url: process.env.DATABASE_URL!,\n * pool: {\n * min: 5,\n * max: 20,\n * idleTimeoutMs: 30000,\n * connectionTimeoutMs: 10000,\n * },\n * ssl: {\n * rejectUnauthorized: true,\n * ca: process.env.CA_CERT,\n * },\n * applicationName: 'my-app',\n * statementTimeout: 30000,\n * });\n * ```\n *\n * @public\n */\nexport function createPostgresAdapter(\n config: PostgresAdapterConfig\n): PostgresAdapter {\n return new PostgresAdapter(config);\n}\n\n/**\n * Type guard to check if a value is a PostgresAdapter instance.\n *\n * @param value - Value to check\n * @returns True if value is a PostgresAdapter\n *\n * @example\n * ```typescript\n * import { isPostgresAdapter } from '@nextlyhq/adapter-postgres';\n *\n * if (isPostgresAdapter(adapter)) {\n * // TypeScript knows adapter is PostgresAdapter\n * console.log(adapter.dialect); // 'postgresql'\n * }\n * ```\n *\n * @public\n */\nexport function isPostgresAdapter(value: unknown): value is PostgresAdapter {\n return value instanceof PostgresAdapter;\n}\n"]}
|