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