From cc644af98b484bb1ec5f45409c7661a8c9d2df71 Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Tue, 20 Sep 2022 16:04:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=B2=E7=BB=91=E5=AE=9A=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E7=BB=91=E5=AE=9A=E5=BA=94=E7=94=A8=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/project/index.js | 14 ++------ api/app/lib/index.js | 15 ++++++--- api/app/lib/models/app.js | 43 ++++++++++++++++++++++++ api/app/lib/models/project_app.js | 32 +++++------------- api/sequelize-automate.config.js | 2 +- script/0.0.3/3.alert_project_app.sql | 12 ++++++- script/0.0.3/4.create_app.sql | 12 +++++++ script/0.0.3/5.alert_app_alarm.sql | 5 +++ script/0.0.3/6.alert_app_inspection.sql | 6 ++++ 9 files changed, 100 insertions(+), 41 deletions(-) create mode 100644 api/app/lib/models/app.js create mode 100644 script/0.0.3/4.create_app.sql create mode 100644 script/0.0.3/5.alert_app_alarm.sql create mode 100644 script/0.0.3/6.alert_app_inspection.sql diff --git a/api/app/lib/controllers/project/index.js b/api/app/lib/controllers/project/index.js index 5fdcd08..7248d2a 100644 --- a/api/app/lib/controllers/project/index.js +++ b/api/app/lib/controllers/project/index.js @@ -4,10 +4,8 @@ async function appList (ctx) { try { const models = ctx.fs.dc.models; - const appRes = await models.ProjectApp.findAll({ - attributes: { - exclude: ['projectId'] - } + const appRes = await models.App.findAll({ + }) ctx.status = 200; ctx.body = appRes @@ -33,13 +31,7 @@ async function pomsProject (ctx) { }, distinct: true, include: { - model: models.ProjectApp, - where: { - lock: false - }, - attributes: { - exclude: ['projectId'] - } + model: models.App, } } diff --git a/api/app/lib/index.js b/api/app/lib/index.js index 201f946..ece53c4 100644 --- a/api/app/lib/index.js +++ b/api/app/lib/index.js @@ -55,15 +55,20 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq }); const { - AppInspection, ProjectApp, ProjectCorrelation, AppAlarm + AppInspection, ProjectApp, ProjectCorrelation, AppAlarm, App } = dc.models; - AppInspection.belongsTo(ProjectApp, { foreignKey: 'projectAppId', targetKey: 'id' }); - ProjectApp.hasMany(AppInspection, { foreignKey: 'projectAppId', sourceKey: 'id' }); + AppInspection.belongsTo(App, { foreignKey: 'projectAppId', targetKey: 'id' }); + App.hasMany(AppInspection, { foreignKey: 'projectAppId', sourceKey: 'id' }); ProjectApp.belongsTo(ProjectCorrelation, { foreignKey: 'projectId', targetKey: 'id' }); ProjectCorrelation.hasMany(ProjectApp, { foreignKey: 'projectId', sourceKey: 'id' }); - AppAlarm.belongsTo(ProjectApp, { foreignKey: 'projectAppId', targetKey: 'id' }); - ProjectApp.hasMany(AppAlarm, { foreignKey: 'projectAppId', sourceKey: 'id' }); + ProjectApp.belongsTo(App, { foreignKey: 'appId', targetKey: 'id' }); + App.hasMany(ProjectApp, { foreignKey: 'appId', sourceKey: 'id' }); + + ProjectCorrelation.belongsToMany(App, { through: ProjectApp, foreignKey: 'projectId', otherKey: 'appId' }); + + AppAlarm.belongsTo(App, { foreignKey: 'projectAppId', targetKey: 'id' }); + App.hasMany(AppAlarm, { foreignKey: 'projectAppId', sourceKey: 'id' }); }; diff --git a/api/app/lib/models/app.js b/api/app/lib/models/app.js new file mode 100644 index 0000000..e1205ad --- /dev/null +++ b/api/app/lib/models/app.js @@ -0,0 +1,43 @@ +/* eslint-disable*/ +'use strict'; + +module.exports = dc => { + const DataTypes = dc.ORM; + const sequelize = dc.orm; + const App = sequelize.define("app", { + id: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: true, + field: "id", + autoIncrement: true, + unique: "app_id_uindex" + }, + name: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: false, + field: "name", + autoIncrement: false + }, + url: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: false, + field: "url", + autoIncrement: false + } + }, { + tableName: "app", + comment: "", + indexes: [] + }); + dc.models.App = App; + return App; +}; \ No newline at end of file diff --git a/api/app/lib/models/project_app.js b/api/app/lib/models/project_app.js index 24c2640..1dcf3b3 100644 --- a/api/app/lib/models/project_app.js +++ b/api/app/lib/models/project_app.js @@ -15,24 +15,6 @@ module.exports = dc => { autoIncrement: true, unique: "project_app_id_uindex" }, - name: { - type: DataTypes.STRING, - allowNull: false, - defaultValue: null, - comment: null, - primaryKey: false, - field: "name", - autoIncrement: false - }, - url: { - type: DataTypes.STRING, - allowNull: false, - defaultValue: null, - comment: null, - primaryKey: false, - field: "url", - autoIncrement: false - }, projectId: { type: DataTypes.INTEGER, allowNull: false, @@ -46,14 +28,18 @@ module.exports = dc => { model: "projectCorrelation" } }, - lock: { - type: DataTypes.BOOLEAN, - allowNull: false, + appId: { + type: DataTypes.INTEGER, + allowNull: true, defaultValue: null, comment: null, primaryKey: false, - field: "lock", - autoIncrement: false + field: "app_id", + autoIncrement: false, + references: { + key: "id", + model: "app" + } } }, { tableName: "project_app", diff --git a/api/sequelize-automate.config.js b/api/sequelize-automate.config.js index e34d53b..41e326f 100644 --- a/api/sequelize-automate.config.js +++ b/api/sequelize-automate.config.js @@ -26,7 +26,7 @@ module.exports = { dir: './app/lib/models', // 指定输出 models 文件的目录 typesDir: 'models', // 指定输出 TypeScript 类型定义的文件目录,只有 TypeScript / Midway 等会有类型定义 emptyDir: false, // !!! 谨慎操作 生成 models 之前是否清空 `dir` 以及 `typesDir` - tables: ['app_alarm'], // 指定生成哪些表的 models,如 ['user', 'user_post'];如果为 null,则忽略改属性 + tables: ['project_app'], // 指定生成哪些表的 models,如 ['user', 'user_post'];如果为 null,则忽略改属性 skipTables: [], // 指定跳过哪些表的 models,如 ['user'];如果为 null,则忽略改属性 tsNoCheck: false, // 是否添加 `@ts-nocheck` 注释到 models 文件中 ignorePrefix: [], // 生成的模型名称忽略的前缀,因为 项目中有以下表名是以 t_ 开头的,在实际模型中不需要, 可以添加多个 [ 't_data_', 't_',] ,长度较长的 前缀放前面 diff --git a/script/0.0.3/3.alert_project_app.sql b/script/0.0.3/3.alert_project_app.sql index 5a45415..b8f012b 100644 --- a/script/0.0.3/3.alert_project_app.sql +++ b/script/0.0.3/3.alert_project_app.sql @@ -1,2 +1,12 @@ +alter table project_app drop column name; + alter table project_app - add lock boolean default false not null; \ No newline at end of file + add app_id int; + +alter table project_app drop column url; + +alter table project_app drop column lock; + +alter table project_app + add constraint project_app_app_id_fk + foreign key (app_id) references app; diff --git a/script/0.0.3/4.create_app.sql b/script/0.0.3/4.create_app.sql new file mode 100644 index 0000000..bdfec52 --- /dev/null +++ b/script/0.0.3/4.create_app.sql @@ -0,0 +1,12 @@ +create table if not exists app +( + id serial not null, + name varchar(256) not null, + url varchar(1024) not null, + constraint app_pk + primary key (id) +); + +create unique index if not exists app_id_uindex + on app (id); + diff --git a/script/0.0.3/5.alert_app_alarm.sql b/script/0.0.3/5.alert_app_alarm.sql new file mode 100644 index 0000000..84a8991 --- /dev/null +++ b/script/0.0.3/5.alert_app_alarm.sql @@ -0,0 +1,5 @@ +alter table app_alarm drop constraint app_alarm_project_app_id_fk; + +alter table app_alarm + add constraint app_alarm_app_id_fk + foreign key (project_app_id) references app; diff --git a/script/0.0.3/6.alert_app_inspection.sql b/script/0.0.3/6.alert_app_inspection.sql new file mode 100644 index 0000000..1b9c46f --- /dev/null +++ b/script/0.0.3/6.alert_app_inspection.sql @@ -0,0 +1,6 @@ +alter table app_inspection drop constraint app_inspection_project_app_id_fk; + +alter table app_inspection + add constraint app_inspection_app_id_fk + foreign key (project_app_id) references app; +