update schema

This commit is contained in:
Bill Yang 2025-03-13 19:53:35 -07:00
parent 0e95a88768
commit db06705649
3 changed files with 186 additions and 119 deletions

View file

@ -1,6 +1,6 @@
import { FastifyRequest, FastifyReply } from "fastify"; import { FastifyRequest, FastifyReply } from "fastify";
import { db } from "../db/postgres/postgres.js"; import { db } from "../db/postgres/postgres.js";
import { member, users } from "../db/postgres/schema.js"; import { member, user } from "../db/postgres/schema.js";
import { eq, and } from "drizzle-orm"; import { eq, and } from "drizzle-orm";
import { auth } from "../lib/auth.js"; import { auth } from "../lib/auth.js";
@ -61,13 +61,13 @@ export async function listOrganizationMembers(
organizationId: member.organizationId, organizationId: member.organizationId,
createdAt: member.createdAt, createdAt: member.createdAt,
// User fields // User fields
userName: users.name, userName: user.name,
userEmail: users.email, userEmail: user.email,
userImage: users.image, userImage: user.image,
userActualId: users.id, userActualId: user.id,
}) })
.from(member) .from(member)
.leftJoin(users, eq(member.userId, users.id)) .leftJoin(user, eq(member.userId, user.id))
.where(eq(member.organizationId, organizationId)); .where(eq(member.organizationId, organizationId));
// Transform the results to the expected format // Transform the results to the expected format

View file

