nucleus-core-ts 0.10.147 → 0.10.151
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/LICENSE +48 -48
- package/dist/.build-ok +1 -1
- package/dist/fe/components/AuthorizationPage/labels.js +13 -13
- package/dist/fe/components/Captcha/labels.js +17 -17
- package/dist/fe/components/ChatPanel/helpers.js +17 -17
- package/dist/fe/components/ChatPanel/labels.js +16 -16
- package/dist/fe/components/DataTable/labels.js +16 -16
- package/dist/fe/components/DevicesPage/components/DeviceCard.js +15 -15
- package/dist/fe/components/DevicesPage/components/DevicesPage.js +9 -9
- package/dist/fe/components/DevicesPage/labels.js +12 -12
- package/dist/fe/components/ForgotPasswordPage/labels.js +8 -8
- package/dist/fe/components/FormBuilder/components/FormField.js +5 -5
- package/dist/fe/components/FormBuilder/utils/fieldMapping.js +9 -9
- package/dist/fe/components/IntegrationsPage/components/ConnectionAuthFields.js +8 -8
- package/dist/fe/components/IntegrationsPage/components/ConnectionBasicFields.js +3 -3
- package/dist/fe/components/IntegrationsPage/components/ConnectionExport.js +23 -23
- package/dist/fe/components/IntegrationsPage/components/ConnectionForm.js +22 -22
- package/dist/fe/components/IntegrationsPage/components/ConnectionList.js +7 -7
- package/dist/fe/components/IntegrationsPage/components/ConnectionOauthFields.js +6 -6
- package/dist/fe/components/IntegrationsPage/components/ConnectionSettingsTab.js +24 -24
- package/dist/fe/components/IntegrationsPage/components/ConnectionSwitcher.js +19 -19
- package/dist/fe/components/IntegrationsPage/components/ConnectionTabs.js +15 -15
- package/dist/fe/components/IntegrationsPage/components/ConnectionTest.js +21 -21
- package/dist/fe/components/IntegrationsPage/components/ConnectionTransfer.js +18 -18
- package/dist/fe/components/IntegrationsPage/components/CredentialFields.js +19 -19
- package/dist/fe/components/IntegrationsPage/components/CredentialSlotRow.js +8 -8
- package/dist/fe/components/IntegrationsPage/components/EndpointForm.js +7 -7
- package/dist/fe/components/IntegrationsPage/components/EndpointFormFields.js +12 -12
- package/dist/fe/components/IntegrationsPage/components/EndpointList.js +7 -7
- package/dist/fe/components/IntegrationsPage/components/EndpointSample.js +36 -36
- package/dist/fe/components/IntegrationsPage/components/EndpointsTab.js +29 -29
- package/dist/fe/components/IntegrationsPage/components/FieldMappingRow.js +7 -7
- package/dist/fe/components/IntegrationsPage/components/FieldMappingRows.js +6 -6
- package/dist/fe/components/IntegrationsPage/components/IntegrationsPage.js +60 -60
- package/dist/fe/components/IntegrationsPage/components/MappingAccountFields.js +15 -15
- package/dist/fe/components/IntegrationsPage/components/MappingEditor.js +9 -9
- package/dist/fe/components/IntegrationsPage/components/MappingList.js +23 -23
- package/dist/fe/components/IntegrationsPage/components/MappingPasswordField.js +12 -12
- package/dist/fe/components/IntegrationsPage/components/MappingScheduleFields.js +23 -23
- package/dist/fe/components/IntegrationsPage/components/MappingWriteModeFields.js +31 -31
- package/dist/fe/components/IntegrationsPage/components/MappingsTab.js +56 -56
- package/dist/fe/components/IntegrationsPage/components/PaginationFields.js +11 -11
- package/dist/fe/components/IntegrationsPage/components/ParamBindingRows.js +15 -15
- package/dist/fe/components/IntegrationsPage/components/Primitives.js +28 -28
- package/dist/fe/components/IntegrationsPage/components/ReferenceCheckRows.js +9 -9
- package/dist/fe/components/IntegrationsPage/components/RunConfirmStep.js +6 -6
- package/dist/fe/components/IntegrationsPage/components/RunHistory.js +6 -6
- package/dist/fe/components/IntegrationsPage/components/RunHistoryRow.js +9 -9
- package/dist/fe/components/IntegrationsPage/components/RunPanel.js +40 -40
- package/dist/fe/components/IntegrationsPage/components/RunPasswordField.js +18 -18
- package/dist/fe/components/IntegrationsPage/components/RunPlanIssues.js +6 -6
- package/dist/fe/components/IntegrationsPage/components/RunPlanReferences.js +12 -12
- package/dist/fe/components/IntegrationsPage/components/RunPlanSuggestions.js +11 -11
- package/dist/fe/components/IntegrationsPage/components/RunPlanSummary.js +6 -6
- package/dist/fe/components/IntegrationsPage/components/RunProgressBar.js +6 -6
- package/dist/fe/components/IntegrationsPage/components/RunRevertConflicts.js +6 -6
- package/dist/fe/components/IntegrationsPage/components/Skeletons.js +7 -7
- package/dist/fe/components/IntegrationsPage/components/authOptions.js +4 -4
- package/dist/fe/components/IntegrationsPage/components/connectionDraft.js +27 -27
- package/dist/fe/components/IntegrationsPage/components/credentialSlots.js +25 -25
- package/dist/fe/components/IntegrationsPage/components/deleteIsConfirmed.js +9 -9
- package/dist/fe/components/IntegrationsPage/components/fieldMappingOptions.js +4 -4
- package/dist/fe/components/IntegrationsPage/components/formSupport.js +34 -34
- package/dist/fe/components/IntegrationsPage/components/jsonArray.js +14 -14
- package/dist/fe/components/IntegrationsPage/components/jsonObjectField.js +15 -15
- package/dist/fe/components/IntegrationsPage/components/mappingDraft.js +10 -10
- package/dist/fe/components/IntegrationsPage/components/mappingStaleness.js +22 -22
- package/dist/fe/components/IntegrationsPage/components/nextRuns.js +8 -8
- package/dist/fe/components/IntegrationsPage/components/passwordIsNeeded.js +13 -13
- package/dist/fe/components/IntegrationsPage/components/pathParams.js +6 -6
- package/dist/fe/components/IntegrationsPage/components/runFormat.js +18 -18
- package/dist/fe/components/IntegrationsPage/components/settleOwnership.js +18 -18
- package/dist/fe/components/IntegrationsPage/components/useInfiniteList.js +8 -8
- package/dist/fe/components/IntegrationsPage/store/index.js +20 -20
- package/dist/fe/components/IntegrationsPage/store/upsertInto.js +12 -12
- package/dist/fe/components/IntegrationsPage/text/context.js +6 -6
- package/dist/fe/components/IntegrationsPage/text/index.js +18 -18
- package/dist/fe/components/IntegrationsPage/theme/index.js +29 -29
- package/dist/fe/components/IntegrationsPage/types/records.js +10 -10
- package/dist/fe/components/IntegrationsPage/types/results.js +3 -3
- package/dist/fe/components/NotificationCenter/labels.js +17 -17
- package/dist/fe/components/ProfilePage/labels.js +10 -10
- package/dist/fe/components/ResetPasswordPage/labels.js +4 -4
- package/dist/fe/components/SetPasswordPage/labels.js +11 -11
- package/dist/fe/components/UsersPage/labels.js +14 -14
- package/dist/fe/utils/columnUtils.js +24 -24
- package/dist/fe/utils/searchFold.js +36 -36
- package/dist/index.js +6 -6
- package/dist/server.js +6 -6
- package/dist/src/Client/Proxy/authz.js +4 -4
- package/dist/src/Client/Proxy/httpProxy.js +136 -136
- package/dist/src/Client/Proxy/largeBody.js +29 -29
- package/dist/src/Client/Proxy/utils.js +32 -32
- package/dist/src/Client/Proxy/wsProxy.js +59 -59
- package/dist/src/Managers/Redis/index.d.ts +14 -0
- package/dist/src/Nucleus/routes/auth/admin/impersonationStore.d.ts +3 -4
- package/dist/src/Nucleus/routes/auth/refresh/issuedPair.d.ts +67 -0
- package/dist/src/Nucleus/routes/auth/refresh/recordIssued.d.ts +15 -6
- package/dist/src/Nucleus/routes/auth/refresh/refreshChain.d.ts +56 -0
- package/dist/src/Services/Auth/SessionStore/utils.d.ts +2 -0
- package/dist/src/Shared/cron.js +71 -71
- package/dist/src/Shared/redactSecrets.js +156 -156
- package/dist/src/Shared/redaction/bare.js +61 -61
- package/dist/src/Shared/redaction/floor.js +48 -48
- package/dist/src/Shared/redaction/freeText.js +70 -70
- package/dist/src/Shared/redaction/json.js +30 -30
- package/dist/src/Shared/redaction/lists.js +23 -23
- package/dist/src/Shared/redaction/markers.js +28 -28
- package/dist/src/Shared/redaction/names.js +49 -49
- package/dist/src/Shared/redaction/pairs.js +54 -54
- package/dist/src/Shared/redaction/secretString.js +29 -29
- package/dist/src/Shared/redaction/structured.js +52 -52
- package/dist/src/Shared/redaction/walk.js +25 -25
- package/infra/scripts/generate-project.ts +766 -766
- package/infra/templates/README.md +57 -57
- package/infra/templates/backend/.dockerignore +9 -9
- package/infra/templates/backend/.env.example +15 -15
- package/infra/templates/backend/README.md +28 -28
- package/infra/templates/backend/azure-pipelines.yml +135 -135
- package/infra/templates/backend/k8s/Dockerfile +31 -31
- package/infra/templates/backend/k8s/configmap.yaml +26 -26
- package/infra/templates/backend/k8s/deployment.yaml +85 -85
- package/infra/templates/backend/k8s/pvc.yaml +12 -12
- package/infra/templates/backend/k8s/service.yaml +13 -13
- package/infra/templates/backend/tsconfig.json +22 -22
- package/infra/templates/frontend/.dockerignore +10 -10
- package/infra/templates/frontend/.env.example +8 -8
- package/infra/templates/frontend/.env.local.example +10 -10
- package/infra/templates/frontend/README.md +69 -69
- package/infra/templates/frontend/app/[tablename]/page.tsx +37 -37
- package/infra/templates/frontend/app/_components/AuthWrapper/index.tsx +63 -63
- package/infra/templates/frontend/app/authorization/page.tsx +43 -43
- package/infra/templates/frontend/app/change-password/page.tsx +23 -23
- package/infra/templates/frontend/app/devices/page.tsx +127 -127
- package/infra/templates/frontend/app/forgot-password/page.tsx +20 -20
- package/infra/templates/frontend/app/globals.css +28 -28
- package/infra/templates/frontend/app/layout.tsx +35 -35
- package/infra/templates/frontend/app/login/page.tsx +33 -33
- package/infra/templates/frontend/app/page.tsx +565 -565
- package/infra/templates/frontend/app/profile/page.tsx +92 -92
- package/infra/templates/frontend/app/register/page.tsx +27 -27
- package/infra/templates/frontend/app/reset-password/page.tsx +60 -60
- package/infra/templates/frontend/app/set-password/page.tsx +43 -43
- package/infra/templates/frontend/app/ui-docs/page.tsx +52 -52
- package/infra/templates/frontend/app/users/page.tsx +52 -52
- package/infra/templates/frontend/app/verify-email/page.tsx +41 -41
- package/infra/templates/frontend/app/verify-magic-link/page.tsx +46 -46
- package/infra/templates/frontend/azure-pipelines.yml +137 -137
- package/infra/templates/frontend/code-and-design-principles.md +438 -438
- package/infra/templates/frontend/k8s/Dockerfile +50 -50
- package/infra/templates/frontend/k8s/configmap.yaml +18 -18
- package/infra/templates/frontend/k8s/deployment.yaml +72 -72
- package/infra/templates/frontend/k8s/ingress.yaml +36 -36
- package/infra/templates/frontend/k8s/service.yaml +14 -14
- package/infra/templates/frontend/lib/api/endpoints.ts +37 -37
- package/infra/templates/frontend/lib/api/factory.ts +44 -44
- package/infra/templates/frontend/lib/api/hook.ts +10 -10
- package/infra/templates/frontend/next.config.ts +19 -19
- package/infra/templates/frontend/package.json +34 -34
- package/infra/templates/frontend/postcss.config.mjs +5 -5
- package/infra/templates/frontend/server.ts +114 -114
- package/infra/templates/frontend/tsconfig.json +23 -23
- package/package.json +1 -1
- package/public/components/CTA.tsx +25 -25
- package/public/components/Capabilities.tsx +67 -67
- package/public/components/Features.tsx +107 -107
- package/public/components/Footer.tsx +58 -58
- package/public/components/Hero.tsx +51 -51
- package/public/components/Managers.tsx +55 -55
- package/public/components/Nav.tsx +91 -91
- package/public/components/Stack.tsx +36 -36
- package/public/components/docs/Docs.tsx +60 -60
- package/public/components/docs/DocsContent.tsx +176 -176
- package/public/components/docs/DocsRightSidebar.tsx +53 -53
- package/public/components/docs/DocsSidebar.tsx +54 -54
- package/public/components/docs/content/authentication.ts +836 -836
- package/public/components/docs/content/authorization.ts +268 -268
- package/public/components/docs/content/backupRestore.ts +173 -173
- package/public/components/docs/content/clientModule.ts +287 -287
- package/public/components/docs/content/entityCrud.ts +682 -682
- package/public/components/docs/content/monitoring.ts +313 -313
- package/public/components/docs/content/multiTenant.ts +269 -269
- package/public/components/docs/content/oauthApiKeys.ts +325 -325
- package/public/components/docs/content/pubsub.ts +131 -131
- package/public/components/docs/content/storageCdn.ts +171 -171
- package/public/components/docs/content/verification.ts +289 -289
- package/public/components/docs/docsData.ts +113 -113
- package/public/components/docs/index.ts +6 -6
- package/public/components/index.ts +9 -9
- package/public/docs.css +629 -629
- package/public/index.html +81 -81
- package/public/index.tsx +42 -42
- package/public/store.ts +76 -76
- package/public/styles.css +858 -858
- package/schemas/config.nucleus.json +2651 -2651
- package/scripts/audit-purge-noise.ts +120 -120
- package/scripts/build.ts +582 -582
- package/scripts/generate-system-tables-types.ts +18 -18
- package/scripts/generate-types-schema.test.ts +73 -73
- package/scripts/generate-types-schema.ts +1284 -1284
- package/scripts/packagedScripts.test.ts +87 -87
- package/scripts/publish.ts +276 -276
- package/scripts/skillCitations.test.ts +107 -107
- package/scripts/verify-publishable.ts +40 -40
|
@@ -1,269 +1,269 @@
|
|
|
1
|
-
import type { DocItem } from '../docsData'
|
|
2
|
-
|
|
3
|
-
export const multiTenantItems: DocItem[] = [
|
|
4
|
-
{
|
|
5
|
-
id: 'mt-architecture',
|
|
6
|
-
title: 'Architecture',
|
|
7
|
-
subItems: [
|
|
8
|
-
{
|
|
9
|
-
id: 'mt-overview',
|
|
10
|
-
title: 'Multi-Tenant Overview',
|
|
11
|
-
content: `
|
|
12
|
-
## Multi-Tenant Architecture
|
|
13
|
-
|
|
14
|
-
Nucleus implements **per-schema full isolation** — each tenant gets its own PostgreSQL schema with ALL tables (users, sessions, roles, entities, etc.). There are no shared tables between tenants. This provides the strongest possible data isolation while using a single database.
|
|
15
|
-
|
|
16
|
-
### Core Principle
|
|
17
|
-
|
|
18
|
-
\`\`\`
|
|
19
|
-
PostgreSQL Database
|
|
20
|
-
├── public (main schema) ← Control Plane
|
|
21
|
-
│ ├── tenants ← Tenant registry
|
|
22
|
-
│ ├── tenant_events ← Lifecycle audit
|
|
23
|
-
│ ├── tenant_features ← Feature flags per tenant
|
|
24
|
-
│ ├── users ← Main schema users (platform admins)
|
|
25
|
-
│ ├── ... all system tables
|
|
26
|
-
│ └── ... all entity tables
|
|
27
|
-
│
|
|
28
|
-
├── acme (tenant schema) ← Tenant "Acme Corp"
|
|
29
|
-
│ ├── users ← Acme's own users
|
|
30
|
-
│ ├── roles, claims ← Acme's own RBAC
|
|
31
|
-
│ ├── user_sessions ← Acme's sessions
|
|
32
|
-
│ └── ... all entity tables
|
|
33
|
-
│
|
|
34
|
-
└── lorem (tenant schema) ← Tenant "Lorem Inc"
|
|
35
|
-
├── users ← Lorem's own users
|
|
36
|
-
└── ... complete isolation
|
|
37
|
-
\`\`\`
|
|
38
|
-
|
|
39
|
-
### Key Design Decisions
|
|
40
|
-
|
|
41
|
-
- **No shared users table** — each tenant has an independent user base
|
|
42
|
-
- **Schema list resolved at startup**, not per-request (30-40s startup is OK)
|
|
43
|
-
- **Single DB connection pool** shared across all schemas
|
|
44
|
-
- **Per-tenant schemaTables cached in memory** via \`Map<schemaName, Record<string, DrizzleTable>>\`
|
|
45
|
-
- **Redis cache** for subdomain → schema_name lookup (TTL 5-10 min)
|
|
46
|
-
|
|
47
|
-
### Multi-Service Pattern
|
|
48
|
-
|
|
49
|
-
The IDP (full mode) serves ALL schemas — main + all tenants. Consumer APIs serve only the schemas relevant to their features:
|
|
50
|
-
|
|
51
|
-
\`\`\`
|
|
52
|
-
IDP (full) → main, acme, lorem schemas → all system + entity tables
|
|
53
|
-
Blog API → main, lorem schemas → only blog tables
|
|
54
|
-
\`\`\`
|
|
55
|
-
|
|
56
|
-
Acme doesn't have the blog feature, so Blog API doesn't serve the acme schema.
|
|
57
|
-
`,
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
id: 'mt-config',
|
|
61
|
-
title: 'Configuration',
|
|
62
|
-
content: `
|
|
63
|
-
## Multi-Tenant Configuration
|
|
64
|
-
|
|
65
|
-
### Database Config
|
|
66
|
-
|
|
67
|
-
\`\`\`json
|
|
68
|
-
{
|
|
69
|
-
"database": {
|
|
70
|
-
"url": "DATABASE_URL",
|
|
71
|
-
"isMultiTenant": true,
|
|
72
|
-
"schemas": ["public", "acme", "lorem"],
|
|
73
|
-
"tenantResolution": "both",
|
|
74
|
-
"tenantHeader": "x-tenant-id",
|
|
75
|
-
"sharedSchema": "public"
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
\`\`\`
|
|
79
|
-
|
|
80
|
-
| Field | Type | Description |
|
|
81
|
-
|-------|------|-------------|
|
|
82
|
-
| \`isMultiTenant\` | boolean | Enable multi-tenant mode |
|
|
83
|
-
| \`schemas\` | string[] | List of schemas to serve (resolved at startup from tenants table) |
|
|
84
|
-
| \`tenantResolution\` | string | How to resolve tenant: \`"subdomain"\`, \`"header"\`, or \`"both"\` |
|
|
85
|
-
| \`tenantHeader\` | string | Header name for header-based resolution (default: \`x-tenant-id\`) |
|
|
86
|
-
| \`sharedSchema\` | string | The main/control-plane schema name (default: \`"public"\`) |
|
|
87
|
-
|
|
88
|
-
### Tenant Resolution Priority
|
|
89
|
-
|
|
90
|
-
When \`tenantResolution\` is \`"both"\`, Nucleus resolves the tenant in this order:
|
|
91
|
-
|
|
92
|
-
1. **Subdomain** — \`acme.yourapp.com\` → schema \`acme\`
|
|
93
|
-
2. **Header** — \`x-tenant-id: acme\` (only if the tenant's \`trusted_sources\` allows it)
|
|
94
|
-
3. **Main schema** — If no tenant identifier found, use the main/control-plane schema
|
|
95
|
-
|
|
96
|
-
### Trusted Sources
|
|
97
|
-
|
|
98
|
-
Each tenant has a \`trusted_sources\` JSONB field that controls who can access it via header-based resolution:
|
|
99
|
-
|
|
100
|
-
\`\`\`json
|
|
101
|
-
{
|
|
102
|
-
"allowed_ips": ["10.0.0.0/8", "172.16.0.0/12"],
|
|
103
|
-
"allowed_services": ["blog-api", "payment-api"],
|
|
104
|
-
"allow_header_auth": true
|
|
105
|
-
}
|
|
106
|
-
\`\`\`
|
|
107
|
-
|
|
108
|
-
This prevents arbitrary clients from spoofing the tenant header. Only requests from trusted IPs or services can use header-based resolution.
|
|
109
|
-
|
|
110
|
-
### Entity Schema Restrictions
|
|
111
|
-
|
|
112
|
-
Entities can be restricted to or excluded from specific schemas:
|
|
113
|
-
|
|
114
|
-
\`\`\`json
|
|
115
|
-
{
|
|
116
|
-
"table_name": "premium_features",
|
|
117
|
-
"available_schemas": ["acme", "lorem"],
|
|
118
|
-
"excluded_schemas": ["free_tier_tenant"]
|
|
119
|
-
}
|
|
120
|
-
\`\`\`
|
|
121
|
-
`,
|
|
122
|
-
},
|
|
123
|
-
],
|
|
124
|
-
},
|
|
125
|
-
{
|
|
126
|
-
id: 'mt-provisioning',
|
|
127
|
-
title: 'Provisioning',
|
|
128
|
-
subItems: [
|
|
129
|
-
{
|
|
130
|
-
id: 'mt-provision-flow',
|
|
131
|
-
title: 'Tenant Provisioning Flow',
|
|
132
|
-
content: `
|
|
133
|
-
## Tenant Provisioning
|
|
134
|
-
|
|
135
|
-
Provisioning creates a new tenant: metadata record, database schema, tables, and the initial admin user.
|
|
136
|
-
|
|
137
|
-
### Endpoint
|
|
138
|
-
|
|
139
|
-
\`\`\`
|
|
140
|
-
POST /tenants/provision
|
|
141
|
-
\`\`\`
|
|
142
|
-
|
|
143
|
-
**Requires godmin authentication.**
|
|
144
|
-
|
|
145
|
-
### Request Body
|
|
146
|
-
|
|
147
|
-
\`\`\`json
|
|
148
|
-
{
|
|
149
|
-
"name": "Acme Corp",
|
|
150
|
-
"schemaName": "acme",
|
|
151
|
-
"domain": "acme.yourapp.com",
|
|
152
|
-
"plan": "enterprise",
|
|
153
|
-
"godAdminEmail": "admin@acme.com",
|
|
154
|
-
"godAdminPassword": "SecurePass123!",
|
|
155
|
-
"settings": {
|
|
156
|
-
"maxUsers": 500,
|
|
157
|
-
"features": ["blog", "analytics"]
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
\`\`\`
|
|
161
|
-
|
|
162
|
-
### What Happens
|
|
163
|
-
|
|
164
|
-
1. **Insert tenant metadata** — A record is created in the \`tenants\` table with \`status: "provisioning"\`
|
|
165
|
-
2. **Create schema** — \`CREATE SCHEMA IF NOT EXISTS acme\` (with SQL identifier validation to prevent injection)
|
|
166
|
-
3. **Push tables** — All system tables + entity tables are created in the new schema via Drizzle
|
|
167
|
-
4. **Seed godmin** — The tenant's first admin user is created with the provided email/password, assigned the godmin role
|
|
168
|
-
5. **Log event** — A \`tenant_events\` record is created with type \`"created"\`
|
|
169
|
-
6. **Update status** — Tenant status changes to \`"active"\`, \`provisioned_at\` is set
|
|
170
|
-
7. **Invalidate cache** — Redis cache for tenant lookups is refreshed
|
|
171
|
-
|
|
172
|
-
### Response
|
|
173
|
-
|
|
174
|
-
\`\`\`json
|
|
175
|
-
{
|
|
176
|
-
"success": true,
|
|
177
|
-
"data": {
|
|
178
|
-
"id": "tenant-uuid",
|
|
179
|
-
"name": "Acme Corp",
|
|
180
|
-
"schemaName": "acme",
|
|
181
|
-
"status": "active",
|
|
182
|
-
"provisionedAt": "2024-01-15T10:30:00Z"
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
\`\`\`
|
|
186
|
-
|
|
187
|
-
### Tenants Table Schema
|
|
188
|
-
|
|
189
|
-
| Column | Type | Description |
|
|
190
|
-
|--------|------|-------------|
|
|
191
|
-
| \`id\` | uuid | Primary key |
|
|
192
|
-
| \`name\` | varchar | Display name |
|
|
193
|
-
| \`schemaName\` | varchar | PostgreSQL schema name |
|
|
194
|
-
| \`domain\` | varchar | Subdomain or custom domain |
|
|
195
|
-
| \`status\` | varchar | provisioning, active, suspended, deleted |
|
|
196
|
-
| \`plan\` | varchar | Subscription plan |
|
|
197
|
-
| \`godAdminEmail\` | varchar | First admin email |
|
|
198
|
-
| \`settings\` | jsonb | Tenant-specific settings |
|
|
199
|
-
| \`trustedSources\` | jsonb | IP/service whitelist for header auth |
|
|
200
|
-
| \`provisionedAt\` | timestamptz | When provisioning completed |
|
|
201
|
-
| \`createdAt\` | timestamptz | Record creation time |
|
|
202
|
-
`,
|
|
203
|
-
},
|
|
204
|
-
{
|
|
205
|
-
id: 'mt-management',
|
|
206
|
-
title: 'Tenant Management',
|
|
207
|
-
content: `
|
|
208
|
-
## Tenant Management Endpoints
|
|
209
|
-
|
|
210
|
-
All management endpoints require godmin authentication.
|
|
211
|
-
|
|
212
|
-
### List Tenants
|
|
213
|
-
|
|
214
|
-
\`\`\`
|
|
215
|
-
GET /tenants
|
|
216
|
-
\`\`\`
|
|
217
|
-
|
|
218
|
-
Returns all tenants with their metadata, status, and plan info.
|
|
219
|
-
|
|
220
|
-
### Get Tenant Details
|
|
221
|
-
|
|
222
|
-
\`\`\`
|
|
223
|
-
GET /tenants/:id
|
|
224
|
-
\`\`\`
|
|
225
|
-
|
|
226
|
-
Returns full tenant details including settings and trusted sources.
|
|
227
|
-
|
|
228
|
-
### Suspend Tenant
|
|
229
|
-
|
|
230
|
-
\`\`\`
|
|
231
|
-
POST /tenants/:id/suspend
|
|
232
|
-
\`\`\`
|
|
233
|
-
|
|
234
|
-
Sets tenant status to \`"suspended"\`. All requests to the tenant's schema will be rejected. A \`tenant_events\` record is logged with type \`"suspended"\`.
|
|
235
|
-
|
|
236
|
-
### Reactivate Tenant
|
|
237
|
-
|
|
238
|
-
\`\`\`
|
|
239
|
-
POST /tenants/:id/reactivate
|
|
240
|
-
\`\`\`
|
|
241
|
-
|
|
242
|
-
Restores tenant status to \`"active"\`. A \`tenant_events\` record is logged with type \`"reactivated"\`.
|
|
243
|
-
|
|
244
|
-
### Tenant Events (Audit Trail)
|
|
245
|
-
|
|
246
|
-
Every lifecycle change is recorded in \`tenant_events\`:
|
|
247
|
-
|
|
248
|
-
| Event Type | Description |
|
|
249
|
-
|------------|-------------|
|
|
250
|
-
| \`created\` | Tenant provisioned |
|
|
251
|
-
| \`provisioned\` | Schema and tables created |
|
|
252
|
-
| \`suspended\` | Tenant suspended |
|
|
253
|
-
| \`reactivated\` | Tenant reactivated |
|
|
254
|
-
| \`plan_changed\` | Subscription plan changed |
|
|
255
|
-
| \`settings_updated\` | Tenant settings modified |
|
|
256
|
-
|
|
257
|
-
### How Per-Request Resolution Works
|
|
258
|
-
|
|
259
|
-
In the \`onRequest\` middleware:
|
|
260
|
-
|
|
261
|
-
1. \`TenantRegistry.resolveFromRequest(request)\` extracts the tenant identifier
|
|
262
|
-
2. Sets \`x-tenant-schema\` header on the request
|
|
263
|
-
3. Every route handler calls \`resolveAuthTablesForRequest(request)\` or \`getReqSchema(request)\` to get the tenant-specific table references
|
|
264
|
-
4. All database queries are scoped to the resolved schema
|
|
265
|
-
`,
|
|
266
|
-
},
|
|
267
|
-
],
|
|
268
|
-
},
|
|
269
|
-
]
|
|
1
|
+
import type { DocItem } from '../docsData'
|
|
2
|
+
|
|
3
|
+
export const multiTenantItems: DocItem[] = [
|
|
4
|
+
{
|
|
5
|
+
id: 'mt-architecture',
|
|
6
|
+
title: 'Architecture',
|
|
7
|
+
subItems: [
|
|
8
|
+
{
|
|
9
|
+
id: 'mt-overview',
|
|
10
|
+
title: 'Multi-Tenant Overview',
|
|
11
|
+
content: `
|
|
12
|
+
## Multi-Tenant Architecture
|
|
13
|
+
|
|
14
|
+
Nucleus implements **per-schema full isolation** — each tenant gets its own PostgreSQL schema with ALL tables (users, sessions, roles, entities, etc.). There are no shared tables between tenants. This provides the strongest possible data isolation while using a single database.
|
|
15
|
+
|
|
16
|
+
### Core Principle
|
|
17
|
+
|
|
18
|
+
\`\`\`
|
|
19
|
+
PostgreSQL Database
|
|
20
|
+
├── public (main schema) ← Control Plane
|
|
21
|
+
│ ├── tenants ← Tenant registry
|
|
22
|
+
│ ├── tenant_events ← Lifecycle audit
|
|
23
|
+
│ ├── tenant_features ← Feature flags per tenant
|
|
24
|
+
│ ├── users ← Main schema users (platform admins)
|
|
25
|
+
│ ├── ... all system tables
|
|
26
|
+
│ └── ... all entity tables
|
|
27
|
+
│
|
|
28
|
+
├── acme (tenant schema) ← Tenant "Acme Corp"
|
|
29
|
+
│ ├── users ← Acme's own users
|
|
30
|
+
│ ├── roles, claims ← Acme's own RBAC
|
|
31
|
+
│ ├── user_sessions ← Acme's sessions
|
|
32
|
+
│ └── ... all entity tables
|
|
33
|
+
│
|
|
34
|
+
└── lorem (tenant schema) ← Tenant "Lorem Inc"
|
|
35
|
+
├── users ← Lorem's own users
|
|
36
|
+
└── ... complete isolation
|
|
37
|
+
\`\`\`
|
|
38
|
+
|
|
39
|
+
### Key Design Decisions
|
|
40
|
+
|
|
41
|
+
- **No shared users table** — each tenant has an independent user base
|
|
42
|
+
- **Schema list resolved at startup**, not per-request (30-40s startup is OK)
|
|
43
|
+
- **Single DB connection pool** shared across all schemas
|
|
44
|
+
- **Per-tenant schemaTables cached in memory** via \`Map<schemaName, Record<string, DrizzleTable>>\`
|
|
45
|
+
- **Redis cache** for subdomain → schema_name lookup (TTL 5-10 min)
|
|
46
|
+
|
|
47
|
+
### Multi-Service Pattern
|
|
48
|
+
|
|
49
|
+
The IDP (full mode) serves ALL schemas — main + all tenants. Consumer APIs serve only the schemas relevant to their features:
|
|
50
|
+
|
|
51
|
+
\`\`\`
|
|
52
|
+
IDP (full) → main, acme, lorem schemas → all system + entity tables
|
|
53
|
+
Blog API → main, lorem schemas → only blog tables
|
|
54
|
+
\`\`\`
|
|
55
|
+
|
|
56
|
+
Acme doesn't have the blog feature, so Blog API doesn't serve the acme schema.
|
|
57
|
+
`,
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
id: 'mt-config',
|
|
61
|
+
title: 'Configuration',
|
|
62
|
+
content: `
|
|
63
|
+
## Multi-Tenant Configuration
|
|
64
|
+
|
|
65
|
+
### Database Config
|
|
66
|
+
|
|
67
|
+
\`\`\`json
|
|
68
|
+
{
|
|
69
|
+
"database": {
|
|
70
|
+
"url": "DATABASE_URL",
|
|
71
|
+
"isMultiTenant": true,
|
|
72
|
+
"schemas": ["public", "acme", "lorem"],
|
|
73
|
+
"tenantResolution": "both",
|
|
74
|
+
"tenantHeader": "x-tenant-id",
|
|
75
|
+
"sharedSchema": "public"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
\`\`\`
|
|
79
|
+
|
|
80
|
+
| Field | Type | Description |
|
|
81
|
+
|-------|------|-------------|
|
|
82
|
+
| \`isMultiTenant\` | boolean | Enable multi-tenant mode |
|
|
83
|
+
| \`schemas\` | string[] | List of schemas to serve (resolved at startup from tenants table) |
|
|
84
|
+
| \`tenantResolution\` | string | How to resolve tenant: \`"subdomain"\`, \`"header"\`, or \`"both"\` |
|
|
85
|
+
| \`tenantHeader\` | string | Header name for header-based resolution (default: \`x-tenant-id\`) |
|
|
86
|
+
| \`sharedSchema\` | string | The main/control-plane schema name (default: \`"public"\`) |
|
|
87
|
+
|
|
88
|
+
### Tenant Resolution Priority
|
|
89
|
+
|
|
90
|
+
When \`tenantResolution\` is \`"both"\`, Nucleus resolves the tenant in this order:
|
|
91
|
+
|
|
92
|
+
1. **Subdomain** — \`acme.yourapp.com\` → schema \`acme\`
|
|
93
|
+
2. **Header** — \`x-tenant-id: acme\` (only if the tenant's \`trusted_sources\` allows it)
|
|
94
|
+
3. **Main schema** — If no tenant identifier found, use the main/control-plane schema
|
|
95
|
+
|
|
96
|
+
### Trusted Sources
|
|
97
|
+
|
|
98
|
+
Each tenant has a \`trusted_sources\` JSONB field that controls who can access it via header-based resolution:
|
|
99
|
+
|
|
100
|
+
\`\`\`json
|
|
101
|
+
{
|
|
102
|
+
"allowed_ips": ["10.0.0.0/8", "172.16.0.0/12"],
|
|
103
|
+
"allowed_services": ["blog-api", "payment-api"],
|
|
104
|
+
"allow_header_auth": true
|
|
105
|
+
}
|
|
106
|
+
\`\`\`
|
|
107
|
+
|
|
108
|
+
This prevents arbitrary clients from spoofing the tenant header. Only requests from trusted IPs or services can use header-based resolution.
|
|
109
|
+
|
|
110
|
+
### Entity Schema Restrictions
|
|
111
|
+
|
|
112
|
+
Entities can be restricted to or excluded from specific schemas:
|
|
113
|
+
|
|
114
|
+
\`\`\`json
|
|
115
|
+
{
|
|
116
|
+
"table_name": "premium_features",
|
|
117
|
+
"available_schemas": ["acme", "lorem"],
|
|
118
|
+
"excluded_schemas": ["free_tier_tenant"]
|
|
119
|
+
}
|
|
120
|
+
\`\`\`
|
|
121
|
+
`,
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
id: 'mt-provisioning',
|
|
127
|
+
title: 'Provisioning',
|
|
128
|
+
subItems: [
|
|
129
|
+
{
|
|
130
|
+
id: 'mt-provision-flow',
|
|
131
|
+
title: 'Tenant Provisioning Flow',
|
|
132
|
+
content: `
|
|
133
|
+
## Tenant Provisioning
|
|
134
|
+
|
|
135
|
+
Provisioning creates a new tenant: metadata record, database schema, tables, and the initial admin user.
|
|
136
|
+
|
|
137
|
+
### Endpoint
|
|
138
|
+
|
|
139
|
+
\`\`\`
|
|
140
|
+
POST /tenants/provision
|
|
141
|
+
\`\`\`
|
|
142
|
+
|
|
143
|
+
**Requires godmin authentication.**
|
|
144
|
+
|
|
145
|
+
### Request Body
|
|
146
|
+
|
|
147
|
+
\`\`\`json
|
|
148
|
+
{
|
|
149
|
+
"name": "Acme Corp",
|
|
150
|
+
"schemaName": "acme",
|
|
151
|
+
"domain": "acme.yourapp.com",
|
|
152
|
+
"plan": "enterprise",
|
|
153
|
+
"godAdminEmail": "admin@acme.com",
|
|
154
|
+
"godAdminPassword": "SecurePass123!",
|
|
155
|
+
"settings": {
|
|
156
|
+
"maxUsers": 500,
|
|
157
|
+
"features": ["blog", "analytics"]
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
\`\`\`
|
|
161
|
+
|
|
162
|
+
### What Happens
|
|
163
|
+
|
|
164
|
+
1. **Insert tenant metadata** — A record is created in the \`tenants\` table with \`status: "provisioning"\`
|
|
165
|
+
2. **Create schema** — \`CREATE SCHEMA IF NOT EXISTS acme\` (with SQL identifier validation to prevent injection)
|
|
166
|
+
3. **Push tables** — All system tables + entity tables are created in the new schema via Drizzle
|
|
167
|
+
4. **Seed godmin** — The tenant's first admin user is created with the provided email/password, assigned the godmin role
|
|
168
|
+
5. **Log event** — A \`tenant_events\` record is created with type \`"created"\`
|
|
169
|
+
6. **Update status** — Tenant status changes to \`"active"\`, \`provisioned_at\` is set
|
|
170
|
+
7. **Invalidate cache** — Redis cache for tenant lookups is refreshed
|
|
171
|
+
|
|
172
|
+
### Response
|
|
173
|
+
|
|
174
|
+
\`\`\`json
|
|
175
|
+
{
|
|
176
|
+
"success": true,
|
|
177
|
+
"data": {
|
|
178
|
+
"id": "tenant-uuid",
|
|
179
|
+
"name": "Acme Corp",
|
|
180
|
+
"schemaName": "acme",
|
|
181
|
+
"status": "active",
|
|
182
|
+
"provisionedAt": "2024-01-15T10:30:00Z"
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
\`\`\`
|
|
186
|
+
|
|
187
|
+
### Tenants Table Schema
|
|
188
|
+
|
|
189
|
+
| Column | Type | Description |
|
|
190
|
+
|--------|------|-------------|
|
|
191
|
+
| \`id\` | uuid | Primary key |
|
|
192
|
+
| \`name\` | varchar | Display name |
|
|
193
|
+
| \`schemaName\` | varchar | PostgreSQL schema name |
|
|
194
|
+
| \`domain\` | varchar | Subdomain or custom domain |
|
|
195
|
+
| \`status\` | varchar | provisioning, active, suspended, deleted |
|
|
196
|
+
| \`plan\` | varchar | Subscription plan |
|
|
197
|
+
| \`godAdminEmail\` | varchar | First admin email |
|
|
198
|
+
| \`settings\` | jsonb | Tenant-specific settings |
|
|
199
|
+
| \`trustedSources\` | jsonb | IP/service whitelist for header auth |
|
|
200
|
+
| \`provisionedAt\` | timestamptz | When provisioning completed |
|
|
201
|
+
| \`createdAt\` | timestamptz | Record creation time |
|
|
202
|
+
`,
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
id: 'mt-management',
|
|
206
|
+
title: 'Tenant Management',
|
|
207
|
+
content: `
|
|
208
|
+
## Tenant Management Endpoints
|
|
209
|
+
|
|
210
|
+
All management endpoints require godmin authentication.
|
|
211
|
+
|
|
212
|
+
### List Tenants
|
|
213
|
+
|
|
214
|
+
\`\`\`
|
|
215
|
+
GET /tenants
|
|
216
|
+
\`\`\`
|
|
217
|
+
|
|
218
|
+
Returns all tenants with their metadata, status, and plan info.
|
|
219
|
+
|
|
220
|
+
### Get Tenant Details
|
|
221
|
+
|
|
222
|
+
\`\`\`
|
|
223
|
+
GET /tenants/:id
|
|
224
|
+
\`\`\`
|
|
225
|
+
|
|
226
|
+
Returns full tenant details including settings and trusted sources.
|
|
227
|
+
|
|
228
|
+
### Suspend Tenant
|
|
229
|
+
|
|
230
|
+
\`\`\`
|
|
231
|
+
POST /tenants/:id/suspend
|
|
232
|
+
\`\`\`
|
|
233
|
+
|
|
234
|
+
Sets tenant status to \`"suspended"\`. All requests to the tenant's schema will be rejected. A \`tenant_events\` record is logged with type \`"suspended"\`.
|
|
235
|
+
|
|
236
|
+
### Reactivate Tenant
|
|
237
|
+
|
|
238
|
+
\`\`\`
|
|
239
|
+
POST /tenants/:id/reactivate
|
|
240
|
+
\`\`\`
|
|
241
|
+
|
|
242
|
+
Restores tenant status to \`"active"\`. A \`tenant_events\` record is logged with type \`"reactivated"\`.
|
|
243
|
+
|
|
244
|
+
### Tenant Events (Audit Trail)
|
|
245
|
+
|
|
246
|
+
Every lifecycle change is recorded in \`tenant_events\`:
|
|
247
|
+
|
|
248
|
+
| Event Type | Description |
|
|
249
|
+
|------------|-------------|
|
|
250
|
+
| \`created\` | Tenant provisioned |
|
|
251
|
+
| \`provisioned\` | Schema and tables created |
|
|
252
|
+
| \`suspended\` | Tenant suspended |
|
|
253
|
+
| \`reactivated\` | Tenant reactivated |
|
|
254
|
+
| \`plan_changed\` | Subscription plan changed |
|
|
255
|
+
| \`settings_updated\` | Tenant settings modified |
|
|
256
|
+
|
|
257
|
+
### How Per-Request Resolution Works
|
|
258
|
+
|
|
259
|
+
In the \`onRequest\` middleware:
|
|
260
|
+
|
|
261
|
+
1. \`TenantRegistry.resolveFromRequest(request)\` extracts the tenant identifier
|
|
262
|
+
2. Sets \`x-tenant-schema\` header on the request
|
|
263
|
+
3. Every route handler calls \`resolveAuthTablesForRequest(request)\` or \`getReqSchema(request)\` to get the tenant-specific table references
|
|
264
|
+
4. All database queries are scoped to the resolved schema
|
|
265
|
+
`,
|
|
266
|
+
},
|
|
267
|
+
],
|
|
268
|
+
},
|
|
269
|
+
]
|