|
| 1 | +# Module registry and versioning |
| 2 | + |
| 3 | +The module registry installs pluggable packages without adding them to the core |
| 4 | +application module graph. It validates manifests, checks compatibility with the |
| 5 | +running API version, stores one current version per module name, and isolates |
| 6 | +enablement and configuration by tenant. |
| 7 | + |
| 8 | +## Author a module |
| 9 | + |
| 10 | +Create a `module.manifest.json` next to the module entry point: |
| 11 | + |
| 12 | +```json |
| 13 | +{ |
| 14 | + "name": "portfolio-exporter", |
| 15 | + "version": "1.0.0", |
| 16 | + "core": ">=0.1.0 <1.0.0", |
| 17 | + "hooks": { |
| 18 | + "onInstall": true, |
| 19 | + "onUpgrade": true, |
| 20 | + "onUninstall": false |
| 21 | + }, |
| 22 | + "entryPoint": "@organization/portfolio-exporter" |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +The schema requires: |
| 27 | + |
| 28 | +- `name`: lowercase module identifier. |
| 29 | +- `version`: valid semantic version. |
| 30 | +- `core`: semantic-version range supported by the module. |
| 31 | +- `hooks`: booleans declaring `onInstall`, `onUpgrade`, and `onUninstall`. |
| 32 | +- `entryPoint`: installed package reference or a runtime JavaScript entry point |
| 33 | + inside the root `modules/` directory. Local entry points outside that directory |
| 34 | + are rejected so manifests cannot load core source files. |
| 35 | + |
| 36 | +The machine-readable schema is in |
| 37 | +`src/modules/registry/module-manifest.schema.json`. Runtime validation uses the |
| 38 | +equivalent class-validator DTO plus `semver`, so invalid versions and ranges are |
| 39 | +rejected even when a client does not use the JSON Schema. |
| 40 | + |
| 41 | +Local modules must expose JavaScript that Node can load without `ts-node`. |
| 42 | +TypeScript authors should compile their package before registration and ship |
| 43 | +declarations that implement `ModuleLifecycle`. Published npm package entry points |
| 44 | +are resolved normally from `node_modules`. |
| 45 | + |
| 46 | +The entry point must default-export a lifecycle class or object implementing: |
| 47 | + |
| 48 | +```ts |
| 49 | +interface ModuleLifecycle { |
| 50 | + onInstall?(): Promise<void>; |
| 51 | + onUpgrade?(fromVersion: string, toVersion: string): Promise<void>; |
| 52 | + onUninstall?(): Promise<void>; |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +A hook marked `true` must be implemented. Hooks execute inside the registry's |
| 57 | +database transaction where possible. A thrown error rolls back the registry |
| 58 | +version and status. Because external services cannot participate in the database |
| 59 | +transaction, hooks must be idempotent and compensate for external side effects. |
| 60 | + |
| 61 | +## Register and upgrade |
| 62 | + |
| 63 | +Registry endpoints require an authenticated administrator. Registry management |
| 64 | +bypasses the general user KYC guard because it is an administrative control-plane |
| 65 | +operation; the global authentication and role guards still apply. |
| 66 | + |
| 67 | +POST the manifest and its metadata to `POST /api/v1/modules`: |
| 68 | + |
| 69 | +```json |
| 70 | +{ |
| 71 | + "manifest": { |
| 72 | + "name": "portfolio-exporter", |
| 73 | + "version": "1.0.0", |
| 74 | + "core": ">=0.1.0 <1.0.0", |
| 75 | + "hooks": { |
| 76 | + "onInstall": true, |
| 77 | + "onUpgrade": true, |
| 78 | + "onUninstall": false |
| 79 | + }, |
| 80 | + "entryPoint": "@organization/portfolio-exporter" |
| 81 | + }, |
| 82 | + "description": "Exports tenant portfolios", |
| 83 | + "author": "Organization" |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +The API reads the core version from the root `package.json`. Registration and |
| 88 | +enablement fail with a message containing the required range and actual version |
| 89 | +when the module is incompatible. |
| 90 | + |
| 91 | +Posting a new name runs `onInstall`. Posting the same name with a strictly newer |
| 92 | +version runs `onUpgrade` and updates the existing registry row. Equal versions and |
| 93 | +downgrades are rejected. |
| 94 | + |
| 95 | +## Tenant and global enablement |
| 96 | + |
| 97 | +Enable a module with `POST /api/v1/modules/:id/enable` and disable it with |
| 98 | +`POST /api/v1/modules/:id/disable`: |
| 99 | + |
| 100 | +```json |
| 101 | +{ |
| 102 | + "tenantId": "tenant-123", |
| 103 | + "config": { "format": "csv" } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +Each operation changes only that tenant's `TenantModuleState`. The project has no |
| 108 | +canonical tenant entity, so `tenantId` is an opaque identifier supplied by the |
| 109 | +caller. Omitting `tenantId` creates or updates the nullable global-default state; |
| 110 | +it does not alter any explicit tenant row. An explicit tenant state therefore |
| 111 | +remains isolated from the default. |
| 112 | + |
| 113 | +Resolve the effective state for a tenant with: |
| 114 | + |
| 115 | +```text |
| 116 | +GET /api/v1/modules/:id/state?tenantId=tenant-123 |
| 117 | +``` |
| 118 | + |
| 119 | +Resolution uses the explicit tenant row first, then the global-default row. When |
| 120 | +neither exists the result is an implicit disabled state. A disabled explicit row |
| 121 | +therefore overrides an enabled global default. |
| 122 | + |
| 123 | +A module can be removed with `DELETE /api/v1/modules/:id` only when every tenant |
| 124 | +and global-default state is disabled. Deregistration runs `onUninstall` when it is |
| 125 | +declared. |
| 126 | + |
| 127 | +## Run the example locally |
| 128 | + |
| 129 | +The working example is in `modules/example-grant-module`. Its CommonJS entry |
| 130 | +point is loadable by the development server, the bundled application, and the |
| 131 | +production Docker image; `index.d.ts` declares the lifecycle TypeScript contract. |
| 132 | + |
| 133 | +```bash |
| 134 | +npm install |
| 135 | +npm run migration:run |
| 136 | +npm run start:dev |
| 137 | +MODULE_REGISTRY_TOKEN=<admin-token> npm run module:example:register |
| 138 | +``` |
| 139 | + |
| 140 | +`MODULE_REGISTRY_TOKEN` must contain an administrator bearer token. Set |
| 141 | +`MODULE_REGISTRY_URL` to use a non-default API URL. Inspect the registered module |
| 142 | +with `GET /api/v1/modules`, then use its returned UUID in the enable, disable, and |
| 143 | +state-resolution endpoints. |
0 commit comments