Multi-Tenant / Subdomain Users

  1. User Schema```typescriptexport const userTable = pgTable("user",{ tenantId: text("tenant_id").notNull(), // Add this line id: text("id").primaryKey(), name: text("name").notNull(), email: text("email").notNull(), emailVerified: boolean("email_verified").notNull().default(false), image: text("image"), createdAt: timestamp("created_at").notNull().defaultNow(), updatedAt: timestamp("updated_at").notNull().defaultNow(), verifiedAt: timestamp("verified_at"),},(t) => [ { unq: unique().on(t.tenantId, t.email), },]);```
  2. Organization Schema```typescriptexport const organizationTable = pgTable("organization", {id: uuid().defaultRandom().primaryKey(),name: varchar({ length: 255 }).notNull(),slug: varchar({ length: 255 }),bio: varchar({ length: 255 }),max_allowed_memberships: integer(),active: boolean().notNull().default(true),is_personal: boolean().notNull().default(false),created_at: timestamp().defaultNow(),created_by: text() .references(() => userTable.id, { onDelete: "cascade" }) .notNull(),});```
  3. Application Schema```typescriptexport const applicationTable = pgTable("application", {id: uuid().defaultRandom().primaryKey(),org_id: uuid() .references(() => organizationTable.id, { onDelete: "cascade", }) .notNull(),slug: varchar({ length: 255 }).notNull().unique(),name: varchar({ length: 255 }).notNull(),bio: varchar({ length: 255 }),active: boolean().notNull().default(true),created_at: timestamp().defaultNow(),created_by: serial() .references(() => organizationUserTable.id, { onDelete: "cascade" }) .notNull(),});```
Was this page helpful?