[WIP] Refactor multi-tenancy approach for service-tenant#1184
Closed
[WIP] Refactor multi-tenancy approach for service-tenant#1184
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original prompt
Background
The current
@objectstack/service-tenant(v4.0.4) implements multi-tenancy with a misleading "one-database-per-tenant" model wheresys_tenant_databasehasorganization_idas FK. This conflates "tenant" and "organization" terminology, lacks anenvironmentdimension, and stores auth tokens inline in the main table.After architectural review (see discussion summary below), we are adopting the Power Platform-style model:
This gives physical isolation between environments (no cross-env leakage possible), simple Dev→Prod solution deployment (export/import across DBs), independent schema evolution per environment, and aligns with Turso's cost model (DB count is cheap, isolation is free).
Decisions (all confirmed by product owner)
sys_environmentin Control Plane;sys_environment_database1:1 with environmentenvironment_idcolumn (physical isolation makes it redundant)sys_package_installationmoves from Control Plane to Data Plane (lives inside each environment DB);tenant_id/environment_idcolumns removed — environment is implicit from the DB connectionactive_environment_idonly (organization reverse-looked-up from environment)sys_tenant_databaseis deprecated: v4.x keeps a deprecation shim, v5.0 removes itdefaultenvironment + its DBdriverfield onsys_environment_databaseto support turso / postgres / sqliteScope of this PR
A. New ADR
docs/adr/0002-environment-database-isolation.md— record the full decision with rationale, trade-offs vs. per-organization model, Turso cost analysis, and migration path.B. New objects in
packages/services/service-tenant/src/objects/sys-environment.object.ts— environment registry in Control Planeid,organization_id(FK),slug,name,description,env_type(default/sandbox/development/production/trial),region,is_default,is_protected,status(provisioning/active/suspended/archived),dlp_policy_id,created_by,metadata,created_at,updated_at(organization_id, slug),(organization_id, is_default)sys-environment-database.object.ts— 1:1 with environment, replacessys-tenant-database.object.tsid,environment_id(FK, UNIQUE),database_name(UUID-based, immutable),database_url,driver(turso/postgres/sqlite),region,storage_limit_mb,last_accessed_at,provisioned_at,metadata,created_at,updated_atenvironment_id, UNIQUEdatabase_nameauth_tokencolumn — decoupled intosys_database_credentialsys-database-credential.object.ts— decoupled credentials with rotation supportid,database_id(FK → sys_environment_database),token_cipher,kms_key_id,scope(readwrite/readonly/admin),status(active/rotating/revoked),issued_at,expires_at,rotated_from(database_id, status),expires_atsys-environment-member.object.ts— environment-level RBACid,environment_id,organization_id(denormalised for auth checks),user_id,role(env_admin/env_maker/env_user/env_viewer),created_at(environment_id, user_id),user_idC. Deprecate old objects
sys-tenant-database.object.ts— add deprecation JSDoc, keep file for v4.x compatibility, mark for removal in v5.0.sys-package-installation.object.ts— move out of Control Plane registration. Keep an empty Data Plane variant (notenant_id, noenvironment_id— environment is implicit from DB connection). This new object should live alongside other Data Plane system objects and be registered by the environment schema initializer instead of the tenant plugin.D. Spec updates in
packages/spec/src/cloud/tenant.zod.tsEnvironmentSchema,EnvironmentDatabaseSchema,DatabaseCredentialSchema,EnvironmentMemberSchemaZod schemas.TenantContextSchemato includeenvironmentIdandorganizationId.TenantDatabaseSchemaas a deprecated alias ofEnvironmentDatabaseSchemafor v4.x.ProvisionEnvironmentRequestSchema/ProvisionEnvironmentResponseSchema.E. Contract updates in `packages/spec/src/contracts...
This pull request was created from Copilot chat.