import { sequelize } from '../modules/sequelize.ts'; import { DataTypes, Model } from 'sequelize'; export type RouterCode = { id: string; path: string; key: string; active: boolean; project: string; code: string; exec: string; type: RouterCodeType; middleware: string[]; next: string; data: any; validator: any; }; export enum RouterCodeType { route = 'route', middleware = 'middleware', } export class RouterCodeModel extends Model { declare id: string; declare path: string; declare key: string; declare active: boolean; declare project: string; declare code: string; declare exec: string; declare type: RouterCodeType; declare middleware: string[]; declare next: string; // 如果是中间件,不存在 declare data: any; // 内容 declare validator: any; } RouterCodeModel.init( { id: { type: DataTypes.UUID, primaryKey: true, defaultValue: DataTypes.UUIDV4, comment: 'code id', }, path: { type: DataTypes.STRING, allowNull: false, }, key: { type: DataTypes.STRING, allowNull: true, }, active: { type: DataTypes.BOOLEAN, defaultValue: false, }, project: { type: DataTypes.STRING, defaultValue: 'default', }, code: { type: DataTypes.TEXT, defaultValue: '', }, exec: { type: DataTypes.TEXT, // 对代码进行编译后的代码 defaultValue: '', }, type: { type: DataTypes.ENUM(RouterCodeType.route, RouterCodeType.middleware), defaultValue: RouterCodeType.route, }, middleware: { type: DataTypes.JSON, defaultValue: [], }, next: { type: DataTypes.JSON, defaultValue: {}, }, data: { type: DataTypes.JSON, defaultValue: {}, }, validator: { type: DataTypes.JSON, defaultValue: {}, }, }, { sequelize, tableName: 'cf_router_code', // container flow router code paranoid: true, }, ); // RouterCodeModel 检测不存在,不存在则创建 RouterCodeModel.sync({ alter: true, logging: false }) .then((res) => { console.log('RouterCodeModel sync', res); }) .catch((e) => { console.error('RouterCodeModel sync', e.message); RouterCodeModel.sync({ force: true }) .then((res) => { console.log('RouterCodeModel force sync', res); }) .catch((e) => { console.error('RouterCodeModel force sync error'); }); }); export const TableIsExist = async () => { // 判断cf_router_code表是否存在 const tableIsExist = await sequelize.getQueryInterface().showAllTables({ logging: false, }); return tableIsExist.includes('cf_router_code'); };