@classytic/mongokit 3.11.1 → 3.13.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/README.md +75 -5
- package/dist/{PaginationEngine-CvDFfVXT.mjs → PaginationEngine-CfQRM1yz.mjs} +16 -16
- package/dist/Repository-BY40uhcv.mjs +3603 -0
- package/dist/actions/index.d.mts +1 -1
- package/dist/actions/index.mjs +1 -1
- package/dist/adapter/index.d.mts +190 -0
- package/dist/adapter/index.mjs +2 -0
- package/dist/adapter-DTDMnUOH.mjs +158 -0
- package/dist/ai/index.d.mts +1 -1
- package/dist/ai/index.mjs +3 -3
- package/dist/better-auth/index.d.mts +149 -0
- package/dist/better-auth/index.mjs +81 -0
- package/dist/field-selection-Br3WPOEb.mjs +99 -0
- package/dist/{index-dNTmpgK8.d.mts → index-BbHaKwi5.d.mts} +33 -13
- package/dist/index.d.mts +678 -36
- package/dist/index.mjs +240 -17
- package/dist/{logger-DJ7egMnd.mjs → logger-DprijP5h.mjs} +1 -5
- package/dist/{mongooseToJsonSchema-D1sdCOmj.mjs → mongooseToJsonSchema-BYbG894y.mjs} +68 -20
- package/dist/{mongooseToJsonSchema-DXxjFkLB.d.mts → mongooseToJsonSchema-BgRAaXX-.d.mts} +10 -2
- package/dist/pagination/PaginationEngine.d.mts +3 -2
- package/dist/pagination/PaginationEngine.mjs +1 -1
- package/dist/plugins/index.d.mts +2 -2
- package/dist/plugins/index.mjs +2 -2
- package/dist/query/primitives/coercion.d.mts +1 -1
- package/dist/query/primitives/coercion.mjs +1 -1
- package/dist/query/primitives/indexes.d.mts +1 -1
- package/dist/{types-Dc2aDVUi.d.mts → types-BbaPZleN.d.mts} +419 -205
- package/dist/{update-DEJYcgfV.mjs → update-DLcSSa5b.mjs} +184 -45
- package/dist/utils/index.d.mts +2 -91
- package/dist/utils/index.mjs +5 -5
- package/dist/{validation-chain.plugin-B0VSdcwa.d.mts → validation-chain.plugin-CAPYCj_T.d.mts} +228 -98
- package/dist/validation-chain.plugin-_-WuNml2.mjs +2399 -0
- package/package.json +25 -5
- package/dist/field-selection-dI0m4RBs.mjs +0 -221
- package/dist/validation-chain.plugin-Dllv-cml.mjs +0 -5122
- /package/dist/{coercion-oM8G4b6m.d.mts → coercion-TRMmR49h.d.mts} +0 -0
- /package/dist/{id-resolution-e4BT9zAV.mjs → id-resolution-B0Odvvou.mjs} +0 -0
- /package/dist/{indexes-C26GNZRU.d.mts → indexes-CgOF7Wjd.d.mts} +0 -0
package/dist/{validation-chain.plugin-B0VSdcwa.d.mts → validation-chain.plugin-CAPYCj_T.d.mts}
RENAMED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { B as ObjectId,
|
|
1
|
+
import { B as ObjectId, C as FieldPreset, G as Plugin, J as PopulateSpec, Q as RepositoryContext, _t as ValidationChainOptions, et as RepositoryInstance, it as SelectSpec, j as Logger, l as CascadeOptions, st as SoftDeleteOptions, ut as SortSpec, vt as ValidatorDefinition } from "./types-BbaPZleN.mjs";
|
|
2
2
|
import mongoose from "mongoose";
|
|
3
|
+
import { CacheAdapter as CacheAdapter$1, CacheOptions, RepositoryCacheHandle, RepositoryCachePluginOptions, cachePlugin } from "@classytic/repo-core/cache";
|
|
3
4
|
import { BulkWriteResult } from "@classytic/repo-core/repository";
|
|
4
5
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
|
+
import { OffsetPaginationResult } from "@classytic/repo-core/pagination";
|
|
7
|
+
import { TenantConfig } from "@classytic/repo-core/tenant";
|
|
5
8
|
|
|
6
9
|
//#region src/plugins/aggregate-helpers.plugin.d.ts
|
|
7
10
|
/**
|
|
@@ -89,12 +92,6 @@ interface AggregateHelpersMethods {
|
|
|
89
92
|
}
|
|
90
93
|
//#endregion
|
|
91
94
|
//#region src/plugins/audit-log.plugin.d.ts
|
|
92
|
-
/**
|
|
93
|
-
* Audit log plugin that logs all repository operations
|
|
94
|
-
*
|
|
95
|
-
* @example
|
|
96
|
-
* const repo = new Repository(Model, [auditLogPlugin(console)]);
|
|
97
|
-
*/
|
|
98
95
|
declare function auditLogPlugin(logger: Logger): Plugin;
|
|
99
96
|
//#endregion
|
|
100
97
|
//#region src/plugins/audit-trail.plugin.d.ts
|
|
@@ -119,6 +116,22 @@ interface AuditTrailOptions {
|
|
|
119
116
|
* These fields are redacted in the `changes` diff.
|
|
120
117
|
*/
|
|
121
118
|
excludeFields?: string[];
|
|
119
|
+
/**
|
|
120
|
+
* Custom callback fired when an audit write fails. Receives the error
|
|
121
|
+
* and the entry that failed to land. Default: `console.warn` only.
|
|
122
|
+
*
|
|
123
|
+
* **Use this for compliance-grade audit trails (SOC 2 / PCI / HIPAA)
|
|
124
|
+
* where missing entries must be surfaced upstream** — e.g. forward to
|
|
125
|
+
* a dead-letter queue, page on-call, or short-circuit the request.
|
|
126
|
+
* The default `warn` log is appropriate for development and
|
|
127
|
+
* eventually-consistent observability use cases but is invisible to
|
|
128
|
+
* compliance review.
|
|
129
|
+
*
|
|
130
|
+
* The callback runs on the same fire-and-forget microtask that wrote
|
|
131
|
+
* the entry, so throwing from here will surface as an unhandled
|
|
132
|
+
* rejection — handle (or rethrow into your error reporter) inside.
|
|
133
|
+
*/
|
|
134
|
+
onWriteError?: (error: Error, entry: Omit<AuditEntry, 'timestamp'>) => void;
|
|
122
135
|
}
|
|
123
136
|
type AuditOperation = 'create' | 'update' | 'delete' | 'findOneAndUpdate';
|
|
124
137
|
interface AuditEntry {
|
|
@@ -148,7 +161,7 @@ interface AuditQueryOptions {
|
|
|
148
161
|
limit?: number;
|
|
149
162
|
}
|
|
150
163
|
interface AuditQueryResult {
|
|
151
|
-
|
|
164
|
+
data: AuditEntry[];
|
|
152
165
|
page: number;
|
|
153
166
|
limit: number;
|
|
154
167
|
total: number;
|
|
@@ -284,64 +297,6 @@ interface BatchOperationsMethods {
|
|
|
284
297
|
}): Promise<BulkWriteResult>;
|
|
285
298
|
}
|
|
286
299
|
//#endregion
|
|
287
|
-
//#region src/plugins/cache.plugin.d.ts
|
|
288
|
-
/**
|
|
289
|
-
* Cache plugin factory
|
|
290
|
-
*
|
|
291
|
-
* @param options - Cache configuration
|
|
292
|
-
* @returns Plugin instance
|
|
293
|
-
*/
|
|
294
|
-
declare function cachePlugin(options: CacheOptions): Plugin;
|
|
295
|
-
/**
|
|
296
|
-
* TypeScript interface for cache plugin methods
|
|
297
|
-
*
|
|
298
|
-
* @example
|
|
299
|
-
* ```typescript
|
|
300
|
-
* import type { CacheMethods } from '@classytic/mongokit';
|
|
301
|
-
*
|
|
302
|
-
* type ProductRepoWithCache = ProductRepo & CacheMethods;
|
|
303
|
-
*
|
|
304
|
-
* const productRepo = new ProductRepo(ProductModel, [
|
|
305
|
-
* methodRegistryPlugin(),
|
|
306
|
-
* cachePlugin({ adapter: redisAdapter, ttl: 60 }),
|
|
307
|
-
* ]) as ProductRepoWithCache;
|
|
308
|
-
*
|
|
309
|
-
* // TypeScript autocomplete for cache methods
|
|
310
|
-
* await productRepo.invalidateCache(productId);
|
|
311
|
-
* await productRepo.invalidateListCache();
|
|
312
|
-
* await productRepo.invalidateAllCache();
|
|
313
|
-
* const stats = productRepo.getCacheStats();
|
|
314
|
-
* productRepo.resetCacheStats();
|
|
315
|
-
* ```
|
|
316
|
-
*/
|
|
317
|
-
interface CacheMethods {
|
|
318
|
-
/**
|
|
319
|
-
* Invalidate cache for a specific document
|
|
320
|
-
* Use when document was updated outside this service
|
|
321
|
-
* @param id - Document ID to invalidate
|
|
322
|
-
*/
|
|
323
|
-
invalidateCache(id: string): Promise<void>;
|
|
324
|
-
/**
|
|
325
|
-
* Invalidate all list caches for this model
|
|
326
|
-
* Use when bulk changes happened outside this service
|
|
327
|
-
*/
|
|
328
|
-
invalidateListCache(): Promise<void>;
|
|
329
|
-
/**
|
|
330
|
-
* Invalidate ALL cache entries for this model
|
|
331
|
-
* Nuclear option - use sparingly
|
|
332
|
-
*/
|
|
333
|
-
invalidateAllCache(): Promise<void>;
|
|
334
|
-
/**
|
|
335
|
-
* Get cache statistics for monitoring
|
|
336
|
-
* @returns Cache statistics (hits, misses, sets, invalidations)
|
|
337
|
-
*/
|
|
338
|
-
getCacheStats(): CacheStats;
|
|
339
|
-
/**
|
|
340
|
-
* Reset cache statistics
|
|
341
|
-
*/
|
|
342
|
-
resetCacheStats(): void;
|
|
343
|
-
}
|
|
344
|
-
//#endregion
|
|
345
300
|
//#region src/plugins/cascade.plugin.d.ts
|
|
346
301
|
/**
|
|
347
302
|
* Cascade delete plugin.
|
|
@@ -533,6 +488,85 @@ declare function elasticSearchPlugin(options: ElasticSearchOptions): Plugin;
|
|
|
533
488
|
*/
|
|
534
489
|
declare function fieldFilterPlugin(fieldPreset: FieldPreset): Plugin;
|
|
535
490
|
//#endregion
|
|
491
|
+
//#region src/plugins/lease.plugin.d.ts
|
|
492
|
+
interface LeasePluginOptions {
|
|
493
|
+
/** Field carrying the row's status. @default `'status'` */
|
|
494
|
+
statusField?: string;
|
|
495
|
+
/** Field stamping the lease holder. @default `'leasedBy'` */
|
|
496
|
+
leasedByField?: string;
|
|
497
|
+
/** Field carrying the lease expiry timestamp. @default `'leaseExpiresAt'` */
|
|
498
|
+
leaseExpiresAtField?: string;
|
|
499
|
+
/** Status value an unleased row carries. @default `'pending'` */
|
|
500
|
+
pendingStatus?: string;
|
|
501
|
+
/** Status value a leased row carries. @default `'processing'` */
|
|
502
|
+
processingStatus?: string;
|
|
503
|
+
/** Status value a successfully released row carries. @default `'done'` */
|
|
504
|
+
doneStatus?: string;
|
|
505
|
+
/** Sort fields used to pick the FIFO winner from the lease pool. @default `{ createdAt: 1 }` */
|
|
506
|
+
sort?: Record<string, 1 | -1>;
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Methods contributed by `leasePlugin()`. Use as a type assertion when
|
|
510
|
+
* constructing the repo so call sites get autocomplete:
|
|
511
|
+
*
|
|
512
|
+
* ```ts
|
|
513
|
+
* type OutboxRepo = Repository<IOutboxRow> & LeaseMethods<IOutboxRow>;
|
|
514
|
+
* const repo = new Repository(OutboxModel, [
|
|
515
|
+
* methodRegistryPlugin(),
|
|
516
|
+
* leasePlugin({ pendingStatus: 'queued' }),
|
|
517
|
+
* ]) as OutboxRepo;
|
|
518
|
+
* ```
|
|
519
|
+
*/
|
|
520
|
+
interface LeaseMethods<TDoc> {
|
|
521
|
+
/**
|
|
522
|
+
* Atomically claim the next available row. Matches rows that are
|
|
523
|
+
* either in `pendingStatus` OR have an expired `leaseExpiresAt` (dead
|
|
524
|
+
* lease recovery). Sets status to `processingStatus`, stamps
|
|
525
|
+
* `leasedBy`, and pushes `leaseExpiresAt = now + leaseFor`.
|
|
526
|
+
*
|
|
527
|
+
* @returns The leased doc, or `null` when no row matches.
|
|
528
|
+
*/
|
|
529
|
+
lease(opts: {
|
|
530
|
+
filter?: Record<string, unknown>;
|
|
531
|
+
leaseFor: number;
|
|
532
|
+
leasedBy: string;
|
|
533
|
+
options?: Record<string, unknown>;
|
|
534
|
+
}): Promise<TDoc | null>;
|
|
535
|
+
/**
|
|
536
|
+
* Push a held lease's expiry further. Only succeeds when the lease
|
|
537
|
+
* is still ours (`leasedBy === leasedBy && leaseExpiresAt > now`).
|
|
538
|
+
* Returns `null` if the lease was lost — caller should drop the
|
|
539
|
+
* work, since someone else has already started replaying it.
|
|
540
|
+
*/
|
|
541
|
+
extend(id: string | ObjectId, opts: {
|
|
542
|
+
leasedBy: string;
|
|
543
|
+
leaseFor: number;
|
|
544
|
+
options?: Record<string, unknown>;
|
|
545
|
+
}): Promise<TDoc | null>;
|
|
546
|
+
/**
|
|
547
|
+
* Release a held lease. CAS-checks `leasedBy === opts.leasedBy` AND
|
|
548
|
+
* `leaseExpiresAt > now` so only the live lease holder may finalise
|
|
549
|
+
* the row — symmetrical with `extend()`. A worker whose lease has
|
|
550
|
+
* been recovered (lease lost) MUST NOT mark someone else's
|
|
551
|
+
* in-progress work as `done` / `failed`; this method returns `null`
|
|
552
|
+
* in that race rather than overwriting.
|
|
553
|
+
*
|
|
554
|
+
* Sets status to `opts.finalStatus` (default `doneStatus`), clears
|
|
555
|
+
* `leasedBy` and `leaseExpiresAt`. Use `{ finalStatus: 'failed' }`
|
|
556
|
+
* for error paths so terminal-status filters (next caller's
|
|
557
|
+
* `lease()` predicate) skip this row.
|
|
558
|
+
*
|
|
559
|
+
* Returns the released doc, or `null` when the CAS lost (lease
|
|
560
|
+
* expired / held by another worker / row gone).
|
|
561
|
+
*/
|
|
562
|
+
release(id: string | ObjectId, opts: {
|
|
563
|
+
leasedBy: string;
|
|
564
|
+
finalStatus?: string;
|
|
565
|
+
options?: Record<string, unknown>;
|
|
566
|
+
}): Promise<TDoc | null>;
|
|
567
|
+
}
|
|
568
|
+
declare function leasePlugin(options?: LeasePluginOptions): Plugin;
|
|
569
|
+
//#endregion
|
|
536
570
|
//#region src/plugins/method-registry.plugin.d.ts
|
|
537
571
|
/**
|
|
538
572
|
* Extended repository interface with method registry
|
|
@@ -750,16 +784,50 @@ interface MongoOperationsMethods<TDoc> {
|
|
|
750
784
|
* });
|
|
751
785
|
*/
|
|
752
786
|
atomicUpdate(id: string | ObjectId, operators: Record<string, Record<string, unknown>>, options?: Record<string, unknown>): Promise<TDoc>;
|
|
787
|
+
/**
|
|
788
|
+
* Atomic conditional increment — increments the field only when it is
|
|
789
|
+
* strictly below `ceiling`. Returns the updated document on success,
|
|
790
|
+
* `null` when already at/above the ceiling.
|
|
791
|
+
*
|
|
792
|
+
* Single-round-trip CAS via `findOneAndUpdate({ _id, field: { $lt:
|
|
793
|
+
* ceiling }}, { $inc: { field: by }})`. Use for per-document
|
|
794
|
+
* concurrency limits, quotas, and capacity caps where a read-then-
|
|
795
|
+
* write loop would race.
|
|
796
|
+
*
|
|
797
|
+
* @param id - Document ID
|
|
798
|
+
* @param field - Numeric field to increment
|
|
799
|
+
* @param ceiling - Increment only when the field is `< ceiling`
|
|
800
|
+
* @param by - Increment delta (default `1`; pass negative to decrement-with-floor by inverting the predicate via the call site)
|
|
801
|
+
* @param options - Operation options (session, etc.)
|
|
802
|
+
* @returns The updated document, or `null` when at/above ceiling
|
|
803
|
+
*
|
|
804
|
+
* @example Per-tenant concurrency limit
|
|
805
|
+
* ```ts
|
|
806
|
+
* const claimed = await repo.incrementIfBelow(tenantId, 'activeRuns', 10);
|
|
807
|
+
* if (!claimed) throw new ConcurrencyLimitExceeded();
|
|
808
|
+
* try { await runWorkflow(); } finally {
|
|
809
|
+
* await repo.decrement(tenantId, 'activeRuns');
|
|
810
|
+
* }
|
|
811
|
+
* ```
|
|
812
|
+
*/
|
|
813
|
+
incrementIfBelow(id: string | ObjectId, field: string, ceiling: number, by?: number, options?: Record<string, unknown>): Promise<TDoc | null>;
|
|
753
814
|
}
|
|
754
815
|
//#endregion
|
|
755
816
|
//#region src/plugins/multi-tenant.plugin.d.ts
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
817
|
+
/**
|
|
818
|
+
* Static tenant fields are picked from `@classytic/repo-core/tenant` so the
|
|
819
|
+
* vocabulary (`tenantField`, `contextKey`, `required`, `fieldType`) stays
|
|
820
|
+
* locked to the org-wide canonical source. Mongokit-specific runtime
|
|
821
|
+
* callbacks (`skipWhen`, `resolveContext`, `skipOperations`,
|
|
822
|
+
* `allowDataInjection`) extend on top — those genuinely differ from
|
|
823
|
+
* sqlitekit's runtime shape because mongokit's `RepositoryContext` carries
|
|
824
|
+
* Mongoose-specific bits the resolver may inspect.
|
|
825
|
+
*
|
|
826
|
+
* Mongokit defaults `fieldType` to `'string'` (not repo-core's `'objectId'`)
|
|
827
|
+
* for back-compat — pre-3.x stored tenant ids as raw strings; flipping the
|
|
828
|
+
* default would silently change cast behavior for existing apps.
|
|
829
|
+
*/
|
|
830
|
+
interface MultiTenantOptions extends Pick<TenantConfig, 'tenantField' | 'contextKey' | 'required' | 'fieldType'> {
|
|
763
831
|
/** Operations to skip tenant injection (e.g., for admin/system queries) */
|
|
764
832
|
skipOperations?: string[];
|
|
765
833
|
/**
|
|
@@ -785,39 +853,101 @@ interface MultiTenantOptions {
|
|
|
785
853
|
*/
|
|
786
854
|
resolveContext?: () => string | undefined;
|
|
787
855
|
/**
|
|
788
|
-
*
|
|
789
|
-
*
|
|
790
|
-
*
|
|
791
|
-
* - `'objectId'`: cast to `mongoose.Types.ObjectId` before injecting.
|
|
792
|
-
*
|
|
793
|
-
* Choose `'objectId'` when the schema declares the tenant field as
|
|
794
|
-
* `Schema.Types.ObjectId` (e.g., Flow, accounting). This enables
|
|
795
|
-
* `$lookup` and `.populate()` against the referenced collection.
|
|
796
|
-
*/
|
|
797
|
-
fieldType?: 'string' | 'objectId';
|
|
798
|
-
/**
|
|
799
|
-
* When `true` (default), bypass the `required` throw if the operation's
|
|
800
|
-
* policy target already carries the tenant field. This lets hosts that
|
|
801
|
-
* stamp the tenant into the payload themselves (e.g., `data[tenantField]`
|
|
856
|
+
* When `true`, bypass the `required` throw if the operation's policy
|
|
857
|
+
* target already carries the tenant field. This lets hosts that stamp
|
|
858
|
+
* the tenant into the payload themselves (e.g., `data[tenantField]`
|
|
802
859
|
* on writes, `filters[tenantField]` on paginated reads) use the plugin
|
|
803
860
|
* without having to hand-roll a `skipWhen` that duplicates this check.
|
|
804
861
|
*
|
|
805
|
-
*
|
|
806
|
-
*
|
|
807
|
-
*
|
|
862
|
+
* **Default: `false` (secure / fail-closed).** Tenant scope MUST come
|
|
863
|
+
* from `context` or `resolveContext`, otherwise the plugin throws
|
|
864
|
+
* (when `required: true`). This is the right posture for a security-
|
|
865
|
+
* sensitive boundary: caller-supplied scope on the payload cannot be
|
|
866
|
+
* trusted as authentication. If a host control-plane has already
|
|
867
|
+
* authenticated the tenant out-of-band and stamps it onto every
|
|
868
|
+
* write/read, set this to `true` explicitly — making the trust model
|
|
869
|
+
* visible at the call site.
|
|
870
|
+
*
|
|
871
|
+
* **Migration from < 4.0:** the prior default was `true`. To preserve
|
|
872
|
+
* the old behavior verbatim, set `allowDataInjection: true`. To get
|
|
873
|
+
* the new secure default, leave it unset.
|
|
808
874
|
*
|
|
809
|
-
* Composition: a user-supplied `skipWhen` still runs first and can
|
|
810
|
-
* opt out. This option controls the behavior after `skipWhen`
|
|
811
|
-
* `resolveContext` have had their turn.
|
|
875
|
+
* Composition: a user-supplied `skipWhen` still runs first and can
|
|
876
|
+
* always opt out. This option controls the behavior after `skipWhen`
|
|
877
|
+
* and `resolveContext` have had their turn.
|
|
812
878
|
*
|
|
813
|
-
* Safety: when the payload supplies the tenant
|
|
814
|
-
* injection (it does not overwrite). Cross-
|
|
815
|
-
* only as strong as the caller's own
|
|
816
|
-
* hand-rolled `skipWhen`.
|
|
879
|
+
* Safety: when the payload supplies the tenant AND this is `true`,
|
|
880
|
+
* the plugin skips its own injection (it does not overwrite). Cross-
|
|
881
|
+
* tenant isolation is therefore only as strong as the caller's own
|
|
882
|
+
* stamping — same guarantee as a hand-rolled `skipWhen`.
|
|
817
883
|
*/
|
|
818
884
|
allowDataInjection?: boolean;
|
|
819
885
|
}
|
|
820
886
|
declare function multiTenantPlugin(options?: MultiTenantOptions): Plugin;
|
|
887
|
+
/**
|
|
888
|
+
* Build the canonical `skipWhen` callback for role-based tenant
|
|
889
|
+
* bypass — replaces the hand-rolled
|
|
890
|
+
* `(ctx) => ctx.role === 'superadmin'` callback that ~6 packages
|
|
891
|
+
* across the classytic codebase carry as boilerplate. Eliminates the
|
|
892
|
+
* "I forgot to include 'platform_admin'" drift class — the role
|
|
893
|
+
* vocabulary lives in one place.
|
|
894
|
+
*
|
|
895
|
+
* **Composes with `bypassTenant: true` per-call**, doesn't replace
|
|
896
|
+
* it. `bypassTenant` is the deliberate per-call form for migration
|
|
897
|
+
* scripts and one-off admin queries; `adminBypass` is the
|
|
898
|
+
* always-on role-based bypass for users whose JWT / session carries
|
|
899
|
+
* a privileged role.
|
|
900
|
+
*
|
|
901
|
+
* **NOT a substitute for authentication or RBAC.** This factory only
|
|
902
|
+
* answers "should the tenant scope hook fire for this call?" — it
|
|
903
|
+
* does NOT validate that the caller is authenticated, that their
|
|
904
|
+
* role claim is genuine, or that the requested operation is
|
|
905
|
+
* permitted. Always run auth + RBAC before the call reaches the
|
|
906
|
+
* repository layer; this is the LAST gate, not the only one.
|
|
907
|
+
*
|
|
908
|
+
* @example Single role
|
|
909
|
+
* ```ts
|
|
910
|
+
* import { adminBypass, multiTenantPlugin } from '@classytic/mongokit';
|
|
911
|
+
*
|
|
912
|
+
* const repo = new Repository(InvoiceModel, [
|
|
913
|
+
* multiTenantPlugin({
|
|
914
|
+
* tenantField: 'organizationId',
|
|
915
|
+
* skipWhen: adminBypass({ adminRoles: ['superadmin'] }),
|
|
916
|
+
* }),
|
|
917
|
+
* ]);
|
|
918
|
+
*
|
|
919
|
+
* await repo.getAll({}, { role: 'superadmin' }); // sees all orgs
|
|
920
|
+
* await repo.getAll({}, { role: 'user', organizationId: 'org-1' }); // scoped
|
|
921
|
+
* ```
|
|
922
|
+
*
|
|
923
|
+
* @example Multiple admin roles + custom field name
|
|
924
|
+
* ```ts
|
|
925
|
+
* skipWhen: adminBypass({
|
|
926
|
+
* roleField: 'principalRole', // ctx.principalRole, not ctx.role
|
|
927
|
+
* adminRoles: ['superadmin', 'platform_admin', 'support'],
|
|
928
|
+
* }),
|
|
929
|
+
* ```
|
|
930
|
+
*
|
|
931
|
+
* @example Composing with custom logic
|
|
932
|
+
* ```ts
|
|
933
|
+
* // adminBypass for the role check, then your own additional logic:
|
|
934
|
+
* const adminCheck = adminBypass({ adminRoles: ['superadmin'] });
|
|
935
|
+
* skipWhen: (ctx, op) => adminCheck(ctx, op) || ctx.internalSystemFlag === true,
|
|
936
|
+
* ```
|
|
937
|
+
*
|
|
938
|
+
* @param options.roleField - Path on the context to read the role
|
|
939
|
+
* from. Defaults to `'role'`. Top-level keys only — for nested
|
|
940
|
+
* shapes (e.g. `ctx.user.role`), wrap your own `(ctx) => …` callback
|
|
941
|
+
* instead.
|
|
942
|
+
* @param options.adminRoles - Role values that grant the bypass. The
|
|
943
|
+
* factory does an exact-match `includes` check — case-sensitive,
|
|
944
|
+
* no fuzzy matching. Lowercase your role vocabulary upstream.
|
|
945
|
+
* @returns A `skipWhen`-compatible callback `(ctx, op) → boolean`.
|
|
946
|
+
*/
|
|
947
|
+
declare function adminBypass(options: {
|
|
948
|
+
roleField?: string;
|
|
949
|
+
adminRoles: readonly string[];
|
|
950
|
+
}): (context: RepositoryContext, operation: string) => boolean;
|
|
821
951
|
//#endregion
|
|
822
952
|
//#region src/plugins/observability.plugin.d.ts
|
|
823
953
|
interface OperationMetric {
|
|
@@ -1090,4 +1220,4 @@ declare function immutableField(field: string): ValidatorDefinition;
|
|
|
1090
1220
|
*/
|
|
1091
1221
|
declare function uniqueField(field: string, errorMessage?: string): ValidatorDefinition;
|
|
1092
1222
|
//#endregion
|
|
1093
|
-
export {
|
|
1223
|
+
export { AuditQueryResult as $, elasticSearchPlugin as A, sequentialId as B, MethodRegistryRepository as C, leasePlugin as D, LeasePluginOptions as E, SequentialIdOptions as F, RepositoryCachePluginOptions as G, CacheAdapter$1 as H, customIdPlugin as I, BulkWriteResult as J, cachePlugin as K, dateSequentialId as L, DateSequentialIdOptions as M, IdGenerator as N, fieldFilterPlugin as O, PrefixedIdOptions as P, AuditQueryOptions as Q, getNextSequence as R, mongoOperationsPlugin as S, LeaseMethods as T, CacheOptions as U, cascadePlugin as V, RepositoryCacheHandle as W, AuditEntry as X, batchOperationsPlugin as Y, AuditOperation as Z, observabilityPlugin as _, uniqueField as a, AggregateHelpersMethods as at, multiTenantPlugin as b, TenantContext as c, SubdocumentMethods as d, AuditTrailMethods as et, subdocumentPlugin as f, OperationMetric as g, ObservabilityOptions as h, requireField as i, auditLogPlugin as it, CustomIdOptions as j, ElasticSearchOptions as k, TenantStore as l, softDeletePlugin as m, blockIf as n, AuditTrailQuery as nt, validationChainPlugin as o, aggregateHelpersPlugin as ot, SoftDeleteMethods as p, BatchOperationsMethods as q, immutableField as r, auditTrailPlugin as rt, timestampPlugin as s, autoInject as t, AuditTrailOptions as tt, createTenantContext as u, MultiTenantOptions as v, methodRegistryPlugin as w, MongoOperationsMethods as x, adminBypass as y, prefixedId as z };
|