@niledatabase/server 5.0.0-alpha.29 → 5.0.0-alpha.30

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
@@ -351,9 +351,8 @@ var ctx = {
351
351
  }
352
352
  }
353
353
  }
354
- if ("tenantId" in partial)
355
- store.tenantId = partial.tenantId ?? store.tenantId;
356
- if ("userId" in partial) store.userId = partial.userId ?? store.userId;
354
+ if ("tenantId" in partial) store.tenantId = partial.tenantId;
355
+ if ("userId" in partial) store.userId = partial.userId;
357
356
  if ("preserveHeaders" in partial)
358
357
  store.preserveHeaders = Boolean(partial.preserveHeaders);
359
358
  silly(`[SET] ${serializeContext(store)}`);
@@ -385,14 +384,14 @@ function withNileContext(config, fn, name = "unknown") {
385
384
  mergedHeaders.set(key17, value);
386
385
  });
387
386
  }
388
- const tenantId = "tenantId" in initialContext && initialContext.tenantId;
389
- const userId = "userId" in initialContext && initialContext.userId;
390
- const preserveHeaders = "preserveHeaders" in initialContext && initialContext.preserveHeaders;
387
+ const hasTenantId = "tenantId" in initialContext;
388
+ const hasUserId = "userId" in initialContext;
389
+ const hasPreserveHeaders = "preserveHeaders" in initialContext;
391
390
  const context = {
392
391
  headers: mergedHeaders,
393
- tenantId: tenantId ? tenantId : existing.tenantId,
394
- userId: userId ? userId : existing.userId,
395
- preserveHeaders: preserveHeaders ? preserveHeaders : existing.preserveHeaders ?? false
392
+ tenantId: hasTenantId ? initialContext.tenantId : existing.tenantId,
393
+ userId: hasUserId ? initialContext.userId : existing.userId,
394
+ preserveHeaders: hasPreserveHeaders ? Boolean(initialContext.preserveHeaders) : existing.preserveHeaders ?? false
396
395
  };
397
396
  silly(`${name} [INITIAL - Partial<Context>] ${serializeContext(context)}`);
398
397
  return ctx.run(context, async () => {
@@ -2244,8 +2243,7 @@ function makeAfterCreate(config, id) {
2244
2243
  });
2245
2244
  done(e, conn);
2246
2245
  });
2247
- const tenantId = config.context.tenantId;
2248
- const userId = config.context.userId;
2246
+ const { tenantId, userId } = ctx.getLastUsed();
2249
2247
  if (tenantId) {
2250
2248
  const query = [`SET nile.tenant_id = '${tenantId}'`];
2251
2249
  if (userId) {
@@ -2313,7 +2311,8 @@ var DBManager = class {
2313
2311
  };
2314
2312
  getConnection = (config) => {
2315
2313
  const { info } = Logger(config)("[DBManager]");
2316
- const id = this.makeId(config.context.tenantId, config.context.userId);
2314
+ const { tenantId, userId } = ctx.getLastUsed();
2315
+ const id = this.makeId(tenantId, userId);
2317
2316
  const existing = this.connections.get(id);
2318
2317
  info(`# of instances: ${this.connections.size}`);
2319
2318
  if (existing) {
@@ -3417,8 +3416,6 @@ var Server = class {
3417
3416
  #config;
3418
3417
  #handlers;
3419
3418
  #manager;
3420
- // #headers: undefined | Headers;
3421
- // #preserveHeaders: boolean;
3422
3419
  constructor(config) {
3423
3420
  this.#config = new Config({
3424
3421
  ...config,
@@ -3474,6 +3471,7 @@ var Server = class {
3474
3471
  }
3475
3472
  }
3476
3473
  get db() {
3474
+ this.#config.context = { ...this.getContext() };
3477
3475
  const pool = this.#manager.getConnection(this.#config);
3478
3476
  return Object.assign(pool, {
3479
3477
  clearConnections: () => {
@@ -3513,16 +3511,12 @@ var Server = class {
3513
3511
  this.#config.paths = paths;
3514
3512
  }
3515
3513
  async withContext(context, fn) {
3516
- const { ddl, ...ctx2 } = context ?? defaultContext;
3517
- this.#config.context = { ...ctx2 };
3514
+ const { ...initialContext } = context ?? defaultContext;
3515
+ this.#config.context = { ...initialContext };
3518
3516
  const preserve = (context && "preserveHeaders" in context && context.preserveHeaders) ?? true;
3519
3517
  if (preserve) {
3520
3518
  this.#config.context = { ...this.getContext(), ...context };
3521
3519
  }
3522
- if (ddl) {
3523
- delete this.#config.context.tenantId;
3524
- delete this.#config.context.userId;
3525
- }
3526
3520
  return withNileContext(this.#config, async () => {
3527
3521
  if (fn) {
3528
3522
  return fn(this);
@@ -3530,6 +3524,17 @@ var Server = class {
3530
3524
  return this;
3531
3525
  });
3532
3526
  }
3527
+ async noContext(fn) {
3528
+ this.#config.context.tenantId = void 0;
3529
+ this.#config.context.userId = void 0;
3530
+ return withNileContext(this.#config, async () => {
3531
+ ctx.set({ userId: void 0, tenantId: void 0 });
3532
+ if (fn) {
3533
+ return fn(this);
3534
+ }
3535
+ return this;
3536
+ });
3537
+ }
3533
3538
  /**
3534
3539
  *
3535
3540
  * @returns the last used (basically global) context object, useful for debugging or making your own context
@@ -3539,6 +3544,7 @@ var Server = class {
3539
3544
  }
3540
3545
  /**
3541
3546
  * Merge headers together
3547
+ * Saves them in a singleton for use in a request later. It's basically the "default" value
3542
3548
  * Internally, passed a NileConfig, externally, should be using Headers
3543
3549
  */
3544
3550
  #handleHeaders(config) {