@classytic/access 0.2.0 → 0.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,33 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0 — 2026-09-13
4
+
5
+ ### Added — `indexes`: the host declares the indexes this package cannot know about
6
+
7
+ The gate indexes the entitlement collection by (subject, resource) and by
8
+ resource; a host's creator roster sorts by activity, progress or name within a
9
+ course, its rank rollup counts purchased grants per org, and its retention
10
+ sweep spans every tenant. None of those can be known here, and before this
11
+ seam the only way to add one was a `collection.createIndex()` after bind, which
12
+ the deploy step cannot see and the next `syncIndexes` drops.
13
+
14
+ `AccessShape.indexes` takes `IndexDeclaration`s from `@classytic/mongokit/tenant`:
15
+ `scope: 'tenant'` (default) is prefixed exactly like the package's own,
16
+ `scope: 'global'` is kept as written for a read that has no tenant to give and
17
+ must be named. A bad declaration is refused at describe time. Reported on
18
+ `engine.config.indexes`.
19
+
20
+ ### Changed — tenant injection is `@classytic/mongokit/tenant`'s
21
+
22
+ `src/models/inject-tenant.ts` re-exports the shared implementation (PACKAGE_RULES
23
+ P11) instead of carrying a copy. Behaviour is unchanged for the package's own
24
+ indexes. Requires `@classytic/mongokit >=3.39.0`.
25
+
26
+ ### Changed — peer floors raised to current
27
+
28
+ - `@classytic/primitives` `>=0.23.0` → **`>=0.28.0`**
29
+ - `@classytic/repo-core` `>=0.24.0` → **`>=0.28.0`**
30
+
3
31
  ## 0.2.0 — 2026-08-17
4
32
 
5
33
  ### Added
package/README.md CHANGED
@@ -88,7 +88,7 @@ if (!allowed) return reply.code(403).send({ code: `access.${reason}` });
88
88
  | `revoke(id, { reason }, ctx)` | Pull access permanently (terminal). |
89
89
  | `expireDue({ now, limit }, ctx)` | **Sweeper** (host cron): CAS `active → expired` for lapsed windows, emits `access:expired`. The row survives. |
90
90
  | `refundByOrigin(model, ref, ctx)` | Cascade: mark every live grant from an order/transaction `refunded`. |
91
- | `listForSubject(subjectRef, ctx)` | A subject's grants, newest-first ("my access"). |
91
+ | `listForSubject(subjectRef, ctx, options?)` | A bounded subject grant list, newest-first; options narrow subject model, resource type, and live grants. |
92
92
 
93
93
  ## Metered access (API / downloads / seats)
94
94
 
package/dist/index.d.mts CHANGED
@@ -5,6 +5,7 @@ import { ResolvedTenantConfig, TenantConfig } from "@classytic/repo-core/tenant"
5
5
  import { InProcessEventBus, InProcessEventBusOptions } from "@classytic/primitives/event-infra";
6
6
  import { Connection, Document, Model, Schema, Types } from "mongoose";
7
7
  import { StateMachine } from "@classytic/primitives/state-machine";
8
+ import { IndexDeclaration, IndexDeclaration as IndexDeclaration$1 } from "@classytic/mongokit/tenant";
8
9
  import { StatusHistory } from "@classytic/primitives/status-history";
9
10
  import { SuspensionPolicy } from "@classytic/primitives/suspension";
10
11
  import { EventTransport } from "@classytic/primitives/events";
@@ -256,6 +257,15 @@ interface DefineAccessModelsConfig {
256
257
  collectionPrefix?: string | undefined;
257
258
  /** On a name collision: `false` (default) throws; `true` re-registers (test/dev fixtures). */
258
259
  forceRecreate?: boolean | undefined;
260
+ /**
261
+ * Indexes the HOST needs on the entitlement collection that this package cannot know
262
+ * about: a creator roster sorted a way the gate never sorts, a retention sweep across
263
+ * every tenant. Applied through `@classytic/mongokit/tenant`, so `scope: 'tenant'` (the
264
+ * default) is led by the tenant field like the package's own and `scope: 'global'` is
265
+ * kept as written for a read that has no tenant to give. On the schema, so they build
266
+ * and verify with everything else.
267
+ */
268
+ indexes?: readonly IndexDeclaration$1[] | undefined;
259
269
  }
260
270
  /**
261
271
  * Describe the Access model set as a connection-free blueprint. Pure — builds no schema and
@@ -824,7 +834,12 @@ declare class EntitlementRepository extends Repository<EntitlementDocument> {
824
834
  matched: number;
825
835
  }>;
826
836
  /** A subject's grants, newest-first ("my access"). */
