78 lines
2.7 KiB
TypeScript
78 lines
2.7 KiB
TypeScript
import { pgTable, text, timestamp, uuid, uniqueIndex, index } from "drizzle-orm/pg-core";
|
|
|
|
export const users = pgTable(
|
|
"users",
|
|
{
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
email: text("email").notNull(),
|
|
plan: text("plan").notNull().default("free"),
|
|
stripeCustomerId: text("stripe_customer_id"),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(t) => ({
|
|
emailUnique: uniqueIndex("users_email_unique").on(t.email),
|
|
}),
|
|
);
|
|
|
|
export const sessions = pgTable(
|
|
"sessions",
|
|
{
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
userId: uuid("user_id")
|
|
.notNull()
|
|
.references(() => users.id, { onDelete: "cascade" }),
|
|
tokenHash: text("token_hash").notNull(),
|
|
userAgent: text("user_agent"),
|
|
ipHash: text("ip_hash"),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
lastUsedAt: timestamp("last_used_at", { withTimezone: true }).notNull().defaultNow(),
|
|
expiresAt: timestamp("expires_at", { withTimezone: true }).notNull(),
|
|
},
|
|
(t) => ({
|
|
tokenHashUnique: uniqueIndex("sessions_token_hash_unique").on(t.tokenHash),
|
|
userIdx: index("sessions_user_idx").on(t.userId),
|
|
}),
|
|
);
|
|
|
|
export const magicLinks = pgTable(
|
|
"magic_links",
|
|
{
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
email: text("email").notNull(),
|
|
tokenHash: text("token_hash").notNull(),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
expiresAt: timestamp("expires_at", { withTimezone: true }).notNull(),
|
|
usedAt: timestamp("used_at", { withTimezone: true }),
|
|
},
|
|
(t) => ({
|
|
tokenHashUnique: uniqueIndex("magic_links_token_hash_unique").on(t.tokenHash),
|
|
emailIdx: index("magic_links_email_idx").on(t.email),
|
|
}),
|
|
);
|
|
|
|
export const userDevices = pgTable(
|
|
"user_devices",
|
|
{
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
userId: uuid("user_id")
|
|
.notNull()
|
|
.references(() => users.id, { onDelete: "cascade" }),
|
|
deviceId: text("device_id").notNull(),
|
|
name: text("name").notNull(),
|
|
type: text("type").notNull(),
|
|
avatar: text("avatar"),
|
|
linkedAt: timestamp("linked_at", { withTimezone: true }).notNull().defaultNow(),
|
|
lastSeenAt: timestamp("last_seen_at", { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(t) => ({
|
|
userDeviceUnique: uniqueIndex("user_devices_user_device_unique").on(t.userId, t.deviceId),
|
|
}),
|
|
);
|
|
|
|
export type User = typeof users.$inferSelect;
|
|
export type NewUser = typeof users.$inferInsert;
|
|
export type Session = typeof sessions.$inferSelect;
|
|
export type MagicLink = typeof magicLinks.$inferSelect;
|
|
export type UserDevice = typeof userDevices.$inferSelect;
|