import { sequelize } from '../../../modules/sequelize.ts'; import { DataTypes, Model } from 'sequelize'; export type DomainList = Partial>; import { redis } from '../../../modules/redis.ts'; // 审核,通过,驳回 const appDomainStatus = ['audit', 'auditReject', 'auditPending', 'running', 'stop'] as const; type AppDomainStatus = (typeof appDomainStatus)[number]; /** * 应用域名管理 */ export class AppDomainModel extends Model { declare id: string; declare domain: string; declare appId: string; // 状态, declare status: AppDomainStatus; declare uid: string; declare data: Record; declare createdAt: Date; declare updatedAt: Date; checkCanUpdateStatus(newStatus: AppDomainStatus) { // 原本是运行中,可以改为停止,原本是停止,可以改为运行。 if (this.status === 'running' || this.status === 'stop') { return true; } // 原本是审核状态,不能修改。 return false; } async clearCache() { // 清除缓存 const cacheKey = `domain:${this.domain}`; const checkHas = async () => { const has = await redis.get(cacheKey); return has; }; const has = await checkHas(); if (has) { await redis.set(cacheKey, '', 'EX', 1); } } } AppDomainModel.init( { id: { type: DataTypes.UUID, primaryKey: true, defaultValue: DataTypes.UUIDV4, comment: 'id', }, domain: { type: DataTypes.STRING, allowNull: false, unique: true, }, data: { type: DataTypes.JSONB, allowNull: true, }, status: { type: DataTypes.STRING, allowNull: false, defaultValue: 'running', }, appId: { type: DataTypes.STRING, allowNull: true, }, uid: { type: DataTypes.STRING, allowNull: true, }, }, { sequelize, tableName: 'kv_app_domain', paranoid: true, }, ); // AppDomainModel.sync({ alter: true, logging: false }).catch((e) => { // console.error('AppDomainModel sync', e); // });