Files
code-center/src/routes/app-manager/module/app-domain.ts
2025-12-28 16:16:23 +08:00

88 lines
2.1 KiB
TypeScript

import { sequelize } from '../../../modules/sequelize.ts';
import { DataTypes, Model } from 'sequelize';
export type DomainList = Partial<InstanceType<typeof AppDomainModel>>;
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<string, any>;
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);
// });