827
- listForSubject(subjectRef: string, ctx: AccessContext): Promise<OffsetPaginationResult<EntitlementDocument>>;
837
+ listForSubject(subjectRef: string, ctx: AccessContext, options?: {
838
+ subjectModel?: string | undefined;
839
+ resourceType?: string | undefined;
840
+ liveOnly?: boolean | undefined;
841
+ limit?: number | undefined;
842
+ }): Promise<OffsetPaginationResult<EntitlementDocument>>;
828
843
  private transition;
829
844
  private transitionInner;
830
845
  /**
@@ -937,6 +952,13 @@ interface AccessShape {
937
952
  * Maps to mongokit's `replace` policy, which itself throws under `NODE_ENV=production`.
938
953
  */
939
954
  forceRecreate?: boolean | undefined;
955
+ /**
956
+ * Indexes the HOST needs on the entitlement collection that this package cannot know
957
+ * about (a roster sort, a cross-tenant sweep). `scope: 'tenant'` (default) is led by
958
+ * the tenant field like the package's own; `scope: 'global'` is kept as written and
959
+ * must be paired with a `bypassTenant` read. See `@classytic/mongokit/tenant`.
960
+ */
961
+ indexes?: readonly IndexDeclaration[] | undefined;
940
962
  }
941
963
  /**
942
964
  * BIND-time runtime collaborators — the live connection is the first `bind` arg; these are
@@ -999,6 +1021,8 @@ interface ResolvedAccessConfig {
999
1021
  readonly tenant: ResolvedTenantConfig;
1000
1022
  readonly autoIndex: boolean | Partial<Record<AccessModelName, boolean>> | undefined;
1001
1023
  readonly collectionPrefix: string | undefined;
1024
+ /** The host's extra index declarations, as given. */
1025
+ readonly indexes: readonly IndexDeclaration[] | undefined;
1002
1026
  }
