@spfn/core 0.2.0-beta.33 → 0.2.0-beta.35

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/dist/db/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { drizzle } from 'drizzle-orm/postgres-js';
2
2
  import { env } from '@spfn/core/config';
3
3
  import { logger } from '@spfn/core/logger';
4
+ import net from 'net';
4
5
  import postgres from 'postgres';
5
6
  import { QueryError, ConnectionError, DeadlockError, TransactionError, ConstraintViolationError, DuplicateEntryError, DatabaseError } from '@spfn/core/errors';
6
7
  import { parseNumber, parseBoolean } from '@spfn/core/env';
@@ -129,6 +130,12 @@ function fromPostgresError(error) {
129
130
  }
130
131
 
131
132
  // src/db/manager/connection.ts
133
+ function getSocketFamily() {
134
+ const family = process.env.DATABASE_SOCKET_FAMILY;
135
+ if (family === "4") return 4;
136
+ if (family === "6") return 6;
137
+ return void 0;
138
+ }
132
139
  var dbLogger = logger.child("@spfn/core:database");
133
140
  var DEFAULT_CONNECT_TIMEOUT = 10;
134
141
  function delay(ms) {
@@ -189,10 +196,21 @@ async function createDatabaseConnection(connectionString, poolConfig, retryConfi
189
196
  let client;
190
197
  for (let attempt = 0; attempt <= retryConfig.maxRetries; attempt++) {
191
198
  try {
199
+ const socketFamily = getSocketFamily();
192
200
  client = postgres(connectionString, {
193
201
  max: poolConfig.max,
194
202
  idle_timeout: poolConfig.idleTimeout,
195
- connect_timeout: DEFAULT_CONNECT_TIMEOUT
203
+ connect_timeout: DEFAULT_CONNECT_TIMEOUT,
204
+ ...socketFamily && {
205
+ socket: ({ host, port }) => new Promise((resolve, reject) => {
206
+ const socket = new net.Socket();
207
+ socket.on("error", reject);
208
+ socket.connect(
209
+ { port: port[0], host: host[0], family: socketFamily },
210
+ () => resolve(socket)
211
+ );
212
+ })
213
+ }
196
214
  });
197
215
  await client`SELECT 1 as test`;
198
216
  if (attempt > 0) {