user-credentials.model.ts68 lines, 1.4 KB
| 1 | import { DataTypes, Model, type Optional } from "sequelize"; |
| 2 | import { sequelize } from "@/db/sequelize"; |
| 3 | |
| 4 | export interface UserCredentialsAttributes { |
| 5 | id: string; |
| 6 | email: string; |
| 7 | displayName: string; |
| 8 | passwordHash: string; |
| 9 | createdAt: Date; |
| 10 | updatedAt: Date; |
| 11 | } |
| 12 | |
| 13 | export type UserCredentialsCreationAttributes = Optional< |
| 14 | UserCredentialsAttributes, |
| 15 | "id" | "createdAt" | "updatedAt" |
| 16 | >; |
| 17 | |
| 18 | export class UserCredentials |
| 19 | extends Model<UserCredentialsAttributes, UserCredentialsCreationAttributes> |
| 20 | implements UserCredentialsAttributes |
| 21 | { |
| 22 | declare id: string; |
| 23 | declare email: string; |
| 24 | declare displayName: string; |
| 25 | declare passwordHash: string; |
| 26 | declare createdAt: Date; |
| 27 | declare updatedAt: Date; |
| 28 | } |
| 29 | |
| 30 | UserCredentials.init( |
| 31 | { |
| 32 | id: { |
| 33 | type: DataTypes.UUID, |
| 34 | primaryKey: true, |
| 35 | defaultValue: DataTypes.UUIDV4, |
| 36 | }, |
| 37 | email: { |
| 38 | type: DataTypes.STRING, |
| 39 | allowNull: false, |
| 40 | unique: true, |
| 41 | validate: { |
| 42 | isEmail: true, |
| 43 | }, |
| 44 | }, |
| 45 | passwordHash: { |
| 46 | type: DataTypes.STRING, |
| 47 | allowNull: false, |
| 48 | }, |
| 49 | displayName: { |
| 50 | type: DataTypes.STRING, |
| 51 | allowNull: false, |
| 52 | }, |
| 53 | createdAt: { |
| 54 | type: DataTypes.DATE, |
| 55 | allowNull: false, |
| 56 | defaultValue: DataTypes.NOW, |
| 57 | }, |
| 58 | updatedAt: { |
| 59 | type: DataTypes.DATE, |
| 60 | allowNull: false, |
| 61 | defaultValue: DataTypes.NOW, |
| 62 | }, |
| 63 | }, |
| 64 | { |
| 65 | sequelize, |
| 66 | tableName: "user_credentials", |
| 67 | }, |
| 68 | ); |