@ -6,11 +6,12 @@ import {
serial, serial,
text, text,
timestamp, timestamp,
foreignKey,
unique, unique,
} from "drizzle-orm/pg-core"; } from "drizzle-orm/pg-core";
// User table // User table
export const users = pgTable( export const user = pgTable(
"user", "user",
{ {
id: text().primaryKey().notNull(), id: text().primaryKey().notNull(),
@ -34,28 +35,42 @@ export const users = pgTable(
] ]
); );
// Verification table
export const verification = pgTable("verification", { export const verification = pgTable("verification", {
id: text("id").primaryKey().notNull(), id: text().primaryKey().notNull(),
identifier: text("identifier").notNull(), identifier: text().notNull(),
value: text("value").notNull(), value: text().notNull(),
expiresAt: timestamp("expiresAt").notNull(), expiresAt: timestamp({ mode: "string" }).notNull(),
createdAt: timestamp("createdAt"), createdAt: timestamp({ mode: "string" }),
updatedAt: timestamp("updatedAt"), updatedAt: timestamp({ mode: "string" }),
}); });
// Sites table // Sites table
export const sites = pgTable("sites", { export const sites = pgTable(
siteId: serial("site_id").primaryKey().notNull(), "sites",
name: text("name").notNull(), {
domain: text("domain").notNull().unique(), siteId: serial("site_id").primaryKey().notNull(),
createdAt: timestamp("created_at").defaultNow(), name: text("name").notNull(),
updatedAt: timestamp("updated_at").defaultNow(), domain: text("domain").notNull().unique(),
createdBy: text("created_by") createdAt: timestamp("created_at").defaultNow(),
.notNull() updatedAt: timestamp("updated_at").defaultNow(),
.references(() => users.id), createdBy: text("created_by")
organizationId: text("organization_id").references(() => organization.id), .notNull()
}); .references(() => user.id),
organizationId: text("organization_id").references(() => organization.id),
},
(table) => [
foreignKey({
columns: [table.createdBy],
foreignColumns: [user.id],
name: "sites_created_by_user_id_fk",
}),
foreignKey({
columns: [table.organizationId],
foreignColumns: [organization.id],
name: "sites_organization_id_organization_id_fk",
}),
]
);
// Active sessions table // Active sessions table
export const activeSessions = pgTable("active_sessions", { export const activeSessions = pgTable("active_sessions", {
@ -77,104 +92,156 @@ export const activeSessions = pgTable("active_sessions", {
referrer: text("referrer"), referrer: text("referrer"),
}); });
export const reports = pgTable("reports", { export const reports = pgTable(
reportId: serial("report_id").primaryKey().notNull(), "reports",
siteId: integer("site_id").references(() => sites.siteId), {
userId: text("user_id").references(() => users.id), reportId: serial("report_id").primaryKey().notNull(),
reportType: text("report_type"), siteId: integer("site_id"),
data: jsonb("data"), userId: text("user_id"),
createdAt: timestamp("created_at").defaultNow(), reportType: text("report_type"),
updatedAt: timestamp("updated_at").defaultNow(), data: jsonb(),
}); createdAt: timestamp("created_at", { mode: "string" }).defaultNow(),
updatedAt: timestamp("updated_at", { mode: "string" }).defaultNow(),
},
(table) => [
foreignKey({
columns: [table.siteId],
foreignColumns: [sites.siteId],
name: "reports_site_id_sites_site_id_fk",
}),
foreignKey({
columns: [table.userId],
foreignColumns: [user.id],
name: "reports_user_id_user_id_fk",
}),
]
);
// Account table export const account = pgTable(
export const account = pgTable("account", { "account",
id: text("id").primaryKey().notNull(), {
accountId: text("accountId").notNull(), id: text().primaryKey().notNull(),
providerId: text("providerId").notNull(), accountId: text().notNull(),
userId: text("userId") providerId: text().notNull(),
.notNull() userId: text().notNull(),
.references(() => users.id), accessToken: text(),
accessToken: text("accessToken"), refreshToken: text(),
refreshToken: text("refreshToken"), idToken: text(),
idToken: text("idToken"), accessTokenExpiresAt: timestamp({ mode: "string" }),
accessTokenExpiresAt: timestamp("accessTokenExpiresAt"), refreshTokenExpiresAt: timestamp({ mode: "string" }),
refreshTokenExpiresAt: timestamp("refreshTokenExpiresAt"), scope: text(),
scope: text("scope"), password: text(),
password: text("password"), createdAt: timestamp({ mode: "string" }).notNull(),
createdAt: timestamp("createdAt").notNull(), updatedAt: timestamp({ mode: "string" }).notNull(),
updatedAt: timestamp("updatedAt").notNull(), },
}); (table) => [
foreignKey({
columns: [table.userId],
foreignColumns: [user.id],
name: "account_userId_user_id_fk",
}),
]
);
// Organization table export const organization = pgTable(
export const organization = pgTable("organization", { "organization",
id: text("id").primaryKey().notNull(), {
name: text("name").notNull(), id: text().primaryKey().notNull(),
slug: text("slug").notNull().unique(), name: text().notNull(),
logo: text("logo"), slug: text().notNull(),
createdAt: timestamp("createdAt").notNull(), logo: text(),
metadata: text("metadata"), createdAt: timestamp({ mode: "string" }).notNull(),
}); metadata: text(),
},
(table) => [unique("organization_slug_unique").on(table.slug)]
);
// Member table export const member = pgTable(
export const member = pgTable("member", { "member",
id: text("id").primaryKey().notNull(), {
organizationId: text("organizationId") id: text().primaryKey().notNull(),
.notNull() organizationId: text().notNull(),
.references(() => organization.id), userId: text().notNull(),
userId: text("userId") role: text().notNull(),
.notNull() createdAt: timestamp({ mode: "string" }).notNull(),
.references(() => users.id), },
role: text("role").notNull(), (table) => [
createdAt: timestamp("createdAt").notNull(), foreignKey({
}); columns: [table.organizationId],
foreignColumns: [organization.id],
name: "member_organizationId_organization_id_fk",
}),
foreignKey({
columns: [table.userId],
foreignColumns: [user.id],
name: "member_userId_user_id_fk",
}),
]
);
// Invitation table export const invitation = pgTable(
export const invitation = pgTable("invitation", { "invitation",
id: text("id").primaryKey().notNull(), {
email: text("email").notNull(), id: text().primaryKey().notNull(),
inviterId: text("inviterId") email: text().notNull(),
.notNull() inviterId: text().notNull(),
.references(() => users.id), organizationId: text().notNull(),
organizationId: text("organizationId") role: text().notNull(),
.notNull() status: text().notNull(),
.references(() => organization.id), expiresAt: timestamp({ mode: "string" }).notNull(),
role: text("role").notNull(), createdAt: timestamp({ mode: "string" }).notNull(),
status: text("status").notNull(), },
expiresAt: timestamp("expiresAt").notNull(), (table) => [
createdAt: timestamp("createdAt").notNull(), foreignKey({
}); columns: [table.inviterId],
foreignColumns: [user.id],
name: "invitation_inviterId_user_id_fk",
}),
foreignKey({
columns: [table.organizationId],
foreignColumns: [organization.id],
name: "invitation_organizationId_organization_id_fk",
}),
]
);
// Session table export const session = pgTable(
export const session = pgTable("session", { "session",
id: text("id").primaryKey().notNull(), {
expiresAt: timestamp("expiresAt").notNull(), id: text().primaryKey().notNull(),
token: text("token").notNull().unique(), expiresAt: timestamp({ mode: "string" }).notNull(),
createdAt: timestamp("createdAt").notNull(), token: text().notNull(),
updatedAt: timestamp("updatedAt").notNull(), createdAt: timestamp({ mode: "string" }).notNull(),
ipAddress: text("ipAddress"), updatedAt: timestamp({ mode: "string" }).notNull(),
userAgent: text("userAgent"), ipAddress: text(),
userId: text("userId") userAgent: text(),
.notNull() userId: text().notNull(),
.references(() => users.id), impersonatedBy: text(),
impersonatedBy: text("impersonatedBy"), activeOrganizationId: text(),
activeOrganizationId: text("activeOrganizationId"), },
}); (table) => [
foreignKey({
columns: [table.userId],
foreignColumns: [user.id],
name: "session_userId_user_id_fk",
}),
unique("session_token_unique").on(table.token),
]
);
// Subscription table
export const subscription = pgTable("subscription", { export const subscription = pgTable("subscription", {
id: text("id").primaryKey().notNull(), id: text().primaryKey().notNull(),
plan: text("plan").notNull(), plan: text().notNull(),
referenceId: text("referenceId").notNull(), referenceId: text().notNull(),
stripeCustomerId: text("stripeCustomerId"), stripeCustomerId: text(),
stripeSubscriptionId: text("stripeSubscriptionId"), stripeSubscriptionId: text(),
status: text("status").notNull(), status: text().notNull(),
periodStart: timestamp("periodStart", { mode: "string" }), periodStart: timestamp({ mode: "string" }),
periodEnd: timestamp("periodEnd", { mode: "string" }), periodEnd: timestamp({ mode: "string" }),
cancelAtPeriodEnd: boolean("cancelAtPeriodEnd"), cancelAtPeriodEnd: boolean(),
seats: integer("seats"), seats: integer(),
trialStart: timestamp("trialStart", { mode: "string" }), trialStart: timestamp({ mode: "string" }),
trialEnd: timestamp("trialEnd", { mode: "string" }), trialEnd: timestamp({ mode: "string" }),
createdAt: timestamp("createdAt").defaultNow().notNull(), createdAt: timestamp({ mode: "string" }).defaultNow().notNull(),
updatedAt: timestamp("updatedAt").defaultNow().notNull(), updatedAt: timestamp({ mode: "string" }).defaultNow().notNull(),
}); });

View file

@ -166,7 +166,7 @@ export function initAuth(allowedOrigins: string[]) {
provider: "pg", provider: "pg",
schema: { schema: {
// Map our schema tables to what better-auth expects // Map our schema tables to what better-auth expects
user: schema.users, user: schema.user,
account: schema.account, account: schema.account,
session: schema.session, session: schema.session,
verification: schema.verification, verification: schema.verification,