实体定义
枚举 和 varchar 类型
ts
import {
integer,
pgTable,
varchar,
pgEnum,
text,
timestamp,
} from "drizzle-orm/pg-core";
import { generateBaseFields } from "./base.js";
export const otpChannelEnum = pgEnum("otp_channel", ["sms", "email"]);
export const otpStatusEnum = pgEnum("otp_status", [
"pending",
"verified",
"expired",
"locked",
]);
export const OTP_PURPOSE = {
USER_LOGIN: "user_login",
USER_REGISTER: "user_register",
USER_RESET_PASSWORD: "user_reset_password",
} as const;
export type OtpPurpose = (typeof OTP_PURPOSE)[keyof typeof OTP_PURPOSE];
export const otpTable = pgTable("otp", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
channel: otpChannelEnum("channel").notNull(),
status: otpStatusEnum("status").notNull().default("pending"),
purpose: varchar("purpose", { length: 64 }).$type<OtpPurpose>().notNull(),
/**
* 手机号 | 邮箱
*/
target: text("target").notNull(),
codeHash: varchar("code_hash", { length: 128 }).notNull(),
expiresAt: timestamp("expires_at", {
withTimezone: true,
mode: "date",
}).notNull(),
attemptsLeft: integer("attempts_left").notNull().default(3),
...generateBaseFields(),
});