@niledatabase/server 5.3.1 → 5.3.2-alpha.0

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/index.mjs CHANGED
@@ -747,27 +747,20 @@ async function GET(url, init, config) {
747
747
 
748
748
  // src/utils/fetch.ts
749
749
  function getTokenFromCookie(headers, cookieKey) {
750
- const cookie = headers.get("cookie")?.split("; ");
751
- const _cookies = {};
752
- if (cookie) {
753
- for (const parts of cookie) {
754
- const cookieParts = parts.split("=");
755
- const _cookie = cookieParts.slice(1).join("=");
756
- const name = cookieParts[0];
757
- _cookies[name] = _cookie;
758
- }
750
+ const cookieHeader = headers.get("cookie");
751
+ if (!cookieHeader) {
752
+ return null;
759
753
  }
760
- if (cookie) {
761
- for (const parts of cookie) {
762
- const cookieParts = parts.split("=");
763
- const _cookie = cookieParts.slice(1).join("=");
764
- const name = cookieParts[0];
765
- _cookies[name] = _cookie;
754
+ const cookies = cookieHeader.split("; ");
755
+ for (const parts of cookies) {
756
+ const cookieParts = parts.split("=");
757
+ const name = cookieParts[0];
758
+ const value = cookieParts.slice(1).join("=");
759
+ if (name === cookieKey) {
760
+ return value;
766
761
  }
767
762
  }
768
- {
769
- return _cookies[cookieKey];
770
- }
763
+ return null;
771
764
  }
772
765
  function getTenantFromHttp(headers, context) {
773
766
  const cookieTenant = getTokenFromCookie(headers, TENANT_COOKIE);
@@ -2262,43 +2255,45 @@ var updateHeaders = (val) => {
2262
2255
  eventer.publish("headers" /* Headers */, val);
2263
2256
  };
2264
2257
  var watchHeaders = (cb) => eventer.subscribe("headers" /* Headers */, cb);
2265
-
2266
- // src/db/PoolProxy.ts
2267
- function createProxyForPool(pool, config, logger, context) {
2268
- const { info, error } = logger("[pool]");
2269
- return new Proxy(pool, {
2270
- get(target, property) {
2271
- if (property === "query") {
2272
- if (!config.connectionString) {
2273
- if (!config.user || !config.password) {
2274
- error(
2275
- "Cannot connect to the database. User and/or password are missing. Generate them at https://console.thenile.dev"
2276
- );
2277
- } else if (!config.database) {
2278
- error(
2279
- "Unable to obtain database name. Is process.env.NILEDB_POSTGRES_URL set?"
2280
- );
2281
- }
2282
- }
2283
- const caller = target[property];
2284
- return function query(...args) {
2285
- let log = "[QUERY]";
2286
- const [tenantId, userId] = context;
2287
- if (tenantId) {
2288
- log = `${log}[TENANT:${tenantId}]`;
2289
- }
2290
- if (userId) {
2291
- log = `${log}[USER:${userId}]`;
2292
- }
2293
- info(log, ...args);
2294
- const called = caller.apply(this, args);
2295
- return called;
2296
- };
2258
+ var PoolWithLogger = class extends pg.Pool {
2259
+ nileConfig;
2260
+ logger;
2261
+ context;
2262
+ info;
2263
+ error;
2264
+ constructor(config, logger, context) {
2265
+ super(config);
2266
+ this.nileConfig = config;
2267
+ this.logger = logger;
2268
+ this.context = context;
2269
+ const { info, error } = this.logger("[pool]");
2270
+ this.info = info;
2271
+ this.error = error;
2272
+ }
2273
+ query(...args) {
2274
+ if (!this.nileConfig.connectionString) {
2275
+ if (!this.nileConfig.user || !this.nileConfig.password) {
2276
+ this.error(
2277
+ "Cannot connect to the database. User and/or password are missing. Generate them at https://console.thenile.dev"
2278
+ );
2279
+ } else if (!this.nileConfig.database) {
2280
+ this.error(
2281
+ "Unable to obtain database name. Is process.env.NILEDB_POSTGRES_URL set?"
2282
+ );
2297
2283
  }
2298
- return target[property];
2299
2284
  }
2300
- });
2301
- }
2285
+ let log = "[QUERY]";
2286
+ const [tenantId, userId] = this.context;
2287
+ if (tenantId) {
2288
+ log = `${log}[TENANT:${tenantId}]`;
2289
+ }
2290
+ if (userId) {
2291
+ log = `${log}[USER:${userId}]`;
2292
+ }
2293
+ this.info(log, ...args);
2294
+ return super.query(...args);
2295
+ }
2296
+ };
2302
2297
 
2303
2298
  // src/db/NileInstance.ts
2304
2299
  var NileDatabase = class {
@@ -2323,8 +2318,7 @@ var NileDatabase = class {
2323
2318
  const cloned = { ...config };
2324
2319
  cloned.password = "***";
2325
2320
  this.logger.debug(`Connection pool config ${JSON.stringify(cloned)}`);
2326
- this.pool = createProxyForPool(
2327
- new pg.Pool(remaining),
2321
+ this.pool = new PoolWithLogger(
2328
2322
  this.config,
2329
2323
  logger,
2330
2324
  id === "base" ? [] : id.split(":")