Block A of the EchoDo plan. Workspaces used to live as `objects(type='workspace')`,
which made it impossible to put a real RLS-friendly tenant boundary on the schema
or to give each workspace a stable URL slug. This commit:
- Adds a top-level `workspaces` table (slug unique, owner FK, plan_tier hook).
- Migrates the 8 anchor tables (objects, workspace_members, object_type_defs,
property_definitions, templates, forms, markdown_backlog_items,
cursor_sync_mappings) to FK into `workspaces.id` instead of `objects.id`,
with a hand-augmented data-copy migration that preserves IDs and slug-collision-
proofs on backfill.
- Introduces a `workspaceProcedure` tRPC middleware + `resolveWorkspace` helper
that take a UUID-or-slug `workspace` handle and expose `ctx.workspace`. All
tenant-scoped routers (objects, types, properties, templates, forms, search,
ai, relations, favorites) now flow through it.
- Updates the web app to pass `workspace` slugs from the URL (or store) instead
of the old `workspaceId`, including a workspace-sync layer that rewrites
/<UUID>/... links to /<slug>/...
- Updates the MCP tools (list_objects, create_object, search_objects) and the
workspace://{handle}/tree resource to accept either a slug or UUID so existing
agents keep working.
- Adds a Create Workspace dialog and a Workspace Settings page (rename + slug
rename with redirect, owner-only archive).
Verified locally against a fresh Postgres: migration applies cleanly, slug
uniqueness holds, tenant data is isolated by workspace_id, slug↔UUID resolution
works in both directions, and ON DELETE CASCADE cleans up child rows in the
correct workspace only.
Co-authored-by: Cursor <cursoragent@cursor.com>
215 lines
6.4 KiB
TypeScript
215 lines
6.4 KiB
TypeScript
import {
|
|
pgTable,
|
|
uuid,
|
|
varchar,
|
|
timestamp,
|
|
index,
|
|
uniqueIndex,
|
|
} from "drizzle-orm/pg-core";
|
|
import { relations } from "drizzle-orm";
|
|
import { objects, objectAssignees, workspaceMembers } from "./objects";
|
|
import { users, accounts, sessions } from "./users";
|
|
import { propertyDefinitions } from "./properties";
|
|
import { propertyValues } from "./values";
|
|
import { views } from "./views";
|
|
import { templates } from "./templates";
|
|
import { objectTypeDefs } from "./types";
|
|
import { markdownBacklogItems } from "./markdown_backlog";
|
|
import { cursorSyncMappings } from "./cursor_sync";
|
|
import { workspaces } from "./workspaces";
|
|
|
|
export const objectRelations = pgTable(
|
|
"object_relations",
|
|
{
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
sourceId: uuid("source_id")
|
|
.notNull()
|
|
.references(() => objects.id, { onDelete: "cascade" }),
|
|
targetId: uuid("target_id")
|
|
.notNull()
|
|
.references(() => objects.id, { onDelete: "cascade" }),
|
|
relationType: varchar("relation_type", { length: 50 }).notNull(),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
|
|
},
|
|
(table) => ({
|
|
sourceIdIdx: index("object_relations_source_id_idx").on(table.sourceId),
|
|
targetIdIdx: index("object_relations_target_id_idx").on(table.targetId),
|
|
relationTypeIdx: index("object_relations_relation_type_idx").on(table.relationType),
|
|
sourceTargetTypeUnique: uniqueIndex("object_relations_source_target_type_unique").on(
|
|
table.sourceId,
|
|
table.targetId,
|
|
table.relationType,
|
|
),
|
|
}),
|
|
);
|
|
|
|
// --- Drizzle ORM relations ---
|
|
|
|
export const usersRelations = relations(users, ({ many }) => ({
|
|
objectsCreated: many(objects),
|
|
workspaceMemberships: many(workspaceMembers),
|
|
ownedWorkspaces: many(workspaces),
|
|
objectAssignees: many(objectAssignees),
|
|
accounts: many(accounts),
|
|
sessions: many(sessions),
|
|
}));
|
|
|
|
export const workspacesRelations = relations(workspaces, ({ one, many }) => ({
|
|
owner: one(users, {
|
|
fields: [workspaces.ownerUserId],
|
|
references: [users.id],
|
|
}),
|
|
members: many(workspaceMembers),
|
|
objects: many(objects),
|
|
templates: many(templates),
|
|
objectTypeDefs: many(objectTypeDefs),
|
|
propertyDefinitions: many(propertyDefinitions),
|
|
markdownBacklogItems: many(markdownBacklogItems),
|
|
}));
|
|
|
|
export const objectsRelations = relations(objects, ({ one, many }) => ({
|
|
parent: one(objects, {
|
|
fields: [objects.parentId],
|
|
references: [objects.id],
|
|
relationName: "objectHierarchy",
|
|
}),
|
|
children: many(objects, { relationName: "objectHierarchy" }),
|
|
workspace: one(workspaces, {
|
|
fields: [objects.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
template: one(templates, {
|
|
fields: [objects.templateId],
|
|
references: [templates.id],
|
|
}),
|
|
creator: one(users, {
|
|
fields: [objects.createdBy],
|
|
references: [users.id],
|
|
}),
|
|
propertyValues: many(propertyValues),
|
|
views: many(views),
|
|
assignees: many(objectAssignees),
|
|
outgoingRelations: many(objectRelations, { relationName: "relationSource" }),
|
|
incomingRelations: many(objectRelations, { relationName: "relationTarget" }),
|
|
}));
|
|
|
|
export const objectAssigneesRelations = relations(objectAssignees, ({ one }) => ({
|
|
object: one(objects, {
|
|
fields: [objectAssignees.objectId],
|
|
references: [objects.id],
|
|
}),
|
|
user: one(users, {
|
|
fields: [objectAssignees.userId],
|
|
references: [users.id],
|
|
}),
|
|
}));
|
|
|
|
export const workspaceMembersRelations = relations(workspaceMembers, ({ one }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [workspaceMembers.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
user: one(users, {
|
|
fields: [workspaceMembers.userId],
|
|
references: [users.id],
|
|
}),
|
|
}));
|
|
|
|
export const propertyDefinitionsRelations = relations(propertyDefinitions, ({ one, many }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [propertyDefinitions.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
values: many(propertyValues),
|
|
}));
|
|
|
|
export const propertyValuesRelations = relations(propertyValues, ({ one }) => ({
|
|
object: one(objects, {
|
|
fields: [propertyValues.objectId],
|
|
references: [objects.id],
|
|
}),
|
|
propertyDefinition: one(propertyDefinitions, {
|
|
fields: [propertyValues.propertyDefId],
|
|
references: [propertyDefinitions.id],
|
|
}),
|
|
}));
|
|
|
|
export const viewsRelations = relations(views, ({ one }) => ({
|
|
object: one(objects, {
|
|
fields: [views.objectId],
|
|
references: [objects.id],
|
|
}),
|
|
}));
|
|
|
|
export const templatesRelations = relations(templates, ({ one, many }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [templates.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
objects: many(objects),
|
|
}));
|
|
|
|
export const objectTypeDefsRelations = relations(objectTypeDefs, ({ one }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [objectTypeDefs.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
}));
|
|
|
|
export const accountsRelations = relations(accounts, ({ one }) => ({
|
|
user: one(users, {
|
|
fields: [accounts.userId],
|
|
references: [users.id],
|
|
}),
|
|
}));
|
|
|
|
export const sessionsRelations = relations(sessions, ({ one }) => ({
|
|
user: one(users, {
|
|
fields: [sessions.userId],
|
|
references: [users.id],
|
|
}),
|
|
}));
|
|
|
|
export const objectRelationsRelations = relations(objectRelations, ({ one }) => ({
|
|
source: one(objects, {
|
|
fields: [objectRelations.sourceId],
|
|
references: [objects.id],
|
|
relationName: "relationSource",
|
|
}),
|
|
target: one(objects, {
|
|
fields: [objectRelations.targetId],
|
|
references: [objects.id],
|
|
relationName: "relationTarget",
|
|
}),
|
|
}));
|
|
|
|
export const markdownBacklogItemsRelations = relations(
|
|
markdownBacklogItems,
|
|
({ one, many }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [markdownBacklogItems.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
parent: one(markdownBacklogItems, {
|
|
fields: [markdownBacklogItems.parentId],
|
|
references: [markdownBacklogItems.id],
|
|
relationName: "backlogHierarchy",
|
|
}),
|
|
children: many(markdownBacklogItems, { relationName: "backlogHierarchy" }),
|
|
cursorMapping: one(cursorSyncMappings, {
|
|
fields: [markdownBacklogItems.id],
|
|
references: [cursorSyncMappings.backlogItemId],
|
|
}),
|
|
}),
|
|
);
|
|
|
|
export const cursorSyncMappingsRelations = relations(cursorSyncMappings, ({ one }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [cursorSyncMappings.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
backlogItem: one(markdownBacklogItems, {
|
|
fields: [cursorSyncMappings.backlogItemId],
|
|
references: [markdownBacklogItems.id],
|
|
}),
|
|
}));
|