@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.d.mts CHANGED
@@ -538,8 +538,13 @@ declare class Server {
538
538
  * Basically means you want to disregard cookies and do everything manually
539
539
  * If we elect to DDL, we don't want to use tenant id or user id, so remove those.
540
540
  */
541
- withContext(context?: ContextParams): Promise<this>;
542
- withContext<T>(context: ContextParams, fn: (sdk: this) => Promise<T>): Promise<T>;
541
+ withContext(context?: PartialContext): Promise<this>;
542
+ withContext<T>(context: PartialContext, fn: (sdk: this) => Promise<T>): Promise<T>;
543
+ /**
544
+ * Creates a context without a user id and a tenant id, but keeps the headers around for auth at least.
545
+ */
546
+ noContext(): Promise<this>;
547
+ noContext<T>(fn: (sdk: this) => Promise<T>): Promise<T>;
543
548
  /**
544
549
  *
545
550
  * @returns the last used (basically global) context object, useful for debugging or making your own context
@@ -564,9 +569,6 @@ type PartialContext = {
564
569
  userId?: string | undefined | null;
565
570
  preserveHeaders?: boolean;
566
571
  };
567
- type ContextParams = PartialContext & {
568
- ddl?: boolean;
569
- };
570
572
  type CTX = {
571
573
  run: <T>(ctx: Partial<Context>, fn: () => T) => T;
572
574
  get: () => Context;
@@ -812,4 +814,4 @@ declare const USER_COOKIE = "nile.user-id";
812
814
  declare const HEADER_ORIGIN = "nile-origin";
813
815
  declare const HEADER_SECURE_COOKIES = "nile-secure-cookies";
814
816
 
815
- export { type APIError, APIErrorErrorCodeEnum, type ActiveSession, type AfterCreate, type CTX, type CTXHandlerType, type Context, type ContextParams, type ContextReturn, type CreateBasicUserRequest, type CreateTenantUserRequest, type Extension, type ExtensionResult, ExtensionState, HEADER_ORIGIN, HEADER_SECURE_COOKIES, type Invite, type JWT, type LoggerType, type LoginUserResponse, type LoginUserResponseToken, LoginUserResponseTokenTypeEnum, create as Nile, type NileConfig, type NileDb, type NileHandlers, type NilePoolConfig, type NileRequest, type NileResponse, type Opts, type PartialContext, type Providers, type RouteFunctions, type RouteReturn, Server, TENANT_COOKIE, type Tenant, USER_COOKIE, type User, parseCSRF, parseCallback, parseResetToken, parseTenantId, parseToken };
817
+ export { type APIError, APIErrorErrorCodeEnum, type ActiveSession, type AfterCreate, type CTX, type CTXHandlerType, type Context, type ContextReturn, type CreateBasicUserRequest, type CreateTenantUserRequest, type Extension, type ExtensionResult, ExtensionState, HEADER_ORIGIN, HEADER_SECURE_COOKIES, type Invite, type JWT, type LoggerType, type LoginUserResponse, type LoginUserResponseToken, LoginUserResponseTokenTypeEnum, create as Nile, type NileConfig, type NileDb, type NileHandlers, type NilePoolConfig, type NileRequest, type NileResponse, type Opts, type PartialContext, type Providers, type RouteFunctions, type RouteReturn, Server, TENANT_COOKIE, type Tenant, USER_COOKIE, type User, parseCSRF, parseCallback, parseResetToken, parseTenantId, parseToken };
package/dist/index.d.ts CHANGED
@@ -538,8 +538,13 @@ declare class Server {
538
538
  * Basically means you want to disregard cookies and do everything manually
539
539
  * If we elect to DDL, we don't want to use tenant id or user id, so remove those.
540
540
  */
541
- withContext(context?: ContextParams): Promise<this>;
542
- withContext<T>(context: ContextParams, fn: (sdk: this) => Promise<T>): Promise<T>;
541
+ withContext(context?: PartialContext): Promise<this>;
542
+ withContext<T>(context: PartialContext, fn: (sdk: this) => Promise<T>): Promise<T>;
543
+ /**
544
+ * Creates a context without a user id and a tenant id, but keeps the headers around for auth at least.
545
+ */
546
+ noContext(): Promise<this>;
547
+ noContext<T>(fn: (sdk: this) => Promise<T>): Promise<T>;
543
548
  /**
544
549
  *
545
550
  * @returns the last used (basically global) context object, useful for debugging or making your own context
@@ -564,9 +569,6 @@ type PartialContext = {
564
569
  userId?: string | undefined | null;
565
570
  preserveHeaders?: boolean;
566
571
  };
567
- type ContextParams = PartialContext & {
568
- ddl?: boolean;
569
- };
570
572
  type CTX = {
571
573
  run: <T>(ctx: Partial<Context>, fn: () => T) => T;
572
574
  get: () => Context;
@@ -812,4 +814,4 @@ declare const USER_COOKIE = "nile.user-id";
812
814
  declare const HEADER_ORIGIN = "nile-origin";
813
815
  declare const HEADER_SECURE_COOKIES = "nile-secure-cookies";
814
816
 
815
- export { type APIError, APIErrorErrorCodeEnum, type ActiveSession, type AfterCreate, type CTX, type CTXHandlerType, type Context, type ContextParams, type ContextReturn, type CreateBasicUserRequest, type CreateTenantUserRequest, type Extension, type ExtensionResult, ExtensionState, HEADER_ORIGIN, HEADER_SECURE_COOKIES, type Invite, type JWT, type LoggerType, type LoginUserResponse, type LoginUserResponseToken, LoginUserResponseTokenTypeEnum, create as Nile, type NileConfig, type NileDb, type NileHandlers, type NilePoolConfig, type NileRequest, type NileResponse, type Opts, type PartialContext, type Providers, type RouteFunctions, type RouteReturn, Server, TENANT_COOKIE, type Tenant, USER_COOKIE, type User, parseCSRF, parseCallback, parseResetToken, parseTenantId, parseToken };
817
+ export { type APIError, APIErrorErrorCodeEnum, type ActiveSession, type AfterCreate, type CTX, type CTXHandlerType, type Context, type ContextReturn, type CreateBasicUserRequest, type CreateTenantUserRequest, type Extension, type ExtensionResult, ExtensionState, HEADER_ORIGIN, HEADER_SECURE_COOKIES, type Invite, type JWT, type LoggerType, type LoginUserResponse, type LoginUserResponseToken, LoginUserResponseTokenTypeEnum, create as Nile, type NileConfig, type NileDb, type NileHandlers, type NilePoolConfig, type NileRequest, type NileResponse, type Opts, type PartialContext, type Providers, type RouteFunctions, type RouteReturn, Server, TENANT_COOKIE, type Tenant, USER_COOKIE, type User, parseCSRF, parseCallback, parseResetToken, parseTenantId, parseToken };
package/dist/index.js CHANGED
@@ -357,9 +357,8 @@ var ctx = {
357
357
  }
358
358
  }
359
359
  }
360
- if ("tenantId" in partial)
361
- store.tenantId = partial.tenantId ?? store.tenantId;
362
- if ("userId" in partial) store.userId = partial.userId ?? store.userId;
360
+ if ("tenantId" in partial) store.tenantId = partial.tenantId;
361
+ if ("userId" in partial) store.userId = partial.userId;
363
362
  if ("preserveHeaders" in partial)
364
363
  store.preserveHeaders = Boolean(partial.preserveHeaders);
365
364
  silly(`[SET] ${serializeContext(store)}`);
@@ -391,14 +390,14 @@ function withNileContext(config, fn, name = "unknown") {
391
390
  mergedHeaders.set(key17, value);
392
391
  });
393
392
  }
394
- const tenantId = "tenantId" in initialContext && initialContext.tenantId;
395
- const userId = "userId" in initialContext && initialContext.userId;
396
- const preserveHeaders = "preserveHeaders" in initialContext && initialContext.preserveHeaders;
393
+ const hasTenantId = "tenantId" in initialContext;
394
+ const hasUserId = "userId" in initialContext;
395
+ const hasPreserveHeaders = "preserveHeaders" in initialContext;
397
396
  const context = {
398
397
  headers: mergedHeaders,
399
- tenantId: tenantId ? tenantId : existing.tenantId,
400
- userId: userId ? userId : existing.userId,
401
- preserveHeaders: preserveHeaders ? preserveHeaders : existing.preserveHeaders ?? false
398
+ tenantId: hasTenantId ? initialContext.tenantId : existing.tenantId,
399
+ userId: hasUserId ? initialContext.userId : existing.userId,
400
+ preserveHeaders: hasPreserveHeaders ? Boolean(initialContext.preserveHeaders) : existing.preserveHeaders ?? false
402
401
  };
403
402
  silly(`${name} [INITIAL - Partial<Context>] ${serializeContext(context)}`);
404
403
  return ctx.run(context, async () => {
@@ -2250,8 +2249,7 @@ function makeAfterCreate(config, id) {
2250
2249
  });
2251
2250
  done(e, conn);
2252
2251
  });
2253
- const tenantId = config.context.tenantId;
2254
- const userId = config.context.userId;
2252
+ const { tenantId, userId } = ctx.getLastUsed();
2255
2253
  if (tenantId) {
2256
2254
  const query = [`SET nile.tenant_id = '${tenantId}'`];
2257
2255
  if (userId) {
@@ -2319,7 +2317,8 @@ var DBManager = class {
2319
2317
  };
2320
2318
  getConnection = (config) => {
2321
2319
  const { info } = Logger(config)("[DBManager]");
2322
- const id = this.makeId(config.context.tenantId, config.context.userId);
2320
+ const { tenantId, userId } = ctx.getLastUsed();
2321
+ const id = this.makeId(tenantId, userId);
2323
2322
  const existing = this.connections.get(id);
2324
2323
  info(`# of instances: ${this.connections.size}`);
2325
2324
  if (existing) {
@@ -3423,8 +3422,6 @@ var Server = class {
3423
3422
  #config;
3424
3423
  #handlers;
3425
3424
  #manager;
3426
- // #headers: undefined | Headers;
3427
- // #preserveHeaders: boolean;
3428
3425
  constructor(config) {
3429
3426
  this.#config = new Config({
3430
3427
  ...config,
@@ -3480,6 +3477,7 @@ var Server = class {
3480
3477
  }
3481
3478
  }
3482
3479
  get db() {
3480
+ this.#config.context = { ...this.getContext() };
3483
3481
  const pool = this.#manager.getConnection(this.#config);
3484
3482
  return Object.assign(pool, {
3485
3483
  clearConnections: () => {
@@ -3519,16 +3517,12 @@ var Server = class {
3519
3517
  this.#config.paths = paths;
3520
3518
  }
3521
3519
  async withContext(context, fn) {
3522
- const { ddl, ...ctx2 } = context ?? defaultContext;
3523
- this.#config.context = { ...ctx2 };
3520
+ const { ...initialContext } = context ?? defaultContext;
3521
+ this.#config.context = { ...initialContext };
3524
3522
  const preserve = (context && "preserveHeaders" in context && context.preserveHeaders) ?? true;
3525
3523
  if (preserve) {
3526
3524
  this.#config.context = { ...this.getContext(), ...context };
3527
3525
  }
3528
- if (ddl) {
3529
- delete this.#config.context.tenantId;
3530
- delete this.#config.context.userId;
3531
- }
3532
3526
  return withNileContext(this.#config, async () => {
3533
3527
  if (fn) {
3534
3528
  return fn(this);
@@ -3536,6 +3530,17 @@ var Server = class {
3536
3530
  return this;
3537
3531
  });
3538
3532
  }
3533
+ async noContext(fn) {
3534
+ this.#config.context.tenantId = void 0;
3535
+ this.#config.context.userId = void 0;
3536
+ return withNileContext(this.#config, async () => {
3537
+ ctx.set({ userId: void 0, tenantId: void 0 });
3538
+ if (fn) {
3539
+ return fn(this);
3540
+ }
3541
+ return this;
3542
+ });
3543
+ }
3539
3544
  /**
3540
3545
  *
3541
3546
  * @returns the last used (basically global) context object, useful for debugging or making your own context
@@ -3545,6 +3550,7 @@ var Server = class {
3545
3550
  }
3546
3551
  /**
3547
3552
  * Merge headers together
3553
+ * Saves them in a singleton for use in a request later. It's basically the "default" value
3548
3554
  * Internally, passed a NileConfig, externally, should be using Headers
3549
3555
  */
3550
3556
  #handleHeaders(config) {