1003
1027
  interface AccessEngine {
1004
1028
  readonly config: ResolvedAccessConfig;
package/dist/index.mjs CHANGED
@@ -4,8 +4,9 @@ import { Repository, createOptionsExtractor, defineModels, multiTenantPlugin, tr
4
4
  import { assertNoLegacyTenantKeys, resolveTenantConfig } from "@classytic/repo-core/tenant";
5
5
  import { InProcessEventBus } from "@classytic/primitives/event-infra";
6
6
  import { CURRENCY_PATTERN } from "@classytic/primitives/currency";
7
- import mongoose, { Schema } from "mongoose";
7
+ import { Schema } from "mongoose";
8
8
  import { defineStateMachine } from "@classytic/primitives/state-machine";
9
+ import { assertIndexDeclaration, injectTenantField } from "@classytic/mongokit/tenant";
9
10
  import { appendStatus } from "@classytic/primitives/status-history";
10
11
  import { inclusiveEnd, resolveDateSpan } from "@classytic/primitives/period";
11
12
  import { evaluateSuspension, spansFromStatusHistory } from "@classytic/primitives/suspension";
@@ -579,33 +580,6 @@ function buildEntitlementSchema() {
579
580
  return schema;
580
581
  }
581
582
  //#endregion
582
- //#region src/models/inject-tenant.ts
583
- /**
584
- * Mongoose adapter around `@classytic/repo-core/tenant.resolveTenantConfig`.
585
- * Thin (rule P11) — repo-core owns resolution; this handles schema mutations
586
- * only: adds the tenant field with the engine-configured type, and prepends
587
- * the tenant key to every existing compound index (rule 36).
588
- */
589
- function injectTenantField(schema, tenant) {
590
- const isFieldStrategy = tenant.strategy === "field";
591
- const isObjectId = tenant.fieldType === "objectId";
592
- schema.add({ [tenant.tenantField]: {
593
- type: isObjectId ? mongoose.Schema.Types.ObjectId : String,
594
- ...tenant.enabled && tenant.required ? { required: true } : {},
595
- ...isObjectId && tenant.ref ? { ref: tenant.ref } : {}
596
- } });
597
- if (!tenant.enabled || !isFieldStrategy) return;
598
- const existingIndexes = schema._indexes;
599
- if (existingIndexes && existingIndexes.length > 0) for (const indexEntry of existingIndexes) {
600
- const fields = indexEntry[0];
601
- if (fields[tenant.tenantField] !== void 0) continue;
602
- const newFields = { [tenant.tenantField]: 1 };
603
- for (const [key, val] of Object.entries(fields)) newFields[key] = val;
604
- indexEntry[0] = newFields;
605
- }
606
- if (!(existingIndexes ?? []).some(([fields]) => Object.keys(fields)[0] === tenant.tenantField)) schema.index({ [tenant.tenantField]: 1 });
607
- }
608
- //#endregion
609
583
  //#region src/models/create-models.ts
610
584
  /**
611
585
  * Access model specification — the ONE authoritative place the Entitlement model is
@@ -631,13 +605,14 @@ function resolveAutoIndex(name, autoIndex) {
631
605
  */
632
606
  function defineAccessModels(config) {
633
607
  const prefix = config.collectionPrefix ?? "";
608
+ for (const declaration of config.indexes ?? []) assertIndexDeclaration(declaration, config.tenant);
634
609
  return defineModels({
635
610
  models: [{
636
611
  name: "Entitlement",
637
612
  collection: prefix + DEFAULT_COLLECTIONS.Entitlement,
638
613
  schema: () => {
639
614
  const schema = buildEntitlementSchema();
640
- injectTenantField(schema, config.tenant);
615
+ injectTenantField(schema, config.tenant, { ...config.indexes ? { indexes: config.indexes } : {} });
641
616
  const ai = resolveAutoIndex("Entitlement", config.autoIndex);
642
617
  if (ai !== void 0) schema.set("autoIndex", ai);
643
618
  return schema;
@@ -1708,10 +1683,16 @@ var EntitlementRepository = class extends Repository {
1708
1683
  };
1709
1684
  }
1710
1685
  /** A subject's grants, newest-first ("my access"). */
1711
- async listForSubject(subjectRef, ctx) {
1686
+ async listForSubject(subjectRef, ctx, options = {}) {
1712
1687
  return await this.getAll({
1713
- filters: { subjectRef },
1714
- sort: { updatedAt: -1 }
1688
+ filters: {
1689
+ subjectRef,
1690
+ ...options.subjectModel ? { subjectModel: options.subjectModel } : {},
1691
+ ...options.resourceType ? { resourceType: options.resourceType } : {},
1692
+ ...options.liveOnly ? { live: true } : {}
1693
+ },
1694
+ sort: { updatedAt: -1 },
1695
+ limit: Math.min(Math.max(options.limit ?? 100, 1), 200)
1715
1696
  }, repoOptionsFromCtx(ctx));
1716
1697
  }
1717
1698
  async transition(id, to, event, ctx, patch) {
@@ -2050,6 +2031,7 @@ function defineAccess(shape = {}) {
2050
2031
  tenant,
2051
2032
  ...shape.autoIndex !== void 0 ? { autoIndex: shape.autoIndex } : {},
2052
2033
  ...shape.collectionPrefix !== void 0 ? { collectionPrefix: shape.collectionPrefix } : {},
2034
+ ...shape.indexes !== void 0 ? { indexes: shape.indexes } : {},
2053
2035
  forceRecreate
2054
2036
  });
2055
2037
  return {
@@ -2136,6 +2118,7 @@ function defineAccess(shape = {}) {
2136
2118
  tenant,
2137
2119
  autoIndex: shape.autoIndex,
2138
2120
  collectionPrefix: shape.collectionPrefix,
2121
+ indexes: shape.indexes,
2139
2122
  forceRecreate
2140
2123
  });
2141
2124
  return Object.freeze({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classytic/access",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Access ENTITLEMENT engine - time-bound (or lifetime) grants of access to any resource: course enrollment, file download, API access, membership, feature flag, any service. Like a session, but durable: the grant is a permanent record, the RIGHT to access is COMPUTED (never TTL-deleted). Single responsibility: it answers 'does this subject currently hold a live grant to this resource', NOT the purchase (@classytic/order), the money (@classytic/revenue), or the catalog item (@classytic/catalog). Powered by @classytic/mongokit.",
5
5
  "author": "Classytic",
6
6
  "license": "MIT",
@@ -50,17 +50,17 @@
50
50
  "push": "classytic-push"
51
51
  },
52
52
  "peerDependencies": {
53
- "@classytic/mongokit": ">=3.33.0",
54
- "@classytic/primitives": ">=0.23.0",
55
- "@classytic/repo-core": ">=0.24.0",
53
+ "@classytic/mongokit": ">=3.39.0",
54
+ "@classytic/primitives": ">=0.28.0",
55
+ "@classytic/repo-core": ">=0.28.0",
56
56
  "mongoose": ">=9.4.1",
57
57
  "zod": ">=4.0.0"
58
58
  },
59
59
  "devDependencies": {
60
60
  "@classytic/dev-tools": "^0.2.0",
61
- "@classytic/mongokit": "^3.33.0",
62
- "@classytic/primitives": ">=0.23.0",
63
- "@classytic/repo-core": ">=0.24.0",
61
+ "@classytic/mongokit": "^3.39.0",
62
+ "@classytic/primitives": ">=0.28.0",
63
+ "@classytic/repo-core": ">=0.28.0",
64
64
  "@types/node": "^22.19.21",
65
65
  "@vitest/coverage-v8": "^4.1.9",
66
66
  "mongodb-memory-server": "^11.2.0",